src/Solutions/Invoke-XrmSolutionPackager.ps1
|
<# .SYNOPSIS Run solution packager tool. .DESCRIPTION Pack or unpack given solution file with Solution Packager. The telemetry of Solution Packager is turned off (PP_TOOLS_TELEMETRY_OPTOUT) unless the session already sets PP_TOOLS_TELEMETRY_OPTOUT. The configuration files shipped with Solution Packager do not write telemetry logs in the module folder, which is read-only when the module is installed for all users. A failure of Solution Packager (exit code other than 0) raises an error with its output, so that a build stops there. .PARAMETER SolutionFilePath Full path to solution file (.zip). .PARAMETER SolutionPackagerExeFilePath Full path to solution packager executable. (Default: Assemblies\CoreTools\SolutionPackager.exe in the module) .PARAMETER Action The action to perform. The action can be either to extract a solution .zip file to a folder, or to pack a folder into a .zip file. .PARAMETER PackageType The type of package to process. (Default: Both) This argument may be omitted in most occasions because the package type can be read from inside the .zip file or component files. When extracting and Both is specified, managed and unmanaged solution .zip files must be present and are processed into a single folder. When packing and Both is specified, managed and unmanaged solution .zip files will be produced from one folder. .PARAMETER FolderPath Full path to a folder where solution will be extracted or packed. When extracting, this folder is created and populated with component files. When packing, this folder must already exist and contain previously extracted component files. .PARAMETER ErrorLevel Indicates the level of logging information to output. (Default: Error) .PARAMETER LogFilePath Full path to log file, for Extract and Pack. If the file already exists, new logging information is appended to the file. .OUTPUTS String[]. Console output of SolutionPackager, one line per string. .EXAMPLE $output = Invoke-XrmSolutionPackager -SolutionFilePath "C:\Temp\contoso_core.zip" -Action Extract -FolderPath "C:\Sources\contoso_core"; .LINK https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/Invoke-XrmSolutionPackager.md #> function Invoke-XrmSolutionPackager { [CmdletBinding()] [OutputType([String[]])] param ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $SolutionFilePath, [Parameter(Mandatory = $false)] [ValidateNotNullOrEmpty()] [ValidateScript( { Test-Path $_ })] [String] $SolutionPackagerExeFilePath = [System.IO.Path]::Combine($PSScriptRoot, "..", "Assemblies", "CoreTools", "SolutionPackager.exe"), [Parameter(Mandatory = $true)] [ValidateSet("Extract", "Pack")] [String] $Action, [Parameter(Mandatory = $false)] [ValidateSet("Unmanaged", "Managed", "Both")] [String] $PackageType = "Both", [Parameter(Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $FolderPath, [Parameter(Mandatory = $false)] [ValidateSet("Off", "Error", "Warning", "Info", "Both", "Verbose")] [String] $ErrorLevel = "Error", [Parameter(Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $LogFilePath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), "SolutionPackager.log") ) begin { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); } process { Assert-XrmWindowsInternal -Feature "SolutionPackager.exe (.NET Framework)" -Alternative "Use the Power Platform CLI instead: pac solution unpack / pac solution pack."; # SolutionPackager records its telemetry under %LOCALAPPDATA%\Microsoft\PowerAppsCli\telemetry-cache, then starts pacTelemetryUpload.exe to send it # (always, opted out or not), whose upload the service refused, logged next to it: in the module folder (B43). The opt-out stops the recording; # the trace switches of SolutionPackager.exe.config and pacTelemetryUpload.exe.config are set to Off (2.0.0.79) for the logs. # The variable is set for the child process only, and left as the caller set it. # Through $env: : in PowerShell 7, [Environment]::SetEnvironmentVariable(name, $null) receives "" and leaves an empty variable. $telemetryOptOutSet = Test-Path -Path "Env:PP_TOOLS_TELEMETRY_OPTOUT"; try { if (-not $telemetryOptOutSet) { $env:PP_TOOLS_TELEMETRY_OPTOUT = "true"; } $solutionPackagerOuput = ""; if ($Action -eq "Extract") { $solutionPackagerOuput = & "$SolutionPackagerExeFilePath" /action:$Action /zipfile:"$SolutionFilePath" /folder:"$FolderPath" /clobber /allowWrite:Yes /allowDelete:Yes /errorlevel:$ErrorLevel /log:$LogFilePath /nologo; } elseif ($Action -eq "Pack") { $solutionPackagerOuput = & "$SolutionPackagerExeFilePath" /action:$Action /zipfile:"$SolutionFilePath" /folder:"$FolderPath" /packagetype:$PackageType /errorlevel:$ErrorLevel /log:$LogFilePath /nologo; } $exitCode = $LASTEXITCODE; } finally { if (-not $telemetryOptOutSet) { $env:PP_TOOLS_TELEMETRY_OPTOUT = $null; } } # A failed extract or pack must stop the build that called it (M23) if ($exitCode -ne 0) { $details = (@($solutionPackagerOuput) | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | Select-Object -Last 20) -join [System.Environment]::NewLine; throw "SolutionPackager $Action of '$SolutionFilePath' failed (exit code $exitCode, log '$LogFilePath'):$([System.Environment]::NewLine)$details"; } $solutionPackagerOuput; } end { $StopWatch.Stop(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch; } } Export-ModuleMember -Function Invoke-XrmSolutionPackager -Alias *; |