From f0f7e56bf73a23c5cea274da3d99815545da0592 Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Sat, 2 Dec 2017 19:23:08 -0800 Subject: [PATCH 1/9] refactor start-pspackage into functions --- tools/packaging/packaging.psm1 | 529 +++++++++++++++++++++------------ 1 file changed, 340 insertions(+), 189 deletions(-) diff --git a/tools/packaging/packaging.psm1 b/tools/packaging/packaging.psm1 index 05cf70a59a4..6e6885a75b2 100644 --- a/tools/packaging/packaging.psm1 +++ b/tools/packaging/packaging.psm1 @@ -586,28 +586,8 @@ function New-UnixPackage { } } - foreach ($Dependency in "fpm", "ronn") { - if (!(precheck $Dependency "Package dependency '$Dependency' not found. Run Start-PSBootstrap -Package")) { - # These tools are not added to the path automatically on OpenSUSE 13.2 - # try adding them to the path and re-tesing first - [string] $gemsPath = $null - [string] $depenencyPath = $null - $gemsPath = Get-ChildItem -Path /usr/lib64/ruby/gems | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName - if($gemsPath) { - $depenencyPath = Get-ChildItem -Path (Join-Path -Path $gemsPath -ChildPath "gems" -AdditionalChildPath $Dependency) -Recurse | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty DirectoryName - $originalPath = $env:PATH - $env:PATH = $ENV:PATH +":" + $depenencyPath - if((precheck $Dependency "Package dependency '$Dependency' not found. Run Start-PSBootstrap -Package")) { - continue - } - else { - $env:PATH = $originalPath - } - } - - throw "Dependency precheck failed!" - } - } + # Verify depenecies are installed and in the path + Test-Dependecies $Description = $packagingStrings.Description @@ -637,31 +617,14 @@ function New-UnixPackage { } elseif ($Environment.IsMacOS) { "/usr/local/bin" } + $linkSource = "/tmp/pwsh" if($pscmdlet.ShouldProcess("Create package file system")) { - New-Item -Force -ItemType SymbolicLink -Path "/tmp/pwsh" -Target "$Destination/pwsh" >$null - - if ($Environment.IsRedHatFamily) { - # add two symbolic links to system shared libraries that libmi.so is dependent on to handle - # platform specific changes. This is the only set of platforms needed for this currently - # as Ubuntu has these specific library files in the platform and macOS builds for itself - # against the correct versions. - New-Item -Force -ItemType SymbolicLink -Target "/lib64/libssl.so.10" -Path "$Staging/libssl.so.1.0.0" >$null - New-Item -Force -ItemType SymbolicLink -Target "/lib64/libcrypto.so.10" -Path "$Staging/libcrypto.so.1.0.0" >$null - - $AfterInstallScript = [io.path]::GetTempFileName() - $AfterRemoveScript = [io.path]::GetTempFileName() - $packagingStrings.RedHatAfterInstallScript -f "$Link/pwsh" | Out-File -FilePath $AfterInstallScript -Encoding ascii - $packagingStrings.RedHatAfterRemoveScript -f "$Link/pwsh" | Out-File -FilePath $AfterRemoveScript -Encoding ascii - } - elseif ($Environment.IsUbuntu -or $Environment.IsDebian) { - $AfterInstallScript = [io.path]::GetTempFileName() - $AfterRemoveScript = [io.path]::GetTempFileName() - $packagingStrings.UbuntuAfterInstallScript -f "$Link/pwsh" | Out-File -FilePath $AfterInstallScript -Encoding ascii - $packagingStrings.UbuntuAfterRemoveScript -f "$Link/pwsh" | Out-File -FilePath $AfterRemoveScript -Encoding ascii - } + New-Item -Force -ItemType SymbolicLink -Path $linkSource -Target "$Destination/pwsh" >$null + # Generate After Install and After Remove scripts + $AfterScriptInfo = New-AfterScripts # there is a weird bug in fpm # if the target of the powershell symlink exists, `fpm` aborts @@ -676,145 +639,54 @@ function New-UnixPackage { } } - # run ronn to convert man page to roff - $RonnFile = Join-Path $PSScriptRoot "/../../assets/pwsh.1.ronn" - $RoffFile = $RonnFile -replace "\.ronn$" - - # Run ronn on assets file - # Run does not play well with files named powershell6.0.1, so we generate and then rename - Start-NativeExecution { ronn --roff $RonnFile } - - # gzip in assets directory - $GzipFile = "$RoffFile.gz" - Start-NativeExecution { gzip -f $RoffFile } - - $ManFile = Join-Path "/usr/local/share/man/man1" (Split-Path -Leaf $GzipFile) + # Generate gzip of mann file + $ManGzipInfo = New-ManGzip # Change permissions for packaging Start-NativeExecution { find $Staging -type d | xargs chmod 755 find $Staging -type f | xargs chmod 644 - chmod 644 $GzipFile + chmod 644 $ManGzipInfo.GzipFile chmod 755 "$Staging/pwsh" # only the executable should be executable } } - # Setup package dependencies - # These should match those in the Dockerfiles, but exclude tools like Git, which, and curl - $Dependencies = @() - if ($Environment.IsUbuntu -or $Environment.IsDebian) { - $Dependencies = @( - "libc6", - "libcurl3", - "libgcc1", - "libgssapi-krb5-2", - "liblttng-ust0", - "libstdc++6", - "libunwind8", - "libuuid1", - "zlib1g" - ) - - switch ($DebDistro) { - "ubuntu.14.04" { $Dependencies += @("libssl1.0.0", "libicu52") } - "ubuntu.16.04" { $Dependencies += @("libssl1.0.0", "libicu55") } - "ubuntu.17.04" { $Dependencies += @("libssl1.0.0", "libicu57") } - "debian.8" { $Dependencies += @("libssl1.0.0", "libicu52") } - "debian.9" { $Dependencies += @("libssl1.0.2", "libicu57") } - default { throw "Debian distro '$DebDistro' is not supported." } - } - } elseif ($Environment.IsRedHatFamily) { - $Dependencies = @( - "libunwind", - "libcurl", - "openssl-libs", - "libicu" - ) - } - - $Arguments = @( - "--force", "--verbose", - "--name", $Name, - "--version", $Version, - "--iteration", $Iteration, - "--maintainer", "PowerShell Team ", - "--vendor", "Microsoft Corporation", - "--url", "https://microsoft.com/powershell", - "--license", "MIT License", - "--description", $Description, - "--category", "shells", - "-t", $Type, - "-s", "dir" - ) - if ($Environment.IsRedHatFamily) { - $Arguments += @("--rpm-dist", "rhel.7") - $Arguments += @("--rpm-os", "linux") - } - if ($Environment.IsMacOS) { - $Arguments += @("--osxpkg-identifier-prefix", "com.microsoft") - } - foreach ($Dependency in $Dependencies) { - $Arguments += @("--depends", $Dependency) - } - if ($AfterInstallScript) { - $Arguments += @("--after-install", $AfterInstallScript) - } - if ($AfterRemoveScript) { - $Arguments += @("--after-remove", $AfterRemoveScript) - } - $Arguments += @( - "$Staging/=$Destination/", - "$GzipFile=$ManFile", - "/tmp/pwsh=$Link" - ) - # Add macOS powershell launcher if($Type -eq "osxpkg") { - if($pscmdlet.ShouldProcess("Add macOS launch application")) { - # Define folder for launch application. - $macosapp = "$PSScriptRoot/macos/launcher/ROOT/Applications/Powershell.app" - - # Update icns file. - $iconfile = "$PSScriptRoot/../../assets/Powershell.icns" - $iconfilebase = (Get-Item -Path $iconfile).BaseName - - # Create Resources folder, ignore error if exists. - New-Item -Force -ItemType Directory -Path "$macosapp/Contents/Resources" | Out-Null - Copy-Item -Force -Path $iconfile -Destination "$macosapp/Contents/Resources" - - # Set values in plist. - $plist = "$macosapp/Contents/Info.plist" - Start-NativeExecution { - defaults write $plist CFBundleIdentifier com.microsoft.powershell - defaults write $plist CFBundleVersion $Version - defaults write $plist CFBundleShortVersionString $Version - defaults write $plist CFBundleGetInfoString $Version - defaults write $plist CFBundleIconFile $iconfilebase - } - - # Convert to XML plist, needed because defaults native - # app auto converts it to binary format when it modify - # the plist file. - Start-NativeExecution { - plutil -convert xml1 $plist - } - - # Set permissions for plist and shell script. Note that - # defaults native app sets 700 when writing to the plist - # file from above. Both of these will be reset post fpm. - $shellscript = "$macosapp/Contents/MacOS/PowerShell.sh" - Start-NativeExecution { - chmod 644 $plist - chmod 755 $shellscript - } - - # Add app folder to fpm paths. - $appsfolder = (Resolve-Path -Path "$macosapp/..").Path + if($pscmdlet.ShouldProcess("Add macOS launch application")) + { + # Generate launcher app folder + $appsfolder = New-MacOSLauncher -Version $Version $Arguments += "$appsfolder=/" } } + $packageDependenciesParams = @{} + if($DebDistro) + { + $packageDependenciesParams['Distribution']=$DebDistro + } + + # Setup package dependencies + $Dependencies = Get-PackageDependencies @packageDependenciesParams + + $Arguments = Get-FpmArguments ` + -Name $Name ` + -Version $Version ` + -Iteration $Iteration ` + -Description $Description ` + -Type $Type ` + -Dependencies $Dependecies ` + -AfterInstallScript $AfterScriptInfo.AfterInstallScript ` + -AfterRemoveScript $AfterScriptInfo.AfterRemoveScript ` + -Staging $Staging ` + -Destination $Destination ` + -MannGzipFile $ManGzipInfo.GzipFile ` + -MannDestination $ManGzipInfo.ManFile ` + -LinkSource $LinkSource ` + -LinkDestination $Link + # Build package try { if($pscmdlet.ShouldProcess("Create $type package")) { @@ -824,24 +696,7 @@ function New-UnixPackage { if ($Environment.IsMacOS) { if($pscmdlet.ShouldProcess("Cleanup macOS launcher")) { - # This is needed to prevent installer from picking up - # the launcher app in the build structure and updating - # it which locks out subsequent package builds due to - # increase permissions. - $macosapp = "$PSScriptRoot/macos/launcher/ROOT/Applications/Powershell.app" - $plist = "$macosapp/Contents/Info.plist" - $tempguid = (New-Guid).Guid - Start-NativeExecution { - defaults write $plist CFBundleIdentifier $tempguid - plutil -convert xml1 $plist - } - - # Restore default permissions. - $shellscript = "$macosapp/Contents/MacOS/PowerShell.sh" - Start-NativeExecution { - chmod 644 $shellscript - chmod 644 $plist - } + Clear-MacOSLauncher } # this is continuation of a fpm hack for a weird bug @@ -850,13 +705,13 @@ function New-UnixPackage { Move-Item $hack_dest $symlink_dest } } - if ($AfterInstallScript) { - Remove-Item -erroraction 'silentlycontinue' $AfterInstallScript + if ($AfterScriptInfo.AfterInstallScript) { + Remove-Item -erroraction 'silentlycontinue' $AfterScriptInfo.AfterInstallScript } - if ($AfterRemoveScript) { - Remove-Item -erroraction 'silentlycontinue' $AfterRemoveScript + if ($AfterScriptInfo.AfterRemoveScript) { + Remove-Item -erroraction 'silentlycontinue' $AfterScriptInfo.AfterRemoveScript } - Remove-Item -Path $GzipFile -Force -ErrorAction SilentlyContinue + Remove-Item -Path $ManGzipInfo.GzipFile -Force -ErrorAction SilentlyContinue } # Magic to get path output @@ -886,6 +741,302 @@ function New-UnixPackage { } } + +function Get-FpmArguments +{ + 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='Installer Type')] + [String]$Type, + + [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 mann file.')] + [String]$MannGzipFile, + + [Parameter(Mandatory,HelpMessage='The destination of the mann file')] + [String]$MannDestination, + + [Parameter(Mandatory,HelpMessage='Symlink to powershell executable')] + [String]$LinkSource, + + [Parameter(Mandatory,HelpMessage='Destination for symlink to powershell executable')] + [String]$LinkDestination, + + [Parameter(HelpMessage='Packages required to install this package. Not applicable for MacOS.')] + [String[]]$Dependencies, + + [Parameter(HelpMessage='Script to run after the package installation.')] + [AllowNull()] + [String]$AfterInstallScript, + + [Parameter(HelpMessage='Script to run after the package removal.')] + [AllowNull()] + [String]$AfterRemoveScript + ) + + $Arguments = @( + "--force", "--verbose", + "--name", $Name, + "--version", $Version, + "--iteration", $Iteration, + "--maintainer", "PowerShell Team ", + "--vendor", "Microsoft Corporation", + "--url", "https://microsoft.com/powershell", + "--license", "MIT License", + "--description", $Description, + "--category", "shells", + "-t", $Type, + "-s", "dir" + ) + if ($Environment.IsRedHatFamily) { + $Arguments += @("--rpm-dist", "rhel.7") + $Arguments += @("--rpm-os", "linux") + } + if ($Environment.IsMacOS) { + $Arguments += @("--osxpkg-identifier-prefix", "com.microsoft") + } + foreach ($Dependency in $Dependencies) { + $Arguments += @("--depends", $Dependency) + } + if ($AfterInstallScript) { + $Arguments += @("--after-install", $AfterInstallScript) + } + if ($AfterRemoveScript) { + $Arguments += @("--after-remove", $AfterRemoveScript) + } + $Arguments += @( + "$Staging/=$Destination/", + "$MannGzipFile=$MannDestination", + "$LinkSource=$LinkDestination" + ) + + return $Arguments +} +function Get-PackageDependencies +{ + param() + DynamicParam { + if ($Environment.IsUbuntu -or $Environment.IsDebian) { + # Add a dynamic parameter '-Distribution' when the environment is Ubuntu or Debian + # The '-Distribution' parameter can be used to indicate which Debian distro this pacakge is targeting. + $ParameterAttr = New-Object "System.Management.Automation.ParameterAttribute" + $ParameterAttr.Mandatory = $true + $ValidateSetAttr = New-Object "System.Management.Automation.ValidateSetAttribute" -ArgumentList $Script:DebianDistributions + $Attributes = New-Object "System.Collections.ObjectModel.Collection``1[System.Attribute]" + $Attributes.Add($ParameterAttr) > $null + $Attributes.Add($ValidateSetAttr) > $null + + $Parameter = New-Object "System.Management.Automation.RuntimeDefinedParameter" -ArgumentList ("Distribution", [string], $Attributes) + $Dict = New-Object "System.Management.Automation.RuntimeDefinedParameterDictionary" + $Dict.Add("Distribution", $Parameter) > $null + return $Dict + } + } + + End { + if ($PSBoundParameters.ContainsKey('Distribution')) { + $DebDistro = $PSBoundParameters['Distribution'] + } + + # These should match those in the Dockerfiles, but exclude tools like Git, which, and curl + $Dependencies = @() + if ($Environment.IsUbuntu -or $Environment.IsDebian) { + $Dependencies = @( + "libc6", + "libcurl3", + "libgcc1", + "libgssapi-krb5-2", + "liblttng-ust0", + "libstdc++6", + "libunwind8", + "libuuid1", + "zlib1g" + ) + + switch ($DebDistro) { + "ubuntu.14.04" { $Dependencies += @("libssl1.0.0", "libicu52") } + "ubuntu.16.04" { $Dependencies += @("libssl1.0.0", "libicu55") } + "ubuntu.17.04" { $Dependencies += @("libssl1.0.0", "libicu57") } + "debian.8" { $Dependencies += @("libssl1.0.0", "libicu52") } + "debian.9" { $Dependencies += @("libssl1.0.2", "libicu57") } + default { throw "Debian distro '$DebDistro' is not supported." } + } + } elseif ($Environment.IsRedHatFamily) { + $Dependencies = @( + "libunwind", + "libcurl", + "openssl-libs", + "libicu" + ) + } + + return $Dependencies + } +} + +function Test-Dependecies +{ + foreach ($Dependency in "fpm", "ronn") { + if (!(precheck $Dependency "Package dependency '$Dependency' not found. Run Start-PSBootstrap -Package")) { + # These tools are not added to the path automatically on OpenSUSE 13.2 + # try adding them to the path and re-tesing first + [string] $gemsPath = $null + [string] $depenencyPath = $null + $gemsPath = Get-ChildItem -Path /usr/lib64/ruby/gems | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName + if($gemsPath) { + $depenencyPath = Get-ChildItem -Path (Join-Path -Path $gemsPath -ChildPath "gems" -AdditionalChildPath $Dependency) -Recurse | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty DirectoryName + $originalPath = $env:PATH + $env:PATH = $ENV:PATH +":" + $depenencyPath + if((precheck $Dependency "Package dependency '$Dependency' not found. Run Start-PSBootstrap -Package")) { + continue + } + else { + $env:PATH = $originalPath + } + } + + throw "Dependency precheck failed!" + } + } +} + +function New-AfterScripts +{ + if ($Environment.IsRedHatFamily) { + # add two symbolic links to system shared libraries that libmi.so is dependent on to handle + # platform specific changes. This is the only set of platforms needed for this currently + # as Ubuntu has these specific library files in the platform and macOS builds for itself + # against the correct versions. + New-Item -Force -ItemType SymbolicLink -Target "/lib64/libssl.so.10" -Path "$Staging/libssl.so.1.0.0" >$null + New-Item -Force -ItemType SymbolicLink -Target "/lib64/libcrypto.so.10" -Path "$Staging/libcrypto.so.1.0.0" >$null + + $AfterInstallScript = [io.path]::GetTempFileName() + $AfterRemoveScript = [io.path]::GetTempFileName() + $packagingStrings.RedHatAfterInstallScript -f "$Link/pwsh" | Out-File -FilePath $AfterInstallScript -Encoding ascii + $packagingStrings.RedHatAfterRemoveScript -f "$Link/pwsh" | Out-File -FilePath $AfterRemoveScript -Encoding ascii + } + elseif ($Environment.IsUbuntu -or $Environment.IsDebian) { + $AfterInstallScript = [io.path]::GetTempFileName() + $AfterRemoveScript = [io.path]::GetTempFileName() + $packagingStrings.UbuntuAfterInstallScript -f "$Link/pwsh" | Out-File -FilePath $AfterInstallScript -Encoding ascii + $packagingStrings.UbuntuAfterRemoveScript -f "$Link/pwsh" | Out-File -FilePath $AfterRemoveScript -Encoding ascii + } + + return [PSCustomObject] @{ + AfterInstallScript = $AfterInstallScript + AfterRemoveScript = $AfterRemoveScript + } +} + +function New-ManGzip +{ + # run ronn to convert man page to roff + $RonnFile = Join-Path $PSScriptRoot "/../../assets/pwsh.1.ronn" + $RoffFile = $RonnFile -replace "\.ronn$" + + # Run ronn on assets file + # Run does not play well with files named powershell6.0.1, so we generate and then rename + Start-NativeExecution { ronn --roff $RonnFile } + + # gzip in assets directory + $GzipFile = "$RoffFile.gz" + Start-NativeExecution { gzip -f $RoffFile } + + $ManFile = Join-Path "/usr/local/share/man/man1" (Split-Path -Leaf $GzipFile) + + return [PSCustomObject ] @{ + GZipFile = $GzipFile + ManFile = $ManFile + } +} +function New-MacOSLauncher +{ + param( + [Parameter(Mandatory)] + [String]$Version + ) + + # Define folder for launch application. + $macosapp = "$PSScriptRoot/macos/launcher/ROOT/Applications/Powershell.app" + + # Update icns file. + $iconfile = "$PSScriptRoot/../../assets/Powershell.icns" + $iconfilebase = (Get-Item -Path $iconfile).BaseName + + # Create Resources folder, ignore error if exists. + New-Item -Force -ItemType Directory -Path "$macosapp/Contents/Resources" | Out-Null + Copy-Item -Force -Path $iconfile -Destination "$macosapp/Contents/Resources" + + # Set values in plist. + $plist = "$macosapp/Contents/Info.plist" + Start-NativeExecution { + defaults write $plist CFBundleIdentifier com.microsoft.powershell + defaults write $plist CFBundleVersion $Version + defaults write $plist CFBundleShortVersionString $Version + defaults write $plist CFBundleGetInfoString $Version + defaults write $plist CFBundleIconFile $iconfilebase + } + + # Convert to XML plist, needed because defaults native + # app auto converts it to binary format when it modify + # the plist file. + Start-NativeExecution { + plutil -convert xml1 $plist + } + + # Set permissions for plist and shell script. Note that + # defaults native app sets 700 when writing to the plist + # file from above. Both of these will be reset post fpm. + $shellscript = "$macosapp/Contents/MacOS/PowerShell.sh" + Start-NativeExecution { + chmod 644 $plist + chmod 755 $shellscript + } + + # Add app folder to fpm paths. + $appsfolder = (Resolve-Path -Path "$macosapp/..").Path + + return $appsfolder +} + +function Clear-MacOSLauncher +{ + # This is needed to prevent installer from picking up + # the launcher app in the build structure and updating + # it which locks out subsequent package builds due to + # increase permissions. + $macosapp = "$PSScriptRoot/macos/launcher/ROOT/Applications/Powershell.app" + $plist = "$macosapp/Contents/Info.plist" + $tempguid = (New-Guid).Guid + Start-NativeExecution { + defaults write $plist CFBundleIdentifier $tempguid + plutil -convert xml1 $plist + } + + # Restore default permissions. + $shellscript = "$macosapp/Contents/MacOS/PowerShell.sh" + Start-NativeExecution { + chmod 644 $shellscript + chmod 644 $plist + } +} + function New-StagingFolder { param( From a4a6ec979d2fba506ac6bf8a4572e69820d6254c Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Mon, 4 Dec 2017 12:14:00 -0800 Subject: [PATCH 2/9] [Package] Added instrumentation --- tools/packaging/packaging.psm1 | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/packaging/packaging.psm1 b/tools/packaging/packaging.psm1 index 6e6885a75b2..36ee7990717 100644 --- a/tools/packaging/packaging.psm1 +++ b/tools/packaging/packaging.psm1 @@ -732,6 +732,7 @@ function New-UnixPackage { if (Test-Path $createdPackage) { + Write-Verbose "Created package: $createdPackage" -Verbose return $createdPackage } else From 9795528e1970f8e7b7313637ec65a8605b466028 Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Mon, 4 Dec 2017 15:00:40 -0800 Subject: [PATCH 3/9] [Package] update change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e03903f904..5220b931762 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Remove Pester as a module include with the PowerShell Packages. In the future, you should be able to add it by running `Install-Module Pester`. (#5623, #5631) - Make Travis CI use `libcurl+openssl+gssapi` (#5629, @markekraus) +- Refactor `New-UnixPackaging` into funtions to make the large function more readable. (#5625) ## v6.0.0-rc - 2017-11-16 From ac522e412f930c080675860adbdd3bcaee71a5c2 Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Mon, 4 Dec 2017 16:48:35 -0800 Subject: [PATCH 4/9] [Package] Fix distribution parameter in get-dependecies --- tools/packaging/packaging.psm1 | 48 ++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/tools/packaging/packaging.psm1 b/tools/packaging/packaging.psm1 index 36ee7990717..d9f5e4988a4 100644 --- a/tools/packaging/packaging.psm1 +++ b/tools/packaging/packaging.psm1 @@ -829,32 +829,34 @@ function Get-FpmArguments return $Arguments } -function Get-PackageDependencies + +function Test-Distribution { - param() - DynamicParam { - if ($Environment.IsUbuntu -or $Environment.IsDebian) { - # Add a dynamic parameter '-Distribution' when the environment is Ubuntu or Debian - # The '-Distribution' parameter can be used to indicate which Debian distro this pacakge is targeting. - $ParameterAttr = New-Object "System.Management.Automation.ParameterAttribute" - $ParameterAttr.Mandatory = $true - $ValidateSetAttr = New-Object "System.Management.Automation.ValidateSetAttribute" -ArgumentList $Script:DebianDistributions - $Attributes = New-Object "System.Collections.ObjectModel.Collection``1[System.Attribute]" - $Attributes.Add($ParameterAttr) > $null - $Attributes.Add($ValidateSetAttr) > $null + param( + [String] + $Distribution + ) - $Parameter = New-Object "System.Management.Automation.RuntimeDefinedParameter" -ArgumentList ("Distribution", [string], $Attributes) - $Dict = New-Object "System.Management.Automation.RuntimeDefinedParameterDictionary" - $Dict.Add("Distribution", $Parameter) > $null - return $Dict - } + if ( ($Environment.IsUbuntu -or $Environment.IsDebian) -and !$Distribution ) + { + throw "$Distribution is require for a Debian based distribution." } - End { - if ($PSBoundParameters.ContainsKey('Distribution')) { - $DebDistro = $PSBoundParameters['Distribution'] - } + if($Script:DebianDistributions -notcontains $Distribution) + { + throw "$Distribution should be one of the following: $Script:DebianDistributions" + } + return $true +} +function Get-PackageDependencies +{ + param( + [String] + [ValidateScript({Test-Distribution -Distribution $_})] + $Distribution + ) + End { # These should match those in the Dockerfiles, but exclude tools like Git, which, and curl $Dependencies = @() if ($Environment.IsUbuntu -or $Environment.IsDebian) { @@ -870,13 +872,13 @@ function Get-PackageDependencies "zlib1g" ) - switch ($DebDistro) { + switch ($Distribution) { "ubuntu.14.04" { $Dependencies += @("libssl1.0.0", "libicu52") } "ubuntu.16.04" { $Dependencies += @("libssl1.0.0", "libicu55") } "ubuntu.17.04" { $Dependencies += @("libssl1.0.0", "libicu57") } "debian.8" { $Dependencies += @("libssl1.0.0", "libicu52") } "debian.9" { $Dependencies += @("libssl1.0.2", "libicu57") } - default { throw "Debian distro '$DebDistro' is not supported." } + default { throw "Debian distro '$Distribution' is not supported." } } } elseif ($Environment.IsRedHatFamily) { $Dependencies = @( From ccf103ac75f8b932a6136fc807a9aa56c5947fb2 Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Mon, 4 Dec 2017 17:31:28 -0800 Subject: [PATCH 5/9] [Package] fix dependencies --- tools/packaging/packaging.psm1 | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tools/packaging/packaging.psm1 b/tools/packaging/packaging.psm1 index d9f5e4988a4..9b027e4d223 100644 --- a/tools/packaging/packaging.psm1 +++ b/tools/packaging/packaging.psm1 @@ -677,7 +677,7 @@ function New-UnixPackage { -Iteration $Iteration ` -Description $Description ` -Type $Type ` - -Dependencies $Dependecies ` + -Dependencies $Dependencies ` -AfterInstallScript $AfterScriptInfo.AfterInstallScript ` -AfterRemoveScript $AfterScriptInfo.AfterRemoveScript ` -Staging $Staging ` @@ -780,14 +780,34 @@ function Get-FpmArguments [String]$LinkDestination, [Parameter(HelpMessage='Packages required to install this package. Not applicable for MacOS.')] - [String[]]$Dependencies, + [ValidateScript({ + if (!$Environment.IsMacOS -and $_.Count -eq 0) + { + throw "Must not be null or empty on this environment." + } + return $true + })] [String[]]$Dependencies, [Parameter(HelpMessage='Script to run after the package installation.')] [AllowNull()] + [ValidateScript({ + if (!$Environment.IsMacOS -and !$_) + { + throw "Must not be null on this environment." + } + return $true + })] [String]$AfterInstallScript, [Parameter(HelpMessage='Script to run after the package removal.')] [AllowNull()] + [ValidateScript({ + if (!$Environment.IsMacOS -and !$_) + { + throw "Must not be null on this environment." + } + return $true + })] [String]$AfterRemoveScript ) From dc120ddca06ff9799fc20d3ecf17069c0adaf30a Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Wed, 6 Dec 2017 15:29:20 -0800 Subject: [PATCH 6/9] Address PR comments --- tools/packaging/packaging.psm1 | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/tools/packaging/packaging.psm1 b/tools/packaging/packaging.psm1 index 9b027e4d223..abf8f8d4d22 100644 --- a/tools/packaging/packaging.psm1 +++ b/tools/packaging/packaging.psm1 @@ -587,7 +587,7 @@ function New-UnixPackage { } # Verify depenecies are installed and in the path - Test-Dependecies + Test-Dependencies $Description = $packagingStrings.Description @@ -639,7 +639,7 @@ function New-UnixPackage { } } - # Generate gzip of mann file + # Generate gzip of man file $ManGzipInfo = New-ManGzip # Change permissions for packaging @@ -682,8 +682,8 @@ function New-UnixPackage { -AfterRemoveScript $AfterScriptInfo.AfterRemoveScript ` -Staging $Staging ` -Destination $Destination ` - -MannGzipFile $ManGzipInfo.GzipFile ` - -MannDestination $ManGzipInfo.ManFile ` + -ManGzipFile $ManGzipInfo.GzipFile ` + -ManDestination $ManGzipInfo.ManFile ` -LinkSource $LinkSource ` -LinkDestination $Link @@ -758,6 +758,8 @@ function Get-FpmArguments [Parameter(Mandatory,HelpMessage='Package description')] [String]$Description, + # From start-PSPackage without modification, already validated + # Values: deb, rpm, osxpkg [Parameter(Mandatory,HelpMessage='Installer Type')] [String]$Type, @@ -767,11 +769,11 @@ function Get-FpmArguments [Parameter(Mandatory,HelpMessage='Install path on target machine')] [String]$Destination, - [Parameter(Mandatory,HelpMessage='The built and gzipped mann file.')] - [String]$MannGzipFile, + [Parameter(Mandatory,HelpMessage='The built and gzipped man file.')] + [String]$ManGzipFile, - [Parameter(Mandatory,HelpMessage='The destination of the mann file')] - [String]$MannDestination, + [Parameter(Mandatory,HelpMessage='The destination of the man file')] + [String]$ManDestination, [Parameter(Mandatory,HelpMessage='Symlink to powershell executable')] [String]$LinkSource, @@ -829,18 +831,23 @@ function Get-FpmArguments $Arguments += @("--rpm-dist", "rhel.7") $Arguments += @("--rpm-os", "linux") } + if ($Environment.IsMacOS) { $Arguments += @("--osxpkg-identifier-prefix", "com.microsoft") } + foreach ($Dependency in $Dependencies) { $Arguments += @("--depends", $Dependency) } + if ($AfterInstallScript) { $Arguments += @("--after-install", $AfterInstallScript) } + if ($AfterRemoveScript) { $Arguments += @("--after-remove", $AfterRemoveScript) } + $Arguments += @( "$Staging/=$Destination/", "$MannGzipFile=$MannDestination", @@ -859,7 +866,7 @@ function Test-Distribution if ( ($Environment.IsUbuntu -or $Environment.IsDebian) -and !$Distribution ) { - throw "$Distribution is require for a Debian based distribution." + throw "$Distribution is required for a Debian based distribution." } if($Script:DebianDistributions -notcontains $Distribution) @@ -913,7 +920,7 @@ function Get-PackageDependencies } } -function Test-Dependecies +function Test-Dependencies { foreach ($Dependency in "fpm", "ronn") { if (!(precheck $Dependency "Package dependency '$Dependency' not found. Run Start-PSBootstrap -Package")) { From c709e2ce4c6d60f945cc8576b4a9be49d5cbc227 Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Wed, 6 Dec 2017 14:57:17 -0800 Subject: [PATCH 7/9] fix spelling issue --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5220b931762..9343e5bbf31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ - Remove Pester as a module include with the PowerShell Packages. In the future, you should be able to add it by running `Install-Module Pester`. (#5623, #5631) - Make Travis CI use `libcurl+openssl+gssapi` (#5629, @markekraus) -- Refactor `New-UnixPackaging` into funtions to make the large function more readable. (#5625) +- Refactor `New-UnixPackaging` into functions to make the large function more readable. (#5625) ## v6.0.0-rc - 2017-11-16 From c8492bee82b229ed559ec60690a2f8dce19e1fa3 Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Wed, 6 Dec 2017 16:40:56 -0800 Subject: [PATCH 8/9] [package] Address PR comments --- tools/packaging/packaging.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/packaging/packaging.psm1 b/tools/packaging/packaging.psm1 index abf8f8d4d22..49f428e6fe3 100644 --- a/tools/packaging/packaging.psm1 +++ b/tools/packaging/packaging.psm1 @@ -850,7 +850,7 @@ function Get-FpmArguments $Arguments += @( "$Staging/=$Destination/", - "$MannGzipFile=$MannDestination", + "$ManGzipFile=$ManDestination", "$LinkSource=$LinkDestination" ) From 2a5635cff229d2823e90ae6075064af474e7daed Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Wed, 6 Dec 2017 18:01:32 -0800 Subject: [PATCH 9/9] [Package] fix issues with validate script --- tools/packaging/packaging.psm1 | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tools/packaging/packaging.psm1 b/tools/packaging/packaging.psm1 index 49f428e6fe3..e8bbdedc325 100644 --- a/tools/packaging/packaging.psm1 +++ b/tools/packaging/packaging.psm1 @@ -669,7 +669,7 @@ function New-UnixPackage { } # Setup package dependencies - $Dependencies = Get-PackageDependencies @packageDependenciesParams + $Dependencies = @(Get-PackageDependencies @packageDependenciesParams) $Arguments = Get-FpmArguments ` -Name $Name ` @@ -718,7 +718,7 @@ function New-UnixPackage { $createdPackage = Get-Item (Join-Path $PWD (($Output[-1] -split ":path=>")[-1] -replace '["{}]')) if ($Environment.IsMacOS) { - if($pscmdlet.ShouldProcess("Fix package name")) + if ($pscmdlet.ShouldProcess("Fix package name")) { # Add the OS information to the macOS package file name. $packageExt = [System.IO.Path]::GetExtension($createdPackage.Name) @@ -726,7 +726,14 @@ function New-UnixPackage { $newPackageName = "{0}-{1}{2}" -f $packageNameWithoutExt, $script:Options.Runtime, $packageExt $newPackagePath = Join-Path $createdPackage.DirectoryName $newPackageName - $createdPackage = Rename-Item $createdPackage.FullName $newPackagePath -PassThru -Force:$Force + + # -Force is not deleting the NewName if it exists, so delete it if it does + if ($Force -and (Test-Path -Path $newPackagePath)) + { + Remove-Item -Force $newPackagePath + } + + $createdPackage = Rename-Item -Path $createdPackage.FullName -NewName $newPackagePath -PassThru -ErrorAction Stop } } @@ -788,7 +795,8 @@ function Get-FpmArguments throw "Must not be null or empty on this environment." } return $true - })] [String[]]$Dependencies, + })] + [String[]]$Dependencies, [Parameter(HelpMessage='Script to run after the package installation.')] [AllowNull()]