Update module version to 1.0.1 and add new functions for exporting and deploying DSC configuration data
This commit is contained in:
29
Private/ConvertTo-ConfigurationDataRelativePath.ps1
Normal file
29
Private/ConvertTo-ConfigurationDataRelativePath.ps1
Normal file
@@ -0,0 +1,29 @@
|
||||
function ConvertTo-ConfigurationDataRelativePath {
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]
|
||||
$Path,
|
||||
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]
|
||||
$BasePath
|
||||
)
|
||||
|
||||
$ResolvedPath = Repair-ConfigurationDataPathEncoding -Path $Path
|
||||
$ResolvedBasePath = Repair-ConfigurationDataPathEncoding -Path $BasePath
|
||||
|
||||
try {
|
||||
$PathUri = [System.Uri]::new((Resolve-Path -LiteralPath $ResolvedPath).ProviderPath)
|
||||
$BaseUriPath = [System.IO.Path]::GetFullPath($ResolvedBasePath)
|
||||
if(-not $BaseUriPath.EndsWith([System.IO.Path]::DirectorySeparatorChar)){
|
||||
$BaseUriPath += [System.IO.Path]::DirectorySeparatorChar
|
||||
}
|
||||
|
||||
$BaseUri = [System.Uri]::new($BaseUriPath)
|
||||
return [System.Uri]::UnescapeDataString($BaseUri.MakeRelativeUri($PathUri).ToString()).Replace('/', [System.IO.Path]::DirectorySeparatorChar)
|
||||
}
|
||||
catch {
|
||||
return $ResolvedPath
|
||||
}
|
||||
}
|
||||
64
Private/ConvertTo-PowerShellDataFileText.ps1
Normal file
64
Private/ConvertTo-PowerShellDataFileText.ps1
Normal file
@@ -0,0 +1,64 @@
|
||||
function ConvertTo-PowerShellDataFileText {
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[AllowNull()]
|
||||
$InputObject,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[int]
|
||||
$Indent = 0
|
||||
)
|
||||
|
||||
$IndentText = " " * $Indent
|
||||
$ChildIndent = $Indent + 4
|
||||
$ChildIndentText = " " * $ChildIndent
|
||||
|
||||
if($null -eq $InputObject){
|
||||
return '$null'
|
||||
}
|
||||
|
||||
if($InputObject -is [bool]){
|
||||
if($InputObject){
|
||||
return '$true'
|
||||
}
|
||||
|
||||
return '$false'
|
||||
}
|
||||
|
||||
if($InputObject -is [int] -or $InputObject -is [long] -or $InputObject -is [decimal] -or $InputObject -is [double]){
|
||||
return ([string]$InputObject)
|
||||
}
|
||||
|
||||
if($InputObject -is [string]){
|
||||
return "'$($InputObject.Replace("'", "''"))'"
|
||||
}
|
||||
|
||||
if($InputObject -is [System.Collections.IDictionary]){
|
||||
$Lines = @("@{")
|
||||
foreach($Key in $InputObject.Keys){
|
||||
$KeyText = "'$(([string]$Key).Replace("'", "''"))'"
|
||||
$ValueText = ConvertTo-PowerShellDataFileText -InputObject $InputObject[$Key] -Indent $ChildIndent
|
||||
$Lines += "$ChildIndentText$KeyText = $ValueText"
|
||||
}
|
||||
$Lines += "$IndentText}"
|
||||
return ($Lines -join [Environment]::NewLine)
|
||||
}
|
||||
|
||||
if($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]){
|
||||
$Items = @($InputObject)
|
||||
if($Items.Count -eq 0){
|
||||
return '@()'
|
||||
}
|
||||
|
||||
$Lines = @("@(")
|
||||
foreach($Item in $Items){
|
||||
$ValueText = ConvertTo-PowerShellDataFileText -InputObject $Item -Indent $ChildIndent
|
||||
$Lines += "$ChildIndentText$ValueText"
|
||||
}
|
||||
$Lines += "$IndentText)"
|
||||
return ($Lines -join [Environment]::NewLine)
|
||||
}
|
||||
|
||||
return "'$(([string]$InputObject).Replace("'", "''"))'"
|
||||
}
|
||||
21
Private/Repair-ConfigurationDataPathEncoding.ps1
Normal file
21
Private/Repair-ConfigurationDataPathEncoding.ps1
Normal file
@@ -0,0 +1,21 @@
|
||||
function Repair-ConfigurationDataPathEncoding {
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[AllowEmptyString()]
|
||||
[string]
|
||||
$Path
|
||||
)
|
||||
|
||||
if($Path -notmatch "[ÃÂ]"){
|
||||
return $Path
|
||||
}
|
||||
|
||||
try {
|
||||
$Bytes = [System.Text.Encoding]::GetEncoding(1252).GetBytes($Path)
|
||||
return [System.Text.Encoding]::UTF8.GetString($Bytes)
|
||||
}
|
||||
catch {
|
||||
return $Path
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,31 @@ function Resolve-ConfigurationDataTemplatePath {
|
||||
$BasePath = (Get-Location).Path
|
||||
)
|
||||
|
||||
$CandidatePath = $Path
|
||||
if(-not [System.IO.Path]::IsPathRooted($CandidatePath)){
|
||||
$CandidatePath = Join-Path -Path $BasePath -ChildPath $CandidatePath
|
||||
$CandidatePaths = @()
|
||||
foreach($CurrentPath in @($Path, (Repair-ConfigurationDataPathEncoding -Path $Path))){
|
||||
if([string]::IsNullOrWhiteSpace($CurrentPath)){
|
||||
continue
|
||||
}
|
||||
|
||||
$CandidatePath = $CurrentPath
|
||||
$CandidateBasePath = Repair-ConfigurationDataPathEncoding -Path $BasePath
|
||||
if(-not [System.IO.Path]::IsPathRooted($CandidatePath)){
|
||||
$CandidatePath = Join-Path -Path $CandidateBasePath -ChildPath $CandidatePath
|
||||
}
|
||||
|
||||
if($CandidatePaths -notcontains $CandidatePath){
|
||||
$CandidatePaths += $CandidatePath
|
||||
}
|
||||
}
|
||||
|
||||
$ResolvedPath = $null
|
||||
foreach($CandidatePath in $CandidatePaths){
|
||||
$ResolvedPath = Resolve-Path -LiteralPath $CandidatePath -ErrorAction SilentlyContinue
|
||||
if($null -ne $ResolvedPath){
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
$ResolvedPath = Resolve-Path -Path $CandidatePath -ErrorAction SilentlyContinue
|
||||
if($null -eq $ResolvedPath){
|
||||
throw "Configuration data template [$Path] was not found. Base path [$BasePath]."
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user