-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from EvotecIT/PlaceHolders
Add custom and builtin place holders replacement functionality
- Loading branch information
Showing
9 changed files
with
156 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
function Repair-CustomPlaceHolders { | ||
[CmdletBinding()] | ||
param( | ||
[Parameter(Mandatory)][string] $Path, | ||
[System.Collections.IDictionary] $Configuration | ||
) | ||
|
||
$ModuleName = $Configuration.Information.ModuleName | ||
$ModuleVersion = $Configuration.Information.Manifest.ModuleVersion | ||
$TagName = "v$($ModuleVersion)" | ||
if ($Configuration.CurrentSettings.PreRelease) { | ||
$ModuleVersionWithPreRelease = "$($ModuleVersion)-$($Configuration.CurrentSettings.PreRelease)" | ||
$TagModuleVersionWithPreRelease = "v$($ModuleVersionWithPreRelease)" | ||
} else { | ||
$ModuleVersionWithPreRelease = $ModuleVersion | ||
$TagModuleVersionWithPreRelease = "v$($ModuleVersion)" | ||
} | ||
|
||
$BuiltinPlaceHolders = @( | ||
@{ Find = '{ModuleName}'; Replace = $ModuleName } | ||
@{ Find = '<ModuleName>'; Replace = $ModuleName } | ||
@{ Find = '{ModuleVersion}'; Replace = $ModuleVersion } | ||
@{ Find = '<ModuleVersion>'; Replace = $ModuleVersion } | ||
@{ Find = '{ModuleVersionWithPreRelease}'; Replace = $ModuleVersionWithPreRelease } | ||
@{ Find = '<ModuleVersionWithPreRelease>'; Replace = $ModuleVersionWithPreRelease } | ||
@{ Find = '{TagModuleVersionWithPreRelease}'; Replace = $TagModuleVersionWithPreRelease } | ||
@{ Find = '<TagModuleVersionWithPreRelease>'; Replace = $TagModuleVersionWithPreRelease } | ||
@{ Find = '{TagName}'; Replace = $TagName } | ||
@{ Find = '<TagName>'; Replace = $TagName } | ||
) | ||
|
||
Write-TextWithTime -Text "Replacing built-in and custom placeholders" -Color Yellow { | ||
$PSM1Content = Get-Content -LiteralPath $Path -Raw -Encoding UTF8 -ErrorAction Stop | ||
|
||
# Replace built-in placeholders | ||
if ($Configuration.PlaceHolderOption.SkipBuiltinReplacements -ne $true) { | ||
foreach ($PlaceHolder in $BuiltinPlaceHolders) { | ||
$PSM1Content = $PSM1Content.Replace($PlaceHolder.Find, $PlaceHolder.Replace) | ||
} | ||
} | ||
# Replace custom placeholders provided by the user | ||
foreach ($PlaceHolder in $Configuration.PlaceHolders) { | ||
$PSM1Content = $PSM1Content.Replace($PlaceHolder.Find, $PlaceHolder.Replace) | ||
} | ||
|
||
Set-Content -LiteralPath $Path -Value $PSM1Content -Encoding UTF8 -ErrorAction Stop -Force | ||
} -PreAppend Plus -SpacesBefore ' ' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
function New-ConfigurationPlaceHolder { | ||
<# | ||
.SYNOPSIS | ||
Command helping define custom placeholders replacing content within a script or module during the build process. | ||
.DESCRIPTION | ||
Command helping define custom placeholders replacing content within a script or module during the build process. | ||
It modifies only the content of the script or module (PSM1) and does not modify the sources. | ||
.PARAMETER CustomReplacement | ||
Hashtable array with custom placeholders to replace. Each hashtable must contain two keys: Find and Replace. | ||
.PARAMETER Find | ||
The string to find in the script or module content. | ||
.PARAMETER Replace | ||
The string to replace the Find string in the script or module content. | ||
.EXAMPLE | ||
New-ConfigurationPlaceHolder -Find '{CustomName}' -Replace 'SpecialCase' | ||
.EXAMPLE | ||
New-ConfigurationPlaceHolder -CustomReplacement @( | ||
@{ Find = '{CustomName}'; Replace = 'SpecialCase' } | ||
@{ Find = '{CustomVersion}'; Replace = '1.0.0' } | ||
) | ||
.NOTES | ||
General notes | ||
#> | ||
[CmdletBinding(DefaultParameterSetName = 'FindAndReplace')] | ||
param( | ||
[Parameter(Mandatory, ParameterSetName = 'CustomReplacement')][System.Collections.IDictionary[]] $CustomReplacement, | ||
[Parameter(Mandatory, ParameterSetName = 'FindAndReplace')][string] $Find, | ||
[Parameter(Mandatory, ParameterSetName = 'FindAndReplace')][string] $Replace | ||
) | ||
|
||
foreach ($Replacement in $CustomReplacement) { | ||
[ordered] @{ | ||
Type = 'PlaceHolder' | ||
Configuration = $Replacement | ||
} | ||
} | ||
if ($PSBoundParameters.ContainsKey("Find") -and $PSBoundParameters.ContainsKey("Replace")) { | ||
[ordered] @{ | ||
Type = 'PlaceHolder' | ||
Configuration = @{ | ||
Find = $Find | ||
Replace = $Replace | ||
} | ||
} | ||
} | ||
} |