Skip to content

Commit

Permalink
Update module, function with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiospizzi committed Feb 18, 2019
1 parent 93acfad commit 97ad58c
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 289 deletions.
24 changes: 15 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,36 @@ The format is mainly based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## Unreleased

* Changed: Get the default config path from the PS call stack
* Changed: The default configuration file type is now JSON (BREAKING CHANGE)


## 2.0.0

- Changed: Convert module to new deployment model
- Changed: Rework code against high quality module guidelines by Microsoft
- Changed: Remove positional parameters (BREAKING CHANGE)
* Changed: Convert module to new deployment model
* Changed: Rework code against high quality module guidelines by Microsoft
* Changed: Remove positional parameters (BREAKING CHANGE)


## 1.0.3

- Added: Formats and types resources
* Added: Formats and types resources


## 1.0.2

- Fixed: Default path issue for Get-ScriptConfig
* Fixed: Default path issue for Get-ScriptConfig


## 1.0.1

- Updated: File encoding to UTF8 w/o BOM
- Updated: Demo and help
- Added: Enhance parameters for Get-ScriptConfig function
* Updated: File encoding to UTF8 w/o BOM
* Updated: Demo and help
* Added: Enhance parameters for Get-ScriptConfig function


## 1.0.0

- Added: Initial public release
* Added: Initial public release
78 changes: 45 additions & 33 deletions Modules/ScriptConfig/Functions/Get-ScriptConfig.ps1
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
<#
.SYNOPSIS
Load a script configuration from a config file.
Load a script configuration from a config file.
.DESCRIPTION
Load a script configuration from a config file. By default, the config file
next to the script is loaded. So for the script MyScript.ps1, the config
file MyScript.ps1.config will be loaded. The config file format will be
detected by it's extension (.xml, .ini, .json). If the file format can't be
detected, the default format (XML) will be used.
Load a script configuration from a config file. By default, the config
file next to the script is loaded. So for the script MyScript.ps1, the
config file MyScript.ps1.config will be loaded. The config file format
will be detected by it's extension (.xml, .ini, .json). If the file
format can't be detected, the default format (JSON) will be used.
.EXAMPLE
PS C:\> $config = Get-ScriptConfig
Loads the default XML formatted configuration file.
PS C:\> $config = Get-ScriptConfig
Loads the default JSON formatted configuration file.
.EXAMPLE
PS C:\> $config = Get-ScriptConfig -Path 'C:\MyApp\global.config'
Loads a custom configuration file, with default XML format.
PS C:\> $config = Get-ScriptConfig -Path 'C:\MyApp\global.config'
Loads a custom configuration file, with default JSON format.
.EXAMPLE
PS C:\> $config = Get-ScriptConfig -Format JSON
Loads the default configuration file but in JSON format.
PS C:\> $config = Get-ScriptConfig -Format XML
Loads the default configuration file but in XML format.
.NOTES
Author : Claudio Spizzi
License : MIT License
Author : Claudio Spizzi
License : MIT License
.LINK
https://github.com/claudiospizzi/ScriptConfig
https://github.com/claudiospizzi/ScriptConfig
#>

function Get-ScriptConfig
{
[CmdletBinding()]
Expand All @@ -37,9 +36,8 @@ function Get-ScriptConfig
# Specify the path to the configuration file. By default, the current
# script file path will be used with an appended '.config' extension.
[Parameter(Mandatory = $false)]
[ValidateScript({Test-Path -Path $_})]
[System.String]
$Path = ([String] $Global:MyInvocation.MyCommand.Definition).Trim() + '.config',
$Path,

# Override the format detection and default value.
[Parameter(Mandatory = $false)]
Expand All @@ -48,18 +46,38 @@ function Get-ScriptConfig
$Format
)

# Only work with absolute path, makes error handling easier
$Path = (Resolve-Path -Path $Path).Path
# If the Path parameter was not specified, add a default value. If possible,
# use the last script called this function. Else throw an exception.
if (-not $PSBoundParameters.ContainsKey('Path'))
{
$lastScriptPath = Get-PSCallStack | Select-Object -Skip 1 -First 1 -ExpandProperty 'ScriptName'

# If no format was specified, try to detect it
if ([String]::IsNullOrEmpty($Format))
if (-not [System.String]::IsNullOrEmpty($lastScriptPath))
{
$Path = $lastScriptPath + '.config'
}
else
{
throw "Configuration file not specified"
}
}

# Now check, if the configuration file exists
if (-not (Test-Path -Path $Path))
{
throw "Configuration file not found: $Path"
}

# If the Format parameter was not specified, try to detect by the file
# extension or use the default format JSON.
if (-not $PSBoundParameters.ContainsKey('Format'))
{
switch -Wildcard ($Path)
{
'*.xml' { $Format = 'XML' }
'*.json' { $Format = 'JSON' }
'*.ini' { $Format = 'INI' }
default { $Format = 'XML' }
default { $Format = 'JSON' }
}
}

