Skip to content

Commit bd0b096

Browse files
author
Friedrich Weinmann
committed
bugfix & Actions update
1 parent 5ff669a commit bd0b096

15 files changed

+199
-19
lines changed
512 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

PSModuleDevelopment/bin/PSModuleDevelopment.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PSModuleDevelopment/changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## ???
4+
5+
+ New: Action: deployModule - Deploys a module to the target computer(s)
6+
+ Upd: Invoke-PSMDBuildProject - Added parameter resolution from artifacts and configuration
7+
+ Upd: Invoke-PSMDBuildProject - Added automatic artifacts resolution based on value of a parameter: %!Name!% will be resolved to the artifact named "Name".
8+
+ Upd: Invoke-PSMDBuildProject - Added `-InheritArtifacts` parameter to allow keeping artifacts around created before starting the pipeline
9+
+ Fix: Invoke-PSMDTemplate - Invoking templates on PS5.1 fails with invalid replace
10+
311
## 2.2.10.123 (2021-07-21)
412

513
- Fix: Template PSFProject - fixed string test modulename

PSModuleDevelopment/functions/build/Invoke-PSMDBuildProject.ps1

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,27 @@
1818
Mandatory if no build project has been selected as the default project.
1919
Use the Select-PSMDBuildProject to define a default project (and optionally persist the choice across sessions)
2020
21+
.PARAMETER InheritArtifacts
22+
Accept artifacts that were generated before ever executing this pipeline.
23+
By default, any artifacts previously provisioned are cleared on pipeline start.
24+
2125
.PARAMETER RetainArtifacts
2226
Whether, after executing the project, its artifacts should be retained.
2327
By default, any artifacts created during a build project will be discarded upon project completion.
24-
28+
2529
Artifacts are similar to variables to the pipeline and can be used to pass data throughout the pipeline.
2630
2731
+ Use Publish-PSMDBuildArtifact to create a new artifact.
2832
+ Use Get-PSMDBuildArtifact to access existing build artifacts.
2933
3034
.EXAMPLE
3135
PS C:\> Invoke-PSMDBuildProject -Path .\VMDeployment.build.Json
32-
36+
3337
Execute the build file "VMDeployment.build.json" from the current folder
3438
3539
.EXAMPLE
3640
PS C:\> build
37-
41+
3842
Execute the default build project.
3943
#>
4044
[Alias('build')]
@@ -43,12 +47,17 @@
4347
[string]
4448
$Path,
4549

50+
[switch]
51+
$InheritArtifacts,
52+
4653
[switch]
4754
$RetainArtifacts
4855
)
4956

5057
begin {
51-
$script:buildArtifacts = @{ }
58+
if (-not $InheritArtifacts) {
59+
$script:buildArtifacts = @{ }
60+
}
5261
$buildStatus = @{ }
5362

5463
$projectPath = $Path
@@ -158,6 +167,10 @@
158167
}
159168
if (-not $parameters.Parameters) { $parameters.Parameters = @{ } }
160169
if (-not $parameters.ParametersFromArtifacts) { $parameters.ParametersFromArtifacts = @{ } }
170+
171+
# Resolve Parameters
172+
$parameters.Parameters = Resolve-PSMDBuildStepParameter -Parameters $parameters.Parameters -FromArtifacts $parameters.ParametersFromArtifacts -ProjectName $parameters.ProjectName -StepName $parameters.StepName
173+
161174
try { $null = & $actionObject.Action $parameters }
162175
catch {
163176
Write-StepResult @resultDef -Status Failed -Data $_ -ContinueLabel main

PSModuleDevelopment/functions/build/Resolve-PSMDBuildStepParameter.ps1

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
function Resolve-PSMDBuildStepParameter {
22
<#
33
.SYNOPSIS
4-
Update missing build action parameters from the configuration system.
4+
Resolves and consolidates the overall parameters of a given step.
55
66
.DESCRIPTION
7-
Update missing build action parameters from the configuration system.
8-
This command is for use within the defined code of build actions.
7+
Resolves and consolidates the overall parameters of a given step.
8+
This ensures that individual actions do not have to implement manual resolution and complex conditionals.
9+
Sources of parameters:
10+
- Explicitly defined parameter in the step
11+
- Value from Artifacts
12+
- Value from Configuration (only if not otherwise sourced)
13+
- Value from implicit artifact resolution: Any value that is formatted like this:
14+
"%!NameOfArtifact!%" will be replaced with the value of the artifact of the same name.
15+
This supports wildcard resolution, so "%!Session.*!%" will resolve to all artifacts with a name starting with "Session."
916
1017
.PARAMETER Parameters
1118
The hashtable containing the currently specified parameters from the step configuration within the build project file.
@@ -64,6 +71,14 @@
6471
$Parameters[$pair.Key] = (Get-PSMDBuildArtifact -Name $pair.Value).Value
6572
}
6673

74+
# Resolve implicit artifact references
75+
foreach ($pair in $Parameters.GetEnumerator()) {
76+
if ($pair.Value -notlike '%!*!%') { continue }
77+
78+
$artifactName = $pair.Value -replace '^%!(.+?)!%$', '$1'
79+
$Parameters[$pair.Key] = (Get-PSMDBuildArtifact -Name $artifactName).Value
80+
}
81+
6782
$Parameters
6883
}
6984
}

