Skip to content

Commit

Permalink
Added: Auto-detect the config file if the path was not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiospizzi committed Sep 16, 2019
1 parent bb5e1f5 commit eb0a65a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
8 changes: 3 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,35 @@ All notable changes to this project will be documented in this file.
The format is mainly based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## 3.1.0 - 2019-09-16

* Added: Auto-detect the config file if -Path was not specified

## 3.0.0 - 2019-02-18

* 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 - 2016-12-12

* 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 - 2016-02-09

* Added: Formats and types resources


## 1.0.2 - 2016-02-03

* Fixed: Default path issue for Get-ScriptConfig


## 1.0.1 - 2016-01-29

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


## 1.0.0 - 2016-01-21

* Added: Initial public release
24 changes: 20 additions & 4 deletions Modules/ScriptConfig/Functions/Get-ScriptConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ 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)]
[AllowEmptyString()]
[System.String]
$Path,

Expand All @@ -48,13 +49,28 @@ function Get-ScriptConfig

# 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'))
if (-not $PSBoundParameters.ContainsKey('Path') -or [System.String]::IsNullOrEmpty($Path))
{
$lastScriptPath = Get-PSCallStack | Select-Object -Skip 1 -First 1 -ExpandProperty 'ScriptName'

if (-not [System.String]::IsNullOrEmpty($lastScriptPath))
{
$Path = $lastScriptPath + '.config'
if (Test-Path -Path "$lastScriptPath.ini")
{
$Path = "$lastScriptPath.ini"
}
elseif (Test-Path -Path "$lastScriptPath.json")
{
$Path = "$lastScriptPath.json"
}
elseif (Test-Path -Path "$lastScriptPath.xml")
{
$Path = "$lastScriptPath.xml"
}
else
{
$Path = "$lastScriptPath.config"
}
}
else
{
Expand All @@ -74,9 +90,9 @@ function Get-ScriptConfig
{
switch -Wildcard ($Path)
{
'*.xml' { $Format = 'XML' }
'*.json' { $Format = 'JSON' }
'*.ini' { $Format = 'INI' }
'*.json' { $Format = 'JSON' }
'*.xml' { $Format = 'XML' }
default { $Format = 'JSON' }
}
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/ScriptConfig/ScriptConfig.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RootModule = 'ScriptConfig.psm1'

# Version number of this module.
ModuleVersion = '3.0.0'
ModuleVersion = '3.1.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down

0 comments on commit eb0a65a

Please sign in to comment.