Expand All @@ -68,17 +86,11 @@ function Get-ScriptConfig
# Load raw content, parse it later
$content = Get-Content -Path $Path -ErrorAction Stop

# Use custom functions to parse the files
# Use custom functions to parse the files and return the config object
switch ($Format)
{
'XML' { $configHashtable = ConvertFrom-ScriptConfigXml -Content $content }
'JSON' { $configHashtable = ConvertFrom-ScriptConfigJson -Content $content }
'INI' { $configHashtable = ConvertFrom-ScriptConfigIni -Content $content }
'XML' { ConvertFrom-ScriptConfigXml -Content $content }
'JSON' { ConvertFrom-ScriptConfigJson -Content $content }
'INI' { ConvertFrom-ScriptConfigIni -Content $content }
}

# Create a config object with a custom type
$config = New-Object -TypeName PSObject -Property $configHashtable
$config.PSTypeNames.Insert(0, 'ScriptConfig.Configuration')

Write-Output $config
}
20 changes: 11 additions & 9 deletions Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigIni.ps1
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<#
.SYNOPSIS
Convert the INI file content to a hashtable containing the configuration.
Convert the INI file content to a hashtable containing the
configuration.
.EXAMPLE
PS C:\> Get-Content -Path 'config.ini' | ConvertFrom-ScriptConfigIni
Use the pipeline input to parse the INI file content.
PS C:\> Get-Content -Path 'config.ini' | ConvertFrom-ScriptConfigIni
Use the pipeline input to parse the INI file content.
.NOTES
Author : Claudio Spizzi
License : MIT License
Author : Claudio Spizzi
License : MIT License
.LINK
https://github.com/claudiospizzi/ScriptConfig
https://github.com/claudiospizzi/ScriptConfig
#>

function ConvertFrom-ScriptConfigIni
{
[CmdletBinding()]
Expand All @@ -26,7 +26,9 @@ function ConvertFrom-ScriptConfigIni
$Content
)

$config = @{}
$config = @{
PSTypeName = 'ScriptConfig.Configuration'
}

try
{
Expand Down Expand Up @@ -99,7 +101,7 @@ function ConvertFrom-ScriptConfigIni
}
}