PSModuleDevelopment/functions/templating/Invoke-PSMDTemplate.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,10 @@
320320
$fileName = $Item.Name
321321
if (-not $Raw) {
322322
foreach ($param in $Item.FileSystemParameterFlat) {
323-
$fileName = $fileName.Replace("$($identifier)$($param)$($identifier)", $ParameterFlat[$param], $true, $null)
323+
$fileName = [PSModuleDevelopment.Utility.UtilityHost]::Replace($fileName,"$($identifier)$($param)$($identifier)", $ParameterFlat[$param], $false)
324324
}
325325
foreach ($param in $Item.FileSystemParameterScript) {
326-
$fileName = $fileName.Replace("$($identifier)$($param)$($identifier)", $ParameterScript[$param], $true, $null)
326+
$fileName = [PSModuleDevelopment.Utility.UtilityHost]::Replace($fileName, "$($identifier)$($param)$($identifier)", $ParameterScript[$param], $false)
327327
}
328328
}
329329
$destPath = Join-Path $Path $fileName
@@ -332,10 +332,10 @@
332332
$text = $Item.Value
333333
if (-not $Raw) {
334334
foreach ($param in $Item.ContentParameterFlat) {
335-
$text = $text.Replace("$($identifier)$($param)$($identifier)", $ParameterFlat[$param], $true, $null)
335+
$text = [PSModuleDevelopment.Utility.UtilityHost]::Replace($text, "$($identifier)$($param)$($identifier)", $ParameterFlat[$param], $false)
336336
}
337337
foreach ($param in $Item.ContentParameterScript) {
338-
$text = $text.Replace("$($identifier)!$($param)!$($identifier)", $ParameterScript[$param], $true, $null)
338+
$text = [PSModuleDevelopment.Utility.UtilityHost]::Replace($text, "$($identifier)!$($param)!$($identifier)", $ParameterScript[$param], $false)
339339
}
340340
}
341341
[System.IO.File]::WriteAllText($destPath, $text, $Encoding)

PSModuleDevelopment/internal/buildActions/command.action.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
$rootPath = $Parameters.RootPath
77
$actualParameters = $Parameters.Parameters
8-
$actualParameters = Resolve-PSMDBuildStepParameter -Parameters $actualParameters -FromArtifacts $Parameters.ParametersFromArtifacts -ProjectName $Parameters.ProjectName -StepName $Parameters.StepName
98

