Skip to content

Commit cb9faba

Browse files
committed
Merge pull request #1 from claudiospizzi/dev
Release 1.0.0
2 parents f3981db + 9655df3 commit cb9faba

22 files changed

Lines changed: 1043 additions & 2 deletions

Examples/IniConfigDemo.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
# Load the default configuration file in INI format
3+
$Config = Get-ScriptConfig -Format INI
4+
5+
# Access the configuration settings
6+
Write-Host "String :" $Config.MyString
7+
Write-Host "Integer Positive :" $Config.MyIntegerPositive
8+
Write-Host "Integer Negative :" $Config.MyIntegerNegative
9+
Write-Host "Boolean True :" $Config.MyBooleanTrue
10+
Write-Host "Boolean False :" $Config.MyBooleanFalse
11+
Write-Host "Array :" "@(" (($Config.MyArray | ForEach-Object { '"{0}"' -f $_ }) -join ', ') ")"
12+
Write-Host "Hashtable :" "@{" (($Config.MyHashtable.GetEnumerator() | ForEach-Object { '{0} = "{1}"' -f $_.Name, $_.Value }) -join '; ') "}"

Examples/IniConfigDemo.ps1.config

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MyString=This is a test INI config file!
2+
MyIntegerPositive=42
3+
MyIntegerNegative=-153
4+
MyBooleanTrue=True
5+
MyBooleanFalse=False
6+
MyArray[]=Lorem
7+
MyArray[]=Ipsum
8+
MyHashtable[Foo]=Bar
9+
MyHashtable[Hello]=World

Examples/JsonConfigDemo.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
# Load the default configuration file in JSON format
3+
$Config = Get-ScriptConfig -Format JSON
4+
5+
# Access the configuration settings
6+
Write-Host "String :" $Config.MyString
7+
Write-Host "Integer Positive :" $Config.MyIntegerPositive
8+
Write-Host "Integer Negative :" $Config.MyIntegerNegative
9+
Write-Host "Boolean True :" $Config.MyBooleanTrue
10+
Write-Host "Boolean False :" $Config.MyBooleanFalse
11+
Write-Host "Array :" "@(" (($Config.MyArray | ForEach-Object { '"{0}"' -f $_ }) -join ', ') ")"
12+
Write-Host "Hashtable :" "@{" (($Config.MyHashtable.GetEnumerator() | ForEach-Object { '{0} = "{1}"' -f $_.Name, $_.Value }) -join '; ') "}"

Examples/JsonConfigDemo.ps1.config

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"MyString": "This is a test JSON config file!",
3+
"MyIntegerPositive": 42,
4+
"MyIntegerNegative": -153,
5+
"MyBooleanTrue": true,
6+
"MyBooleanFalse": false,
7+
"MyArray": [
8+
"Lorem",
9+
"Ipsum"
10+
],
11+
"MyHashtable": {
12+
"Foo": "Bar",
13+
"Hello": "World"
14+
}
15+
}

Examples/XmlConfigDemo.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
# Load the default configuration file in XML format
3+
$Config = Get-ScriptConfig -Format XML
4+
5+
# Access the configuration settings
6+
Write-Host "String :" $Config.MyString
7+
Write-Host "Integer Positive :" $Config.MyIntegerPositive
8+
Write-Host "Integer Negative :" $Config.MyIntegerNegative
9+
Write-Host "Boolean True :" $Config.MyBooleanTrue
10+
Write-Host "Boolean False :" $Config.MyBooleanFalse
11+
Write-Host "Array :" "@(" (($Config.MyArray | ForEach-Object { '"{0}"' -f $_ }) -join ', ') ")"
12+
Write-Host "Hashtable :" "@{" (($Config.MyHashtable.GetEnumerator() | ForEach-Object { '{0} = "{1}"' -f $_.Name, $_.Value }) -join '; ') "}"