Write-Output $config
[PSCustomObject] $config
}
catch
{
Expand Down
14 changes: 7 additions & 7 deletions Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigJson.ps1
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<#
.SYNOPSIS
Convert the JSON file content to a hashtable containing the configuration.
Convert the JSON file content to a hashtable containing the
configuration.
.EXAMPLE
PS C:\> Get-Content -Path 'config.json' | ConvertFrom-ScriptConfigJson
Use the pipeline input to parse the JSON file content.
PS C:\> Get-Content -Path 'config.json' | ConvertFrom-ScriptConfigJson
Use the pipeline input to parse the JSON file content.
.NOTES
Author : Claudio Spizzi
License : MIT License
Author : Claudio Spizzi
License : MIT License
.LINK
https://github.com/claudiospizzi/ScriptConfig
https://github.com/claudiospizzi/ScriptConfig
#>

function ConvertFrom-ScriptConfigJson
{
[CmdletBinding()]
Expand Down
14 changes: 7 additions & 7 deletions Modules/ScriptConfig/Helpers/ConvertFrom-ScriptConfigXml.ps1
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<#
.SYNOPSIS
Convert the XML file content to a hashtable containing the configuration.
Convert the XML file content to a hashtable containing the
configuration.
.EXAMPLE
PS C:\> Get-Content -Path 'config.xml' | ConvertFrom-ScriptConfigXml
Use the pipeline input to parse the XML file content.
PS C:\> Get-Content -Path 'config.xml' | ConvertFrom-ScriptConfigXml
Use the pipeline input to parse the XML file content.
.NOTES
Author : Claudio Spizzi
License : MIT License
Author : Claudio Spizzi
License : MIT License
.LINK
https://github.com/claudiospizzi/ScriptConfig
https://github.com/claudiospizzi/ScriptConfig
#>

function ConvertFrom-ScriptConfigXml
{
[CmdletBinding()]
Expand Down
24 changes: 12 additions & 12 deletions Modules/ScriptConfig/Tests/Integration/IniConfigDemo.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

# Load the default configuration file in INI format
$Config = Get-ScriptConfig -Format INI
$Config = Get-ScriptConfig -Format 'INI'

# Access the configuration settings
Write-Host "String :" $Config.MyString
Write-Host "Integer Positive :" $Config.MyIntegerPositive
Write-Host "Integer Negative :" $Config.MyIntegerNegative
Write-Host "Boolean True :" $Config.MyBooleanTrue
Write-Host "Boolean False :" $Config.MyBooleanFalse
Write-Host "Array :" $Config.MyArray
Write-Host "Array Item :" $Config.MyArray[0]
Write-Host "Hashtable :" $Config.MyHashtable
Write-Host "Hashtable Item :" $Config.MyHashtable['Hello']
Write-Host "Hashtable Keys :" $Config.MyHashtable.Keys
Write-Host "Hashtable Values :" $Config.MyHashtable.Values
Write-Verbose "String :" $Config.MyString
Write-Verbose "Integer Positive :" $Config.MyIntegerPositive
Write-Verbose "Integer Negative :" $Config.MyIntegerNegative
Write-Verbose "Boolean True :" $Config.MyBooleanTrue
Write-Verbose "Boolean False :" $Config.MyBooleanFalse
Write-Verbose "Array :" $Config.MyArray
Write-Verbose "Array Item :" $Config.MyArray[0]
Write-Verbose "Hashtable :" $Config.MyHashtable
Write-Verbose "Hashtable Item :" $Config.MyHashtable['Hello']
Write-Verbose "Hashtable Keys :" $Config.MyHashtable.Keys
Write-Verbose "Hashtable Values :" $Config.MyHashtable.Values
24 changes: 12 additions & 12 deletions Modules/ScriptConfig/Tests/Integration/JsonConfigDemo.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

# Load the default configuration file in JSON format
$Config = Get-ScriptConfig -Format JSON
$Config = Get-ScriptConfig -Format 'JSON'

# Access the configuration settings
Write-Host "String :" $Config.MyString
Write-Host "Integer Positive :" $Config.MyIntegerPositive
Write-Host "Integer Negative :" $Config.MyIntegerNegative
Write-Host "Boolean True :" $Config.MyBooleanTrue
Write-Host "Boolean False :" $Config.MyBooleanFalse
Write-Host "Array :" $Config.MyArray
Write-Host "Array Item :" $Config.MyArray[0]
Write-Host "Hashtable :" $Config.MyHashtable
Write-Host "Hashtable Item :" $Config.MyHashtable['Hello']
Write-Host "Hashtable Keys :" $Config.MyHashtable.Keys
Write-Host "Hashtable Values :" $Config.MyHashtable.Values
Write-Verbose "String :" $Config.MyString
Write-Verbose "Integer Positive :" $Config.MyIntegerPositive
Write-Verbose "Integer Negative :" $Config.MyIntegerNegative
Write-Verbose "Boolean True :" $Config.MyBooleanTrue
Write-Verbose "Boolean False :" $Config.MyBooleanFalse
Write-Verbose "Array :" $Config.MyArray
Write-Verbose "Array Item :" $Config.MyArray[0]
Write-Verbose "Hashtable :" $Config.MyHashtable
Write-Verbose "Hashtable Item :" $Config.MyHashtable['Hello']
Write-Verbose "Hashtable Keys :" $Config.MyHashtable.Keys
Write-Verbose "Hashtable Values :" $Config.MyHashtable.Values
24 changes: 12 additions & 12 deletions Modules/ScriptConfig/Tests/Integration/XmlConfigDemo.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

# Load the default configuration file in XML format
$Config = Get-ScriptConfig -Format XML
$Config = Get-ScriptConfig -Format 'XML'

# Access the configuration settings
Write-Host "String :" $Config.MyString
Write-Host "Integer Positive :" $Config.MyIntegerPositive
Write-Host "Integer Negative :" $Config.MyIntegerNegative
Write-Host "Boolean True :" $Config.MyBooleanTrue
Write-Host "Boolean False :" $Config.MyBooleanFalse
Write-Host "Array :" $Config.MyArray
Write-Host "Array Item :" $Config.MyArray[0]
Write-Host "Hashtable :" $Config.MyHashtable
Write-Host "Hashtable Item :" $Config.MyHashtable['Hello']
Write-Host "Hashtable Keys :" $Config.MyHashtable.Keys
Write-Host "Hashtable Values :" $Config.MyHashtable.Values
Write-Verbose "String :" $Config.MyString
Write-Verbose "Integer Positive :" $Config.MyIntegerPositive
Write-Verbose "Integer Negative :" $Config.MyIntegerNegative
Write-Verbose "Boolean True :" $Config.MyBooleanTrue
Write-Verbose "Boolean False :" $Config.MyBooleanFalse
Write-Verbose "Array :" $Config.MyArray
Write-Verbose "Array Item :" $Config.MyArray[0]
Write-Verbose "Hashtable :" $Config.MyHashtable
Write-Verbose "Hashtable Item :" $Config.MyHashtable['Hello']
Write-Verbose "Hashtable Keys :" $Config.MyHashtable.Keys
Write-Verbose "Hashtable Values :" $Config.MyHashtable.Values
Loading

0 comments on commit 97ad58c

Please sign in to comment.