From 2f1db362852a337c2cecabc782e0034fa2a1cfa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Moreau?= Date: Wed, 24 Mar 2021 18:32:10 -0400 Subject: [PATCH] Zip PowerShell module + remove extracted files from GitHub releases (#148) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * devolutions-gateway: zip PowerShell module and remove extracted artifacts from GitHub releases * devolutions-gateway: improve PowerShell module integration into build pipeline * devolutions-gateway: fix PowerShell module detection in Windows PowerShell * devolutions-gateway: add PowerShell module nupkg building Co-authored-by: Marc-André Moreau --- ci/azure-pipelines.yaml | 1 + ci/tlk.ps1 | 88 +++++++++++++++++-- .../DevolutionsGateway.psd1 | 2 +- 3 files changed, 83 insertions(+), 8 deletions(-) diff --git a/ci/azure-pipelines.yaml b/ci/azure-pipelines.yaml index 5a4399252..ebf0cf51c 100644 --- a/ci/azure-pipelines.yaml +++ b/ci/azure-pipelines.yaml @@ -409,6 +409,7 @@ stages: DGATEWAY_EXECUTABLE: "$(DGatewayExecutable)" DGATEWAY_PACKAGE: "$(DGatewayPackage)" DGATEWAY_PSMODULE_PATH: "$(DGatewayPSModulePath)" + DGATEWAY_PSMODULE_CLEAN: "1" SIGNTOOL_NAME: "$(SignToolName)" displayName: Packaging Devolutions Gateway diff --git a/ci/tlk.ps1 b/ci/tlk.ps1 index 3d539bab3..5c467ace6 100644 --- a/ci/tlk.ps1 +++ b/ci/tlk.ps1 @@ -54,6 +54,64 @@ function Merge-Tokens $OutputValue } +function New-ModulePackage +{ + [CmdletBinding()] + param( + [Parameter(Mandatory=$true,Position=0)] + [string] $InputPath, + [Parameter(Mandatory=$true,Position=1)] + [string] $OutputPath, + [string] $TempPath + ) + + $UniqueId = New-Guid + + if ([string]::IsNullOrEmpty($TempPath)) { + $TempPath = [System.IO.Path]::GetTempPath() + } + + $PSRepoName = "psrepo-$UniqueId" + $PSRepoPath = Join-Path $TempPath $UniqueId + + if (-Not (Test-Path -Path $InputPath -PathType 'Container')) { + throw "`"$InputPath`" does not exist" + } + + $PSModulePath = $InputPath + $PSManifestFile = $(@(Get-ChildItem -Path $PSModulePath -Depth 1 -Filter "*.psd1")[0]).FullName + $PSManifest = Import-PowerShellDataFile -Path $PSManifestFile + $PSModuleName = $(Get-Item $PSManifestFile).BaseName + $PSModuleVersion = $PSManifest.ModuleVersion + + New-Item -Path $PSRepoPath -ItemType Directory -ErrorAction SilentlyContinue | Out-Null + + $Params = @{ + Name = $PSRepoName; + SourceLocation = $PSRepoPath; + PublishLocation = $PSRepoPath; + InstallationPolicy = "Trusted"; + } + + Register-PSRepository @Params | Out-Null + + $OutputFileName = "${PSModuleName}.${PSModuleVersion}.nupkg" + $PSModulePackage = Join-Path $PSRepoPath $OutputFileName + Remove-Item -Path $PSModulePackage -ErrorAction 'SilentlyContinue' + Publish-Module -Path $PSModulePath -Repository $PSRepoName + + Unregister-PSRepository -Name $PSRepoName | Out-Null + + New-Item -Path $OutputPath -ItemType Directory -ErrorAction SilentlyContinue | Out-Null + $OutputFile = Join-Path $OutputPath $OutputFileName + Copy-Item $PSModulePackage $OutputFile + + Remove-Item $PSmodulePackage + Remove-Item -Path $PSRepoPath + + $OutputFile +} + function Get-TlkPlatform { param( [Parameter(Position=0)] @@ -281,11 +339,9 @@ class TlkRecipe } [void] Package_Windows() { + $PackageVersion = $this.Version $ShortVersion = $this.Version.Substring(2) # msi version $TargetArch = $this.Target.WindowsArchitecture() - - $ModuleName = "DevolutionsGateway" - $ModuleVersion = "2021.1.1" # both versions should match Push-Location Set-Location "$($this.SourcePath)/package/$($this.Target.Platform)" @@ -295,15 +351,28 @@ class TlkRecipe } else { throw ("Specify DGATEWAY_EXECUTABLE environment variable") } - + if (Test-Path Env:DGATEWAY_PSMODULE_PATH) { $DGatewayPSModulePath = $Env:DGATEWAY_PSMODULE_PATH } else { - Save-Module -Name $ModuleName -Force -RequiredVersion $ModuleVersion -Repository 'PSGallery' -Path '.' - Remove-Item -Path "${ModuleName}/${ModuleVersion}/PSGetModuleInfo.xml" -ErrorAction 'SilentlyContinue' - $DGatewayPSModulePath = "${ModuleName}/${ModuleVersion}" + throw ("Specify DGATEWAY_PSMODULE_PATH environment variable") } + $PSManifestFile = $(@(Get-ChildItem -Path $DGatewayPSModulePath -Depth 1 -Filter "*.psd1")[0]).FullName + $PSManifest = Import-PowerShellDataFile -Path $PSManifestFile + $PSModuleName = $(Get-Item $PSManifestFile).BaseName + $PSModuleVersion = $PSManifest.ModuleVersion + + if ($PackageVersion -ne $PSModuleVersion) { + Write-Warning "PowerShell module version mismatch: $PSModuleVersion (expected: $PackageVersion)" + } + + $PSModuleParentPath = Split-Path $DGatewayPSModulePath -Parent + $PSModuleZipFilePath = Join-Path $PSModuleParentPath "$PSModuleName-ps-$PSModuleVersion.zip" + Compress-Archive -Path $DGatewayPSModulePath -Destination $PSModuleZipFilePath + + New-ModulePackage $DGatewayPSModulePath $PSModuleParentPath + $WixExtensions = @('WixUtilExtension', 'WixUIExtension', 'WixFirewallExtension') $WixExtensions += $(Join-Path $(Get-Location) 'WixUserPrivilegesExtension.dll') @@ -347,6 +416,11 @@ class TlkRecipe & 'cscript.exe' "/nologo" "WiLangId.vbs" "$($this.PackageName).msi" "Package" "1033,1036" } + if (Test-Path Env:DGATEWAY_PSMODULE_CLEAN) { + # clean up the extracted PowerShell module directory + Remove-Item -Path $DGatewayPSModulePath -Recurse + } + if (Test-Path Env:DGATEWAY_PACKAGE) { $DGatewayPackage = $Env:DGATEWAY_PACKAGE Copy-Item -Path "$($this.PackageName).msi" -Destination $DGatewayPackage diff --git a/powershell/DevolutionsGateway/DevolutionsGateway.psd1 b/powershell/DevolutionsGateway/DevolutionsGateway.psd1 index e43a1d69d..8df282c6e 100644 --- a/powershell/DevolutionsGateway/DevolutionsGateway.psd1 +++ b/powershell/DevolutionsGateway/DevolutionsGateway.psd1 @@ -7,7 +7,7 @@ RootModule = 'DevolutionsGateway.psm1' # Version number of this module. - ModuleVersion = '2020.3.1' + ModuleVersion = '2021.1.1' # Supported PSEditions CompatiblePSEditions = 'Desktop', 'Core'