|
| 1 | +param([switch]$VerboseSwitch = $false) |
| 2 | + |
| 3 | +# $Verbose=$true -or $VerboseSwitch |
| 4 | +$Verbose=$VerboseSwitch |
| 5 | +$script = $MyInvocation.MyCommand |
| 6 | + |
| 7 | +<# |
| 8 | + .SYNOPSIS |
| 9 | + Loads PowerShell modules defined in a YAML configuration file. |
| 10 | +
|
| 11 | + .DESCRIPTION |
| 12 | + Reads modules.yml (or the path in $env:SnippetsModulesYaml), installs any |
| 13 | + missing modules from PSGallery, and imports them into the current session. |
| 14 | + Requires the powershell-yaml module for YAML parsing (auto-installed if missing). |
| 15 | +#> |
| 16 | + |
| 17 | +function Install-YamlModuleIfMissing { |
| 18 | + param([switch]$Verbose = $false) |
| 19 | + |
| 20 | + $yamlMod = Get-Module -ListAvailable -Name 'powershell-yaml' -ErrorAction SilentlyContinue |
| 21 | + if (-not $yamlMod) { |
| 22 | + Write-Verbose "[$script] Installing powershell-yaml from PSGallery..." -Verbose:$Verbose |
| 23 | + Install-Module -Name 'powershell-yaml' -Scope CurrentUser -Force -AllowClobber -AcceptLicense -ErrorAction Stop |
| 24 | + } |
| 25 | + Import-Module 'powershell-yaml' -ErrorAction Stop -Verbose:$false |
| 26 | +} |
| 27 | + |
| 28 | +function Import-SnippetsModules { |
| 29 | + param([switch]$Verbose = $false) |
| 30 | + |
| 31 | + # Determine YAML config path |
| 32 | + $yamlPath = $env:SnippetsModulesYaml |
| 33 | + if (-not $yamlPath -or -not (Test-Path $yamlPath)) { |
| 34 | + $yamlPath = Join-Path $env:Snippets -ChildPath 'modules.yml' |
| 35 | + } |
| 36 | + |
| 37 | + if (-not (Test-Path $yamlPath)) { |
| 38 | + Write-Verbose "[$script] No modules.yml found at [$yamlPath]. Skipping module auto-load." -Verbose:$Verbose |
| 39 | + return "No modules.yml found." |
| 40 | + } |
| 41 | + |
| 42 | + Write-Verbose "[$script] Loading module definitions from [$yamlPath]" -Verbose:$Verbose |
| 43 | + |
| 44 | + # Ensure powershell-yaml is available |
| 45 | + Install-YamlModuleIfMissing -Verbose:$Verbose |
| 46 | + |
| 47 | + # Parse YAML |
| 48 | + $yamlContent = Get-Content -Path $yamlPath -Raw -ErrorAction Stop |
| 49 | + $config = ConvertFrom-Yaml $yamlContent -ErrorAction Stop |
| 50 | + |
| 51 | + if (-not $config -or -not $config.modules) { |
| 52 | + Write-Verbose "[$script] modules.yml is empty or has no 'modules' key." -Verbose:$Verbose |
| 53 | + return "No modules defined." |
| 54 | + } |
| 55 | + |
| 56 | + $loaded = 0 |
| 57 | + $failed = 0 |
| 58 | + |
| 59 | + foreach ($entry in $config.modules) { |
| 60 | + $moduleName = $entry.name |
| 61 | + $moduleVersion = $entry.version |
| 62 | + $moduleSource = if ($entry.source) { $entry.source } else { 'PSGallery' } |
| 63 | + $moduleRequired = if ($null -ne $entry.required) { $entry.required } else { $true } |
| 64 | + $moduleParams = $entry.parameters |
| 65 | + |
| 66 | + if (-not $moduleName) { |
| 67 | + Write-Verbose "[$script] Skipping entry with no 'name' field." -Verbose:$Verbose |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + # Skip powershell-yaml since we already loaded it above |
| 72 | + if ($moduleName -ieq 'powershell-yaml') { |
| 73 | + $loaded++ |
| 74 | + continue |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + Write-Verbose "[$script] Processing module [$moduleName]..." -Verbose:$Verbose |
| 79 | + |
| 80 | + # Check if already imported |
| 81 | + $existing = Get-Module -Name $moduleName -ErrorAction SilentlyContinue |
| 82 | + if ($existing) { |
| 83 | + if (-not $moduleVersion -or $existing.Version -ge [Version]$moduleVersion) { |
| 84 | + Write-Verbose "[$script] Module [$moduleName] already imported." -Verbose:$Verbose |
| 85 | + $loaded++ |
| 86 | + continue |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + # Check if installed locally |
| 91 | + $installed = Get-Module -ListAvailable -Name $moduleName -ErrorAction SilentlyContinue |
| 92 | + |
| 93 | + $needsInstall = $false |
| 94 | + if (-not $installed) { |
| 95 | + $needsInstall = $true |
| 96 | + } |
| 97 | + elseif ($moduleVersion) { |
| 98 | + $bestVersion = ($installed | Sort-Object Version -Descending | Select-Object -First 1).Version |
| 99 | + if ($bestVersion -lt [Version]$moduleVersion) { |
| 100 | + $needsInstall = $true |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + # Install if needed |
| 105 | + if ($needsInstall) { |
| 106 | + if ($moduleSource -ine 'PSGallery' -and (Test-Path $moduleSource)) { |
| 107 | + Write-Verbose "[$script] Importing [$moduleName] from path [$moduleSource]" -Verbose:$Verbose |
| 108 | + } |
| 109 | + else { |
| 110 | + Write-Verbose "[$script] Installing [$moduleName] from PSGallery..." -Verbose:$Verbose |
| 111 | + $installParams = @{ |
| 112 | + Name = $moduleName |
| 113 | + Scope = 'CurrentUser' |
| 114 | + Force = $true |
| 115 | + AllowClobber = $true |
| 116 | + AcceptLicense = $true |
| 117 | + ErrorAction = 'Stop' |
| 118 | + } |
| 119 | + |
| 120 | + if ($moduleVersion) { |
| 121 | + $installParams['MinimumVersion'] = $moduleVersion |
| 122 | + } |
| 123 | + |
| 124 | + Install-Module @installParams |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + # Import the module |
| 129 | + $importParams = @{ |
| 130 | + Name = if ($moduleSource -ine 'PSGallery' -and (Test-Path $moduleSource)) { $moduleSource } else { $moduleName } |
| 131 | + ErrorAction = 'Stop' |
| 132 | + Verbose = $false |
| 133 | + } |
| 134 | + |
| 135 | + if ($moduleVersion -and $moduleSource -ieq 'PSGallery') { |
| 136 | + $importParams['MinimumVersion'] = $moduleVersion |
| 137 | + } |
| 138 | + |
| 139 | + if ($moduleParams) { |
| 140 | + $importParams['ArgumentList'] = $moduleParams |
| 141 | + } |
| 142 | + |
| 143 | + Import-Module @importParams |
| 144 | + Write-Verbose "[$script] Loaded [$moduleName] successfully." -Verbose:$Verbose |
| 145 | + $loaded++ |
| 146 | + } |
| 147 | + catch { |
| 148 | + $failed++ |
| 149 | + if ($moduleRequired) { |
| 150 | + Write-Error "[$script] Failed to load required module [$moduleName]: $_" |
| 151 | + } |
| 152 | + else { |
| 153 | + Write-Verbose "[$script] Optional module [$moduleName] failed to load: $_" -Verbose:$Verbose |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return "Module auto-load complete: $loaded loaded, $failed failed." |
| 159 | +} |
| 160 | + |
| 161 | +try { |
| 162 | + $result = Import-SnippetsModules -Verbose:$Verbose |
| 163 | + Write-Verbose "[$script] $result" -Verbose:$Verbose |
| 164 | + return $result |
| 165 | +} |
| 166 | +catch { |
| 167 | + Write-Error "[$script] Module auto-loader error: $_" |
| 168 | +} |
| 169 | +finally { |
| 170 | + Write-Verbose "[$script] Leaving..." -Verbose:$Verbose |
| 171 | + $Verbose = $VerboseSwitch |
| 172 | +} |
0 commit comments