|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +# PowerShell Script to build and package PowerShell from specified form and branch |
| 5 | +# Script is intented to use in Docker containers |
| 6 | +# Ensure PowerShell is available in the provided image |
| 7 | + |
| 8 | +param ( |
| 9 | + # Set default location to where VSTS cloned the repository locally. |
| 10 | + [string] $location = $env:BUILD_REPOSITORY_LOCALPATH, |
| 11 | + |
| 12 | + # Destination location of the package on docker host |
| 13 | + [Parameter(Mandatory, ParameterSetName = 'Build')] |
| 14 | + [string] $destination = '/mnt', |
| 15 | + |
| 16 | + [Parameter(Mandatory, ParameterSetName = 'Build')] |
| 17 | + [ValidatePattern("^v\d+\.\d+\.\d+(-\w+(\.\d+)?)?$")] |
| 18 | + [ValidateNotNullOrEmpty()] |
| 19 | + [string]$ReleaseTag, |
| 20 | + |
| 21 | + [Parameter(ParameterSetName = 'Build')] |
| 22 | + [ValidateSet("zip", "tar")] |
| 23 | + [string[]]$ExtraPackage, |
| 24 | + |
| 25 | + [Parameter(Mandatory, ParameterSetName = 'Bootstrap')] |
| 26 | + [switch] $BootStrap, |
| 27 | + |
| 28 | + [Parameter(Mandatory, ParameterSetName = 'Build')] |
| 29 | + [switch] $Build |
| 30 | +) |
| 31 | + |
| 32 | +# We must build in /PowerShell |
| 33 | +$repoRoot = '/PowerShell' |
| 34 | +if ($BootStrap.IsPresent) { |
| 35 | + $repoRoot = $location |
| 36 | +} |
| 37 | + |
| 38 | +if ($Build.IsPresent) { |
| 39 | + # cleanup the folder but don't delete it or the build agent will loose ownership of the folder |
| 40 | + Write-Verbose -Message "cleaning /PowerShell" -Verbose |
| 41 | + Get-ChildItem -Path /PowerShell/* -Attributes Hidden, Normal, Directory | Remove-Item -Recurse -Force |
| 42 | + |
| 43 | + # clone the repository to the location we must build from |
| 44 | + Write-Verbose -Message "cloning to /PowerShell" -Verbose |
| 45 | + git clone $location /PowerShell |
| 46 | + $releaseTagParam = @{} |
| 47 | + if ($ReleaseTag) { |
| 48 | + $releaseTagParam = @{ 'ReleaseTag' = $ReleaseTag } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +Push-Location |
| 54 | +try { |
| 55 | + Write-Verbose -Message "Init..." -Verbose |
| 56 | + Set-Location $repoRoot |
| 57 | + git submodule update --init --recursive --quiet |
| 58 | + Import-Module "$repoRoot/build.psm1" |
| 59 | + Import-Module "$repoRoot/tools/packaging" |
| 60 | + Sync-PSTags -AddRemoteIfMissing |
| 61 | + |
| 62 | + if ($BootStrap.IsPresent) { |
| 63 | + Start-PSBootstrap -Package |
| 64 | + |
| 65 | + # The gem install is run by bootstrap without sudo and fails on macOS. |
| 66 | + # Run the commands with sudo, to resolve the issue |
| 67 | + Write-Verbose -Message "Installing fpm..." -Verbose |
| 68 | + Start-NativeExecution { sudo gem install fpm -v 1.8.1 } |
| 69 | + Write-Verbose -Message "Installing ronn..." -Verbose |
| 70 | + Start-NativeExecution { sudo gem install ronn } |
| 71 | + } |
| 72 | + |
| 73 | + if ($Build.IsPresent) { |
| 74 | + Start-PSBuild -Crossgen -PSModuleRestore @releaseTagParam |
| 75 | + |
| 76 | + Start-PSPackage @releaseTagParam |
| 77 | + switch ($ExtraPackage) { |
| 78 | + "tar" { Start-PSPackage -Type tar @releaseTagParam } |
| 79 | + } |
| 80 | + } |
| 81 | +} finally { |
| 82 | + Pop-Location |
| 83 | +} |
| 84 | + |
| 85 | +if ($Build.IsPresent) { |
| 86 | + $macPackages = Get-ChildItem "$repoRoot/powershell*" -Include *.pkg, *.tar.gz |
| 87 | + foreach ($macPackage in $macPackages) { |
| 88 | + $filePath = $macPackage.FullName |
| 89 | + $name = split-path -Leaf -Path $filePath |
| 90 | + $extension = (Split-Path -Extension -Path $filePath).Replace('.', '') |
| 91 | + Write-Verbose "Copying $filePath to $destination" -Verbose |
| 92 | + Write-Host "##vso[artifact.upload containerfolder=results;artifactname=$name]$filePath" |
| 93 | + Write-Host "##vso[task.setvariable variable=Package-$extension]$filePath" |
| 94 | + Copy-Item -Path $filePath -Destination $destination -force |
| 95 | + } |
| 96 | +} |