109
#region Process Parameters
1110
if (-not $actualParameters.Command) {

PSModuleDevelopment/internal/buildActions/copy-item.action.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
$rootPath = $Parameters.RootPath
77
$actualParameters = $Parameters.Parameters
8-
$actualParameters = Resolve-PSMDBuildStepParameter -Parameters $actualParameters -FromArtifacts $Parameters.ParametersFromArtifacts -ProjectName $Parameters.ProjectName -StepName $Parameters.StepName
98

109
#region Utility Functions
1110
function ConvertTo-PSSession {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
$action = {
2+
param (
3+
$Parameters
4+
)
5+
6+
$rootPath = $Parameters.RootPath
7+
$actualParameters = $Parameters.Parameters
8+
9+
#region Validate Input
10+
if (-not $actualParameters.Session) {
11+
throw "No Sessions specified!"
12+
}
13+
14+
if ($actualParameters.Session | Where-Object State -NE Open) {
15+
throw "Sessions not open!"
16+
}
17+
if ($actualParameters.Repository -and (-not (Get-PSRepository -Name $actualParameters.Repository -ErrorAction Ignore))) {
18+
throw "Repository $($actualParameters.Repository) not found!"
19+
}
20+
21+
foreach ($module in $actualParameters.Module) {
22+
if ($module -notmatch '\\|/') { continue }
23+
24+
try { $null = Resolve-PSFPath -Path $module -Provider FileSystem }
25+
catch { throw "Unable to resolve path: $module"}
26+
}
27+
#endregion Validate Input
28+
29+
#region Prepare modules to transfer
30+
$workingDirectory = Join-Path -Path (Get-PSFPath -Name temp) -ChildPath "psmd_action_$(Get-Random)"
31+
$null = New-Item -Path $workingDirectory -ItemType Directory -Force -ErrorAction Stop
32+
trap {
33+
Remove-Item -Path $workingDirectory -Recurse -Force -ErrorAction SilentlyContinue
34+
throw $_
35+
}
36+
37+
$saveModuleParam = @{
38+
Path = $workingDirectory
39+
Repository = $actualParameters.Repository
40+
}
41+
42+
foreach ($module in $actualParameters.Module) {
43+
if ($module -notmatch '\\|/') {
44+
if ($actualParameters.Repository) {
45+
Save-Module $module @saveModuleParam
46+
continue
47+
}
48+
$moduleObject = Get-Module -Name $module -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1
49+
if (-not $moduleObject) {
50+
throw "Cannot find module $module!"
51+
}
52+
Copy-Item -Path $moduleObject.ModuleBase -Destination "$workingDirectory\$($moduleObject.Name)" -Recurse -Force
53+
continue
54+
}
55+
56+
foreach ($path in Resolve-PSFPath -Path $module -Provider FileSystem) {
57+
if (Test-Path -LiteralPath $path -PathType Leaf) { $path = Split-Path -LiteralPath $path }
58+
Copy-Item -LiteralPath $path -Destination $workingDirectory -Recurse -Force
59+
}
60+
}
61+
#endregion Prepare modules to transfer
62+
63+
foreach ($moduleFolder in Get-ChildItem -Path $workingDirectory) {
64+
if (-not $actualParameters.NoDelete) {
65+
Invoke-Command -Session $actualParameters.Session -ScriptBlock {
66+
param ($Name)
67+
Remove-Item -Path "$env:ProgramFiles\WindowsPowerShell\Modules\$Name" -Recurse -Force
68+
} -ArgumentList $moduleFolder.Name
69+
}
70+
foreach ($session in $actualParameters.Session) {
71+
Copy-Item -LiteralPath $moduleFolder.FullName -Destination "$env:ProgramFiles\WindowsPowerShell\Modules" -Recurse -Force -ToSession $session -ErrorAction Stop
72+
}
73+
}
74+
}
75+
76+
$params = @{
77+
Name = 'deployModule'
78+
Action = $action
79+
Description = 'Deploys a module to the target computer(s)'
80+
Parameters = @{
81+
Session = '(mandatory) The PSRemoting sessions to deploy the module through.'
82+
Module = '(mandatory) A list of names or paths of modules to deploy. Can be used in any combination, specifying by name will use the latest version found on the local computer unless also using he "Repository" parameter to specify an alternate source.'
83+
Repository = 'The repository from which to download the module(s) (and any dependencies). Modules will be sourced locally if empty.'
84+
NoDelete = '[bool] Whether to keep other versions of the target module on the remote machine. By default, all other versions will be deleted.'
85+
}
86+
}
87+
88+
Register-PSMDBuildAction @params

0 commit comments

Comments
 (0)