From eb0a65a78f70e3fe269a67533f0b8d3cdf25e570 Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Mon, 16 Sep 2019 21:35:45 +0200 Subject: [PATCH] Added: Auto-detect the config file if the path was not specified --- CHANGELOG.md | 8 +++---- .../Functions/Get-ScriptConfig.ps1 | 24 +++++++++++++++---- Modules/ScriptConfig/ScriptConfig.psd1 | 2 +- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ffa9e3..30d932b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Modules/ScriptConfig/Functions/Get-ScriptConfig.ps1 b/Modules/ScriptConfig/Functions/Get-ScriptConfig.ps1 index 52acafa..c082a7c 100644 --- a/Modules/ScriptConfig/Functions/Get-ScriptConfig.ps1 +++ b/Modules/ScriptConfig/Functions/Get-ScriptConfig.ps1 @@ -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, @@ -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 { @@ -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' } } } diff --git a/Modules/ScriptConfig/ScriptConfig.psd1 b/Modules/ScriptConfig/ScriptConfig.psd1 index 4f988f9..7f8e1fe 100644 --- a/Modules/ScriptConfig/ScriptConfig.psd1 +++ b/Modules/ScriptConfig/ScriptConfig.psd1 @@ -3,7 +3,7 @@ RootModule = 'ScriptConfig.psm1' # Version number of this module. - ModuleVersion = '3.0.0' + ModuleVersion = '3.1.0' # Supported PSEditions # CompatiblePSEditions = @()