Integrom.Module.Bootstrap.psm1

$dynamicEnumBlock = @"
   public enum %%ENUM_NAME%% {
      %%ENUM_DATA%%
   }
"@


$IntegromLibraryExtension = 'psil'

function getPossibleLibPaths{[CmdletBinding()]param([string]$libName, [string]$baseLibPath, [System.Management.Automation.PSCmdlet]$session = $PSCmdlet)
   # [CmdletBinding()] is necessary to get the correct session to look for the _igmLibPaths environment variable
   [string[]]$pathSuffixes = '', '_lib\'
   [string[]]$result = @()
   
   $baseLibPath = $baseLibPath -replace '([\\/]+$)|(?<=\w$)', '\'
   $regexResult = [regex]::Match("$($baseLibPath)$($libName)", '(?<root>^[a-z]:|/|\.\.|\.|\\\\|(?:http|ftp|https|sftp)(://))?(?<path>.*(\\|/))?((?<libName>.+)(?<ext>\.psil$)|(?<libName>.+))', [Text.RegularExpressions.RegexOptions]::IgnoreCase)
   $root = $regexResult[0].Groups['root'].value
   $path = $regexResult[0].Groups['path'].value
   $libSname = $regexResult[0].Groups['libName'].value
   [string]$envPath = $ExecutionContext.SessionState.InvokeCommand.InvokeScript($session.SessionState,
      {param()
         if ($null -ne $_igmLibPaths) {return $_igmLibPaths} else {return $null}
      }.Ast.GetScriptBlock()
   )
   if ([string]::IsNullOrEmpty($root)) {
      $rootsToTest = '.\', '..\'
      if ($null -ne $envPath) {$rootsToTest += $envPath -replace '([\\/]+$)|(?<=\w$)', '\'}
      if (Test-Path -Path "$($env:ProgramFiles)\Integrom\Libraries\") {$rootsToTest += "$($env:ProgramFiles)\Integrom\Libraries\"}
   } else {
      $rootsToTest = $root
   }
   $pathSuffixes += "$($libSname)\"
   foreach ($rootEntry in $rootsToTest) {
      foreach ($pathSuffix in $pathSuffixes) {
         $result += "$($rootEntry)$($path)$($pathSuffix)$($libSname).$($IntegromLibraryExtension)"
      }
   }
   return $result
}

function getLoadedLibraries{[CmdletBinding()][OutputType([hashtable])]param([System.Management.Automation.PSCmdlet]$session)
   # [CmdletBinding()] is necessary to get the correct session state to search for loaded libraries
   if ($null -eq $session) {$session = $PSCmdlet}
   return $ExecutionContext.SessionState.InvokeCommand.InvokeScript($session.SessionState,
   {param()
      if ($null -ne $_loadedIgmLibraries) {return $_loadedIgmLibraries} else {return @{}}
   }.Ast.GetScriptBlock())
}

function loadLibraries{[CmdletBinding()][OutputType([hashtable])]param([string[]]$libraryNames, [string]$libPath, [System.Management.Automation.PSCmdlet]$session)
   # [CmdletBinding()] is necessary to get the correct session state to load the library into
   [hashtable]$resultData = @{statusCode = 0; failedLibs = @()}
   if ($libraryNames.Count -lt 1) {
      $resultData.statusCode = -5
      return $resultData
   }
   if ($null -eq $session) {$session = $PSCmdlet}
   foreach ($libraryName in $libraryNames) {
      $success = $false
      $librarySourceVersion = getLibrarySourceVersion($libraryName)
      if ((getLoadedLibraries $session)[$libraryName] -eq $librarySourceVersion) {
         Write-Host "INFO: Current version of $($libraryName) is already loaded"
         $success = $true
         continue
      }
      foreach ($fqLibPath in (getPossibleLibPaths -libName $libraryName -baseLibPath $libPath -session $session)) {
         if (Test-Path $fqLibPath) {
            Write-Host "INFO: Loading library $($libraryName)...."
            $rawLibCode = Get-Content -Path $fqLibPath -Raw -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
            if ($null -eq $rawLibCode) {$success = $false; break}
            $regexResult = [regex]::Match($rawLibCode, '^[\s|\t]*#_depends:[\s|\t]*(?<depends>.+)', [Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [Text.RegularExpressions.RegexOptions]::Multiline)
            $dependsRaw = $regexResult[0].Groups['depends'].value
            if (-not [string]::IsNullOrEmpty($dependsRaw)) {
               $dependsList = $dependsRaw -split ','
               foreach ($dependancy in $dependsList) {
                  $dependancyResult = loadLibraries ($dependancy.Trim()) $libPath $session
                  $resultData.statusCode += $dependancyResult.statusCode
                  foreach ($failedLib in $dependancyResult.failedLibs) {$resultData.failedLibs += $dependancyResult.failedLib}
               }
            }
            $parsedLibCode = $rawLibCode -replace '%%PSIL_LOCATION%%', ($fqLibPath -replace '(?:\\|/)[^\\|/]+$', '')
            try {
               $ExecutionContext.SessionState.InvokeCommand.InvokeScript($session.SessionState,
               {param($libCode, $libName, $libVersion)
                  if ($null -eq $_loadedIgmLibraries) {$_loadedIgmLibraries = @{}}
                  if ($null -eq (Invoke-Expression -Command ($libCode))) {[hashtable]$_loadedIgmLibraries.Add($libName, $libVersion)}
               }.Ast.GetScriptBlock(), $parsedLibCode, $libraryName, $librarySourceVersion)
               $success = $true
               break
            } catch {
               break
            }
         }
      }
      if (-not $success) {
         $resultData.statusCode -= 1
         $resultData.failedLibs += $libraryName
      }
   }
   return $resultData
}

function getLibrarySourceVersion{param([string]$libraryName, [string]$libPath)
   foreach ($fqLibPath in (getPossibleLibPaths -libName $libraryName -baseLibPath $libPath)) {
      if (Test-Path $fqLibPath) {
         $libHeader = Get-Content -Path $fqLibPath -Head 10 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-String
         $regexResult = [regex]::Match($libHeader, '^[\s|\t]*#_version:[\s|\t]*(?<version>[\w|.]+)', [Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [Text.RegularExpressions.RegexOptions]::Multiline)
         return $regexResult[0].Groups['version'].value
      }
   }
   return -1
}

function injectEnumFromJSON{[CmdletBinding()]param([string]$jsonFile, [string]$jsonKey, [string]$enumDef = $dynamicEnumBlock)
   # [CmdletBinding()] is necessary to get the correct session state to inject the Enum into
   [int]$v = 4001
   [scriptblock]$unboundCode = {param([string]$enumdef); Add-Type -TypeDefinition $enumdef}.Ast.GetScriptBlock()
   [PSCustomObject]$jsonFileData = Get-Content $jsonfile | ConvertFrom-Json
   if (($jsonFileData.$jsonKey).count -lt 1) {return -5}
   foreach ($item in $jsonFileData.$jsonKey) {$enumItems += "@$($item) = $($v),`n"; $v ++}
   $enumDef = $enumDef`
      -replace '%%ENUM_NAME%%', $jsonKey`
      -replace '%%ENUM_DATA%%', $enumItems
   try {
      $ExecutionContext.SessionState.InvokeCommand.InvokeScript($PSCmdlet.SessionState, $unboundCode, $enumDef)
      return 0
   } catch {
      return -17
   }
}