IntelliSearch.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#requires -version 4
#requires -RunAsAdministrator
#requires -Modules powershell-yaml

function ModuleRoot { Split-Path $MyInvocation.MyCommand.Path -Parent }

# Removes the Zone Identifier from the files.
# This is to help with importing the .dll files, .ps1 files and other executables
Get-Item -Path ".\*" -Stream Zone.Identifier -ErrorAction:SilentlyContinue | Remove-Item -ErrorAction:SilentlyContinue

$Public  = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue )

foreach($import in @($Public + $Private))
{
    try
    {   
        Write-Verbose "Importing cmdlet $($import.fullname)"
        . $import.fullname
    }
    catch
    {
        Write-Error -Message "Failed to import function $($import.fullname): $_"
    }
}

Write-Verbose "Exporting cmdlets"
Export-ModuleMember -Function $Public.BaseName

#region Find NuGet path
Write-Verbose "Finding NuGet.exe path"
$LIB_DIR = Join-Path (ModuleRoot) "Lib"
$NUGET_EXE = Join-Path $LIB_DIR "nuget.exe"
$NUGET_URL = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"

# Ensure Lib folder exists
if ((Test-Path ModuleRoot) -and !(Test-Path $LIB_DIR -PathType Container))
{
    Write-Verbose -Message "Creating tools directory..."
    New-Item -Path $LIB_DIR -Type Directory -Force | Out-Null
}

# Try find NuGet.exe in path if not exists
if (!(Test-Path $NUGET_EXE))
{
    Write-Verbose -Message "Trying to find nuget.exe in PATH..."
    $existingPaths = $Env:Path -Split ';' | Where-Object { (![string]::IsNullOrEmpty($_)) -and (Test-Path $_ -PathType Container) }
    $NUGET_EXE_IN_PATH = Get-ChildItem -Path $existingPaths -Filter "nuget.exe" | Select-Object -First 1
    
    if ($NUGET_EXE_IN_PATH -ne $null -and (Test-Path $NUGET_EXE_IN_PATH.FullName))
    {
        Write-Verbose -Message "Found in PATH at $($NUGET_EXE_IN_PATH.FullName)."
        $NUGET_EXE = $NUGET_EXE_IN_PATH.FullName
    }
}

if (!(Test-Path $NUGET_EXE))
{
    Write-Verbose -Message "Attempting to download NuGet.exe..."
    $wc = New-Object System.Net.WebClient
    $wc.Headers["User-Agent"] = "ISPSModuleInitiator"
    $wc.DownloadFile($NUGET_URL, $NUGET_EXE)
    Unblock-File -Path $NUGET_EXE -ErrorAction:SilentlyContinue
}

# Attempt to export the NuGet.exe path
Write-Verbose "Exporting NuGet.exe path as variable NUGET_EXE"
if (Test-Path $NUGET_EXE)
{
    Export-ModuleMember -Variable "NUGET_EXE"
}
else
{
    Throw "Could not find any path to NuGet.exe."
}
#endregion Find NuGet path

#region Load config file
$IS_SettingsPath = Join-Path $Env:ProgramData "\IntelliSearch\.IntelliShell\settings.config"
if ( -not (Test-Path $IS_SettingsPath))
{
    $IS_SettingsFile = New-Item -Path $IS_SettingsPath -ItemType File -Force
    $IS_SettingsFile.Directory.Attributes = $IS_SettingsFile.Directory.Attributes -bor [io.fileattributes]::Hidden
}

$IS_Settings = ConvertFrom-Yaml -Yaml (Get-Content $IS_SettingsPath -Raw)

if ($IS_Settings -eq $null)
{
    $IS_Settings = New-Object -TypeName System.Collections.Hashtable
}

$IS_Settings.InstanceStore = if ($IS_Settings.InstanceStore) {$IS_Settings.InstanceStore} else {(Join-Path $env:ProgramData "\IntelliSearch")}
$IS_Settings.ComponentStore = if ($IS_Settings.ComponentStore) {$IS_Settings.ComponentStore} else {(Join-Path $env:ProgramFiles "\IntelliSearch")}
$IS_Settings.InstanceCreationPluginStore = if ($IS_Settings.InstanceCreationPluginStore) {$IS_Settings.InstanceCreationPluginStore} else {@((Join-Path (ModuleRoot) "\InstanceCreationPlugins"))}

Write-Verbose "Loaded settings file with following key-values:"
foreach ($Key in $IS_Settings.GetEnumerator())
{
    Write-Verbose ("${Key}: $($IS_Settings.$Key)")
}
Export-ModuleMember -Variable "IS_Settings"
#endregion Load config file