|
| 1 | +#Requires -Version 7.4 |
| 2 | +<# |
| 3 | + .SYNOPSIS |
| 4 | + Package the Powershell module. |
| 5 | + .DESCRIPTION |
| 6 | + This script packages the Powershell module for distribution. |
| 7 | + It is intended to be called by MSBuild during the normal build |
| 8 | + process. The script will be called once for each target framework. |
| 9 | +#> |
| 10 | +[CmdletBinding()] |
| 11 | +param( |
| 12 | + [Parameter()] |
| 13 | + [string] |
| 14 | + [ValidateScript({ $_ -match '[a-zA-Z\.]+' }, ErrorMessage = "The module name '{0}' is invalid.")] |
| 15 | + $ModuleName, |
| 16 | + [Parameter()] |
| 17 | + [string] |
| 18 | + [ValidateScript({ Test-Path $_ }, ErrorMessage = "The path '{0}' is invalid.")] |
| 19 | + $TargetPath, |
| 20 | + [Parameter()] |
| 21 | + [string] |
| 22 | + [ValidateScript({ $_ -match 'net\d+\.?\d+' }, ErrorMessage = "The target framework '{0}' is invalid.")] |
| 23 | + $TargetFramework, |
| 24 | + [Parameter()] |
| 25 | + [string] |
| 26 | + [ValidateScript({ Test-Path $_ }, ErrorMessage = "The path '{0}' is invalid.")] |
| 27 | + $OutputDirectory, |
| 28 | + [Parameter()] |
| 29 | + [string] |
| 30 | + [ValidateScript({ $_ -match '\d+\.\d+\.\d+' }, ErrorMessage = "The version '{0}' is invalid.")] |
| 31 | + $MajorMinorPatch, |
| 32 | + [Parameter()] |
| 33 | + [string] |
| 34 | + $NuGetPreReleaseTag |
| 35 | +) |
| 36 | + |
| 37 | +$PSNativeCommandUseErrorActionPreference = $true |
| 38 | +$ErrorActionPreference = 'Stop' |
| 39 | + |
| 40 | +$excludedFiles = @("System.Management.Automation.dll", "JetBrains.Annotations.dll") |
| 41 | + |
| 42 | +$modulePath = Join-Path $OutputDirectory "PsModule" $ModuleName |
| 43 | +$isWindowsPowershell = $TargetFramework -like 'net4*' |
| 44 | +$moduleAssemblyPath = Join-Path $modulePath ($isWindowsPowershell ? 'desktop' : 'coreclr') |
| 45 | + |
| 46 | +# Prepare the output directory |
| 47 | +if (-not (Test-Path $modulePath)) { |
| 48 | + $null = New-Item -ItemType Directory -Path $modulePath -ErrorAction SilentlyContinue |
| 49 | +} |
| 50 | + |
| 51 | +# Copy the build output |
| 52 | +if (Test-Path $moduleAssemblyPath) { |
| 53 | + Remove-Item -Path $moduleAssemblyPath -Force -Recurse |
| 54 | +} |
| 55 | +$null = New-Item -ItemType Directory -Path $moduleAssemblyPath |
| 56 | +$targetDirectory = (Get-Item $TargetPath).Directory.FullName |
| 57 | +Copy-Item -Path (Join-Path $targetDirectory "*") -Destination $moduleAssemblyPath -Exclude $excludedFiles -Recurse |
| 58 | + |
| 59 | +# Prepare the module manifest |
| 60 | +$config = Get-Content (Join-Path $PSScriptRoot "$ModuleName.psd1") -Raw |
| 61 | +$config = $config.Replace("ModuleVersion = '0.1'", "ModuleVersion = '$MajorMinorPatch'"); |
| 62 | +if (-not [string]::IsNullOrWhiteSpace($NuGetPreReleaseTag)) { |
| 63 | + $config = $config.Replace("# Prerelease = ''", "Prerelease = '$NuGetPreReleaseTag'"); |
| 64 | +} |
| 65 | +Set-Content -Path (Join-Path $modulePath "$ModuleName.psd1") -Value $config |
| 66 | +Copy-Item -Path (Join-Path $PSScriptRoot "$ModuleName.psm1") -Destination $modulePath |
| 67 | + |
| 68 | +# This Powershell module requires the module Eryph.ClientRuntime.Configuration. |
| 69 | +# We download that module first to ensure that it is available. Otherwise, |
| 70 | +# the import during the test below would fail. |
| 71 | +$configData = Import-PowerShellDataFile (Join-Path $modulePath "$ModuleName.psd1") |
| 72 | +$clientRuntimeVersion = $configData.RequiredModules[0].ModuleVersion |
| 73 | +$clientRuntimeModulePath = Join-Path $OutputDirectory "PsModuleDependencies" "Eryph.ClientRuntime.Configuration" |
| 74 | +if (-not (Test-Path (Join-Path $clientRuntimeModulePath $clientRuntimeVersion))) { |
| 75 | + Save-Module -Path (Join-Path $OutputDirectory "PsModuleDependencies") -Name 'Eryph.ClientRuntime.Configuration' -AllowPrerelease -Force |
| 76 | +} |
| 77 | + |
| 78 | +# Verify that all Cmdlets are exposed in the manifest. We must load the modules |
| 79 | +# in separate Powershell processes to avoid conflicts. |
| 80 | +$powershell = $isWindowsPowershell ? 'powershell.exe' : 'pwsh.exe' |
| 81 | +$moduleCmdlets = (& $powershell -Command "Import-Module $clientRuntimeModulePath -RequiredVersion $clientRuntimeVersion; [array](Import-Module -Scope Local $modulePath -PassThru).ExportedCmdlets.Keys -join ','") -split ',' |
| 82 | +$assemblyCmdlets = (& $powershell -Command "[array](Import-Module -Scope Local $TargetPath -PassThru).ExportedCmdlets.Keys -join ','") -split ',' |
| 83 | +$missingCmdlets = [Linq.Enumerable]::Except($assemblyCmdlets, $moduleCmdlets) |
| 84 | +if ($missingCmdlets.Count -gt 0) { |
| 85 | + throw "The following Cmdlets are not exposed in the module manifest: $($missingCmdlets -join ', ')" |
| 86 | +} |
0 commit comments