Examples/XmlConfigDemo.ps1.config

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Configuration>
3+
<Settings>
4+
<Setting Type="string" Key="MyString" Value="This is a test XML config file!" />
5+
<Setting Type="integer" Key="MyIntegerPositive" Value="42" />
6+
<Setting Type="integer" Key="MyIntegerNegative" Value="-153" />
7+
<Setting Type="boolean" Key="MyBooleanTrue" Value="true" />
8+
<Setting Type="boolean" Key="MyBooleanFalse" Value="false" />
9+
<Setting Type="array" Key="MyArray">
10+
<Item Value="Lorem" />
11+
<Item Value="Ipsum" />
12+
</Setting>
13+
<Setting Type="hashtable" Key="MyHashtable">
14+
<Item Key="Foo" Value="Bar" />
15+
<Item Key="Hello" Value="World" />
16+
</Setting>
17+
</Settings>
18+
</Configuration>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<#
2+
.SYNOPSIS
3+
Convert the INI file content to a hashtable containing the configuration.
4+
5+
.DESCRIPTION
6+
Convert the INI file content to a hashtable containing the configuration.
7+
8+
.PARAMETER Content
9+
An array of strings with the INI file content. Each array item is a line.
10+
11+
.EXAMPLE
12+
C:\> Get-Content -Path 'config.ini' | ConvertFrom-ScriptConfigIni
13+
Use the pipeline input to parse the INI file content.
14+
#>
15+
16+
function ConvertFrom-ScriptConfigIni
17+
{
18+
[CmdletBinding()]
19+
param
20+
(
21+
[Parameter(Position=0,
22+
Mandatory=$true,
23+
ValueFromPipeline=$true)]
24+
[AllowEmptyString()]
25+
[String[]] $Content
26+
)
27+
28+
Write-Verbose "Parse script configuration file as INI format ..."
29+
30+
$Config = @{}
31+
32+
try
33+
{
34+
# Iterating each line and parse the setting
35+
foreach ($Line in $Content)
36+
{
37+
switch -Wildcard ($Line)
38+
{
39+
# Comment
40+
';*' {
41+
42+
break
43+
}
44+
45+
# Array
46+
'*`[`]=*'{
47+
$Key = $Line.Split('[]=', 4)[0]
48+
$Value = $Line.Split('[]=', 4)[3]
49+
50+
if ($null -eq $Config[$Key])
51+
{
52+
$Config[$Key] = @()
53+
}
54+
55+
$Config[$Key] += $Value
56+
57+
break
58+
}
59+
60+
# Hashtable
61+
'*`[*`]=*' {
62+
$Key = $Line.Split('[]=', 4)[0]
63+
$Hash = $Line.Split('[]=', 4)[1]
64+
$Value = $Line.Split('[]=', 4)[3]
65+
66+
if ($null -eq $Config[$Key])
67+
{
68+
$Config[$Key] = @{}
69+
}
70+
71+
$Config[$Key][$Hash] = $Value
72+
73+
break
74+
}
75+
76+
# String, Integer or Boolean
77+
'*=*' {
78+
$Key = $Line.Split('=', 2)[0]
79+
$Value = $Line.Split('=', 2)[1]
80+
81+
try { $Value = [Int32]::Parse($Value) } catch { }
82+
83+
if ('True'.Equals($Value)) { $Value = $true }
84+
if ('False'.Equals($Value)) { $Value = $false }
85+
86+
$Config[$Key] = $Value
87+
88+
break
89+
}
90+
}
91+
}
92+
93+
Write-Output $Config
94+
}
95+
catch
96+
{
97+
throw "The configuration file content was in an invalid format: $_"
98+
}
99+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<#
2+
.SYNOPSIS
3+
Convert the JSON file content to a hashtable containing the configuration.
4+
5+
.DESCRIPTION
6+
Convert the JSON file content to a hashtable containing the configuration.
7+
8+
.PARAMETER Content
9+
An array of strings with the JSON file content. Each array item is a line.
10+
11+
.EXAMPLE
12+
C:\> Get-Content -Path 'config.json' | ConvertFrom-ScriptConfigJson
13+
Use the pipeline input to parse the JSON file content.
14+
#>
15+
16+
function ConvertFrom-ScriptConfigJson
17+
{
18+
[CmdletBinding()]
19+
param
20+
(
21+
[Parameter(Position=0,
22+
Mandatory=$true,
23+
ValueFromPipeline=$true)]
24+
[AllowEmptyString()]
25+
[String[]] $Content
26+
)
27+
28+
Write-Verbose "Parse script configuration file as JSON format ..."
29+
30+
$Config = @{}
31+
32+
try
33+
{
34+
# Join all lines into one string
35+
$Content = $Content -join ''
36+
37+
# Parse the JSON content
38+
$JsonContent = $Content | ConvertFrom-Json
39+
40+
# Extract all propeties from the json content
41+
$JsonNodes = $JsonContent | Get-Member -MemberType NoteProperty
42+
43+
foreach ($JsonNode in $JsonNodes)
44+
{
45+
$Key = $JsonNode.Name
46+
$Value = $JsonContent.$Key
47+
48+
# Hashtable / Other
49+
if ($Value -is [System.Management.Automation.PSCustomObject])
50+
{
51+
$Config[$Key] = @{}
52+
53+
foreach ($Property in $Value.PSObject.Properties)
54+
{
55+
$Config[$Key][$Property.Name] = $Property.Value
56+
}
57+
}
58+
else
59+
{
60+
$Config[$Key] = $Value
61+
}
62+
}
63+
64+
Write-Output $Config
65+
}
66+
catch
67+
{
68+
throw "The configuration file content was in an invalid format: $_"
69+
}
70+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<#
2+
.SYNOPSIS
3+
Convert the XML file content to a hashtable containing the configuration.
4+
5+
.DESCRIPTION
6+
Convert the XML file content to a hashtable containing the configuration.
7+
8+
.PARAMETER Content
9+
An array of strings with the XML file content. Each array item is a line.
10+
11+
.EXAMPLE
12+
C:\> Get-Content -Path 'config.xml' | ConvertFrom-ScriptConfigXml
13+
Use the pipeline input to parse the XML file content.
14+
#>
15+
16+
function ConvertFrom-ScriptConfigXml
17+
{
18+
[CmdletBinding()]
19+
param
20+
(
21+
[Parameter(Position=0,
22+
Mandatory=$true,
23+
ValueFromPipeline=$true)]
24+
[AllowEmptyString()]
25+
[String[]] $Content
26+
)
27+
28+
Write-Verbose "Parse script configuration file as XML format ..."
29+
30+
$Config = @{}
31+
32+
try
33+
{
34+
# Try to cast the content into an XmlDocument
35+
$XmlContent = [Xml] $Content
36+
37+
# Extract all setting objects
38+
$Settings = $XmlContent.Configuration.Settings.Setting
39+
40+
foreach ($Setting in $Settings)
41+
{
42+
switch ($Setting.Type)
43+
{
44+
# String
45+
'string' {
46+
$Config[$Setting.Key] = $Setting.Value
47+
}
48+
49+
# Integer
50+
'integer' {
51+
$Config[$Setting.Key] = [Int32]::Parse($Setting.Value)
52+
}
53+
54+
# Boolean
55+
'boolean' {
56+
$Config[$Setting.Key] = 'True' -eq $Setting.Value
57+
}
58+
59+
# Array
60+
'array' {
61+
$Config[$Setting.Key] = @()
62+
foreach ($Item in $Setting.Item)
63+
{
64+
$Config[$Setting.Key] += $Item.Value
65+
}
66+
}
67+
68+
# Hashtable
69+
'hashtable' {
70+
$Config[$Setting.Key] = @{}
71+
foreach ($Item in $Setting.Item)
72+
{
73+
$Config[$Setting.Key][$Item.Key] = $Item.Value
74+
}
75+
}
76+
}
77+
}
78+
79+
Write-Output $Config
80+
}
81+
catch
82+
{
83+
throw "The configuration file content was in an invalid format: $_"
84+
}
85+
}

0 commit comments

Comments
 (0)