Initialize-LocalWinGetModules.ps1 (8752B)
1 # Copyright (c) Microsoft Corporation. All rights reserved. 2 # Licensed under the MIT License. 3 4 <# 5 .SYNOPSIS 6 Helper script to setup the modules locally. 7 - Copies the PowerShell modules output into this location. 8 - Copies the modules files from the project because there's no guarantee they are updated in the module output 9 location. 10 - Adds the module location to PSModulePath if not there. 11 - Import Microsoft.WinGet.* modules. 12 13 .PARAMETER Platform 14 The platform we are building for. 15 16 .PARAMETER Configuration 17 The configuration we are building in. 18 #> 19 20 [CmdletBinding()] 21 param ( 22 [Parameter(Mandatory)] 23 [ValidateSet("Debug", "Release", "ReleaseStatic")] 24 [string] 25 $Configuration, 26 27 [ValidateSet("Client", "DSC", "Configuration", "All")] 28 [string] 29 $ModuleType = "All", 30 31 [string] 32 $BuildRoot = "", 33 34 [switch] 35 $SkipImportModule, 36 37 [switch] 38 $Clean 39 ) 40 41 class WinGetModule 42 { 43 [string]$Name 44 [string]$ModuleRoot 45 [string]$Output 46 [string[]]$ArchSpecificFiles = $null 47 48 WinGetModule([string]$n, [string]$m, [string]$o) 49 { 50 $this.Name = $n 51 $this.ModuleRoot = $m 52 $this.Output = Join-Path $o $this.Name 53 New-Item $this.Output -ItemType Directory -ErrorAction SilentlyContinue 54 55 if (Get-Module -Name $this.Name) 56 { 57 Remove-Module $this.Name -Force 58 } 59 } 60 61 [void]PrepareScriptFiles() 62 { 63 Write-Verbose "Copying script files: $($this.ModuleRoot) -> $($this.Output)" 64 xcopy $this.ModuleRoot $this.Output /d /s /f /y 65 } 66 67 [void]PrepareBinaryFiles([string] $buildRoot, [string] $config) 68 { 69 Write-Verbose "Copying binary files: $buildRoot\AnyCpu\$config\PowerShell\$($this.Name)\* -> $($this.Output)" 70 $copyErrors = $null 71 Copy-Item "$buildRoot\AnyCpu\$config\PowerShell\$($this.Name)\*" $this.Output -Force -Recurse -ErrorVariable copyErrors -ErrorAction SilentlyContinue 72 $copyErrors | ForEach-Object { Write-Warning $_ } 73 } 74 75 # Location is the path relative to the out module directory 76 [void]AddArchSpecificFiles([string[]] $files, [string]$location, [string] $buildRoot, [string] $config) 77 { 78 $x64Path = "$($this.Output)\$location\x64\" 79 $x86Path = "$($this.Output)\$location\x86\" 80 $arm64Path = "$($this.Output)\$location\arm64\" 81 if (-not (Test-Path $x64Path)) 82 { 83 New-Item $x64Path -ItemType directory 84 } 85 86 if (-not (Test-Path $x86Path)) 87 { 88 New-Item $x86Path -ItemType directory 89 } 90 91 if (-not (Test-Path $arm64Path)) 92 { 93 New-Item $arm64Path -ItemType directory 94 } 95 96 foreach ($f in $files) 97 { 98 $copyErrors = $null 99 Copy-Item "$buildRoot\x64\$config\$f" "$($this.Output)\$location\x64\" -Force -ErrorVariable copyErrors -ErrorAction SilentlyContinue 100 $copyErrors | ForEach-Object { Write-Warning $_ } 101 Copy-Item "$buildRoot\x86\$config\$f" "$($this.Output)\$location\x86\" -Force -ErrorVariable copyErrors -ErrorAction SilentlyContinue 102 $copyErrors | ForEach-Object { Write-Warning $_ } 103 Copy-Item "$buildRoot\arm64\$config\$f" "$($this.Output)\$location\arm64\" -Force -ErrorVariable copyErrors -ErrorAction SilentlyContinue 104 $copyErrors | ForEach-Object { Write-Warning $_ } 105 } 106 } 107 108 [void]AddAnyCpuSpecificFilesToArch([string[]] $files, [string]$location, [string] $buildRoot, [string] $config) 109 { 110 $x64Path = "$($this.Output)\$location\x64\" 111 $x86Path = "$($this.Output)\$location\x86\" 112 $arm64Path = "$($this.Output)\$location\arm64\" 113 if (-not (Test-Path $x64Path)) 114 { 115 New-Item $x64Path -ItemType directory 116 } 117 118 if (-not (Test-Path $x86Path)) 119 { 120 New-Item $x86Path -ItemType directory 121 } 122 123 if (-not (Test-Path $arm64Path)) 124 { 125 New-Item $arm64Path -ItemType directory 126 } 127 128 foreach ($f in $files) 129 { 130 $copyErrors = $null 131 Copy-Item "$buildRoot\AnyCpu\$config\$f" "$($this.Output)\$location\x64\" -Force -ErrorVariable copyErrors -ErrorAction SilentlyContinue 132 $copyErrors | ForEach-Object { Write-Warning $_ } 133 Copy-Item "$buildRoot\AnyCpu\$config\$f" "$($this.Output)\$location\x86\" -Force -ErrorVariable copyErrors -ErrorAction SilentlyContinue 134 $copyErrors | ForEach-Object { Write-Warning $_ } 135 Copy-Item "$buildRoot\AnyCpu\$config\$f" "$($this.Output)\$location\arm64\" -Force -ErrorVariable copyErrors -ErrorAction SilentlyContinue 136 $copyErrors | ForEach-Object { Write-Warning $_ } 137 } 138 } 139 } 140 141 [Flags()] enum ModuleType 142 { 143 None = 0 144 Client = 1 145 DSC = 2 146 Configuration = 4 147 } 148 149 $local:moduleToConfigure = [ModuleType]::None 150 switch ($ModuleType) 151 { 152 "Client" 153 { 154 $moduleToConfigure = [ModuleType]::Client; 155 break 156 } 157 158 "DSC" 159 { 160 $moduleToConfigure = [ModuleType]::DSC; 161 break 162 } 163 164 "Configuration" 165 { 166 $moduleToConfigure = [ModuleType]::Configuration; 167 break 168 } 169 170 "All" 171 { 172 $moduleToConfigure = [ModuleType]::Client + [ModuleType]::DSC + [ModuleType]::Configuration; 173 break 174 } 175 } 176 177 # I know it makes sense, but please don't do a clean up of $moduleRootOutput. When the modules are loaded 178 # there's no way to tell PowerShell to release the binary dlls that are loaded. 179 $local:moduleRootOutput = "$PSScriptRoot\Module\" 180 181 if ($BuildRoot -eq "") 182 { 183 $BuildRoot = "$PSScriptRoot\..\.."; 184 } 185 186 if ($Clean -and (Test-Path $moduleRootOutput)) 187 { 188 Remove-Item $moduleRootOutput -Recurse 189 } 190 191 # Modules, they should be in dependency order so that when importing we don't pick up the release modules. 192 $local:modules = @() 193 if ($moduleToConfigure.HasFlag([ModuleType]::Client)) 194 { 195 Write-Host "Setting up Microsoft.WinGet.Client" 196 $module = [WinGetModule]::new("Microsoft.WinGet.Client", "$PSScriptRoot\..\Microsoft.WinGet.Client\ModuleFiles\", $moduleRootOutput) 197 $module.PrepareBinaryFiles($BuildRoot, $Configuration) 198 $module.PrepareScriptFiles() 199 $additionalFiles = @( 200 "Microsoft.Management.Deployment.InProc\Microsoft.Management.Deployment.dll" 201 "Microsoft.Management.Deployment\Microsoft.Management.Deployment.winmd" 202 "WindowsPackageManager\WindowsPackageManager.dll" 203 "UndockedRegFreeWinRT\winrtact.dll" 204 ) 205 $module.AddArchSpecificFiles($additionalFiles, "net8.0-windows10.0.26100.0\SharedDependencies", $BuildRoot, $Configuration) 206 $module.AddArchSpecificFiles($additionalFiles, "net48\SharedDependencies", $BuildRoot, $Configuration) 207 $modules += $module 208 } 209 210 if ($moduleToConfigure.HasFlag([ModuleType]::DSC)) 211 { 212 Write-Host "Setting up Microsoft.WinGet.DSC" 213 $module = [WinGetModule]::new("Microsoft.WinGet.DSC", "$PSScriptRoot\..\Microsoft.WinGet.DSC\", $moduleRootOutput) 214 $module.PrepareScriptFiles() 215 $modules += $module 216 } 217 218 if ($moduleToConfigure.HasFlag([ModuleType]::Configuration)) 219 { 220 Write-Host "Setting up Microsoft.WinGet.Configuration" 221 $module = [WinGetModule]::new("Microsoft.WinGet.Configuration", "$PSScriptRoot\..\Microsoft.WinGet.Configuration\ModuleFiles\", $moduleRootOutput) 222 $module.PrepareBinaryFiles($BuildRoot, $Configuration) 223 $module.PrepareScriptFiles() 224 $additionalFiles = @( 225 "Microsoft.Management.Configuration\Microsoft.Management.Configuration.dll" 226 ) 227 $module.AddArchSpecificFiles($additionalFiles, "SharedDependencies", $BuildRoot, $Configuration) 228 $additionalFiles = @( 229 "Microsoft.Management.Configuration.Projection\net8.0-windows10.0.26100.0\Microsoft.Management.Configuration.Projection.dll" 230 ) 231 $module.AddAnyCpuSpecificFilesToArch($additionalFiles, "SharedDependencies", $BuildRoot, $Configuration) 232 $modules += $module 233 } 234 235 # Add it to module path if not there. 236 if (-not $env:PSModulePath.Contains($moduleRootOutput)) 237 { 238 Write-Host "Added $moduleRootOutput to PSModulePath" -ForegroundColor Green 239 $env:PSModulePath += ";$moduleRootOutput" 240 } 241 242 # Now import modules. 243 if (-not $SkipImportModule) 244 { 245 foreach($module in $modules) 246 { 247 Write-Host "Importing module $($module.Name)" -ForegroundColor Green 248 Import-Module "$moduleRootOutput\$($module.Name)\$($module.Name).psd1" -Force 249 } 250 }