| |
| |
|
|
| . "$PSScriptRoot\..\buildCommon\startNativeExecution.ps1" |
|
|
| $Environment = Get-EnvironmentInformation |
| $RepoRoot = (Resolve-Path -Path "$PSScriptRoot/../..").Path |
|
|
| $packagingStrings = Import-PowerShellDataFile "$PSScriptRoot\packaging.strings.psd1" |
| Import-Module "$PSScriptRoot\..\Xml" -ErrorAction Stop -Force |
| $DebianDistributions = @("deb") |
| $RedhatFullDistributions = @("rh") |
| $RedhatFddDistributions = @("cm") |
| $RedhatDistributions = @() |
| $RedhatDistributions += $RedhatFullDistributions |
| $RedhatDistributions += $RedhatFddDistributions |
| $AllDistributions = @() |
| $AllDistributions += $DebianDistributions |
| $AllDistributions += $RedhatDistributions |
| $AllDistributions += 'macOs' |
| $script:netCoreRuntime = 'net11.0' |
| $script:iconFileName = "Powershell_black_64.png" |
| $script:iconPath = Join-Path -path $PSScriptRoot -ChildPath "../../assets/$iconFileName" -Resolve |
|
|
| class R2RVerification { |
| [ValidateSet('NoR2R','R2R','SdkOnly')] |
| [string] |
| $R2RState = 'R2R' |
|
|
| [System.Reflection.PortableExecutable.Machine] |
| $Architecture = [System.Reflection.PortableExecutable.Machine]::Amd64 |
|
|
| [ValidateSet('Linux','Apple','Windows')] |
| [string] |
| $OperatingSystem = 'Windows' |
| } |
|
|
| function Start-PSPackage { |
| [CmdletBinding(DefaultParameterSetName='Version',SupportsShouldProcess=$true)] |
| param( |
| # PowerShell packages use Semantic Versioning https://semver.org/ |
| [Parameter(ParameterSetName = "Version")] |
| [string]$Version, |
|
|
| [Parameter(ParameterSetName = "ReleaseTag")] |
| [ValidatePattern("^v\d+\.\d+\.\d+(-\w+(\.\d{1,2})?)?$")] |
| [ValidateNotNullOrEmpty()] |
| [string]$ReleaseTag, |
|
|
| # Package name |
| [ValidatePattern("^powershell")] |
| [string]$Name = "powershell", |
|
|
| # Ubuntu, CentOS, Fedora, macOS, and Windows packages are supported |
| [ValidateSet("msix", "deb", "osxpkg", "rpm", "rpm-fxdependent", "rpm-fxdependent-arm64", "zip", "zip-pdb", "tar", "tar-arm", "tar-arm64", "tar-alpine", "fxdependent", "fxdependent-win-desktop", "min-size", "tar-alpine-fxdependent")] |
| [string[]]$Type, |
|
|
| # Generate windows downlevel package |
| [ValidateSet("win7-x86", "win7-x64", "win-arm", "win-arm64")] |
| [string] $WindowsRuntime, |
|
|
| [ValidateSet('osx-x64', 'osx-arm64')] |
| [ValidateScript({$Environment.IsMacOS})] |
| [string] $MacOSRuntime, |
|
|
| [string] $PackageBinPath, |
|
|
| [switch] $Private, |
|
|
| [Switch] $Force, |
|
|
| [Switch] $SkipReleaseChecks, |
|
|
| [switch] $NoSudo, |
|
|
| [switch] $LTS |
| ) |
|
|
| DynamicParam { |
| if ($Type -in ('zip', 'min-size') -or $Type -like 'fxdependent*') { |
| |
| |
| $ParameterAttr = New-Object "System.Management.Automation.ParameterAttribute" |
| $Attributes = New-Object "System.Collections.ObjectModel.Collection``1[System.Attribute]" |
| $Attributes.Add($ParameterAttr) > $null |
|
|
| $Parameter = New-Object "System.Management.Automation.RuntimeDefinedParameter" -ArgumentList ("IncludeSymbols", [switch], $Attributes) |
| $Dict = New-Object "System.Management.Automation.RuntimeDefinedParameterDictionary" |
| $Dict.Add("IncludeSymbols", $Parameter) > $null |
| return $Dict |
| } |
| } |
|
|
| End { |
| $IncludeSymbols = $null |
| if ($PSBoundParameters.ContainsKey('IncludeSymbols')) { |
| Write-Log 'setting IncludeSymbols' |
| $IncludeSymbols = $PSBoundParameters['IncludeSymbols'] |
| } |
|
|
| |
| ($Runtime, $Configuration) = if ($WindowsRuntime) { |
| $WindowsRuntime, "Release" |
| } elseif ($MacOSRuntime) { |
| $MacOSRuntime, "Release" |
| } elseif ($Type.Count -eq 1 -and $Type[0] -eq "tar-alpine") { |
| New-PSOptions -Configuration "Release" -Runtime "linux-musl-x64" -WarningAction SilentlyContinue | ForEach-Object { $_.Runtime, $_.Configuration } |
| } elseif ($Type.Count -eq 1 -and $Type[0] -eq "tar-arm") { |
| New-PSOptions -Configuration "Release" -Runtime "Linux-ARM" -WarningAction SilentlyContinue | ForEach-Object { $_.Runtime, $_.Configuration } |
| } elseif ($Type.Count -eq 1 -and $Type[0] -eq "tar-arm64") { |
| if ($IsMacOS) { |
| New-PSOptions -Configuration "Release" -Runtime "osx-arm64" -WarningAction SilentlyContinue | ForEach-Object { $_.Runtime, $_.Configuration } |
| } else { |
| New-PSOptions -Configuration "Release" -Runtime "Linux-ARM64" -WarningAction SilentlyContinue | ForEach-Object { $_.Runtime, $_.Configuration } |
| } |
| } elseif ($Type.Count -eq 1 -and $Type[0] -eq "rpm-fxdependent") { |
| New-PSOptions -Configuration "Release" -Runtime 'fxdependent-linux-x64' -WarningAction SilentlyContinue | ForEach-Object { $_.Runtime, $_.Configuration } |
| } elseif ($Type.Count -eq 1 -and $Type[0] -eq "rpm-fxdependent-arm64") { |
| New-PSOptions -Configuration "Release" -Runtime 'fxdependent-linux-arm64' -WarningAction SilentlyContinue | ForEach-Object { $_.Runtime, $_.Configuration } |
| } |
| elseif ($Type.Count -eq 1 -and $Type[0] -eq "tar-alpine-fxdependent") { |
| New-PSOptions -Configuration "Release" -Runtime 'fxdependent-noopt-linux-musl-x64' -WarningAction SilentlyContinue | ForEach-Object { $_.Runtime, $_.Configuration } |
| } |
| else { |
| New-PSOptions -Configuration "Release" -WarningAction SilentlyContinue | ForEach-Object { $_.Runtime, $_.Configuration } |
| } |
|
|
| if ($Environment.IsWindows) { |
| |
| |
| switch ($Runtime) { |
| "win-arm64" { $NameSuffix = "win-arm64" } |
| default { $NameSuffix = $_ -replace 'win\d+', 'win' } |
| } |
| } |
|
|
| if ($Type -eq 'fxdependent') { |
| $NameSuffix = "win-fxdependent" |
| Write-Log "Packaging : '$Type'; Packaging Configuration: '$Configuration', Runtime: '$Runtime'" |
| } elseif ($Type -eq 'fxdependent-win-desktop') { |
| $NameSuffix = "win-fxdependentWinDesktop" |
| Write-Log "Packaging : '$Type'; Packaging Configuration: '$Configuration', Runtime: '$Runtime'" |
| } elseif ($MacOSRuntime) { |
| $NameSuffix = $MacOSRuntime |
| Write-Log "Packaging : '$Type'; Packaging Configuration: '$Configuration', Runtime: '$Runtime'" |
| } else { |
| Write-Log "Packaging RID: '$Runtime'; Packaging Configuration: '$Configuration'" |
| } |
|
|
| $Script:Options = Get-PSOptions |
| $actualParams = @() |
|
|
| $PSModuleRestoreCorrect = $false |
|
|
| |
| |
| if (!$IncludeSymbols.IsPresent -and $Script:Options.PSModuleRestore) { |
| $actualParams += '-PSModuleRestore' |
| $PSModuleRestoreCorrect = $true |
| } |
| elseif ($IncludeSymbols.IsPresent -and !$Script:Options.PSModuleRestore) { |
| $PSModuleRestoreCorrect = $true |
| } |
| else { |
| $actualParams += '-PSModuleRestore' |
| } |
|
|
| $precheckFailed = if ($Type -like 'fxdependent*' -or $Type -eq 'tar-alpine') { |
| |
| -not $Script:Options -or |
| -not $PSModuleRestoreCorrect -or |
| $Script:Options.Configuration -ne $Configuration -or |
| $Script:Options.Framework -ne $script:netCoreRuntime |
| } else { |
| -not $Script:Options -or |
| -not $PSModuleRestoreCorrect -or |
| $Script:Options.Runtime -ne $Runtime -or |
| $Script:Options.Configuration -ne $Configuration -or |
| $Script:Options.Framework -ne $script:netCoreRuntime |
| } |
|
|
| |
| if ($precheckFailed) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| Write-Warning -Message "Start-PSPackage: The build PreCheck has failed." |
| if (-not $Script:Options) { |
| Write-Warning -Message "Start-PSPackage: builid options variable is null indicating Start-PSBuild hasn't been run yet." |
| } |
| if (-not $PSModuleRestoreCorrect) { |
| Write-Warning -Message "Start-PSPackage: PSModuleRestoreCorrect variable is null indicating build -PSModuleRestore was not performed." |
| } |
| if ($Script:Options.Configuration -ne $Configuration) { |
| Write-Warning -Message "Start-PSPackage: Build configuration is incorrect: Expected: $Configuration Actual: $($Script:Options.Configuration)" |
| } |
| if ($Script:Options.Framework -ne $script:netCoreRuntime) { |
| Write-Warning -Message "Start-PSPackage: Build .NET version is incorrect: Expected: $($script:netCoreRuntime) Actual: $($Script:Options.Framework)" |
| } |
| if (($Type -notlike 'fxdependent*' -and $Type -ne 'tar-alpine') -and ($Script:Options.Runtime -ne $Runtime)) { |
| Write-Warning -Message "Start-PSPackage: Build RID does not match expected RID: Expected: $Runtime Actual: $($Script:Options.Runtime)" |
| } |
|
|
| $params = @('-Clean') |
|
|
| if (!$IncludeSymbols.IsPresent) { |
| $params += '-PSModuleRestore' |
| } |
|
|
| $actualParams += '-Runtime ' + $Script:Options.Runtime |
|
|
| if ($Type -eq 'fxdependent') { |
| $params += '-Runtime', 'fxdependent' |
| } elseif ($Type -eq 'fxdependent-win-desktop') { |
| $params += '-Runtime', 'fxdependent-win-desktop' |
| } else { |
| $params += '-Runtime', $Runtime |
| } |
|
|
| $params += '-Configuration', $Configuration |
| $actualParams += '-Configuration ' + $Script:Options.Configuration |
|
|
| Write-Warning "Build started with unexpected parameters 'Start-PSBuild $actualParams" |
| throw "Please ensure you have run 'Start-PSBuild $params'!" |
| } |
|
|
| if ($SkipReleaseChecks.IsPresent) { |
| Write-Warning "Skipping release checks." |
| } |
| elseif (!$Script:Options.RootInfo.IsValid){ |
| throw $Script:Options.RootInfo.Warning |
| } |
|
|
| |
| if ($PSCmdlet.ParameterSetName -eq "ReleaseTag") { |
| $Version = $ReleaseTag -Replace '^v' |
| } |
|
|
| |
| if (-not $Version) { |
| $Version = (git --git-dir="$RepoRoot/.git" describe) -Replace '^v' |
| } |
|
|
| $Source = if ($PackageBinPath) { |
| $PackageBinPath |
| } |
| else { |
| Split-Path -Path $Script:Options.Output -Parent |
| } |
|
|
| Write-Verbose -Verbose "Source: $Source" |
|
|
| |
| Copy-Item "$RepoRoot/ThirdPartyNotices.txt" -Destination $Source -Force |
|
|
| |
| Copy-Item "$RepoRoot/assets/default.help.txt" -Destination "$Source/en-US" -Force |
|
|
| if (-not $SkipGenerateReleaseFiles -and -not $env:TF_BUILD) { |
| |
| $psOptionsPath = (Join-Path -Path $Source "psoptions.json") |
| if (-not (Test-Path -Path $psOptionsPath)) { |
| $createdOptionsFile = New-Item -Path $psOptionsPath -Force |
| Write-Verbose -Verbose "Created psoptions file: $createdOptionsFile" |
| } |
|
|
| |
| $manifestSpdxPath = (Join-Path -Path $Source "_manifest\spdx_2.2\manifest.spdx.json") |
| if (-not (Test-Path -Path $manifestSpdxPath)) { |
| $createdSpdxPath = New-Item -Path $manifestSpdxPath -Force |
| Write-Verbose -Verbose "Created manifest.spdx.json file: $createdSpdxPath" |
| } |
|
|
| $manifestSpdxPathSha = (Join-Path -Path $Source "_manifest\spdx_2.2\manifest.spdx.json.sha256") |
| if (-not (Test-Path -Path $manifestSpdxPathSha)) { |
| $createdSpdxPathSha = New-Item -Path $manifestSpdxPathSha -Force |
| Write-Verbose -Verbose "Created manifest.spdx.json.sha256 file: $createdSpdxPathSha" |
| } |
|
|
| $bsiJsonPath = (Join-Path -Path $Source "_manifest\spdx_2.2\bsi.json") |
| if (-not (Test-Path -Path $bsiJsonPath)) { |
| $createdBsiJsonPath = New-Item -Path $bsiJsonPath -Force |
| Write-Verbose -Verbose "Created bsi.json file: $createdBsiJsonPath" |
| } |
|
|
| $manifestCatPath = (Join-Path -Path $Source "_manifest\spdx_2.2\manifest.cat") |
| if (-not (Test-Path -Path $manifestCatPath)) { |
| $createdCatPath = New-Item -Path $manifestCatPath -Force |
| Write-Verbose -Verbose "Created manifest.cat file: $createdCatPath" |
| } |
| } |
|
|
| |
| if ($IncludeSymbols.IsPresent) |
| { |
| $publishSource = $Source |
| $buildSource = Split-Path -Path $Source -Parent |
| $Source = New-TempFolder |
| $symbolsSource = New-TempFolder |
|
|
| try |
| { |
| |
| Get-ChildItem -Path $publishSource | Copy-Item -Destination $Source -Recurse |
|
|
| $signingXml = [xml] (Get-Content (Join-Path $PSScriptRoot "..\releaseBuild\signing.xml" -Resolve)) |
| |
| $filesToInclude = $signingXml.SignConfigXML.job.file.src | Where-Object { -not $_.endswith('pwsh.exe') -and ($_.endswith(".dll") -or $_.endswith(".exe")) } | ForEach-Object { ($_ -split '\\')[-1] } |
| $filesToInclude += $filesToInclude | ForEach-Object { $_ -replace '.dll', '.pdb' } |
| Get-ChildItem -Path $buildSource | Where-Object { $_.Name -in $filesToInclude } | Copy-Item -Destination $symbolsSource -Recurse |
|
|
| |
| $zipSource = Join-Path $symbolsSource -ChildPath '*' |
| $zipPath = Join-Path -Path $Source -ChildPath 'symbols.zip' |
| Save-PSOptions -PSOptionsPath (Join-Path -Path $source -ChildPath 'psoptions.json') -Options $Script:Options |
| Compress-Archive -Path $zipSource -DestinationPath $zipPath |
| } |
| finally |
| { |
| Remove-Item -Path $symbolsSource -Recurse -Force -ErrorAction SilentlyContinue |
| } |
| } |
|
|
| Write-Log "Packaging Source: '$Source'" |
|
|
| |
| if (-not $Type) { |
| $Type = if ($Environment.IsLinux) { |
| if ($Environment.LinuxInfo.ID -match "ubuntu") { |
| "deb", "tar" |
| } elseif ($Environment.IsRedHatFamily) { |
| "rpm" |
| } elseif ($Environment.IsSUSEFamily) { |
| "rpm" |
| } else { |
| throw "Building packages for $($Environment.LinuxInfo.PRETTY_NAME) is unsupported!" |
| } |
| } elseif ($Environment.IsMacOS) { |
| "osxpkg", "tar" |
| } elseif ($Environment.IsWindows) { |
| "zip", "msix" |
| } |
| Write-Warning "-Type was not specified, continuing with $Type!" |
| } |
| Write-Log "Packaging Type: $Type" |
|
|
| |
| |
| if ($IncludeSymbols.IsPresent -and $NameSuffix) { |
| $NameSuffix = "symbols-$NameSuffix" |
| } |
| elseif ($IncludeSymbols.IsPresent) { |
| $NameSuffix = "symbols" |
| } |
|
|
| switch ($Type) { |
| "zip" { |
| $os, $architecture = ($Script:Options.Runtime -split '-') |
| $peOS = ConvertTo-PEOperatingSystem -OperatingSystem $os |
| $peArch = ConvertTo-PEArchitecture -Architecture $architecture |
|
|
| $Arguments = @{ |
| PackageNameSuffix = $NameSuffix |
| PackageSourcePath = $Source |
| PackageVersion = $Version |
| Force = $Force |
| } |
|
|
| if ($architecture -in 'x86', 'x64', 'arm', 'arm64') { |
| $Arguments += @{ R2RVerification = [R2RVerification]@{ |
| R2RState = 'R2R' |
| OperatingSystem = $peOS |
| Architecture = $peArch |
| } |
| } |
| } else { |
| $Arguments += @{ R2RVerification = [R2RVerification]@{ |
| R2RState = 'SdkOnly' |
| OperatingSystem = $peOS |
| Architecture = $peArch |
| } |
| } |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create Zip Package")) { |
| New-ZipPackage @Arguments |
| } |
| } |
| "zip-pdb" { |
| $Arguments = @{ |
| PackageNameSuffix = $NameSuffix |
| PackageSourcePath = $Source |
| PackageVersion = $Version |
| Force = $Force |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create Symbols Zip Package")) { |
| New-PdbZipPackage @Arguments |
| } |
| } |
| "min-size" { |
| |
| if ($Environment.IsWindows) { |
| $Arguments = @{ |
| PackageNameSuffix = "$NameSuffix-gc" |
| PackageSourcePath = $Source |
| PackageVersion = $Version |
| Force = $Force |
| R2RVerification = [R2RVerification]@{ |
| R2RState = 'SdkOnly' |
| OperatingSystem = "Windows" |
| Architecture = "amd64" |
| } |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create Zip Package")) { |
| New-ZipPackage @Arguments |
| } |
| } |
| elseif ($Environment.IsLinux) { |
| $Arguments = @{ |
| PackageSourcePath = $Source |
| Name = $Name |
| PackageNameSuffix = 'gc' |
| Version = $Version |
| Force = $Force |
| R2RVerification = [R2RVerification]@{ |
| R2RState = 'SdkOnly' |
| } |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create tar.gz Package")) { |
| New-TarballPackage @Arguments |
| } |
| } |
| } |
| { $_ -like "fxdependent*"} { |
| if ($Environment.IsWindows) { |
| $Arguments = @{ |
| PackageNameSuffix = $NameSuffix |
| PackageSourcePath = $Source |
| PackageVersion = $Version |
| Force = $Force |
| R2RVerification = [R2RVerification]@{ |
| R2RState = 'NoR2R' |
| } |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create Zip Package")) { |
| New-ZipPackage @Arguments |
| } |
| } elseif ($Environment.IsLinux) { |
| $Arguments = @{ |
| PackageSourcePath = $Source |
| Name = $Name |
| PackageNameSuffix = 'fxdependent' |
| Version = $Version |
| Force = $Force |
| R2RVerification = [R2RVerification]@{ |
| R2RState = 'NoR2R' |
| } |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create tar.gz Package")) { |
| New-TarballPackage @Arguments |
| } |
| } |
| } |
| "tar-alpine-fxdependent" { |
| if ($Environment.IsLinux) { |
| $Arguments = @{ |
| PackageSourcePath = $Source |
| Name = $Name |
| PackageNameSuffix = 'musl-noopt-fxdependent' |
| Version = $Version |
| Force = $Force |
| R2RVerification = [R2RVerification]@{ |
| R2RState = 'NoR2R' |
| OperatingSystem = "Linux" |
| } |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create tar.gz Package")) { |
| New-TarballPackage @Arguments |
| } |
| } |
| } |
| "msix" { |
| $Arguments = @{ |
| ProductNameSuffix = $NameSuffix |
| ProductSourcePath = $Source |
| ProductVersion = $Version |
| Architecture = $WindowsRuntime.Split('-')[1] |
| Force = $Force |
| Private = $Private |
| LTS = $LTS |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create MSIX Package")) { |
| New-MSIXPackage @Arguments |
| } |
| } |
| "tar" { |
| $Arguments = @{ |
| PackageSourcePath = $Source |
| Name = $Name |
| Version = $Version |
| Force = $Force |
| } |
|
|
| if ($MacOSRuntime) { |
| $architecture = $MacOSRuntime.Split('-')[1] |
| $Arguments['Architecture'] = $architecture |
| } |
|
|
| if ($Script:Options.Runtime -match '(linux|osx).*') { |
| $os, $architecture = ($Script:Options.Runtime -split '-') |
| $peOS = ConvertTo-PEOperatingSystem -OperatingSystem $os |
| $peArch = ConvertTo-PEArchitecture -Architecture $architecture |
|
|
| $Arguments['R2RVerification'] = [R2RVerification]@{ |
| R2RState = "R2R" |
| OperatingSystem = $peOS |
| Architecture = $peArch |
| } |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create tar.gz Package")) { |
| New-TarballPackage @Arguments |
| } |
| } |
| "tar-arm" { |
| $peArch = ConvertTo-PEArchitecture -Architecture 'arm' |
| $Arguments = @{ |
| PackageSourcePath = $Source |
| Name = $Name |
| Version = $Version |
| Force = $Force |
| Architecture = "arm32" |
| ExcludeSymbolicLinks = $true |
| R2RVerification = [R2RVerification]@{ |
| R2RState = 'R2R' |
| OperatingSystem = "Linux" |
| Architecture = $peArch |
| } |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create tar.gz Package")) { |
| New-TarballPackage @Arguments |
| } |
| } |
| "tar-arm64" { |
| $Arguments = @{ |
| PackageSourcePath = $Source |
| Name = $Name |
| Version = $Version |
| Force = $Force |
| Architecture = "arm64" |
| ExcludeSymbolicLinks = $true |
| R2RVerification = [R2RVerification]@{ |
| R2RState = 'R2R' |
| OperatingSystem = "Linux" |
| Architecture = "arm64" |
| } |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create tar.gz Package")) { |
| New-TarballPackage @Arguments |
| } |
| } |
| "tar-alpine" { |
| $Arguments = @{ |
| PackageSourcePath = $Source |
| Name = $Name |
| Version = $Version |
| Force = $Force |
| Architecture = "musl-x64" |
| ExcludeSymbolicLinks = $true |
| R2RVerification = [R2RVerification]@{ |
| R2RState = 'R2R' |
| OperatingSystem = "Linux" |
| Architecture = "amd64" |
| } |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create tar.gz Package")) { |
| New-TarballPackage @Arguments |
| } |
| } |
| 'deb' { |
| $Arguments = @{ |
| Type = 'deb' |
| PackageSourcePath = $Source |
| Name = $Name |
| Version = $Version |
| Force = $Force |
| NoSudo = $NoSudo |
| LTS = $LTS |
| HostArchitecture = "amd64" |
| } |
| foreach ($Distro in $Script:DebianDistributions) { |
| $Arguments["Distribution"] = $Distro |
| if ($PSCmdlet.ShouldProcess("Create DEB Package for $Distro")) { |
| New-UnixPackage @Arguments |
| } |
| } |
| } |
| 'rpm' { |
| $Arguments = @{ |
| Type = 'rpm' |
| PackageSourcePath = $Source |
| Name = $Name |
| Version = $Version |
| Force = $Force |
| NoSudo = $NoSudo |
| LTS = $LTS |
| HostArchitecture = "x86_64" |
| } |
| foreach ($Distro in $Script:RedhatFullDistributions) { |
| $Arguments["Distribution"] = $Distro |
| if ($PSCmdlet.ShouldProcess("Create RPM Package for $Distro")) { |
| Write-Verbose -Verbose "Creating RPM Package for $Distro" |
| New-UnixPackage @Arguments |
| } |
| } |
| } |
| 'rpm-fxdependent' { |
| $Arguments = @{ |
| Type = 'rpm' |
| PackageSourcePath = $Source |
| Name = $Name |
| Version = $Version |
| Force = $Force |
| NoSudo = $NoSudo |
| LTS = $LTS |
| HostArchitecture = "x86_64" |
| } |
| foreach ($Distro in $Script:RedhatFddDistributions) { |
| $Arguments["Distribution"] = $Distro |
| if ($PSCmdlet.ShouldProcess("Create RPM Package for $Distro")) { |
| Write-Verbose -Verbose "Creating RPM Package for $Distro" |
| New-UnixPackage @Arguments |
| } |
| } |
| } |
| 'rpm-fxdependent-arm64' { |
| $Arguments = @{ |
| Type = 'rpm' |
| PackageSourcePath = $Source |
| Name = $Name |
| Version = $Version |
| Force = $Force |
| NoSudo = $NoSudo |
| LTS = $LTS |
| HostArchitecture = "aarch64" |
| } |
| foreach ($Distro in $Script:RedhatFddDistributions) { |
| $Arguments["Distribution"] = $Distro |
| if ($PSCmdlet.ShouldProcess("Create RPM Package for $Distro")) { |
| Write-Verbose -Verbose "Creating RPM Package for $Distro" |
| New-UnixPackage @Arguments |
| } |
| } |
| } |
| 'osxpkg' { |
| $HostArchitecture = "x86_64" |
| if ($MacOSRuntime -match "-arm64") { |
| $HostArchitecture = "arm64" |
| } |
| Write-Verbose "HostArchitecture = $HostArchitecture" -Verbose |
|
|
| $Arguments = @{ |
| Type = 'osxpkg' |
| PackageSourcePath = $Source |
| Name = $Name |
| Version = $Version |
| Force = $Force |
| NoSudo = $NoSudo |
| LTS = $LTS |
| HostArchitecture = $HostArchitecture |
| } |
|
|
|
|
| if ($PSCmdlet.ShouldProcess("Create macOS Package")) { |
| New-UnixPackage @Arguments |
| } |
| } |
| default { |
| $Arguments = @{ |
| Type = $_ |
| PackageSourcePath = $Source |
| Name = $Name |
| Version = $Version |
| Force = $Force |
| NoSudo = $NoSudo |
| LTS = $LTS |
| HostArchitecture = "all" |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create $_ Package")) { |
| New-UnixPackage @Arguments |
| } |
| } |
| } |
|
|
| if ($IncludeSymbols.IsPresent) |
| { |
| |
| Remove-Item -Path $Source -Recurse -Force -ErrorAction SilentlyContinue |
| } |
| } |
| } |
|
|
| function New-TarballPackage { |
| [CmdletBinding(SupportsShouldProcess=$true)] |
| param ( |
| [Parameter(Mandatory)] |
| [string] $PackageSourcePath, |
|
|
| # Must start with 'powershell' but may have any suffix |
| [Parameter(Mandatory)] |
| [ValidatePattern("^powershell")] |
| [string] $Name, |
|
|
| # Suffix of the Name |
| [string] $PackageNameSuffix, |
|
|
| [Parameter(Mandatory)] |
| [string] $Version, |
|
|
| [Parameter()] |
| [string] $Architecture = "x64", |
|
|
| [switch] $Force, |
|
|
| [switch] $ExcludeSymbolicLinks, |
|
|
| [string] $CurrentLocation = (Get-Location), |
|
|
| [R2RVerification] $R2RVerification |
| ) |
|
|
| if ($PackageNameSuffix) { |
| $packageName = "$Name-$Version-{0}-$Architecture-$PackageNameSuffix.tar.gz" |
| } else { |
| $packageName = "$Name-$Version-{0}-$Architecture.tar.gz" |
| } |
|
|
| if ($Environment.IsWindows) { |
| throw "Must be on Linux or macOS to build 'tar.gz' packages!" |
| } elseif ($Environment.IsLinux) { |
| $packageName = $packageName -f "linux" |
| } elseif ($Environment.IsMacOS) { |
| $packageName = $packageName -f "osx" |
| } |
|
|
| $packagePath = Join-Path -Path $CurrentLocation -ChildPath $packageName |
| Write-Verbose "Create package $packageName" |
| Write-Verbose "Package destination path: $packagePath" |
|
|
| if (Test-Path -Path $packagePath) { |
| if ($Force -or $PSCmdlet.ShouldProcess("Overwrite existing package file")) { |
| Write-Verbose "Overwrite existing package file at $packagePath" -Verbose |
| Remove-Item -Path $packagePath -Force -ErrorAction Stop -Confirm:$false |
| } |
| } |
|
|
| $Staging = "$PSScriptRoot/staging" |
| New-StagingFolder -StagingPath $Staging -PackageSourcePath $PackageSourcePath -R2RVerification $R2RVerification |
|
|
| if (Get-Command -Name tar -CommandType Application -ErrorAction Ignore) { |
| if ($Force -or $PSCmdlet.ShouldProcess("Create tarball package")) { |
| $options = "-czf" |
| if ($PSBoundParameters.ContainsKey('Verbose') -and $PSBoundParameters['Verbose'].IsPresent) { |
| |
| $options = "-czvf" |
| } |
|
|
| try { |
| Push-Location -Path $Staging |
| tar $options $packagePath * |
| } finally { |
| Pop-Location |
| } |
|
|
| if (Test-Path -Path $packagePath) { |
| Write-Log "You can find the tarball package at $packagePath" |
| return (Get-Item $packagePath) |
| } else { |
| throw "Failed to create $packageName" |
| } |
| } |
| } else { |
| throw "Failed to create the package because the application 'tar' cannot be found" |
| } |
| } |
|
|
| function New-TempFolder |
| { |
| $tempPath = [System.IO.Path]::GetTempPath() |
|
|
| $tempFolder = Join-Path -Path $tempPath -ChildPath ([System.IO.Path]::GetRandomFileName()) |
| if (!(Test-Path -Path $tempFolder)) |
| { |
| $null = New-Item -Path $tempFolder -ItemType Directory |
| } |
|
|
| return $tempFolder |
| } |
|
|
| function New-PSSignedBuildZip |
| { |
| param( |
| [Parameter(Mandatory)] |
| [string]$BuildPath, |
| [Parameter(Mandatory)] |
| [string]$SignedFilesPath, |
| [Parameter(Mandatory)] |
| [string]$DestinationFolder, |
| [parameter(HelpMessage='VSTS variable to set for path to zip')] |
| [string]$VstsVariableName |
| ) |
|
|
| Update-PSSignedBuildFolder -BuildPath $BuildPath -SignedFilesPath $SignedFilesPath |
|
|
| |
| if (Test-Path $signedFilesPath) |
| { |
| Remove-Item -Recurse -Force -Path $signedFilesPath |
| } |
|
|
| New-PSBuildZip -BuildPath $BuildPath -DestinationFolder $DestinationFolder -VstsVariableName $VstsVariableName |
| } |
|
|
| function New-PSBuildZip |
| { |
| param( |
| [Parameter(Mandatory)] |
| [string]$BuildPath, |
| [Parameter(Mandatory)] |
| [string]$DestinationFolder, |
| [parameter(HelpMessage='VSTS variable to set for path to zip')] |
| [string]$VstsVariableName |
| ) |
|
|
| $name = Split-Path -Path $BuildPath -Leaf |
| $zipLocationPath = Join-Path -Path $DestinationFolder -ChildPath "$name-signed.zip" |
| Compress-Archive -Path $BuildPath\* -DestinationPath $zipLocationPath |
| if ($VstsVariableName) |
| { |
| |
| Write-Log "Setting $VstsVariableName to $zipLocationPath" |
| Write-Host "##vso[task.setvariable variable=$VstsVariableName]$zipLocationPath" |
| } |
| else |
| { |
| return $zipLocationPath |
| } |
| } |
|
|
|
|
| function Update-PSSignedBuildFolder |
| { |
| param( |
| [Parameter(Mandatory)] |
| [string]$BuildPath, |
| [Parameter(Mandatory)] |
| [string]$SignedFilesPath, |
| [string[]] $RemoveFilter = ('*.pdb', '*.zip', '*.r2rmap'), |
| [bool]$OfficialBuild = $true |
| ) |
|
|
| $BuildPathNormalized = (Get-Item $BuildPath).FullName |
| $SignedFilesPathNormalized = (Get-Item $SignedFilesPath).FullName |
|
|
| Write-Verbose -Verbose "BuildPath = $BuildPathNormalized" |
| Write-Verbose -Verbose "SignedFilesPath = $signedFilesPath" |
|
|
| |
| $signedFilesFilter = Join-Path -Path $SignedFilesPathNormalized -ChildPath '*' |
| Write-Verbose -Verbose "signedFilesFilter = $signedFilesFilter" |
|
|
| $signedFilesList = Get-ChildItem -Path $signedFilesFilter -Recurse -File |
| foreach ($signedFileObject in $signedFilesList) { |
| |
| |
|
|
| |
|
|
| if ($signedFileObject.Name -eq "pwsh" -or ($signedFileObject.Name -eq "Microsoft.PowerShell.GlobalTool.Shim.exe" -and $env:BUILD_REASON -eq 'PullRequest')) { |
| Write-Verbose -Verbose "Skipping $signedFileObject" |
| continue |
| } |
|
|
| $signedFilePath = $signedFileObject.FullName |
| Write-Verbose -Verbose "Processing $signedFilePath" |
|
|
| |
| if ($IsLinux) { |
| $relativePath = $signedFilePath.Replace($SignedFilesPathNormalized, '') |
| } else { |
| $relativePath = $signedFilePath.ToLowerInvariant().Replace($SignedFilesPathNormalized.ToLowerInvariant(), '') |
| } |
|
|
| Write-Verbose -Verbose "relativePath = $relativePath" |
| $destination = (Get-Item (Join-Path -Path $BuildPathNormalized -ChildPath $relativePath)).FullName |
| Write-Verbose -Verbose "destination = $destination" |
| Write-Log "replacing $destination with $signedFilePath" |
|
|
| if (-not (Test-Path $destination)) { |
| $parent = Split-Path -Path $destination -Parent |
| $exists = Test-Path -Path $parent |
|
|
| if ($exists) { |
| Write-Verbose -Verbose "Parent:" |
| Get-ChildItem -Path $parent | Select-Object -ExpandProperty FullName | Write-Verbose -Verbose |
| } |
|
|
| Write-Error "File not found: $destination, parent - $parent exists: $exists" |
| } |
|
|
| |
| if ($IsWindows) |
| { |
| $signature = Get-AuthenticodeSignature -FilePath $signedFilePath |
|
|
| if ($signature.Status -ne 'Valid' -and $OfficialBuild) { |
| Write-Host "Certificate Issuer: $($signature.SignerCertificate.Issuer)" |
| Write-Host "Certificate Subject: $($signature.SignerCertificate.Subject)" |
| Write-Error "Invalid signature for $signedFilePath" |
| } elseif ($OfficialBuild -eq $false) { |
| if ($signature.Status -eq 'NotSigned') { |
| Write-Warning "File is not signed: $signedFilePath" |
| } elseif ($signature.SignerCertificate.Issuer -notmatch '^CN=(Microsoft|TestAzureEngBuildCodeSign|Windows Internal Build Tools).*') { |
| Write-Warning "File signed with test certificate: $signedFilePath" |
| Write-Host "Certificate Issuer: $($signature.SignerCertificate.Issuer)" |
| Write-Host "Certificate Subject: $($signature.SignerCertificate.Subject)" |
| } else { |
| Write-Verbose -Verbose "File properly signed: $signedFilePath" |
| } |
| } |
| } |
| else |
| { |
| Write-Verbose -Verbose "Skipping certificate check of $signedFilePath on non-Windows" |
| } |
|
|
| Copy-Item -Path $signedFilePath -Destination $destination -Force |
|
|
| } |
|
|
| foreach($filter in $RemoveFilter) { |
| $removePath = Join-Path -Path $BuildPathNormalized -ChildPath $filter |
| Remove-Item -Path $removePath -Recurse -Force |
| } |
| } |
|
|
|
|
| function Expand-PSSignedBuild |
| { |
| param( |
| [Parameter(Mandatory)] |
| [string]$BuildZip, |
|
|
| [Switch]$SkipPwshExeCheck |
| ) |
|
|
| $psModulePath = Split-Path -Path $PSScriptRoot |
| |
| $buildPath = Join-Path -Path $psModulePath -ChildPath 'ExpandedBuild' |
| $null = New-Item -Path $buildPath -ItemType Directory -Force |
| Expand-Archive -Path $BuildZip -DestinationPath $buildPath -Force |
| |
| |
| Remove-Item -Path (Join-Path -Path $buildPath -ChildPath '*.zip') -Recurse |
|
|
| if ($SkipPwshExeCheck) { |
| $executablePath = (Join-Path $buildPath -ChildPath 'pwsh.dll') |
| } else { |
| if ($IsMacOS -or $IsLinux) { |
| $executablePath = (Join-Path $buildPath -ChildPath 'pwsh') |
| } else { |
| $executablePath = (Join-Path $buildPath -ChildPath 'pwsh.exe') |
| } |
| } |
|
|
| Restore-PSModuleToBuild -PublishPath $buildPath |
|
|
| $psOptionsPath = Join-Path $buildPath -ChildPath 'psoptions.json' |
| Restore-PSOptions -PSOptionsPath $psOptionsPath |
|
|
| $options = Get-PSOptions |
|
|
| $options.PSModuleRestore = $true |
|
|
| if (Test-Path -Path $executablePath) { |
| $options.Output = $executablePath |
| } else { |
| throw 'Could not find pwsh' |
| } |
|
|
| Set-PSOptions -Options $options |
| } |
|
|
| function New-UnixPackage { |
| [CmdletBinding(SupportsShouldProcess=$true)] |
| param( |
| [Parameter(Mandatory)] |
| [ValidateSet("deb", "osxpkg", "rpm")] |
| [string]$Type, |
|
|
| [Parameter(Mandatory)] |
| [string]$PackageSourcePath, |
|
|
| # Must start with 'powershell' but may have any suffix |
| [Parameter(Mandatory)] |
| [ValidatePattern("^powershell")] |
| [string]$Name, |
|
|
| [Parameter(Mandatory)] |
| [string]$Version, |
|
|
| # Package iteration version (rarely changed) |
| # This is a string because strings are appended to it |
| [string]$Iteration = "1", |
|
|
| # Host architecture values allowed for deb type packages: amd64 |
| # Host architecture values allowed for rpm type packages include: x86_64, aarch64, native, all, noarch, any |
| # Host architecture values allowed for osxpkg type packages include: x86_64, arm64 |
| [string] |
| [ValidateSet("x86_64", "amd64", "aarch64", "arm64", "native", "all", "noarch", "any")] |
| $HostArchitecture, |
|
|
| [Switch] |
| $Force, |
|
|
| [switch] |
| $NoSudo, |
|
|
| [switch] |
| $LTS, |
|
|
| [string] |
| $CurrentLocation = (Get-Location) |
| ) |
|
|
| DynamicParam { |
| if ($Type -eq "deb" -or $Type -like 'rpm*') { |
| |
| |
| $ParameterAttr = New-Object "System.Management.Automation.ParameterAttribute" |
| if($type -eq 'deb') |
| { |
| $ValidateSetAttr = New-Object "System.Management.Automation.ValidateSetAttribute" -ArgumentList $Script:DebianDistributions |
| } |
| else |
| { |
| $ValidateSetAttr = New-Object "System.Management.Automation.ValidateSetAttribute" -ArgumentList $Script:RedHatDistributions |
| } |
| $Attributes = New-Object "System.Collections.ObjectModel.Collection``1[System.Attribute]" |
| $Attributes.Add($ParameterAttr) > $null |
| $Attributes.Add($ValidateSetAttr) > $null |
| $Dict = New-Object "System.Management.Automation.RuntimeDefinedParameterDictionary" |
| $Parameter = New-Object "System.Management.Automation.RuntimeDefinedParameter" -ArgumentList ("Distribution", [string], $Attributes) |
|
|
| $Dict.Add("Distribution", $Parameter) > $null |
| return $Dict |
| } |
| } |
|
|
| End { |
| |
| |
| $sudo = if (!$NoSudo) { "sudo" } |
|
|
| |
| $ErrorMessage = "Must be on {0} to build '$Type' packages!" |
| switch ($Type) { |
| "deb" { |
| $packageVersion = Get-LinuxPackageSemanticVersion -Version $Version |
| if (!$Environment.IsUbuntu -and !$Environment.IsDebian -and !$Environment.IsMariner) { |
| throw ($ErrorMessage -f "Ubuntu or Debian") |
| } |
|
|
| if ($PSBoundParameters.ContainsKey('Distribution')) { |
| $DebDistro = $PSBoundParameters['Distribution'] |
| } elseif ($Environment.IsUbuntu16) { |
| $DebDistro = "ubuntu.16.04" |
| } elseif ($Environment.IsUbuntu18) { |
| $DebDistro = "ubuntu.18.04" |
| } elseif ($Environment.IsUbuntu20) { |
| $DebDistro = "ubuntu.20.04" |
| } elseif ($Environment.IsDebian9) { |
| $DebDistro = "debian.9" |
| } else { |
| throw "The current Debian distribution is not supported." |
| } |
|
|
| |
| |
| $Iteration += ".$DebDistro" |
| } |
| "rpm" { |
| if ($PSBoundParameters.ContainsKey('Distribution')) { |
| $DebDistro = $PSBoundParameters['Distribution'] |
|
|
| } elseif ($Environment.IsRedHatFamily) { |
| $DebDistro = "rhel.7" |
| } else { |
| throw "The current distribution is not supported." |
| } |
|
|
| $packageVersion = Get-LinuxPackageSemanticVersion -Version $Version |
| } |
| "osxpkg" { |
| $packageVersion = $Version |
| if (!$Environment.IsMacOS) { |
| throw ($ErrorMessage -f "macOS") |
| } |
|
|
| $DebDistro = 'macOS' |
| } |
| } |
|
|
| |
| $IsPreview = Test-IsPreview -Version $Version -IsLTS:$LTS |
|
|
| |
| |
| $Name = if($LTS) { |
| "powershell-lts" |
| } |
| elseif ($IsPreview -and $Type -ne "osxpkg") { |
| "powershell-preview" |
| } |
| else { |
| "powershell" |
| } |
|
|
| |
| Test-Dependencies |
|
|
| $Description = $packagingStrings.Description |
|
|
| |
| $VersionMatch = [regex]::Match($Version, '(\d+)(?:.(\d+)(?:.(\d+)(?:-preview(?:.(\d+))?)?)?)?') |
| $MajorVersion = $VersionMatch.Groups[1].Value |
|
|
| |
| $Suffix = if ($IsPreview) { $MajorVersion + "-preview" } elseif ($LTS) { $MajorVersion + "-lts" } else { $MajorVersion } |
|
|
| |
| $Staging = "$PSScriptRoot/staging" |
| if ($PSCmdlet.ShouldProcess("Create staging folder")) { |
| New-StagingFolder -StagingPath $Staging -PackageSourcePath $PackageSourcePath |
| } |
|
|
| |
| $Destination = if ($Environment.IsLinux) { |
| "/opt/microsoft/powershell/$Suffix" |
| } elseif ($Environment.IsMacOS) { |
| "/usr/local/microsoft/powershell/$Suffix" |
| } |
|
|
| |
| $Link = Get-PwshExecutablePath -IsPreview:$IsPreview |
|
|
| $links = @(New-LinkInfo -LinkDestination $Link -LinkTarget "$Destination/pwsh") |
|
|
| if($LTS) { |
| $links += New-LinkInfo -LinkDestination (Get-PwshExecutablePath -IsLTS:$LTS) -LinkTarget "$Destination/pwsh" |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create package file system")) |
| { |
| |
| $AfterScriptInfo = New-AfterScripts -Link $Link -Distribution $DebDistro -Destination $Destination |
|
|
| |
| $ManGzipInfo = New-ManGzip -IsPreview:$IsPreview -IsLTS:$LTS |
|
|
| |
| Write-Log "Setting permissions..." |
| Start-NativeExecution { |
| find $Staging -type d | xargs chmod 755 |
| find $Staging -type f | xargs chmod 644 |
| chmod 644 $ManGzipInfo.GzipFile |
| |
| chmod 755 "$Staging/pwsh" |
| } |
| } |
|
|
| |
| if ($Type -eq "osxpkg") |
| { |
| Write-Log "Adding macOS launch application..." |
| if ($PSCmdlet.ShouldProcess("Add macOS launch application")) |
| { |
| |
| $AppsFolder = New-MacOSLauncher -Version $Version |
| } |
| } |
|
|
| $packageDependenciesParams = @{} |
| if ($DebDistro) |
| { |
| $packageDependenciesParams['Distribution']=$DebDistro |
| } |
|
|
| |
| $Dependencies = @(Get-PackageDependencies @packageDependenciesParams) |
|
|
| |
| try { |
| if ($Type -eq 'rpm') { |
| |
| if ($PSCmdlet.ShouldProcess("Create RPM package with rpmbuild")) { |
| Write-Log "Creating RPM package with rpmbuild..." |
|
|
| |
| $rpmBuildRoot = Join-Path $env:HOME "rpmbuild" |
| $specsDir = Join-Path $rpmBuildRoot "SPECS" |
| $rpmsDir = Join-Path $rpmBuildRoot "RPMS" |
|
|
| New-Item -ItemType Directory -Path $specsDir -Force | Out-Null |
| New-Item -ItemType Directory -Path $rpmsDir -Force | Out-Null |
|
|
| |
| $specContent = New-RpmSpec ` |
| -Name $Name ` |
| -Version $packageVersion ` |
| -Iteration $Iteration ` |
| -Description $Description ` |
| -Dependencies $Dependencies ` |
| -AfterInstallScript $AfterScriptInfo.AfterInstallScript ` |
| -AfterRemoveScript $AfterScriptInfo.AfterRemoveScript ` |
| -Staging $Staging ` |
| -Destination $Destination ` |
| -ManGzipFile $ManGzipInfo.GzipFile ` |
| -ManDestination $ManGzipInfo.ManFile ` |
| -LinkInfo $Links ` |
| -Distribution $DebDistro ` |
| -HostArchitecture $HostArchitecture |
|
|
| $specFile = Join-Path $specsDir "$Name.spec" |
| $specContent | Out-File -FilePath $specFile -Encoding ascii |
| Write-Verbose "Generated spec file: $specFile" -Verbose |
|
|
| |
| if ($env:GITHUB_ACTIONS -eq 'true') { |
| Write-Host "::group::RPM Spec File Content" |
| Write-Host $specContent |
| Write-Host "::endgroup::" |
| } else { |
| Write-Verbose "RPM Spec File Content:`n$specContent" -Verbose |
| } |
|
|
| |
| try { |
| |
| |
| $targetArch = "" |
| if ($HostArchitecture -ne "x86_64" -and $HostArchitecture -ne "noarch") { |
| $targetArch = "--target $HostArchitecture" |
| } |
| $buildCmd = "rpmbuild -bb --quiet $targetArch --define '_topdir $rpmBuildRoot' --buildroot '$rpmBuildRoot/BUILDROOT' '$specFile'" |
| Write-Verbose "Running: $buildCmd" -Verbose |
| $Output = bash -c $buildCmd 2>&1 |
| $exitCode = $LASTEXITCODE |
|
|
| if ($exitCode -ne 0) { |
| throw "rpmbuild failed with exit code $exitCode" |
| } |
|
|
| |
| $rpmFile = Get-ChildItem -Path (Join-Path $rpmsDir $HostArchitecture) -Filter "*.rpm" -ErrorAction Stop | |
| Sort-Object -Property LastWriteTime -Descending | |
| Select-Object -First 1 |
|
|
| if ($rpmFile) { |
| |
| Copy-Item -Path $rpmFile.FullName -Destination $CurrentLocation -Force |
| $Output = @("Created package {:path=>""$($rpmFile.Name)""}") |
| } else { |
| throw "RPM file not found after build" |
| } |
| } |
| catch { |
| Write-Verbose -Message "!!!Handling error in rpmbuild!!!" -Verbose -ErrorAction SilentlyContinue |
| if ($Output) { |
| Write-Verbose -Message "$Output" -Verbose -ErrorAction SilentlyContinue |
| } |
| Get-Error -InputObject $_ |
| throw |
| } |
| } |
| } elseif ($Type -eq 'deb') { |
| |
| if ($PSCmdlet.ShouldProcess("Create DEB package natively")) { |
| Write-Log "Creating DEB package natively..." |
| try { |
| $result = New-NativeDeb ` |
| -Name $Name ` |
| -Version $packageVersion ` |
| -Iteration $Iteration ` |
| -Description $Description ` |
| -Staging $Staging ` |
| -Destination $Destination ` |
| -ManGzipFile $ManGzipInfo.GzipFile ` |
| -ManDestination $ManGzipInfo.ManFile ` |
| -LinkInfo $Links ` |
| -Dependencies $Dependencies ` |
| -AfterInstallScript $AfterScriptInfo.AfterInstallScript ` |
| -AfterRemoveScript $AfterScriptInfo.AfterRemoveScript ` |
| -HostArchitecture $HostArchitecture ` |
| -CurrentLocation $CurrentLocation |
|
|
| $Output = @("Created package {:path=>""$($result.PackageName)""}") |
| } |
| catch { |
| Write-Verbose -Message "!!!Handling error in native DEB creation!!!" -Verbose -ErrorAction SilentlyContinue |
| } |
| } |
| } elseif ($Type -eq 'osxpkg') { |
| |
| if ($PSCmdlet.ShouldProcess("Create macOS package with pkgbuild/productbuild")) { |
| Write-Log "Creating macOS package with native tools..." |
|
|
| $macPkgArgs = @{ |
| Name = $Name |
| Version = $packageVersion |
| Iteration = $Iteration |
| Staging = $Staging |
| Destination = $Destination |
| ManGzipFile = $ManGzipInfo.GzipFile |
| ManDestination = $ManGzipInfo.ManFile |
| LinkInfo = $Links |
| AfterInstallScript = $AfterScriptInfo.AfterInstallScript |
| AppsFolder = $AppsFolder |
| HostArchitecture = $HostArchitecture |
| CurrentLocation = $CurrentLocation |
| LTS = $LTS |
| } |
|
|
| try { |
| $packageFile = New-MacOSPackage @macPkgArgs |
| $Output = @("Created package {:path=>""$($packageFile.Name)""}") |
| } |
| catch { |
| Write-Verbose -Message "!!!Handling error in macOS packaging!!!" -Verbose -ErrorAction SilentlyContinue |
| Get-Error -InputObject $_ |
| throw |
| } |
| } |
| } else { |
| |
| throw "Unknown package type: $Type" |
| } |
| } finally { |
| if ($Environment.IsMacOS) { |
| Write-Log "Starting Cleanup for mac packaging..." |
| if ($PSCmdlet.ShouldProcess("Cleanup macOS launcher")) |
| { |
| Clear-MacOSLauncher |
| } |
| } |
|
|
| |
| if ($Type -eq 'rpm') { |
| $rpmBuildRoot = Join-Path $env:HOME "rpmbuild" |
| if (Test-Path $rpmBuildRoot) { |
| Write-Verbose "Cleaning up rpmbuild directory: $rpmBuildRoot" -Verbose |
| Remove-Item -Path $rpmBuildRoot -Recurse -Force -ErrorAction SilentlyContinue |
| } |
| } |
|
|
| if ($AfterScriptInfo.AfterInstallScript) { |
| Remove-Item -ErrorAction 'silentlycontinue' $AfterScriptInfo.AfterInstallScript -Force |
| } |
| if ($AfterScriptInfo.AfterRemoveScript) { |
| Remove-Item -ErrorAction 'silentlycontinue' $AfterScriptInfo.AfterRemoveScript -Force |
| } |
| Remove-Item -Path $ManGzipInfo.GzipFile -Force -ErrorAction SilentlyContinue |
| } |
|
|
| |
| $createdPackage = Get-Item (Join-Path $CurrentLocation (($Output[-1] -split ":path=>")[-1] -replace '["{}]')) |
|
|
| |
| |
|
|
| if (Test-Path $createdPackage) |
| { |
| Write-Verbose "Created package: $createdPackage" -Verbose |
| return $createdPackage |
| } |
| else |
| { |
| throw "Failed to create $createdPackage" |
| } |
| } |
| } |
|
|
| Function New-LinkInfo |
| { |
| [CmdletBinding(SupportsShouldProcess=$true)] |
| param( |
| [Parameter(Mandatory)] |
| [string] |
| $LinkDestination, |
| [Parameter(Mandatory)] |
| [string] |
| $linkTarget |
| ) |
|
|
| $linkDir = Join-Path -Path '/tmp' -ChildPath ([System.IO.Path]::GetRandomFileName()) |
| $null = New-Item -ItemType Directory -Path $linkDir |
| $linkSource = Join-Path -Path $linkDir -ChildPath 'pwsh' |
|
|
| Write-Log "Creating link to target '$LinkTarget', with a temp source of '$LinkSource' and a Package Destination of '$LinkDestination'" |
| if ($PSCmdlet.ShouldProcess("Create package symbolic from $linkDestination to $linkTarget")) |
| { |
| |
| New-Item -Force -ItemType SymbolicLink -Path $linkSource -Target $LinkTarget > $null |
| } |
|
|
| [LinkInfo] @{ |
| Source = $linkSource |
| Destination = $LinkDestination |
| } |
| } |
|
|
| function New-MacOsDistributionPackage |
| { |
| [CmdletBinding(SupportsShouldProcess=$true)] |
| param( |
| [Parameter(Mandatory,HelpMessage='The FileInfo of the component package')] |
| [System.IO.FileInfo]$ComponentPackage, |
|
|
| [Parameter(Mandatory,HelpMessage='Package name for the output file')] |
| [string]$PackageName, |
|
|
| [Parameter(Mandatory,HelpMessage='Package version')] |
| [string]$Version, |
|
|
| [Parameter(Mandatory,HelpMessage='Output directory for the final package')] |
| [string]$OutputDirectory, |
|
|
| [Parameter(HelpMessage='x86_64 for Intel or arm64 for Apple Silicon')] |
| [ValidateSet("x86_64", "arm64")] |
| [string] $HostArchitecture = "x86_64", |
|
|
| [Parameter(HelpMessage='Package identifier')] |
| [string]$PackageIdentifier, |
|
|
| [Switch] $IsPreview |
| ) |
|
|
| if (!$Environment.IsMacOS) |
| { |
| throw 'New-MacOsDistributionPackage is only supported on macOS!' |
| } |
|
|
| |
| $tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName()) |
| New-Item -ItemType Directory -Path $tempDir -Force > $null |
|
|
| $resourcesDir = Join-Path -Path $tempDir -ChildPath 'resources' |
| New-Item -ItemType Directory -Path $resourcesDir -Force > $null |
|
|
| |
| $backgroundFile = "$RepoRoot/assets/macDialog.png" |
| if (Test-Path $backgroundFile) { |
| Copy-Item -Path $backgroundFile -Destination $resourcesDir -Force |
| } |
|
|
| |
| $componentFileName = Split-Path -Leaf -Path $ComponentPackage |
| $tempComponentPath = Join-Path -Path $tempDir -ChildPath $componentFileName |
| Copy-Item -Path $ComponentPackage -Destination $tempComponentPath -Force |
|
|
| |
| $distributionXmlPath = Join-Path -Path $tempDir -ChildPath 'powershellDistribution.xml' |
|
|
| |
| if (-not $PackageIdentifier) { |
| if ($IsPreview.IsPresent) { |
| $PackageIdentifier = 'com.microsoft.powershell-preview' |
| } |
| else { |
| $PackageIdentifier = 'com.microsoft.powershell' |
| } |
| } |
|
|
| |
| $minOSVersion = "11.0" |
|
|
| |
| |
| |
| |
| |
| |
| |
| $PackagingStrings.OsxDistributionTemplate -f $PackageName, $Version, $componentFileName, $minOSVersion, $PackageIdentifier, $HostArchitecture | Out-File -Encoding utf8 -FilePath $distributionXmlPath -Force |
|
|
| |
| |
| $packageArchName = if ($HostArchitecture -eq "x86_64") { "x64" } else { $HostArchitecture } |
| $finalPackagePath = Join-Path $OutputDirectory "$PackageName-$Version-osx-$packageArchName.pkg" |
|
|
| |
| if (Test-Path $finalPackagePath) { |
| Write-Warning "Removing existing package: $finalPackagePath" |
| Remove-Item $finalPackagePath -Force |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Build product package with productbuild")) { |
| Write-Log "Applying distribution.xml to package..." |
| Push-Location $tempDir |
| try |
| { |
| |
| Start-NativeExecution -VerboseOutputOnError { |
| productbuild --distribution $distributionXmlPath ` |
| --package-path $tempDir ` |
| --resources $resourcesDir ` |
| $finalPackagePath |
| } |
|
|
| if (Test-Path $finalPackagePath) { |
| Write-Log "Successfully created macOS package: $finalPackagePath" |
| } |
| else { |
| throw "Package was not created at expected location: $finalPackagePath" |
| } |
| } |
| finally |
| { |
| Pop-Location |
| Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue |
| } |
| } |
|
|
| return (Get-Item $finalPackagePath) |
| } |
|
|
| Class LinkInfo |
| { |
| [string] $Source |
| [string] $Destination |
| } |
|
|
| function New-RpmSpec |
| { |
| param( |
| [Parameter(Mandatory,HelpMessage='Package Name')] |
| [String]$Name, |
|
|
| [Parameter(Mandatory,HelpMessage='Package Version')] |
| [String]$Version, |
|
|
| [Parameter(Mandatory)] |
| [String]$Iteration, |
|
|
| [Parameter(Mandatory,HelpMessage='Package description')] |
| [String]$Description, |
|
|
| [Parameter(Mandatory,HelpMessage='Staging folder for installation files')] |
| [String]$Staging, |
|
|
| [Parameter(Mandatory,HelpMessage='Install path on target machine')] |
| [String]$Destination, |
|
|
| [Parameter(Mandatory,HelpMessage='The built and gzipped man file.')] |
| [String]$ManGzipFile, |
|
|
| [Parameter(Mandatory,HelpMessage='The destination of the man file')] |
| [String]$ManDestination, |
|
|
| [Parameter(Mandatory,HelpMessage='Symlink to powershell executable')] |
| [LinkInfo[]]$LinkInfo, |
|
|
| [Parameter(Mandatory,HelpMessage='Packages required to install this package')] |
| [String[]]$Dependencies, |
|
|
| [Parameter(Mandatory,HelpMessage='Script to run after the package installation.')] |
| [String]$AfterInstallScript, |
|
|
| [Parameter(Mandatory,HelpMessage='Script to run after the package removal.')] |
| [String]$AfterRemoveScript, |
|
|
| [String]$Distribution = 'rhel.7', |
| [string]$HostArchitecture |
| ) |
|
|
| |
| |
| $rpmVersion = $Version -replace '-', '_' |
|
|
| |
| |
| $rpmRelease = "$Iteration.$Distribution" |
|
|
| $specContent = @" |
| # RPM spec file for PowerShell |
| # Generated by PowerShell build system |
| |
| Name: $Name |
| Version: $rpmVersion |
| Release: $rpmRelease |
| Summary: PowerShell - Cross-platform automation and configuration tool/framework |
| License: MIT |
| URL: https://microsoft.com/powershell |
| AutoReq: no |
| |
| "@ |
|
|
| |
| |
| if ($HostArchitecture -eq "x86_64" -or $HostArchitecture -eq "noarch") { |
| $specContent += "BuildArch: $HostArchitecture`n`n" |
| } else { |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| $specContent += "%define __strip /bin/true`n" |
|
|
| |
| |
| |
| |
| $specContent += "%global debug_package %{nil}`n`n" |
| } |
|
|
| |
| foreach ($dep in $Dependencies) { |
| $specContent += "Requires: $dep`n" |
| } |
|
|
| $specContent += @" |
| |
| %description |
| $Description |
| |
| %prep |
| # No prep needed - files are already staged |
| |
| %build |
| # No build needed - binaries are pre-built |
| |
| %install |
| rm -rf `$RPM_BUILD_ROOT |
| mkdir -p `$RPM_BUILD_ROOT$Destination |
| mkdir -p `$RPM_BUILD_ROOT$(Split-Path -Parent $ManDestination) |
| |
| # Copy all files from staging to destination |
| cp -r $Staging/* `$RPM_BUILD_ROOT$Destination/ |
| |
| # Copy man page |
| cp $ManGzipFile `$RPM_BUILD_ROOT$ManDestination |
| |
| "@ |
|
|
| |
| foreach ($link in $LinkInfo) { |
| $linkDir = Split-Path -Parent $link.Destination |
| $specContent += "mkdir -p `$RPM_BUILD_ROOT$linkDir`n" |
| |
| |
| |
| $specContent += "cp -P $($link.Source) `$RPM_BUILD_ROOT$($link.Destination)`n" |
| } |
|
|
| |
| $postInstallContent = Get-Content -Path $AfterInstallScript -Raw |
| $specContent += "`n%post`n" |
| $specContent += $postInstallContent |
| $specContent += "`n" |
|
|
| |
| $postUninstallContent = Get-Content -Path $AfterRemoveScript -Raw |
| $specContent += "%postun`n" |
| $specContent += $postUninstallContent |
| $specContent += "`n" |
|
|
| |
| $specContent += "%files`n" |
| $specContent += "%defattr(-,root,root,-)`n" |
| $specContent += "$Destination/*`n" |
| $specContent += "$ManDestination`n" |
|
|
| |
| foreach ($link in $LinkInfo) { |
| $specContent += "$($link.Destination)`n" |
| } |
|
|
| |
| $changelogDate = Get-Date -Format "ddd MMM dd yyyy" |
| $specContent += "`n%changelog`n" |
| $specContent += "* $changelogDate PowerShell Team <PowerShellTeam@hotmail.com> - $rpmVersion-$rpmRelease`n" |
| $specContent += "- Automated build`n" |
|
|
| return $specContent |
| } |
|
|
| function New-NativeDeb |
| { |
| param( |
| [Parameter(Mandatory, HelpMessage='Package Name')] |
| [String]$Name, |
|
|
| [Parameter(Mandatory, HelpMessage='Package Version')] |
| [String]$Version, |
|
|
| [Parameter(Mandatory)] |
| [String]$Iteration, |
|
|
| [Parameter(Mandatory, HelpMessage='Package description')] |
| [String]$Description, |
|
|
| [Parameter(Mandatory, HelpMessage='Staging folder for installation files')] |
| [String]$Staging, |
|
|
| [Parameter(Mandatory, HelpMessage='Install path on target machine')] |
| [String]$Destination, |
|
|
| [Parameter(Mandatory, HelpMessage='The built and gzipped man file.')] |
| [String]$ManGzipFile, |
|
|
| [Parameter(Mandatory, HelpMessage='The destination of the man file')] |
| [String]$ManDestination, |
|
|
| [Parameter(Mandatory, HelpMessage='Symlink to powershell executable')] |
| [LinkInfo[]]$LinkInfo, |
|
|
| [Parameter(HelpMessage='Packages required to install this package.')] |
| [String[]]$Dependencies, |
|
|
| [Parameter(HelpMessage='Script to run after the package installation.')] |
| [String]$AfterInstallScript, |
|
|
| [Parameter(HelpMessage='Script to run after the package removal.')] |
| [String]$AfterRemoveScript, |
|
|
| [string]$HostArchitecture, |
|
|
| [string]$CurrentLocation |
| ) |
|
|
| Write-Log "Creating native DEB package..." |
|
|
| |
| $debBuildRoot = Join-Path $env:HOME "debbuild-$(Get-Random)" |
| $debianDir = Join-Path $debBuildRoot "DEBIAN" |
| $dataDir = Join-Path $debBuildRoot "data" |
|
|
| try { |
| New-Item -ItemType Directory -Path $debianDir -Force | Out-Null |
| New-Item -ItemType Directory -Path $dataDir -Force | Out-Null |
|
|
| |
| $installedSize = 0 |
| Get-ChildItem -Path $Staging -Recurse -File | ForEach-Object { $installedSize += $_.Length } |
| $installedSize += (Get-Item $ManGzipFile).Length |
| $installedSizeKB = [Math]::Ceiling($installedSize / 1024) |
|
|
| |
| |
| $descriptionLines = $Description -split "`n" |
| $shortDescription = $descriptionLines[0] |
| $extendedDescription = if ($descriptionLines.Count -gt 1) { |
| ($descriptionLines[1..($descriptionLines.Count-1)] | ForEach-Object { " $_" }) -join "`n" |
| } |
|
|
| $controlContent = @" |
| Package: $Name |
| Version: $Version-$Iteration |
| Architecture: $HostArchitecture |
| Maintainer: PowerShell Team <PowerShellTeam@hotmail.com> |
| Installed-Size: $installedSizeKB |
| Priority: optional |
| Section: shells |
| Homepage: https://microsoft.com/powershell |
| Depends: $(if ($Dependencies) { $Dependencies -join ', ' }) |
| Description: $shortDescription |
| $(if ($extendedDescription) { $extendedDescription + "`n" }) |
| "@ |
|
|
| $controlFile = Join-Path $debianDir "control" |
| $controlContent | Out-File -FilePath $controlFile -Encoding ascii -NoNewline |
|
|
| Write-Verbose "Control file created: $controlFile" -Verbose |
| Write-LogGroup -Title "DEB Control File Content" -Message $controlContent |
|
|
| |
| if ($AfterInstallScript -and (Test-Path $AfterInstallScript)) { |
| $postinstFile = Join-Path $debianDir "postinst" |
| Copy-Item -Path $AfterInstallScript -Destination $postinstFile -Force |
| Start-NativeExecution { chmod 755 $postinstFile } |
| Write-Verbose "Postinst script copied to: $postinstFile" -Verbose |
| } |
|
|
| |
| if ($AfterRemoveScript -and (Test-Path $AfterRemoveScript)) { |
| $postrmFile = Join-Path $debianDir "postrm" |
| Copy-Item -Path $AfterRemoveScript -Destination $postrmFile -Force |
| Start-NativeExecution { chmod 755 $postrmFile } |
| Write-Verbose "Postrm script copied to: $postrmFile" -Verbose |
| } |
|
|
| |
| $targetPath = Join-Path $dataDir $Destination.TrimStart('/') |
| New-Item -ItemType Directory -Path $targetPath -Force | Out-Null |
| Copy-Item -Path "$Staging/*" -Destination $targetPath -Recurse -Force |
| Write-Verbose "Copied staging files to: $targetPath" -Verbose |
|
|
| |
| $manDestPath = Join-Path $dataDir $ManDestination.TrimStart('/') |
| $manDestDir = Split-Path $manDestPath -Parent |
| New-Item -ItemType Directory -Path $manDestDir -Force | Out-Null |
| Copy-Item -Path $ManGzipFile -Destination $manDestPath -Force |
| Write-Verbose "Copied man page to: $manDestPath" -Verbose |
|
|
| |
| foreach ($link in $LinkInfo) { |
| $linkPath = Join-Path $dataDir $link.Destination.TrimStart('/') |
| $linkDir = Split-Path $linkPath -Parent |
| New-Item -ItemType Directory -Path $linkDir -Force | Out-Null |
|
|
| |
| |
| if (Test-Path $link.Source) { |
| |
| Start-NativeExecution { cp -P $link.Source $linkPath } |
| Write-Verbose "Copied symlink: $linkPath (from $($link.Source))" -Verbose |
| } else { |
| Write-Warning "Symlink source not found: $($link.Source)" |
| } |
| } |
|
|
| |
| Write-Verbose "Setting file permissions..." -Verbose |
| |
| Get-ChildItem $dataDir -Directory -Recurse | ForEach-Object { |
| Start-NativeExecution { chmod 755 $_.FullName } |
| } |
| |
| |
| Get-ChildItem $dataDir -File -Recurse | |
| Where-Object { -not $_.Target } | |
| ForEach-Object { |
| Start-NativeExecution { chmod 644 $_.FullName } |
| } |
|
|
| |
| |
| $pwshPath = "$targetPath/pwsh" |
| if (Test-Path $pwshPath) { |
| Start-NativeExecution { chmod 755 $pwshPath } |
| } |
|
|
| |
| $md5sumsFile = Join-Path $debianDir "md5sums" |
| $md5Content = "" |
| Get-ChildItem -Path $dataDir -Recurse -File | |
| Where-Object { -not $_.Target } | |
| ForEach-Object { |
| $relativePath = $_.FullName.Substring($dataDir.Length + 1) |
| $md5Hash = (Get-FileHash -Path $_.FullName -Algorithm MD5).Hash.ToLower() |
| $md5Content += "$md5Hash $relativePath`n" |
| } |
| $md5Content | Out-File -FilePath $md5sumsFile -Encoding ascii -NoNewline |
| Write-Verbose "MD5 sums file created: $md5sumsFile" -Verbose |
|
|
| |
| $debFileName = "${Name}_${Version}-${Iteration}_${HostArchitecture}.deb" |
| $debFilePath = Join-Path $CurrentLocation $debFileName |
|
|
| Write-Verbose "Building DEB package: $debFileName" -Verbose |
|
|
| |
| $buildDir = Join-Path $debBuildRoot "build" |
| New-Item -ItemType Directory -Path $buildDir -Force | Out-Null |
|
|
| Write-Verbose "debianDir: $debianDir" -Verbose |
| Write-Verbose "dataDir: $dataDir" -Verbose |
| Write-Verbose "buildDir: $buildDir" -Verbose |
|
|
| |
| Start-NativeExecution { cp -a $debianDir "$buildDir/DEBIAN" } |
| Start-NativeExecution { cp -a $dataDir |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| " |
| }, |
| @{ |
| ApplyTo = @("System.Management.Automation") |
| Pattern = "[System.Runtime.CompilerServices.NullableAttribute(new byte[]{ (byte)2, (byte)1})]" |
| Replacement = " " |
| }, |
| @{ |
| ApplyTo = @("System.Management.Automation") |
| Pattern = "[System.Runtime.CompilerServices.CompilerGeneratedAttribute, System.Runtime.CompilerServices.NullableContextAttribute((byte)2)]" |
| Replacement = " " |
| }, |
| @{ |
| ApplyTo = @("System.Management.Automation") |
| Pattern = "[System.Runtime.CompilerServices.CompilerGeneratedAttribute, System.Runtime.CompilerServices.IsReadOnlyAttribute]" |
| Replacement = " " |
| }, |
| @{ |
| ApplyTo = @("System.Management.Automation") |
| Pattern = "[System.Runtime.CompilerServices.CompilerGeneratedAttribute, System.Runtime.CompilerServices.NullableContextAttribute((byte)1)]" |
| Replacement = " " |
| }, |
| @{ |
| ApplyTo = @("System.Management.Automation", "Microsoft.PowerShell.ConsoleHost") |
| Pattern = "[System.Runtime.CompilerServices.NullableAttribute((byte)2)]" |
| Replacement = "" |
| }, |
| @{ |
| ApplyTo = @("System.Management.Automation", "Microsoft.PowerShell.ConsoleHost") |
| Pattern = "[System.Runtime.CompilerServices.NullableAttribute((byte)1)]" |
| Replacement = "" |
| } |
| ) |
| |
| $reader = [System.IO.File]::OpenText($generatedSource) |
| $writer = [System.IO.File]::CreateText($filteredSource) |
| |
| while($null -ne ($line = $reader.ReadLine())) |
| { |
| $lineWasProcessed = $false |
| foreach ($patternToReplace in $patternsToReplace) |
| { |
| if ($assemblyName -in $patternToReplace.ApplyTo -and $line.Contains($patternToReplace.Pattern)) { |
| $line = $line.Replace($patternToReplace.Pattern, $patternToReplace.Replacement) |
| $lineWasProcessed = $true |
| } |
| } |
| |
| if (!$lineWasProcessed) { |
| $match = Select-String -InputObject $line -Pattern $patternsToRemove -SimpleMatch |
| if ($null -ne $match) |
| { |
| $line = " |
| } |
| } |
|
|
| $writer.WriteLine($line) |
| } |
|
|
| if ($null -ne $reader) |
| { |
| $reader.Close() |
| } |
|
|
| if ($null -ne $writer) |
| { |
| $writer.Close() |
| } |
|
|
| Move-Item $filteredSource $generatedSource -Force |
| Write-Log "Code cleanup complete for reference assembly '$assemblyName'." |
| } |
|
|
| < |
| Helper function for New-ReferenceAssembly to get the arguments |
| for building reference assemblies. |
| #> |
| function GenerateBuildArguments |
| { |
| param( |
| [string] $AssemblyName, |
| [string] $RefAssemblyVersion, |
| [string] $SnkFilePath, |
| [string] $SMAReferencePath |
| ) |
|
|
| $arguments = @('build') |
| $arguments += @('-c','Release') |
| $arguments += "/p:RefAsmVersion=$RefAssemblyVersion" |
| $arguments += "/p:SnkFile=$SnkFilePath" |
|
|
| if ($AssemblyName -ne "System.Management.Automation") { |
| $arguments += "/p:SmaRefFile=$SMAReferencePath" |
| } |
|
|
| return $arguments |
| } |
|
|
| < |
| .SYNOPSIS |
| Create a NuGet package from a nuspec. |
|
|
| .DESCRIPTION |
| Creates a NuGet using the nuspec at the specified folder. |
| It is expected that the lib / ref / runtime folders are welformed. |
| The genereated NuGet package is copied over to the $PackageDestinationPath |
|
|
| .PARAMETER NuSpecPath |
| Path to the folder containing the nuspec file. |
|
|
| .PARAMETER PackageDestinationPath |
| Path to which NuGet package should be copied. Destination is created if it does not exist. |
| |
|
|
| function New-NugetPackage |
| { |
| [CmdletBinding()] |
| param( |
| [Parameter(Mandatory = $true)] |
| [string] $NuSpecPath, |
|
|
| [Parameter(Mandatory = $true)] |
| [string] $PackageDestinationPath |
| ) |
|
|
| $nuget = Get-Command -Type Application nuget -ErrorAction SilentlyContinue |
|
|
| if ($null -eq $nuget) |
| { |
| throw 'nuget application is not available in PATH' |
| } |
|
|
| Push-Location $NuSpecPath |
|
|
| Start-NativeExecution { nuget pack . } > $null |
|
|
| if (-not (Test-Path $PackageDestinationPath)) |
| { |
| New-Item $PackageDestinationPath -ItemType Directory -Force > $null |
| } |
|
|
| Copy-Item *.nupkg $PackageDestinationPath -Force -Verbose |
| Pop-Location |
| } |
|
|
| < |
| .SYNOPSIS |
| Publish the specified Nuget Package to MyGet feed. |
|
|
| .DESCRIPTION |
| The specified nuget package is published to the powershell.myget.org/powershell-core feed. |
|
|
| .PARAMETER PackagePath |
| Path to the NuGet Package. |
|
|
| .PARAMETER ApiKey |
| API key for powershell.myget.org |
| |
| function Publish-NugetToMyGet |
| { |
| param( |
| [Parameter(Mandatory = $true)] |
| [string] $PackagePath, |
|
|
| [Parameter(Mandatory = $true)] |
| [string] $ApiKey |
| ) |
|
|
| $nuget = Get-Command -Type Application nuget -ErrorAction SilentlyContinue |
|
|
| if ($null -eq $nuget) |
| { |
| throw 'nuget application is not available in PATH' |
| } |
|
|
| Get-ChildItem $PackagePath | ForEach-Object { |
| Write-Log "Pushing $_ to PowerShell Myget" |
| Start-NativeExecution { nuget push $_.FullName -Source 'https://powershell.myget.org/F/powershell-core/api/v2/package' -ApiKey $ApiKey } > $null |
| } |
| } |
|
|
| function New-SubFolder |
| { |
| [CmdletBinding(SupportsShouldProcess=$true)] |
| param( |
| [string] |
| $Path, |
|
|
| [String] |
| $ChildPath, |
|
|
| [switch] |
| $Clean |
| ) |
|
|
| $subFolderPath = Join-Path -Path $Path -ChildPath $ChildPath |
| if ($Clean.IsPresent -and (Test-Path $subFolderPath)) |
| { |
| Remove-Item -Path $subFolderPath -Recurse -Force -ErrorAction SilentlyContinue |
| } |
|
|
| if (!(Test-Path $subFolderPath)) |
| { |
| $null = New-Item -Path $subFolderPath -ItemType Directory |
| } |
| return $subFolderPath |
| } |
|
|
| |
| |
| function Get-PackageSemanticVersion |
| { |
| [CmdletBinding()] |
| param ( |
| # Version of the Package |
| [Parameter(Mandatory = $true)] |
| [ValidateNotNullOrEmpty()] |
| [string] $Version, |
| [switch] $NuGet |
| ) |
|
|
| Write-Verbose "Extract the semantic version in the form of major.minor[.build-quality[.revision]] for $Version" |
| $packageVersionTokens = $Version.Split('.') |
|
|
| if ($packageVersionTokens.Count -eq 3) { |
| |
| $packageSemanticVersion = $Version |
| } elseif ($packageVersionTokens.Count -eq 4) { |
| |
| $packageRevisionTokens = ($packageVersionTokens[3].Split('-'))[0] |
| if ($NuGet.IsPresent) |
| { |
| $packageRevisionTokens = $packageRevisionTokens.Replace('.','-') |
| } |
| $packageSemanticVersion = $packageVersionTokens[0],$packageVersionTokens[1],$packageVersionTokens[2],$packageRevisionTokens -join '.' |
| } else { |
| throw "Cannot create Semantic Version from the string $Version containing 4 or more tokens" |
| } |
|
|
| $packageSemanticVersion |
| } |
|
|
| |
| |
| function Get-LinuxPackageSemanticVersion |
| { |
| [CmdletBinding()] |
| param ( |
| # Version of the Package |
| [Parameter(Mandatory = $true)] |
| [ValidatePattern("^\d+\.\d+\.\d+(-\w+(\.\d+)?)?$")] |
| [ValidateNotNullOrEmpty()] |
| [string] $Version |
| ) |
|
|
| Write-Verbose "Extract the semantic version in the form of major.minor[.build-quality[.revision]] for $Version" |
| $packageVersionTokens = $Version.Split('-') |
|
|
| if ($packageVersionTokens.Count -eq 1) { |
| |
| $packageSemanticVersion = $Version |
| } elseif ($packageVersionTokens.Count -ge 2) { |
| $packageRevisionTokens = ($packageVersionTokens[1..($packageVersionTokens.Count-1)] -join '-') |
| $packageSemanticVersion = ('{0}-{1}' -f $packageVersionTokens[0], $packageRevisionTokens) |
| } |
|
|
| $packageSemanticVersion |
| } |
|
|
| |
| |
| function Get-NugetSemanticVersion |
| { |
| [CmdletBinding()] |
| param ( |
| # Version of the Package |
| [Parameter(Mandatory = $true)] |
| [ValidateNotNullOrEmpty()] |
| [string] $Version |
| ) |
|
|
| $packageVersionTokens = $Version.Split('.') |
|
|
| Write-Verbose "Extract the semantic version in the form of major.minor[.build-quality[-revision]] for $Version" |
| $versionPartTokens = @() |
| $identifierPortionTokens = @() |
| $inIdentifier = $false |
| foreach($token in $packageVersionTokens) { |
| $tokenParts = $null |
| if ($token -match '-') { |
| $tokenParts = $token.Split('-') |
| } |
| elseif ($inIdentifier) { |
| $tokenParts = @($token) |
| } |
|
|
| |
| if (!$tokenParts) { |
| $versionPartTokens += $token |
| } |
| else { |
| foreach($idToken in $tokenParts) { |
| |
| |
| if (!$inIdentifier) { |
| $versionPartTokens += $idToken |
| $inIdentifier = $true |
| } |
| else { |
| $identifierPortionTokens += $idToken |
| } |
| } |
| } |
| } |
|
|
| if ($versionPartTokens.Count -gt 3) { |
| throw "Cannot create Semantic Version from the string $Version containing 4 or more version tokens" |
| } |
|
|
| $packageSemanticVersion = ($versionPartTokens -join '.') |
| if ($identifierPortionTokens.Count -gt 0) { |
| $packageSemanticVersion += '-' + ($identifierPortionTokens -join '-') |
| } |
|
|
| $packageSemanticVersion |
| } |
|
|
| < |
| .Synopsis |
| Creates a Windows AppX MSIX package and assumes that the binaries are already built using 'Start-PSBuild'. |
| This only works on a Windows machine due to the usage of makeappx.exe. |
| .EXAMPLE |
| |
| cd $RootPathOfPowerShellRepo |
| Import-Module .\build.psm1; Import-Module .\tools\packaging\packaging.psm1 |
| New-MSIXPackage -Verbose -ProductSourcePath '.\src\powershell-win-core\bin\Debug\net8.0\win7-x64\publish' -ProductTargetArchitecture x64 -ProductVersion '1.2.3' |
| |
| function New-MSIXPackage |
| { |
| [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')] |
| param ( |
|
|
| # Name of the Product |
| [ValidateNotNullOrEmpty()] |
| [string] $ProductName = 'PowerShell', |
|
|
| # Suffix of the Name |
| [string] $ProductNameSuffix, |
|
|
| # Version of the Product |
| [Parameter(Mandatory = $true)] |
| [ValidateNotNullOrEmpty()] |
| [string] $ProductVersion, |
|
|
| # Source Path to the Product Files - required to package the contents into an MSIX |
| [Parameter(Mandatory = $true)] |
| [ValidateNotNullOrEmpty()] |
| [string] $ProductSourcePath, |
|
|
| # Processor Architecture |
| [Parameter(Mandatory = $true)] |
| [ValidateSet('x64','x86','arm','arm64')] |
| [string] $Architecture, |
|
|
| # Produce private package for testing in Store |
| [Switch] $Private, |
|
|
| # Produce LTS package |
| [Switch] $LTS, |
|
|
| # Force overwrite of package |
| [Switch] $Force, |
|
|
| [string] $CurrentLocation = (Get-Location) |
| ) |
|
|
| $makeappx = Get-Command makeappx -CommandType Application -ErrorAction Ignore |
| if ($null -eq $makeappx) { |
| |
| $dockerPath = Join-Path $env:SystemDrive "makeappx" |
| if (Test-Path $dockerPath) { |
| $makeappx = Get-ChildItem $dockerPath -Include makeappx.exe -Recurse | Select-Object -First 1 |
| } |
|
|
| if ($null -eq $makeappx) { |
| |
| $makeappx = Get-ChildItem "${env:ProgramFiles(x86)}\Windows Kits\10\bin\*\x64" -Include makeappx.exe -Recurse | Select-Object -First 1 |
| if ($null -eq $makeappx) { |
| throw "Could not locate makeappx.exe, make sure Windows 10 SDK is installed" |
| } |
| } |
| } |
|
|
| $makepri = Get-Item (Join-Path $makeappx.Directory "makepri.exe") -ErrorAction Stop |
|
|
| $displayName = $ProductName |
| $ProductSemanticVersion = Get-PackageSemanticVersion -Version $ProductVersion |
|
|
| if ($Private) { |
| $ProductName = 'PowerShell-Private' |
| $displayName = 'PowerShell-Private' |
| } elseif ($ProductSemanticVersion.Contains('-')) { |
| $ProductName += 'Preview' |
| $displayName += ' Preview' |
| } elseif ($LTS) { |
| $ProductName += '-LTS' |
| $displayName += ' LTS' |
| } |
|
|
| Write-Verbose -Verbose "ProductName: $productName" |
| Write-Verbose -Verbose "DisplayName: $displayName" |
|
|
| $packageName = $ProductName + '-' + $ProductSemanticVersion |
|
|
| |
| if ($ProductNameSuffix) { |
| $packageName += "-$ProductNameSuffix" |
| } |
|
|
| $ProductVersion = Get-WindowsVersion -PackageName $packageName |
|
|
| |
| |
| |
| |
| |
| $PhoneProductId = "5b3ae196-2df7-446e-8060-94b4ad878387" |
|
|
| $isPreview = Test-IsPreview -Version $ProductSemanticVersion |
| if ($isPreview) { |
| |
| $PhoneProductId = "67859fd2-b02a-45be-8fb5-62c569a3e8bf" |
| Write-Verbose "Using Preview assets" -Verbose |
| } elseif ($LTS) { |
| |
| $PhoneProductId = "b7a4b003-3704-47a9-b018-cfcc9801f4fc" |
| Write-Verbose "Using LTS assets" -Verbose |
| } |
|
|
| |
| |
| $releasePublisher = 'CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US' |
|
|
| $appxManifest = Get-Content "$RepoRoot\assets\AppxManifest.xml" -Raw |
| $appxManifest = $appxManifest. |
| Replace('$VERSION$', $ProductVersion). |
| Replace('$ARCH$', $Architecture). |
| Replace('$PRODUCTNAME$', $productName). |
| Replace('$DISPLAYNAME$', $displayName). |
| Replace('$PUBLISHER$', $releasePublisher). |
| Replace('$PHONEPRODUCTID$', $PhoneProductId) |
|
|
| $xml = [xml]$appxManifest |
| if ($isPreview) { |
| Write-Verbose -Verbose "Adding pwsh-preview.exe alias" |
| $aliasNode = $xml.Package.Applications.Application.Extensions.Extension.AppExecutionAlias.ExecutionAlias.Clone() |
| $aliasNode.alias = "pwsh-preview.exe" |
| $xml.Package.Applications.Application.Extensions.Extension.AppExecutionAlias.AppendChild($aliasNode) | Out-Null |
| } |
| $xml.Save("$ProductSourcePath\AppxManifest.xml") |
|
|
| |
| $assets = @( |
| 'Square150x150Logo' |
| 'Square44x44Logo' |
| 'Square44x44Logo.targetsize-48' |
| 'Square44x44Logo.targetsize-48_altform-unplated' |
| 'StoreLogo' |
| ) |
|
|
| if (!(Test-Path "$ProductSourcePath\assets")) { |
| $null = New-Item -ItemType Directory -Path "$ProductSourcePath\assets" |
| } |
|
|
| $assets | ForEach-Object { |
| if ($isPreview) { |
| Copy-Item -Path "$RepoRoot\assets\$_-Preview.png" -Destination "$ProductSourcePath\assets\$_.png" |
| } |
| else { |
| Copy-Item -Path "$RepoRoot\assets\$_.png" -Destination "$ProductSourcePath\assets\" |
| } |
| } |
| |
| if ($PSCmdlet.ShouldProcess("Create .msix package?")) { |
| Write-Verbose "Creating priconfig.xml" -Verbose |
| Start-NativeExecution -VerboseOutputOnError { & $makepri createconfig /o /cf (Join-Path $ProductSourcePath "priconfig.xml") /dq en-US } |
| Write-Verbose "Creating resources.pri" -Verbose |
| Push-Location $ProductSourcePath |
| Start-NativeExecution -VerboseOutputOnError { & $makepri new /v /o /pr $ProductSourcePath /cf (Join-Path $ProductSourcePath "priconfig.xml") } |
| Pop-Location |
| Write-Verbose "Creating msix package" -Verbose |
| Start-NativeExecution -VerboseOutputOnError { & $makeappx pack /o /v /h SHA256 /d $ProductSourcePath /p (Join-Path -Path $CurrentLocation -ChildPath "$packageName.msix") } |
| Write-Verbose "Created $packageName.msix" -Verbose |
| Join-Path -Path $CurrentLocation -ChildPath "$packageName.msix" |
| } |
| } |
| |
| # Removes a ComponentRef node in the files.wxs Xml Doc |
| function Remove-ComponentRefNode |
| { |
| param( |
| [Parameter(Mandatory)] |
| [System.Xml.XmlDocument] |
| $XmlDoc, |
| [Parameter(Mandatory)] |
| [System.Xml.XmlNamespaceManager] |
| $XmlNsManager, |
| [Parameter(Mandatory)] |
| [string] |
| $Id |
| ) |
| |
| $compRefXPath = '//Wix:ComponentRef[@Id="{0}"]' -f $Id |
| $node = Get-XmlNodeByXPath -XmlDoc $XmlDoc -XmlNsManager $XmlNsManager -XPath $compRefXPath |
| if ($node) |
| { |
| Remove-XmlElement -element $node |
| } |
| else |
| { |
| Write-Warning "could not remove node" |
| } |
| } |
| |
| # Get the ComponentGroup node in the files.wxs Xml Doc |
| function Get-ComponentGroupNode |
| { |
| param( |
| [Parameter(Mandatory)] |
| [System.Xml.XmlDocument] |
| $XmlDoc, |
| [Parameter(Mandatory)] |
| [System.Xml.XmlNamespaceManager] |
| $XmlNsManager |
| ) |
| |
| if (!$XmlNsManager.HasNamespace('Wix')) |
| { |
| throw 'Namespace manager must have "wix" defined.' |
| } |
| |
| $compGroupXPath = '//Wix:ComponentGroup' |
| $node = Get-XmlNodeByXPath -XmlDoc $XmlDoc -XmlNsManager $XmlNsManager -XPath $compGroupXPath |
| return $node |
| } |
| |
| # Gets the Directory Node the files.wxs Xml Doc |
| # Creates it if it does not exist |
| function Get-DirectoryNode |
| { |
| param( |
| [Parameter(Mandatory)] |
| [System.Xml.XmlElement] |
| $Node, |
| [Parameter(Mandatory)] |
| [System.Xml.XmlDocument] |
| $XmlDoc, |
| [Parameter(Mandatory)] |
| [System.Xml.XmlNamespaceManager] |
| $XmlNsManager |
| ) |
| |
| if (!$XmlNsManager.HasNamespace('Wix')) |
| { |
| throw 'Namespace manager must have "wix" defined.' |
| } |
| |
| $pathStack = [System.Collections.Stack]::new() |
| |
| [System.Xml.XmlElement] $dirNode = $Node.ParentNode.ParentNode |
| $dirNodeType = $dirNode.LocalName |
| if ($dirNodeType -eq 'DirectoryRef') |
| { |
| return Get-XmlNodeByXPath -XmlDoc $XmlDoc -XmlNsManager $XmlNsManager -XPath " |
| } |
| if ($dirNodeType -eq 'Directory') |
| { |
| while($dirNode.LocalName -eq 'Directory') { |
| $pathStack.Push($dirNode.Name) |
| $dirNode = $dirNode.ParentNode |
| } |
| $path = "//" |
| [System.Xml.XmlElement] $lastNode = $null |
| while($pathStack.Count -gt 0){ |
| $dirName = $pathStack.Pop() |
| $path += 'Wix:Directory[@Name="{0}"]' -f $dirName |
| $node = Get-XmlNodeByXPath -XmlDoc $XmlDoc -XmlNsManager $XmlNsManager -XPath $path |
|
|
| if (!$node) |
| { |
| if (!$lastNode) |
| { |
| |
| $lastNode = Get-XmlNodeByXPath -XmlDoc $XmlDoc -XmlNsManager $XmlNsManager -XPath "//Wix:DirectoryRef" |
| } |
|
|
| $newDirectory = New-XmlElement -XmlDoc $XmlDoc -LocalName 'Directory' -Node $lastNode -PassThru -NamespaceUri 'http://schemas.microsoft.com/wix/2006/wi' |
| New-XmlAttribute -XmlDoc $XmlDoc -Element $newDirectory -Name 'Name' -Value $dirName |
| New-XmlAttribute -XmlDoc $XmlDoc -Element $newDirectory -Name 'Id' -Value (New-WixId -Prefix 'dir') |
| $lastNode = $newDirectory |
| } |
| else |
| { |
| $lastNode = $node |
| } |
| if ($pathStack.Count -gt 0) |
| { |
| $path += '/' |
| } |
| } |
| return $lastNode |
| } |
|
|
| throw "unknown element type: $dirNodeType" |
| } |
|
|
| |
| function New-WixId |
| { |
| param( |
| [Parameter(Mandatory)] |
| [string] |
| $Prefix |
| ) |
|
|
| $guidPortion = (New-Guid).Guid.ToUpperInvariant() -replace '\-' ,'' |
| "$Prefix$guidPortion" |
| } |
|
|
| function Get-WindowsVersion { |
| param ( |
| [parameter(Mandatory)] |
| [string]$PackageName |
| ) |
|
|
| $ProductVersion = Get-PackageVersionAsMajorMinorBuildRevision -Version $ProductVersion |
| if (([Version]$ProductVersion).Revision -eq -1) { |
| $ProductVersion += ".0" |
| } |
|
|
| |
| |
| |
| $pversion = [version]$ProductVersion |
| if ($pversion.Revision -ne 0) { |
| $revision = $pversion.Revision |
| if ($packageName.Contains('-rc')) { |
| |
| $revision += 100 |
| } |
|
|
| $pversion = [version]::new($pversion.Major, $pversion.Minor, $revision, 0) |
| $ProductVersion = $pversion.ToString() |
| } |
|
|
| Write-Verbose "Version: $productversion" -Verbose |
| return $productversion |
| } |
|
|
| |
| |
| function Get-PackageVersionAsMajorMinorBuildRevision |
| { |
| [CmdletBinding()] |
| param ( |
| # Version of the Package |
| [Parameter(Mandatory = $true)] |
| [ValidateNotNullOrEmpty()] |
| [string] $Version, |
| [switch] $IncrementBuildNumber |
| ) |
|
|
| Write-Verbose "Extract the version in the form of major.minor[.build[.revision]] for $Version" |
| $packageVersionTokens = $Version.Split('-') |
| $packageVersion = ([regex]::matches($Version, "\d+(\.\d+)+"))[0].value |
|
|
| if (1 -eq $packageVersionTokens.Count -and ([Version]$packageVersion).Revision -eq -1) { |
| |
| $packageVersion = $packageVersion + '.0' |
| } elseif (1 -lt $packageVersionTokens.Count) { |
| |
| $packageBuildTokens = ([regex]::Matches($packageVersionTokens[1], "\d+"))[0].value |
|
|
| if ($packageBuildTokens) |
| { |
| if($packageBuildTokens.length -gt 4) |
| { |
| |
| $packageBuildTokens = $packageBuildTokens.Substring(0,4) |
| } |
|
|
| if ($packageVersionTokens[1] -match 'rc' -and $IncrementBuildNumber) { |
| $packageBuildTokens = [int]$packageBuildTokens + 100 |
| } |
|
|
| $packageVersion = $packageVersion + '.' + $packageBuildTokens |
| } |
| else |
| { |
| $packageVersion = $packageVersion |
| } |
| } |
|
|
| $packageVersion |
| } |
|
|
| < |
| .SYNOPSIS |
| Create a smaller framework dependent package based off fxdependent package for dotnet-sdk container images. |
|
|
| .PARAMETER Path |
| Path to the folder containing the fxdependent package. |
|
|
| .PARAMETER KeepWindowsRuntimes |
| Specify this switch if the Windows runtimes are to be kept. |
| |
| function ReduceFxDependentPackage |
| { |
| [CmdletBinding()] |
| param( |
| [Parameter(Mandatory)] [string] $Path, |
| [switch] $KeepWindowsRuntimes |
| ) |
|
|
| if (-not (Test-Path $path)) |
| { |
| throw "Path not found: $Path" |
| } |
|
|
| |
| $localeFolderToRemove = 'cs', 'de', 'es', 'fr', 'it', 'ja', 'ko', 'pl', 'pt-BR', 'ru', 'tr', 'zh-Hans', 'zh-Hant' |
| Get-ChildItem $Path -Recurse -Directory | Where-Object { $_.Name -in $localeFolderToRemove } | ForEach-Object { Remove-Item $_.FullName -Force -Recurse -Verbose } |
|
|
| Write-Log -message "Starting to cleanup runtime folders" |
|
|
| $runtimeFolder = Get-ChildItem $Path -Recurse -Directory -Filter 'runtimes' |
|
|
| $runtimeFolderPath = $runtimeFolder | Out-String |
|
|
| if ($runtimeFolder.Count -eq 0) |
| { |
| Write-Log -message "No runtimes folder found under $Path. Completing cleanup." |
| return |
| } |
|
|
| Write-Log -message $runtimeFolderPath |
|
|
| if ($runtimeFolder.Count -eq 0) |
| { |
| throw "runtimes folder not found under $Path" |
| } |
|
|
| Write-Log -message (Get-ChildItem $Path | Out-String) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| $runtimesToKeep = if ($KeepWindowsRuntimes) { |
| 'win10-x64', 'win-arm', 'win-x64', 'win' |
| } else { |
| 'linux-x64', 'linux-musl-x64', 'unix', 'linux', 'linux-arm', 'linux-arm64', 'osx' |
| } |
|
|
| $runtimeFolder | ForEach-Object { |
| Get-ChildItem -Path $_.FullName -Directory -Exclude $runtimesToKeep | Remove-Item -Force -Recurse -Verbose |
| } |
|
|
| |
| Get-ChildItem -Path $Path -Filter "Microsoft.PowerShell.GlobalTool.Shim.*" | Remove-Item -Verbose |
| } |
|
|
| < |
| .SYNOPSIS |
| Performs clean up work for preparation to running New-GlobalToolNupkgSource package source creation. |
|
|
| .DESCRIPTION |
| Unnecessary package source files are removed. |
|
|
| .PARAMETER LinuxBinPath |
| Path to the folder containing the fxdependent package for Linux. |
|
|
| .PARAMETER WindowsBinPath |
| Path to the folder containing the fxdependent package for Windows. |
|
|
| .PARAMETER WindowsDesktopBinPath |
| Path to the folder containing desktop framework package for Windows. |
| |
| function Start-PrepForGlobalToolNupkg |
| { |
| [CmdletBinding()] |
| param( |
| [Parameter(Mandatory)] [string] $LinuxBinPath, |
| [Parameter(Mandatory)] [string] $WindowsBinPath, |
| [Parameter(Mandatory)] [string] $WindowsDesktopBinPath, |
| [Parameter(Mandatory)] [string] $AlpineBinPath |
| ) |
|
|
| Write-Log "Start-PrepForGlobalToolNupkg: Running clean up for New-GlobalToolNupkg package creation." |
|
|
| $libCryptoPath = Join-Path $LinuxBinPath 'libcrypto.so.1.0.0' |
| $libSSLPath = Join-Path $LinuxBinPath 'libssl.so.1.0.0' |
|
|
| if (Test-Path $libCryptoPath) { |
| Remove-Item -Path $libCryptoPath -Verbose -Force |
| } |
|
|
| if (Test-Path $libSSLPath) { |
| Remove-Item -Path $libSSLPath -Verbose -Force |
| } |
|
|
| |
| Get-ChildItem -Path $LinuxBinPath, $WindowsBinPath, $WindowsDesktopBinPath, $AlpineBinPath -Filter *.xml | Remove-Item -Verbose |
| } |
|
|
| < |
| .SYNOPSIS |
| Create a single PowerShell Global tool nuget package NuSpec source directory for the provied |
| package type. |
|
|
| .DESCRIPTION |
| A single NuSpec source directory is created for the individual package type, and the created |
| directory path is set to the environement variable name: "GlobaToolNuSpecSourcePath_${PackageType}". |
|
|
| .PARAMETER PackageType |
| Global tool package type to create. |
|
|
| .PARAMETER LinuxBinPath |
| Path to the folder containing the fxdependent package for Linux. |
|
|
| .PARAMETER WindowsBinPath |
| Path to the folder containing the fxdependent package for Windows. |
|
|
| .PARAMETER WindowsDesktopBinPath |
| Path to the folder containing desktop framework package for Windows. |
|
|
| .PARAMETER PackageVersion |
| Version for the NuGet package that will be generated. |
| |
| function New-GlobalToolNupkgSource |
| { |
| [CmdletBinding()] |
| param( |
| [Parameter(Mandatory)] [string] $PackageType, |
| [Parameter(Mandatory)] [string] $LinuxBinPath, |
| [Parameter(Mandatory)] [string] $WindowsBinPath, |
| [Parameter(Mandatory)] [string] $WindowsDesktopBinPath, |
| [Parameter(Mandatory)] [string] $AlpineBinPath, |
| [Parameter(Mandatory)] [string] $PackageVersion, |
| [Parameter()] [switch] $SkipCGManifest |
| ) |
|
|
| if ($PackageType -ne "Unified") |
| { |
| Write-Log "New-GlobalToolNupkgSource: Reducing package size for non-unified packages." |
|
|
| Write-Log "New-GlobalToolNupkgSource: Reducing size of Linux package" |
| ReduceFxDependentPackage -Path $LinuxBinPath |
|
|
| Write-Log "New-GlobalToolNupkgSource: Reducing size of Alpine package" |
| ReduceFxDependentPackage -Path $AlpineBinPath |
|
|
| Write-Log "New-GlobalToolNupkgSource: Reducing size of Windows package" |
| ReduceFxDependentPackage -Path $WindowsBinPath -KeepWindowsRuntimes |
|
|
| Write-Log "New-GlobalToolNupkgSource: Reducing size of WindowsDesktop package" |
| ReduceFxDependentPackage -Path $WindowsDesktopBinPath -KeepWindowsRuntimes |
| } |
|
|
| Write-Log "New-GlobalToolNupkgSource: Creating package: $PackageType" |
|
|
| switch ($PackageType) |
| { |
| "Unified" |
| { |
| $ShimDllPath = Join-Path $WindowsDesktopBinPath "Microsoft.PowerShell.GlobalTool.Shim.dll" |
|
|
| $PackageName = "PowerShell" |
| $RootFolder = New-TempFolder |
|
|
| Copy-Item -Path $iconPath -Destination "$RootFolder/$iconFileName" -Verbose |
|
|
| $ridFolder = New-Item -Path (Join-Path $RootFolder "tools/$script:netCoreRuntime/any") -ItemType Directory |
| $winFolder = New-Item (Join-Path $ridFolder "win") -ItemType Directory |
| $unixFolder = New-Item (Join-Path $ridFolder "unix") -ItemType Directory |
|
|
| Write-Log "New-GlobalToolNupkgSource: Copying runtime assemblies from $WindowsDesktopBinPath" |
| Copy-Item "$WindowsDesktopBinPath\*" -Destination $winFolder -Recurse |
|
|
| Write-Log "New-GlobalToolNupkgSource: Copying runtime assemblies from $LinuxBinPath" |
| Copy-Item "$LinuxBinPath\*" -Destination $unixFolder -Recurse |
|
|
| Write-Log "New-GlobalToolNupkgSource: Copying shim dll from $ShimDllPath" |
| Copy-Item $ShimDllPath -Destination $ridFolder |
|
|
| $shimConfigFile = Join-Path (Split-Path $ShimDllPath -Parent) 'Microsoft.PowerShell.GlobalTool.Shim.runtimeconfig.json' |
| Write-Log "New-GlobalToolNupkgSource: Copying shim config file from $shimConfigFile" |
| Copy-Item $shimConfigFile -Destination $ridFolder -ErrorAction Stop |
|
|
| $toolSettings = $packagingStrings.GlobalToolSettingsFile -f (Split-Path $ShimDllPath -Leaf) |
| } |
|
|
| "PowerShell.Linux.Alpine" |
| { |
| $PackageName = "PowerShell.Linux.Alpine" |
| $RootFolder = New-TempFolder |
|
|
| Copy-Item -Path $iconPath -Destination "$RootFolder/$iconFileName" -Verbose |
|
|
| $ridFolder = New-Item -Path (Join-Path $RootFolder "tools/$script:netCoreRuntime/any") -ItemType Directory |
|
|
| Write-Log "New-GlobalToolNupkgSource: Copying runtime assemblies from $AlpineBinPath for $PackageType" |
| Copy-Item "$AlpineBinPath/*" -Destination $ridFolder -Recurse |
| $toolSettings = $packagingStrings.GlobalToolSettingsFile -f "pwsh.dll" |
| } |
|
|
| "PowerShell.Linux.x64" |
| { |
| $PackageName = "PowerShell.Linux.x64" |
| $RootFolder = New-TempFolder |
|
|
| Copy-Item -Path $iconPath -Destination "$RootFolder/$iconFileName" -Verbose |
|
|
| $ridFolder = New-Item -Path (Join-Path $RootFolder "tools/$script:netCoreRuntime/any") -ItemType Directory |
|
|
| Write-Log "New-GlobalToolNupkgSource: Copying runtime assemblies from $LinuxBinPath for $PackageType" |
| Copy-Item "$LinuxBinPath/*" -Destination $ridFolder -Recurse |
| Remove-Item -Path $ridFolder/runtimes/linux-arm -Recurse -Force |
| Remove-Item -Path $ridFolder/runtimes/linux-arm64 -Recurse -Force |
| Remove-Item -Path $ridFolder/runtimes/linux-musl-x64 -Recurse -Force |
| Remove-Item -Path $ridFolder/runtimes/osx -Recurse -Force |
| $toolSettings = $packagingStrings.GlobalToolSettingsFile -f "pwsh.dll" |
| } |
|
|
| "PowerShell.Linux.arm32" |
| { |
| $PackageName = "PowerShell.Linux.arm32" |
| $RootFolder = New-TempFolder |
|
|
| Copy-Item -Path $iconPath -Destination "$RootFolder/$iconFileName" -Verbose |
|
|
| $ridFolder = New-Item -Path (Join-Path $RootFolder "tools/$script:netCoreRuntime/any") -ItemType Directory |
|
|
| Write-Log "New-GlobalToolNupkgSource: Copying runtime assemblies from $LinuxBinPath for $PackageType" |
| Copy-Item "$LinuxBinPath/*" -Destination $ridFolder -Recurse |
| Remove-Item -Path $ridFolder/runtimes/linux-arm64 -Recurse -Force |
| Remove-Item -Path $ridFolder/runtimes/linux-musl-x64 -Recurse -Force |
| Remove-Item -Path $ridFolder/runtimes/linux-x64 -Recurse -Force |
| Remove-Item -Path $ridFolder/runtimes/osx -Recurse -Force |
| $toolSettings = $packagingStrings.GlobalToolSettingsFile -f "pwsh.dll" |
| } |
|
|
| "PowerShell.Linux.arm64" |
| { |
| $PackageName = "PowerShell.Linux.arm64" |
| $RootFolder = New-TempFolder |
|
|
| Copy-Item -Path $iconPath -Destination "$RootFolder/$iconFileName" -Verbose |
|
|
| $ridFolder = New-Item -Path (Join-Path $RootFolder "tools/$script:netCoreRuntime/any") -ItemType Directory |
|
|
| Write-Log "New-GlobalToolNupkgSource: Copying runtime assemblies from $LinuxBinPath for $PackageType" |
| Copy-Item "$LinuxBinPath/*" -Destination $ridFolder -Recurse |
| Remove-Item -Path $ridFolder/runtimes/linux-arm -Recurse -Force |
| Remove-Item -Path $ridFolder/runtimes/linux-musl-x64 -Recurse -Force |
| Remove-Item -Path $ridFolder/runtimes/linux-x64 -Recurse -Force |
| Remove-Item -Path $ridFolder/runtimes/osx -Recurse -Force |
| $toolSettings = $packagingStrings.GlobalToolSettingsFile -f "pwsh.dll" |
| } |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| |
|
|
| |
| |
| |
| |
| |
|
|
| "PowerShell.Windows.arm32" |
| { |
| $PackageName = "PowerShell.Windows.arm32" |
| $RootFolder = New-TempFolder |
|
|
| Copy-Item -Path $iconPath -Destination "$RootFolder/$iconFileName" -Verbose |
|
|
| $ridFolder = New-Item -Path (Join-Path $RootFolder "tools/$script:netCoreRuntime/any") -ItemType Directory |
|
|
| Write-Log "New-GlobalToolNupkgSource: Copying runtime assemblies from $WindowsBinPath for $PackageType" |
| Copy-Item "$WindowsBinPath/*" -Destination $ridFolder -Recurse |
| Remove-Item -Path $ridFolder/runtimes/win-x64 -Recurse -Force |
| $toolSettings = $packagingStrings.GlobalToolSettingsFile -f "pwsh.dll" |
| } |
|
|
| default { throw "New-GlobalToolNupkgSource: Unknown package type: $PackageType" } |
| } |
|
|
| $nuSpec = $packagingStrings.GlobalToolNuSpec -f $PackageName, $PackageVersion, $iconFileName |
| $nuSpec | Out-File -FilePath (Join-Path $RootFolder "$PackageName.nuspec") -Encoding ascii |
| $toolSettings | Out-File -FilePath (Join-Path $ridFolder "DotnetToolSettings.xml") -Encoding ascii |
|
|
| |
| Write-Log "New-GlobalToolNupkgSource: Global tool package ($PackageName) source created at: $RootFolder" |
|
|
| |
| $pkgNuSpecSourcePathVar = "GlobalToolNuSpecSourcePath" |
| Write-Log "New-GlobalToolNupkgSource: Creating NuSpec source path VSTS variable: $pkgNuSpecSourcePathVar" |
| Write-Verbose -Verbose "sending: [task.setvariable variable=$pkgNuSpecSourcePathVar]$RootFolder" |
| Write-Host "##vso[task.setvariable variable=$pkgNuSpecSourcePathVar]$RootFolder" |
| $global:GlobalToolNuSpecSourcePath = $RootFolder |
|
|
| |
| $pkgNameVar = "GlobalToolPkgName" |
| Write-Log "New-GlobalToolNupkgSource: Creating current package name variable: $pkgNameVar" |
| Write-Verbose -Verbose "sending: vso[task.setvariable variable=$pkgNameVar]$PackageName" |
| Write-Host "##vso[task.setvariable variable=$pkgNameVar]$PackageName" |
| $global:GlobalToolPkgName = $PackageName |
|
|
| if ($SkipCGManifest.IsPresent) { |
| Write-Verbose -Verbose "New-GlobalToolNupkgSource: Skipping CGManifest creation." |
| return |
| } |
|
|
| |
| $globalToolCGManifestPFilePath = Join-Path -Path "$env:REPOROOT" -ChildPath "tools/cgmanifest/main/cgmanifest.json" |
| $globalToolCGManifestFilePath = Resolve-Path -Path $globalToolCGManifestPFilePath -ErrorAction SilentlyContinue |
| if (($null -eq $globalToolCGManifestFilePath) -or (! (Test-Path -Path $globalToolCGManifestFilePath))) |
| { |
| throw "New-GlobalToolNupkgSource: Invalid build source CGManifest file path: $globalToolCGManifestPFilePath" |
| } |
| $globalToolCGManifestSourceRoot = New-TempFolder |
| Write-Log "New-GlobalToolNupkgSource: Creating new CGManifest.json file at: $globalToolCGManifestSourceRoot" |
| Copy-Item -Path $globalToolCGManifestFilePath -Destination $globalToolCGManifestSourceRoot -Force |
|
|
| $globalToolCGManifestPathVar = "GlobalToolCGManifestPath" |
| Write-Log "New-GlobalToolNupkgSource: Creating CGManifest path variable, $globalToolCGManifestPathVar, for path: $globalToolCGManifestSourceRoot" |
| Write-Host "##vso[task.setvariable variable=$globalToolCGManifestPathVar]$globalToolCGManifestSourceRoot" |
| } |
|
|
| < |
| .SYNOPSIS |
| Create a single PowerShell Global tool nuget package from the provied package source folder. |
|
|
| .DESCRIPTION |
| Creates a single PowerShell Global tool nuget package based on the provided package NuSpec source |
| folder (created by New-GlobalNupkgSource), and places the created package in the provided destination |
| folder. |
|
|
| .PARAMETER PackageNuSpecPath |
| Location of NuSpec path containing source for package creation. |
|
|
| .PARAMETER PackageName |
| Name of Global Tool package being created. |
|
|
| .PARAMETER DestinationPath |
| Path to the folder where the generated package is placed. |
| |
| function New-GlobalToolNupkgFromSource |
| { |
| [CmdletBinding()] |
| param ( |
| [Parameter(Mandatory)] [string] $PackageNuSpecPath, |
| [Parameter(Mandatory)] [string] $PackageName, |
| [Parameter(Mandatory)] [string] $DestinationPath, |
| [Parameter()] [string] $CGManifestPath |
| ) |
|
|
| if (! (Test-Path -Path $PackageNuSpecPath)) |
| { |
| throw "New-GlobalToolNupkgFromSource: failed because NuSpec path does not exist: $PackageNuSpecPath" |
| } |
|
|
| Write-Log "New-GlobalToolNupkgFromSource: Creating package: $PackageName" |
| New-NugetPackage -NuSpecPath $PackageNuSpecPath -PackageDestinationPath $DestinationPath |
|
|
| Write-Log "New-GlobalToolNupkgFromSource: Removing GlobalTool NuSpec source directory: $PackageNuSpecPath" |
| Remove-Item -Path $PackageNuSpecPath -Recurse -Force -ErrorAction SilentlyContinue |
|
|
| if (-not ($PSBoundParameters.ContainsKey('CGManifestPath'))) |
| { |
| Write-Verbose -Verbose "New-GlobalToolNupkgFromSource: CGManifest file path not provided." |
| return |
| } |
|
|
| Write-Log "New-GlobalToolNupkgFromSource: Removing GlobalTool CGManifest source directory: $CGManifestPath" |
| if (! (Test-Path -Path $CGManifestPath)) |
| { |
| Write-Verbose -Verbose -Message "New-GlobalToolNupkgFromSource: CGManifest file does not exist: $CGManifestPath" |
| return |
| } |
| Remove-Item -Path $CGManifestPath -Recurse -Force -ErrorAction SilentlyContinue |
| } |
|
|
| ${mainLinuxBuildFolder} = 'pwshLinuxBuild' |
| ${minSizeLinuxBuildFolder} = 'pwshLinuxBuildMinSize' |
| ${arm32LinuxBuildFolder} = 'pwshLinuxBuildArm32' |
| ${arm64LinuxBuildFolder} = 'pwshLinuxBuildArm64' |
| ${amd64MarinerBuildFolder} = 'pwshMarinerBuildAmd64' |
| ${amd64AlpineFxdBuildFolder} = 'pwshAlpineFxdBuildAmd64' |
| ${arm64MarinerBuildFolder} = 'pwshMarinerBuildArm64' |
|
|
| < |
| Used in Azure DevOps Yaml to package all the linux packages for a channel. |
| |
| function Invoke-AzDevOpsLinuxPackageCreation { |
| param( |
| [switch] |
| $LTS, |
|
|
| [Parameter(Mandatory)] |
| [ValidatePattern("^v\d+\.\d+\.\d+(-\w+(\.\d{1,2})?)?$")] |
| [ValidateNotNullOrEmpty()] |
| [string]$ReleaseTag, |
|
|
| [Parameter(Mandatory)] |
| [ValidateSet('fxdependent', 'alpine', 'deb', 'rpm')] |
| [String]$BuildType |
| ) |
|
|
| if (!${env:SYSTEM_ARTIFACTSDIRECTORY}) { |
| throw "Must be run in Azure DevOps" |
| } |
|
|
| try { |
| Write-Verbose "Packaging '$BuildType'; LTS:$LTS for $ReleaseTag ..." -Verbose |
|
|
| Restore-PSOptions -PSOptionsPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${mainLinuxBuildFolder}-meta\psoptions.json" |
|
|
| $releaseTagParam = @{ 'ReleaseTag' = $ReleaseTag } |
|
|
| switch ($BuildType) { |
| 'fxdependent' { |
| $filePermissionFile = "${env:SYSTEM_ARTIFACTSDIRECTORY}\${mainLinuxBuildFolder}-meta\linuxFilePermission.json" |
| Set-LinuxFilePermission -FilePath $filePermissionFile -RootPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${mainLinuxBuildFolder}" |
| Start-PSPackage -Type 'fxdependent' @releaseTagParam -LTS:$LTS |
| } |
| 'alpine' { |
| $filePermissionFile = "${env:SYSTEM_ARTIFACTSDIRECTORY}\${mainLinuxBuildFolder}-meta\linuxFilePermission.json" |
| Set-LinuxFilePermission -FilePath $filePermissionFile -RootPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${mainLinuxBuildFolder}" |
| Start-PSPackage -Type 'tar-alpine' @releaseTagParam -LTS:$LTS |
| } |
| 'rpm' { |
| Start-PSPackage -Type 'rpm' @releaseTagParam -LTS:$LTS |
| } |
| default { |
| $filePermissionFile = "${env:SYSTEM_ARTIFACTSDIRECTORY}\${mainLinuxBuildFolder}-meta\linuxFilePermission.json" |
| Set-LinuxFilePermission -FilePath $filePermissionFile -RootPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${mainLinuxBuildFolder}" |
| Start-PSPackage @releaseTagParam -LTS:$LTS -Type 'deb', 'tar' |
| } |
| } |
|
|
| if ($BuildType -eq 'deb') { |
| Start-PSPackage -Type tar @releaseTagParam -LTS:$LTS |
|
|
| Restore-PSOptions -PSOptionsPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${minSizeLinuxBuildFolder}-meta\psoptions.json" |
|
|
| $filePermissionFile = "${env:SYSTEM_ARTIFACTSDIRECTORY}\${minSizeLinuxBuildFolder}-meta\linuxFilePermission.json" |
| Set-LinuxFilePermission -FilePath $filePermissionFile -RootPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${minSizeLinuxBuildFolder}" |
|
|
| Write-Verbose -Verbose "---- Min-Size ----" |
| Write-Verbose -Verbose "options.Output: $($options.Output)" |
| Write-Verbose -Verbose "options.Top $($options.Top)" |
|
|
| Start-PSPackage -Type min-size @releaseTagParam -LTS:$LTS |
|
|
| |
| |
| Restore-PSOptions -PSOptionsPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${arm32LinuxBuildFolder}-meta\psoptions.json" |
| $filePermissionFile = "${env:SYSTEM_ARTIFACTSDIRECTORY}\${arm32LinuxBuildFolder}-meta\linuxFilePermission.json" |
| Set-LinuxFilePermission -FilePath $filePermissionFile -RootPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${arm32LinuxBuildFolder}" |
| Start-PSPackage -Type tar-arm @releaseTagParam -LTS:$LTS |
|
|
| |
| |
| Restore-PSOptions -PSOptionsPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${arm64LinuxBuildFolder}-meta\psoptions.json" |
| $filePermissionFile = "${env:SYSTEM_ARTIFACTSDIRECTORY}\${arm64LinuxBuildFolder}-meta\linuxFilePermission.json" |
| Set-LinuxFilePermission -FilePath $filePermissionFile -RootPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${arm64LinuxBuildFolder}" |
| Start-PSPackage -Type tar-arm64 @releaseTagParam -LTS:$LTS |
| } elseif ($BuildType -eq 'rpm') { |
| |
| Write-Verbose -Verbose "Generating mariner amd64 package" |
| Restore-PSOptions -PSOptionsPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${amd64MarinerBuildFolder}-meta\psoptions.json" |
| $filePermissionFile = "${env:SYSTEM_ARTIFACTSDIRECTORY}\${amd64MarinerBuildFolder}-meta\linuxFilePermission.json" |
| Set-LinuxFilePermission -FilePath $filePermissionFile -RootPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${amd64MarinerBuildFolder}" |
|
|
| Write-Verbose -Verbose "---- rpm-fxdependent ----" |
| Write-Verbose -Verbose "options.Output: $($options.Output)" |
| Write-Verbose -Verbose "options.Top $($options.Top)" |
|
|
| Start-PSPackage -Type rpm-fxdependent @releaseTagParam -LTS:$LTS |
|
|
| |
| Write-Verbose -Verbose "Generating mariner arm64 package" |
| Restore-PSOptions -PSOptionsPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${arm64MarinerBuildFolder}-meta\psoptions.json" |
| $filePermissionFile = "${env:SYSTEM_ARTIFACTSDIRECTORY}\${arm64MarinerBuildFolder}-meta\linuxFilePermission.json" |
| Set-LinuxFilePermission -FilePath $filePermissionFile -RootPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${arm64MarinerBuildFolder}" |
|
|
| Write-Verbose -Verbose "---- rpm-fxdependent-arm64 ----" |
| Write-Verbose -Verbose "options.Output: $($options.Output)" |
| Write-Verbose -Verbose "options.Top $($options.Top)" |
|
|
| Start-PSPackage -Type rpm-fxdependent-arm64 @releaseTagParam -LTS:$LTS |
| } elseif ($BuildType -eq 'alpine') { |
| Restore-PSOptions -PSOptionsPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${amd64AlpineFxdBuildFolder}-meta\psoptions.json" |
| $filePermissionFile = "${env:SYSTEM_ARTIFACTSDIRECTORY}\${amd64AlpineFxdBuildFolder}-meta\linuxFilePermission.json" |
| Set-LinuxFilePermission -FilePath $filePermissionFile -RootPath "${env:SYSTEM_ARTIFACTSDIRECTORY}\${amd64AlpineFxdBuildFolder}" |
|
|
| Write-Verbose -Verbose "---- tar-alpine-fxdependent ----" |
| Write-Verbose -Verbose "options.Output: $($options.Output)" |
| Write-Verbose -Verbose "options.Top $($options.Top)" |
|
|
| Start-PSPackage -Type tar-alpine-fxdependent @releaseTagParam -LTS:$LTS |
| } |
| } |
| catch { |
| Get-Error -InputObject $_ |
| throw |
| } |
| } |
|
|
| < |
| Used in Azure DevOps Yaml to do all the builds needed for all Linux packages for a channel. |
| |
| function Invoke-AzDevOpsLinuxPackageBuild { |
| param ( |
| [Parameter(Mandatory)] |
| [ValidatePattern("^v\d+\.\d+\.\d+(-\w+(\.\d{1,2})?)?$")] |
| [ValidateNotNullOrEmpty()] |
| [string]$ReleaseTag, |
|
|
| [Parameter(Mandatory)] |
| [ValidateSet('fxdependent', 'alpine', 'deb', 'rpm')] |
| [String]$BuildType |
| ) |
|
|
| if (!${env:SYSTEM_ARTIFACTSDIRECTORY}) { |
| throw "Must be run in Azure DevOps" |
| } |
|
|
| try { |
|
|
| Write-Verbose "Building '$BuildType' for $ReleaseTag ..." -Verbose |
|
|
| $releaseTagParam = @{ 'ReleaseTag' = $ReleaseTag } |
|
|
| $buildParams = @{ Configuration = 'Release'; PSModuleRestore = $true; Restore = $true } |
|
|
| switch ($BuildType) { |
| 'fxdependent' { |
| $buildParams.Add("Runtime", "fxdependent") |
| } |
| 'alpine' { |
| $buildParams.Add("Runtime", 'linux-musl-x64') |
| } |
| } |
|
|
| $buildFolder = "${env:SYSTEM_ARTIFACTSDIRECTORY}/${mainLinuxBuildFolder}" |
| Start-PSBuild @buildParams @releaseTagParam -Output $buildFolder -PSOptionsPath "${buildFolder}-meta/psoptions.json" |
| Get-ChildItem -Path $buildFolder -Recurse -File | Export-LinuxFilePermission -FilePath "${buildFolder}-meta/linuxFilePermission.json" -RootPath ${buildFolder} -Force |
|
|
| |
| Remove-Item "${buildFolder}\*.pdb" -Force |
|
|
| if ($BuildType -eq 'deb') { |
| |
| $options = Get-PSOptions |
| Write-Verbose -Verbose "---- Min-Size ----" |
| Write-Verbose -Verbose "options.Output: $($options.Output)" |
| Write-Verbose -Verbose "options.Top $($options.Top)" |
| $binDir = Join-Path -Path $options.Top -ChildPath 'bin' |
| if (Test-Path -Path $binDir) { |
| Write-Verbose -Verbose "Remove $binDir, to get a clean build for min-size package" |
| Remove-Item -Path $binDir -Recurse -Force |
| } |
|
|
| $buildParams['ForMinimalSize'] = $true |
| $buildFolder = "${env:SYSTEM_ARTIFACTSDIRECTORY}/${minSizeLinuxBuildFolder}" |
| Start-PSBuild -Clean @buildParams @releaseTagParam -Output $buildFolder -PSOptionsPath "${buildFolder}-meta/psoptions.json" |
| |
| Remove-Item "${buildFolder}\*.pdb", "${buildFolder}\*.xml" -Force |
| Get-ChildItem -Path $buildFolder -Recurse -File | Export-LinuxFilePermission -FilePath "${buildFolder}-meta/linuxFilePermission.json" -RootPath ${buildFolder} -Force |
|
|
| |
| |
| $buildFolder = "${env:SYSTEM_ARTIFACTSDIRECTORY}/${arm32LinuxBuildFolder}" |
| Start-PSBuild -Configuration Release -Restore -Runtime linux-arm -PSModuleRestore @releaseTagParam -Output $buildFolder -PSOptionsPath "${buildFolder}-meta/psoptions.json" |
| |
| Remove-Item "${buildFolder}\*.pdb" -Force |
| Get-ChildItem -Path $buildFolder -Recurse -File | Export-LinuxFilePermission -FilePath "${buildFolder}-meta/linuxFilePermission.json" -RootPath ${buildFolder} -Force |
|
|
| $buildFolder = "${env:SYSTEM_ARTIFACTSDIRECTORY}/${arm64LinuxBuildFolder}" |
| Start-PSBuild -Configuration Release -Restore -Runtime linux-arm64 -PSModuleRestore @releaseTagParam -Output $buildFolder -PSOptionsPath "${buildFolder}-meta/psoptions.json" |
| |
| Remove-Item "${buildFolder}\*.pdb" -Force |
| Get-ChildItem -Path $buildFolder -Recurse -File | Export-LinuxFilePermission -FilePath "${buildFolder}-meta/linuxFilePermission.json" -RootPath ${buildFolder} -Force |
| } elseif ($BuildType -eq 'rpm') { |
| |
| $options = Get-PSOptions |
| Write-Verbose -Verbose "---- Mariner x64 ----" |
| Write-Verbose -Verbose "options.Output: $($options.Output)" |
| Write-Verbose -Verbose "options.Top $($options.Top)" |
| $binDir = Join-Path -Path $options.Top -ChildPath 'bin' |
| if (Test-Path -Path $binDir) { |
| Write-Verbose -Verbose "Remove $binDir, to get a clean build for Mariner x64 package" |
| Remove-Item -Path $binDir -Recurse -Force |
| } |
|
|
| $buildParams['Runtime'] = 'fxdependent-linux-x64' |
| $buildFolder = "${env:SYSTEM_ARTIFACTSDIRECTORY}/${amd64MarinerBuildFolder}" |
| Start-PSBuild -Clean @buildParams @releaseTagParam -Output $buildFolder -PSOptionsPath "${buildFolder}-meta/psoptions.json" |
| |
| Remove-Item "${buildFolder}\*.pdb", "${buildFolder}\*.xml" -Force |
| Get-ChildItem -Path $buildFolder -Recurse -File | Export-LinuxFilePermission -FilePath "${buildFolder}-meta/linuxFilePermission.json" -RootPath ${buildFolder} -Force |
|
|
| |
| $options = Get-PSOptions |
| Write-Verbose -Verbose "---- Mariner arm64 ----" |
|
|
| Write-Verbose -Verbose "options.Output: $($options.Output)" |
| Write-Verbose -Verbose "options.Top $($options.Top)" |
| $binDir = Join-Path -Path $options.Top -ChildPath 'bin' |
| if (Test-Path -Path $binDir) { |
| Write-Verbose -Verbose "Remove $binDir, to get a clean build for Mariner arm64 package" |
| Remove-Item -Path $binDir -Recurse -Force |
| } |
|
|
| $buildParams['Runtime'] = 'fxdependent-linux-arm64' |
| $buildFolder = "${env:SYSTEM_ARTIFACTSDIRECTORY}/${arm64MarinerBuildFolder}" |
|
|
| Start-PSBuild -Clean @buildParams @releaseTagParam -Output $buildFolder -PSOptionsPath "${buildFolder}-meta/psoptions.json" |
| |
| Remove-Item "${buildFolder}\*.pdb", "${buildFolder}\*.xml" -Force |
| Get-ChildItem -Path $buildFolder -Recurse -File | Export-LinuxFilePermission -FilePath "${buildFolder}-meta/linuxFilePermission.json" -RootPath ${buildFolder} -Force |
| } elseif ($BuildType -eq 'alpine') { |
| |
| $options = Get-PSOptions |
| Write-Verbose -Verbose "---- fxdependent alpine x64 ----" |
| Write-Verbose -Verbose "options.Output: $($options.Output)" |
| Write-Verbose -Verbose "options.Top $($options.Top)" |
| $binDir = Join-Path -Path $options.Top -ChildPath 'bin' |
| if (Test-Path -Path $binDir) { |
| Write-Verbose -Verbose "Remove $binDir, to get a clean build for Mariner package" |
| Remove-Item -Path $binDir -Recurse -Force |
| } |
|
|
| $buildParams['Runtime'] = 'fxdependent-noopt-linux-musl-x64' |
| $buildFolder = "${env:SYSTEM_ARTIFACTSDIRECTORY}/${amd64AlpineFxdBuildFolder}" |
| Start-PSBuild -Clean @buildParams @releaseTagParam -Output $buildFolder -PSOptionsPath "${buildFolder}-meta/psoptions.json" |
| |
| Remove-Item "${buildFolder}\*.pdb", "${buildFolder}\*.xml" -Force |
| Get-ChildItem -Path $buildFolder -Recurse -File | Export-LinuxFilePermission -FilePath "${buildFolder}-meta/linuxFilePermission.json" -RootPath ${buildFolder} -Force |
| } |
| } |
| catch { |
| Get-Error -InputObject $_ |
| throw |
| } |
| } |
|
|
| < |
| Apply the file permissions specified in the json file $FilePath to the files under $RootPath. |
| The format of the json file is like: |
|
|
| { |
| "System.Net.WebClient.dll": "744", |
| "Schemas/PSMaml/developer.xsd": "644", |
| "ref/System.Security.AccessControl.dll": "744", |
| "ref/System.IO.dll": "744", |
| "cs/Microsoft.CodeAnalysis.resources.dll": "744", |
| "Schemas/PSMaml/base.xsd": "644", |
| "Schemas/PSMaml/structureProcedure.xsd": "644", |
| "ref/System.Net.Security.dll": "744" |
| } |
| |
| function Set-LinuxFilePermission { |
| [CmdletBinding()] |
| param ( |
| [Parameter(Mandatory)] [string] $FilePath, |
| [Parameter(Mandatory)] [string] $RootPath |
| ) |
|
|
| if (-not (Test-Path $FilePath)) { |
| throw "File does not exist: $FilePath" |
| } |
|
|
| if (-not (Test-Path $RootPath)) { |
| throw "File does not exist: $RootPath" |
| } |
|
|
| try { |
| Push-Location $RootPath |
| $filePermission = Get-Content $FilePath -Raw | ConvertFrom-Json -AsHashtable |
|
|
| Write-Verbose -Verbose -Message "Got file permission: $($filePermission.Count) for $FilePath" |
|
|
| $filePermission.GetEnumerator() | ForEach-Object { |
| $file = $_.Name |
| $permission = $_.Value |
| $fileFullName = Join-Path -Path $RootPath -ChildPath $file |
| Write-Verbose "Set permission $permission to $fileFullName" -Verbose |
| chmod $permission $fileFullName |
| } |
| } |
| finally { |
| Pop-Location |
| } |
| } |
|
|
| < |
| Store the linux file permissions for all the files under root path $RootPath to the json file $FilePath. |
| The json file stores them as relative paths to the root. |
|
|
| The format of the json file is like: |
|
|
| { |
| "System.Net.WebClient.dll": "744", |
| "Schemas/PSMaml/developer.xsd": "644", |
| "ref/System.Security.AccessControl.dll": "744", |
| "ref/System.IO.dll": "744", |
| "cs/Microsoft.CodeAnalysis.resources.dll": "744", |
| "Schemas/PSMaml/base.xsd": "644", |
| "Schemas/PSMaml/structureProcedure.xsd": "644", |
| "ref/System.Net.Security.dll": "744" |
| } |
|
|
| |
| function Export-LinuxFilePermission { |
| [CmdletBinding()] |
| param ( |
| [Parameter(Mandatory)] [string] $FilePath, |
| [Parameter(Mandatory)] [string] $RootPath, |
| [Parameter(Mandatory, ValueFromPipeline = $true)] [System.IO.FileInfo[]] $InputObject, |
| [Parameter()] [switch] $Force |
| ) |
|
|
| begin { |
| if (Test-Path $FilePath) { |
| if (-not $Force) { |
| throw "File '$FilePath' already exists." |
| } |
| else { |
| Remove-Item $FilePath -Force |
| } |
| } |
|
|
| $fileData = @{} |
| } |
|
|
| process { |
| foreach ($object in $InputObject) { |
| Write-Verbose "Processing $($object.FullName)" |
| |
| $filePerms = [convert]::ToString($object.unixstat.mode, 8).substring(3) |
| $relativePath = [System.IO.Path]::GetRelativePath($RootPath, $_.FullName) |
| $fileData.Add($relativePath, $filePerms) |
| } |
| } |
|
|
| end { |
| $fileData | ConvertTo-Json -Depth 10 | Out-File -FilePath $FilePath |
| } |
| } |
|
|
| enum PackageManifestResultStatus { |
| Mismatch |
| Match |
| MissingFromManifest |
| MissingFromPackage |
| } |
|
|
| class PackageManifestResult { |
| [string] $File |
| [string] $ExpectedHash |
| [string] $ActualHash |
| [PackageManifestResultStatus] $Status |
| } |
|
|
| function Test-PackageManifest { |
| param ( |
| [Parameter(Mandatory)] |
| [string] |
| $PackagePath |
| ) |
|
|
| Begin { |
| $spdxManifestPath = Join-Path $PackagePath -ChildPath "/_manifest/spdx_2.2/manifest.spdx.json" |
| $man = Get-Content $spdxManifestPath -ErrorAction Stop | convertfrom-json |
| $inManifest = @() |
| } |
|
|
| Process { |
| Write-Verbose "Processing $($man.files.count) files..." -verbose |
| $man.files | ForEach-Object { |
| $filePath = Join-Path $PackagePath -childPath $_.fileName |
| $checksumObj = $_.checksums | Where-Object {$_.algorithm -eq 'sha256'} |
| $sha256 = $checksumObj.checksumValue |
| $actualHash = $null |
| $actualHash = (Get-FileHash -Path $filePath -Algorithm sha256 -ErrorAction SilentlyContinue).Hash |
| $inManifest += $filePath |
| if($actualHash -ne $sha256) { |
| $status = [PackageManifestResultStatus]::Mismatch |
| if (!$actualHash) { |
| $status = [PackageManifestResultStatus]::MissingFromPackage |
| } |
| [PackageManifestResult] $result = @{ |
| File = $filePath |
| ExpectedHash = $sha256 |
| ActualHash = $actualHash |
| Status = $status |
| } |
| Write-Output $result |
| } |
| else { |
| [PackageManifestResult] $result = @{ |
| File = $filePath |
| ExpectedHash = $sha256 |
| ActualHash = $actualHash |
| Status = [PackageManifestResultStatus]::Match |
| } |
| Write-Output $result |
| } |
| } |
|
|
|
|
| Get-ChildItem $PackagePath -recurse | Select-Object -ExpandProperty FullName | foreach-object { |
| if(!$inManifest -contains $_) { |
| $actualHash = (get-filehash -Path $_ -algorithm sha256 -erroraction silentlycontinue).Hash |
| [PackageManifestResult] $result = @{ |
| File = $_ |
| ExpectedHash = $null |
| ActualHash = $actualHash |
| Status = [PackageManifestResultStatus]::MissingFromManifest |
| } |
| Write-Output $result |
| } |
| } |
| } |
| } |
|
|
| |
| function Get-PEInfo { |
| [CmdletBinding()] |
| param([Parameter(ValueFromPipeline = $true)][string] $File) |
| BEGIN { |
| |
| enum MachineOSOverride { |
| Windows = 0 |
| SunOS = 6546 |
| NetBSD = 6547 |
| Apple = 17988 |
| Linux = 31609 |
| FreeBSD = 44484 |
| } |
|
|
| |
| class PsPeInfo { |
| [string]$File |
| [bool]$CrossGen |
| [Nullable[MachineOSOverride]]$OS |
| [System.Reflection.PortableExecutable.Machine]$Architecture |
| [Nullable[System.Reflection.PortableExecutable.CorFlags]]$Flags |
| } |
|
|
| } |
| PROCESS { |
| $filePath = (get-item $file).fullname |
| $CrossGenFlag = 4 |
| try { |
| $stream = [System.IO.FileStream]::new($FilePath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read) |
| $peReader = [System.Reflection.PortableExecutable.PEReader]::new($stream) |
| $flags = $peReader.PEHeaders.CorHeader.Flags |
| if (-not $flags) { |
| Write-Warning "$filePath is not a managed assembly" |
| } |
| $machine = $peReader.PEHeaders.CoffHeader.Machine |
| if (-not $machine) { |
| throw "Null Machine" |
| } |
| } catch { |
| $er = [system.management.automation.errorrecord]::new(([InvalidOperationException]::new($_)), "Get-PEInfo:InvalidOperation", "InvalidOperation", $filePath) |
| $PSCmdlet.WriteError($er) |
| return |
| } finally { |
| if ($peReader) { |
| $peReader.Dispose() |
| } |
| } |
|
|
| [ushort]$r2rOsArch = $machine |
|
|
| $RealOS = $null |
| $realarch = "unknown" |
| foreach ($os in [enum]::GetValues([MachineOSOverride])) { |
| foreach ($architecture in [Enum]::GetValues([System.Reflection.PortableExecutable.Machine])) { |
| if (([ushort]$architecture -BXOR [ushort]$os) -eq [ushort]$r2rOsArch) { |
| $realOS = $os |
| $realArch = $architecture |
|
|
| [PsPeInfo]@{ |
| File = $File |
| OS = $realos |
| Architecture = $realarch |
| CrossGen = [bool]($flags -band $CrossGenFlag) |
| Flags = $flags |
| } |
| return |
| } |
| } |
| } |
| } |
| } |
|
|
| function ConvertTo-PEArchitecture { |
| [CmdletBinding()] |
| param( |
| [Parameter(ValueFromPipeline = $true)] |
| [string] |
| $Architecture |
| ) |
|
|
| PROCESS { |
| switch ($Architecture) { |
| "x86" { "I386" } |
| "x64" { "AMD64" } |
| "arm" { "ArmThumb2" } |
| default { $Architecture } |
| } |
| } |
| } |
|
|
| function ConvertTo-PEOperatingSystem { |
| [CmdletBinding()] |
| param( |
| [Parameter(ValueFromPipeline = $true)] |
| [string] |
| $OperatingSystem |
| ) |
|
|
| PROCESS { |
| switch -regex ($OperatingSystem) { |
| "win.*" { "Windows" } |
| "Linux" { "Linux" } |
| "OSX" { "Apple" } |
| default { $OperatingSystem } |
| } |
| } |
| } |
|
|
| |
| |
| function Send-AzdoFile { |
| param ( |
| [parameter(Mandatory, ParameterSetName = 'contents')] |
| [string[]] |
| $Contents, |
| [parameter(Mandatory, ParameterSetName = 'contents')] |
| [string] |
| $LogName, |
| [parameter(Mandatory, ParameterSetName = 'path')] |
| [ValidateScript({ Test-Path -Path $_ })] |
| [string] |
| $Path |
| ) |
|
|
| $logFolder = Join-Path -Path $PWD -ChildPath 'logfile' |
| if (!(Test-Path -Path $logFolder)) { |
| $null = New-Item -Path $logFolder -ItemType Directory |
| if ($IsMacOS -or $IsLinux) { |
| $null = chmod a+rw $logFolder |
| } |
| } |
|
|
| if ($LogName) { |
| $effectiveLogName = $LogName + '.txt' |
| } else { |
| $effectiveLogName = Split-Path -Leaf -Path $Path |
| } |
|
|
| $newName = ([System.Io.Path]::GetRandomFileName() + "-$effectiveLogName") |
| if ($Contents) { |
| $logFile = Join-Path -Path $logFolder -ChildPath $newName |
|
|
| $Contents | Out-File -path $logFile -Encoding ascii |
| } else { |
| $logFile = Join-Path -Path $logFolder -ChildPath $newName |
| Copy-Item -Path $Path -Destination $logFile |
| } |
|
|
| Write-Verbose "Capture the log file as '$newName'" -Verbose |
| if($env:TF_BUILD) { |
| |
| Write-Host "##vso[artifact.upload containerfolder=$newName;artifactname=$newName]$logFile" |
| } elseif ($env:GITHUB_WORKFLOW -and $env:SYSTEM_ARTIFACTSDIRECTORY) { |
| |
| $destinationPath = $env:SYSTEM_ARTIFACTSDIRECTORY |
| Write-Verbose "Upload '$logFile' to '$destinationPath' in GitHub Action" -Verbose |
|
|
| |
| if (!(Test-Path -Path $destinationPath)) { |
| $null = New-Item -ItemType Directory -Path $destinationPath -Force |
| } |
|
|
| Copy-Item -Path $logFile -Destination $destinationPath -Force -Verbose |
| } else { |
| Write-Warning "This environment is neither Azure Devops nor GitHub Actions. Cannot capture the log file in this environment." |
| } |
| } |
|
|
| |
| class BomRecord { |
| hidden |
| [string] |
| $Pattern |
|
|
| [ValidateSet("Product", "NonProduct")] |
| [string] |
| $FileType = "NonProduct" |
|
|
| [string[]] |
| $Architecture |
|
|
| |
| |
| [string] |
| GetPattern () { |
| |
| $dirSeparator = [System.io.path]::DirectorySeparatorChar |
|
|
| |
| if ($dirSeparator -ne '/') { |
| return $this.Pattern.replace('/', $dirSeparator) |
| } |
|
|
| |
| return $this.Pattern |
| } |
|
|
| [void] |
| SetPattern ([string]$Pattern) { |
| |
| $dirSeparator = [System.io.path]::DirectorySeparatorChar |
|
|
| |
| if ($dirSeparator -ne '/') { |
| $this.Pattern = $Pattern.Replace($dirSeparator, '/') |
| } |
|
|
| |
| $this.Pattern = $Pattern |
| } |
|
|
| [void] |
| EnsureArchitecture([string[]]$DefaultArchitecture = @("x64","x86","arm64")) { |
| if (-not $this.PSObject.Properties.Match("Architecture")) { |
| $this.Architecture = $DefaultArchitecture |
| } |
| } |
| } |
|
|
| |
| |
| function Test-Bom { |
| param( |
| [ValidateSet('mac','windows','linux')] |
| [string] |
| $BomName, |
| [ValidateScript({ Test-Path $_ })] |
| [string] |
| $Path, |
| [switch] |
| $Fix, |
| [string] |
| $Architecture |
| ) |
|
|
| Write-Log "verifying no unauthorized files have been added or removed..." |
| $root = (Resolve-Path $Path).ProviderPath -replace "\$([System.io.path]::DirectorySeparatorChar)$" |
|
|
| $bomFile = Join-Path -Path $PSScriptRoot -ChildPath "Boms\$BomName.json" |
| Write-Verbose "bomFile: $bomFile" -Verbose |
| [BomRecord[]]$bomRecords = Get-Content -Path $bomFile | ConvertFrom-Json |
| $bomList = [System.Collections.Generic.List[BomRecord]]::new($bomRecords) |
| $noMatch = @() |
| $patternsUsed = @() |
| $files = @(Get-ChildItem -File -Path $Path -Recurse) |
| $totalFiles = $files.Count |
| $currentFileCount = 0 |
|
|
| |
| |
| |
| $files | ForEach-Object { |
| [System.IO.FileInfo] $file = $_ |
| $fileName = $file.Name |
| $filePath = $file.FullName |
| $currentFileCount++ |
|
|
| Write-Progress -Activity "Testing $BomName BOM" -PercentComplete (100*$currentFileCount/$totalFiles) -Status "Processing $fileName" |
|
|
| $match = $false |
| [BomRecord] $matchingRecord = $null |
|
|
| |
| foreach ($bom in $bomList) { |
| $pattern = $root + [system.io.path]::DirectorySeparatorChar + $bom.GetPattern() |
| if ($filePath -like $pattern) { |
| $matchingRecord = $bom |
| $match = $true |
| if ($patternsUsed -notcontains $bom) { |
| $patternsUsed += $bom |
| } |
| break |
| } |
| } |
|
|
| |
| if (!$match) { |
| $relativePath = $_.FullName.Replace($root, "").Substring(1) |
| $isProduct = Test-IsProductFile -Path $relativePath |
| $fileType = "NonProduct" |
| if ($isProduct) { |
| $fileType = "Product" |
| } |
|
|
| [BomRecord] $newBomRecord = [BomRecord] @{ |
| FileType = $fileType |
| } |
|
|
| $newBomRecord.SetPattern([WildcardPattern]::Escape($_.FullName.Replace($root, "").Substring(1))) |
| $noMatch += $newBomRecord |
| } |
| elseif ($matchingRecord -and ![WildcardPattern]::ContainsWildcardCharacters($matchingRecord.GetPattern())) { |
| |
| |
| if ($matchingRecord -is [BomRecord]) { |
| $null = $bomList.Remove($matchingRecord) |
| } else { |
| Write-Warning "Cannot remove matchingRecord $($matchingRecord.GetPattern())" |
| } |
| } |
| } |
|
|
| Write-Progress -Activity "Testing $BomName BOM" -Completed |
|
|
| Write-Verbose "$($noMatch.count) records need to be added to $bomFile" -Verbose |
|
|
| |
| $currentRecords = @() |
| |
| $currentRecords += $noMatch |
| |
| $currentRecords += $patternsUsed |
|
|
| |
| $newBom = Join-Path -Path ([system.io.path]::GetTempPath()) -ChildPath ("${bomName}-" + [system.io.path]::GetRandomFileName() + "-bom.json") |
|
|
| |
| $currentRecords | Sort-Object -Property FileType, Pattern | ConvertTo-Json | Out-File -Encoding utf8NoBOM -FilePath $newBom |
|
|
| |
| $needsRemoval = $bom | Where-Object { |
| $_ -notin $patternsUsed |
| } |
|
|
| Write-Verbose "$($needsRemoval.count) need removal from $bomFile" -Verbose |
|
|
| |
| if ($noMatch.count -gt 0 -or $needsRemoval.Count -gt 0) { |
| Send-AzdoFile -Path $newBom |
|
|
| |
| if ($Fix) { |
| Copy-Item -Path $newBom -Destination $bomFile -Force -Verbose |
| } |
|
|
| throw "Please update $bomFile per the above instructions" |
| } |
| } |
|
|
| |
| function Test-IsProductFile { |
| param( |
| $Path |
| ) |
|
|
| $itemsToCopy = @( |
| "*.ps1" |
| "*Microsoft.PowerShell*.dll" |
| "*Microsoft.PowerShell*.psd1" |
| "*Microsoft.PowerShell*.ps1xml" |
| "*Microsoft.WSMan.Management*.psd1" |
| "*Microsoft.WSMan.Management*.ps1xml" |
| "*pwsh.dll" |
| "*System.Management.Automation.dll" |
| "*PSDiagnostics.ps?1" |
| "*pwsh" |
| "*pwsh.exe" |
| ) |
|
|
| $itemsToExclude = @( |
| |
| "*Microsoft.PowerShell.MarkdownRender.dll" |
|
|
| ) |
| if ($Path -like $itemsToExclude) { |
| return $false |
| } |
|
|
| foreach ($pattern in $itemsToCopy) { |
| if ($Path -like $pattern) { |
| return $true |
| } |
| } |
|
|
| return $false |
| } |
|
|