From 53b370f20c428b12e17251bf0301df8ba29fbb14 Mon Sep 17 00:00:00 2001 From: rulasg Date: Mon, 28 Jul 2025 16:42:07 +0200 Subject: [PATCH 1/3] feat: add configuration management module with essential functions --- include/config.ps1 | 211 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 include/config.ps1 diff --git a/include/config.ps1 b/include/config.ps1 new file mode 100644 index 0000000..132fe05 --- /dev/null +++ b/include/config.ps1 @@ -0,0 +1,211 @@ +# CONFIG +# +# Configuration management module +# +# Include design description +# This is the function ps1. This file is the same for all modules. +# Create a public psq with variables, Set-MyInvokeCommandAlias call and Invoke public function. +# Invoke function will call back `GetConfigRootPath` to use production root path +# Mock this Invoke function with Set-MyInvokeCommandAlias to set the Store elsewhere +# This ps1 has function `GetConfigFile` that will call `Invoke-MyCommand -Command $CONFIG_INVOKE_GET_ROOT_PATH_ALIAS` +# to use the store path, mocked or not, to create the final store file name. +# All functions of this ps1 will depend on `GetConfigFile` for functionality. +# + + +$MODULE_ROOT_PATH = $PSScriptRoot | Split-Path -Parent +$MODULE_NAME = (Get-ChildItem -Path $MODULE_ROOT_PATH -Filter *.psd1 | Select-Object -First 1).BaseName +$CONFIG_ROOT = [System.Environment]::GetFolderPath('UserProfile') | Join-Path -ChildPath ".helpers" -AdditionalChildPath $MODULE_NAME, "config" + +# Create the config root if it does not exist +if(-Not (Test-Path $CONFIG_ROOT)){ + New-Item -Path $CONFIG_ROOT -ItemType Directory +} + +function GetConfigRootPath { + [CmdletBinding()] + param() + + $configRoot = $CONFIG_ROOT + return $configRoot +} + +function GetConfigFile { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true, Position = 0)][string]$Key + ) + + $configRoot = Invoke-MyCommand -Command $CONFIG_INVOKE_GET_ROOT_PATH_ALIAS + $path = Join-Path -Path $configRoot -ChildPath "$Key.json" + return $path +} + +function Test-Configuration { + [CmdletBinding()] + param( + [Parameter(Position = 0)][string]$Key = "config" + ) + + $path = GetConfigFile -Key $Key + + return Test-Path $path +} + +function Get-Configuration { + [CmdletBinding()] + param( + [Parameter(Position = 0)][string]$Key = "config" + ) + + $path = GetConfigFile -Key $Key + + if(-Not (Test-Configuration -Key $Key)){ + return $null + } + + try{ + $ret = Get-Content $path | ConvertFrom-Json -AsHashtable -ErrorAction Stop + return $ret + } + catch{ + Write-Warning "Error reading configuration ($Key) file: $($path). $($_.Exception.Message)" + return $null + } +} + +function Save-Configuration { + [CmdletBinding()] + param( + [Parameter(Position = 0)][string]$Key = "config", + [Parameter(Mandatory = $true, Position = 1)][Object]$Config + ) + + $path = GetConfigFile -Key $Key + + try { + $Config | ConvertTo-Json -Depth 10 | Set-Content $path -ErrorAction Stop + } + catch { + Write-Warning "Error saving configuration ($Key) to file: $($path). $($_.Exception.Message)" + return $false + } + + return $true +} + +############ + + +# Define unique aliases for "MyModule" +$CONFIG_INVOKE_GET_ROOT_PATH_ALIAS = "$($MODULE_NAME)GetConfigRootPath" +$CONFIG_INVOKE_GET_ROOT_PATH_CMD = "Invoke-$($MODULE_NAME)GetConfigRootPath" + +# Set the alias for the root path command +Set-MyInvokeCommandAlias -Alias $CONFIG_INVOKE_GET_ROOT_PATH_ALIAS -Command $CONFIG_INVOKE_GET_ROOT_PATH_CMD + +# Define the function to get the configuration root path +function Invoke-MyModuleGetConfigRootPath { + [CmdletBinding()] + param() + + $configRoot = GetConfigRootPath + return $configRoot +} +$function = "Invoke-MyModuleGetConfigRootPath" +$NewName = $function -Replace "MyModule", $MODULE_NAME +Rename-Item -path Function:$Function -NewName $NewName +Export-ModuleMember -Function $NewName + +# Extra functions not needed by INCLUDE CONFIG + +function Get-MyModuleConfig{ + [CmdletBinding()] + param() + + $config = Get-Configuration + + return $config +} +$function = "Get-MyModuleConfig" +$NewName = $function -Replace "MyModule", $MODULE_NAME +Rename-Item -path Function:$Function -NewName $NewName +Export-ModuleMember -Function $NewName + +function Save-MyModuleConfig{ + [CmdletBinding()] + param( + [Parameter(Mandatory, ValueFromPipeline, Position = 0)][Object]$Config + ) + + return Save-Configuration -Config $Config +} $function = "Save-MyModuleConfig" +$NewName = $function -Replace "MyModule", $MODULE_NAME +Rename-Item -path Function:$Function -NewName $NewName +Export-ModuleMember -Function $NewName + +function Open-MyModuleConfig{ + [CmdletBinding()] + param() + + $path = GetConfigFile -Key "config" + + code $path + +} $function = "Open-MyModuleConfig" +$NewName = $function -Replace "MyModule", $MODULE_NAME +Rename-Item -path Function:$Function -NewName $NewName +Export-ModuleMember -Function $NewName + +function Add-MyModuleConfigAttribute{ + [CmdletBinding()] + param( + [Parameter(Mandatory,Position=0)][ValidateSet("Account", "User", "Opportunity")][string]$objectType, + + [Parameter(Mandatory, ValueFromPipeline, Position = 1)][string]$Attribute + + ) + + begin{ + $config = Get-Configuration + $configAttribute = ($objectType + "_attributes").ToLower() + + if(-Not $config){ + $config = @{} + } + + if(-Not $config.$configAttribute){ + $config.$configAttribute = @() + } + } + + process{ + $config.$configAttribute += $Attribute + } + + End{ + $ret = Save-Configuration -Config $config + if(-Not $ret){ + throw "Error saving configuration" + } + + $config = Get-Configuration + Write-Output $config.$configAttribute + + } + +} $function = "Add-MyModuleConfigAttribute" +$NewName = $function -Replace "MyModule", $MODULE_NAME +Rename-Item -path Function:$Function -NewName $NewName +Export-ModuleMember -Function $NewName + + + + + + + + + + + From 5ce6f03711070464b4142b2c9ccdbf47437925dd Mon Sep 17 00:00:00 2001 From: rulasg Date: Tue, 29 Jul 2025 11:46:32 +0200 Subject: [PATCH 2/3] refactor: update MockCallToString to use Invoke-NotesHelperNotesRoot for path resolution --- Test/public/getNotes.test.ps1 | 4 ++-- Test/public/newNotes.test.ps1 | 10 +++++----- private/resolveNotesPath.ps1 | 12 ++++++++++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Test/public/getNotes.test.ps1 b/Test/public/getNotes.test.ps1 index 84f5115..adebed2 100644 --- a/Test/public/getNotes.test.ps1 +++ b/Test/public/getNotes.test.ps1 @@ -2,7 +2,7 @@ function Test_GetNotes_Category{ Reset-InvokeCommandMock New-TestingFolder "TestNotesRoot" - MockCallToString 'echo $NOTES_ROOT' -OutString "./TestNotesRoot" + MockCallToString 'Invoke-NotesHelperNotesRoot' -OutString "./TestNotesRoot" New-TestingFile "TestNotesRoot/Folder1" "name11-name12-name13.md" $file1 = New-TestingFile "TestNotesRoot/Folder1" "name21-name22-name23.md" -PassThru @@ -30,7 +30,7 @@ function Test_GetNotes_Category_WithDots{ Reset-InvokeCommandMock New-TestingFolder "TestNotesRoot" - MockCallToString 'echo $NOTES_ROOT' -OutString "./TestNotesRoot" + MockCallToString 'Invoke-NotesHelperNotesRoot' -OutString "./TestNotesRoot" New-TestingFile "TestNotesRoot/Folder1" "name01-name02-name03.md" $file1 = New-TestingFile "TestNotesRoot/Folder1" "name11-name12-name13.md" -PassThru diff --git a/Test/public/newNotes.test.ps1 b/Test/public/newNotes.test.ps1 index 975de15..4ec4b0d 100644 --- a/Test/public/newNotes.test.ps1 +++ b/Test/public/newNotes.test.ps1 @@ -3,7 +3,7 @@ function Test_AddNotesToday_Simple{ Reset-InvokeCommandMock New-TestingFolder "TestNotesRoot" - MockCallToString 'echo $NOTES_ROOT' -OutString "./TestNotesRoot" + MockCallToString 'Invoke-NotesHelperNotesRoot' -OutString "./TestNotesRoot" $category = "TestClient" $title = "This is the title of the note" @@ -39,7 +39,7 @@ function Test_AddNotesToday_Simple_AvoidChildFolder { Reset-InvokeCommandMock New-TestingFolder "TestNotesRoot" - MockCallToString 'echo $NOTES_ROOT' -OutString "./TestNotesRoot" + MockCallToString 'Invoke-NotesHelperNotesRoot' -OutString "./TestNotesRoot" $category = "TestClient" $title = "This is the title of the note" @@ -70,7 +70,7 @@ function Test_AddNotesToday_WithContent { Reset-InvokeCommandMock New-TestingFolder "TestNotesRoot" - MockCallToString 'echo $NOTES_ROOT' -OutString "./TestNotesRoot" + MockCallToString 'Invoke-NotesHelperNotesRoot' -OutString "./TestNotesRoot" $category = "TestClient" $title = "This is the title of the client note" @@ -104,7 +104,7 @@ function Test_AddNotesToday_Client_Simple{ Reset-InvokeCommandMock New-TestingFolder "TestNotesRoot" - MockCallToString 'echo $NOTES_ROOT' -OutString "./TestNotesRoot" + MockCallToString 'Invoke-NotesHelperNotesRoot' -OutString "./TestNotesRoot" $category = "Clients" $title = "This is the title of the note" @@ -140,7 +140,7 @@ function Test_NewNotesToday_Failing{ Reset-InvokeCommandMock New-TestingFolder "TestNotesRoot" - MockCallToString 'echo $NOTES_ROOT' -OutString "./TestNotesRoot" + MockCallToString 'Invoke-NotesHelperNotesRoot' -OutString "./TestNotesRoot" New-TestingFolder -Path "./TestNotesRoot/howto" $today = (Get-Date).ToString("yyMMdd") diff --git a/private/resolveNotesPath.ps1 b/private/resolveNotesPath.ps1 index 3e37ab4..d6662bd 100644 --- a/private/resolveNotesPath.ps1 +++ b/private/resolveNotesPath.ps1 @@ -1,5 +1,5 @@ -Set-MyInvokeCommandAlias -Alias GetNotesRoot 'echo $NOTES_ROOT' +Set-MyInvokeCommandAlias -Alias GetNotesRoot 'Invoke-NotesHelperNotesRoot' function Resolve-NotesPath { [CmdletBinding()] @@ -67,4 +67,12 @@ function New-FolderIfNotExists{ } return $Path -} \ No newline at end of file +} + +function Invoke-NotesHelperNotesRoot{ + [CmdletBinding()] + param() + + $config = Get-NotesHelperConfig + return $config.NotesRoot +} Export-ModuleMember -Function Invoke-NotesHelperNotesRoot \ No newline at end of file From 1a4bfebd7199951743b54ba735d2cbb1c9e4dc05 Mon Sep 17 00:00:00 2001 From: rulasg Date: Tue, 29 Jul 2025 11:57:05 +0200 Subject: [PATCH 3/3] refactor: update MODULE_NAME assignment for improved module structure validation --- include/config.ps1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/config.ps1 b/include/config.ps1 index 132fe05..4ee69ce 100644 --- a/include/config.ps1 +++ b/include/config.ps1 @@ -12,9 +12,10 @@ # All functions of this ps1 will depend on `GetConfigFile` for functionality. # +# MODULE_NAME +$MODULE_NAME = ($PSScriptRoot | Split-Path -Parent | Get-ChildItem -Filter *.psd1 | Select-Object -First 1).BaseName +if(-Not $MODULE_NAME){ throw "Module name not found. Please check the module structure." } -$MODULE_ROOT_PATH = $PSScriptRoot | Split-Path -Parent -$MODULE_NAME = (Get-ChildItem -Path $MODULE_ROOT_PATH -Filter *.psd1 | Select-Object -First 1).BaseName $CONFIG_ROOT = [System.Environment]::GetFolderPath('UserProfile') | Join-Path -ChildPath ".helpers" -AdditionalChildPath $MODULE_NAME, "config" # Create the config root if it does not exist