From 899fdb4f6c6e384bd602354bf9e7565017003b7b Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 8 Aug 2025 12:19:49 +0200 Subject: [PATCH 1/2] refactor: Sync-IncludeWithModule --- .devcontainer/devcontainer.json | 9 +- .../copilot-commit-message-instructions.md | 2 +- .github/copilot-instructions.md | 2 +- .../workflows/deploy_module_on_release.yml | 1 + .github/workflows/powershell.yml | 2 + .github/workflows/test_with_TestingHelper.yml | 1 + CsvHelper.psm1 | 10 +- LICENSE | 1 + Test/Test.psm1 | 11 +- Test/helper/module.helper.ps1 | 171 ++++++++++++++++++ Test/include/database.mock.ps1 | 21 +-- Test/include/invokeCommand.Mock.ps1 | 87 +++++++-- deploy.ps1 | 1 + helper/module.helper.ps1 | 171 ++++++++++++++++++ include/databaseV2.ps1 | 57 ++---- include/getHashCode.ps1 | 2 +- release.ps1 | 1 + sync.ps1 | 1 + test.ps1 | 2 +- tools/deploy.Helper.ps1 | 1 + tools/sync.Helper.ps1 | 1 + 21 files changed, 469 insertions(+), 86 deletions(-) create mode 100644 Test/helper/module.helper.ps1 create mode 100644 helper/module.helper.ps1 diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 082b64f..194474c 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -4,10 +4,10 @@ { "name": "PowerShell", // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile - "image": "mcr.microsoft.com/powershell:lts-debian-11", + "image": "mcr.microsoft.com/powershell:lts-ubuntu-22.04", + "features": { "ghcr.io/devcontainers/features/common-utils:2": {}, - "ghcr.io/devcontainers/features/powershell:1": {}, "ghcr.io/devcontainers/features/sshd:1": {}, "ghcr.io/devcontainers/features/dotnet:1": {}, "ghcr.io/devcontainers/features/github-cli:1": {} @@ -27,7 +27,9 @@ // Add the IDs of extensions you want installed when the container is created. "extensions": [ "ms-vscode.powershell", - "github.copilot" + "github.copilot", + "GitHub.copilot-chat", + "mhutchie.git-graph" ] } } @@ -38,3 +40,4 @@ // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. // "remoteUser": "root" } + diff --git a/.github/copilot-commit-message-instructions.md b/.github/copilot-commit-message-instructions.md index 4007169..2297468 100644 --- a/.github/copilot-commit-message-instructions.md +++ b/.github/copilot-commit-message-instructions.md @@ -31,4 +31,4 @@ References: - https://www.conventionalcommits.org/ - https://seesparkbox.com/foundry/semantic_commit_messages -- http://karma-runner.github.io/1.0/dev/git-commit-msg.html \ No newline at end of file +- http://karma-runner.github.io/1.0/dev/git-commit-msg.html diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index 9c4a524..aea90af 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -41,4 +41,4 @@ function Test_GetUserName_UserNotFound{ Assert-NotImplemented } -``` \ No newline at end of file +``` diff --git a/.github/workflows/deploy_module_on_release.yml b/.github/workflows/deploy_module_on_release.yml index e73346b..d3132cb 100644 --- a/.github/workflows/deploy_module_on_release.yml +++ b/.github/workflows/deploy_module_on_release.yml @@ -46,3 +46,4 @@ jobs: } ./deploy.ps1 -VersionTag $tag -NugetApiKey $env:NUGETAPIKEY + diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index 419192b..67120f8 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -44,3 +44,5 @@ jobs: uses: github/codeql-action/upload-sarif@v3 with: sarif_file: results.sarif + + diff --git a/.github/workflows/test_with_TestingHelper.yml b/.github/workflows/test_with_TestingHelper.yml index d08b89d..30bda51 100644 --- a/.github/workflows/test_with_TestingHelper.yml +++ b/.github/workflows/test_with_TestingHelper.yml @@ -43,3 +43,4 @@ jobs: { "All test passed" | Write-Verbose -verbose ; exit 0 } else { "Not all tests passed" | Write-Verbose -verbose ; exit 1 } + diff --git a/CsvHelper.psm1 b/CsvHelper.psm1 index d95aded..13e3365 100644 --- a/CsvHelper.psm1 +++ b/CsvHelper.psm1 @@ -5,9 +5,9 @@ $MODULE_PATH = $PSScriptRoot # Load ps1 files on code folders in order "config","helper","include","private","public" | ForEach-Object { - - Get-ChildItem -Path $MODULE_PATH\$_\*.ps1 -Recurse -ErrorAction SilentlyContinue | ForEach-Object { - try { . $_.fullname } - catch { Write-Error -Message "Failed to import function $($import.fullname): $_" } + foreach ($import in Get-ChildItem -Path $MODULE_PATH\$_\*.ps1 -Recurse -ErrorAction SilentlyContinue) { + try { . $import.fullname } + catch { Write-Error -Message "Failed to import $($import.fullname): $_" } } -} \ No newline at end of file +} + diff --git a/LICENSE b/LICENSE index f51f1d4..9ef1be3 100644 --- a/LICENSE +++ b/LICENSE @@ -19,3 +19,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/Test/Test.psm1 b/Test/Test.psm1 index 844d032..f667fac 100644 --- a/Test/Test.psm1 +++ b/Test/Test.psm1 @@ -1,16 +1,13 @@ -Write-Information -Message ("Loading {0} ..." -f ($PSScriptRoot | Split-Path -Leaf)) -InformationAction continue +Write-Information -Message ("Loading {0} ..." -f ($PSCommandPath | Split-Path -LeafBase)) -InformationAction continue #Module path is where resides the RootModule file. This file. :) $MODULE_PATH = $PSScriptRoot # Load ps1 files on code folders in order "config","helper","include","private","public" | ForEach-Object { - - Get-ChildItem -Path $MODULE_PATH\$_\*.ps1 -Recurse -ErrorAction SilentlyContinue | ForEach-Object { - try { . $_.fullname } - catch { Write-Error -Message "Failed to import function $($import.fullname): $_" } + foreach ($import in Get-ChildItem -Path $MODULE_PATH\$_\*.ps1 -Recurse -ErrorAction SilentlyContinue) { + try { . $import.fullname } + catch { Write-Error -Message "Failed to import $($import.fullname): $_" } } } - Export-ModuleMember -Function Test_* - diff --git a/Test/helper/module.helper.ps1 b/Test/helper/module.helper.ps1 new file mode 100644 index 0000000..7fd1786 --- /dev/null +++ b/Test/helper/module.helper.ps1 @@ -0,0 +1,171 @@ +# Helper for module variables + +function Find-ModuleRootPath{ + [CmdletBinding()] + param( + [Parameter(Mandatory,ValueFromPipeline,Position = 0)] + [string]$Path + ) + + $path = Convert-Path -Path $Path + + while (-not [string]::IsNullOrWhiteSpace($Path)){ + $psd1 = Get-ChildItem -Path $Path -Filter *.psd1 | Select-Object -First 1 + + if ($psd1 | Test-Path) { + + if($psd1.BaseName -eq "Test"){ + #foudn testing module. Continue + $path = $path | Split-Path -Parent + continue + } + + # foudn module + return $path + } + # folder without psd1 file + $path = $path | Split-Path -Parent + } + + # Path is null. Reached driver root. Module not found + return $null +} + +$MODULE_ROOT_PATH = $PSScriptRoot | Find-ModuleRootPath +$MODULE_NAME = (Get-ChildItem -Path $MODULE_ROOT_PATH -Filter *.psd1 | Select-Object -First 1).BaseName + +# Helper for module variables + + +$VALID_FOLDER_NAMES = @('Include', 'Private', 'Public', 'Root', 'TestInclude', 'TestPrivate', 'TestPublic', 'TestRoot', 'Tools', 'DevContainer', 'WorkFlows', 'GitHub', 'Helper', 'Config', 'TestHelper', 'TestConfig') + +class ValidFolderNames : System.Management.Automation.IValidateSetValuesGenerator { + [String[]] GetValidValues() { + return $script:VALID_FOLDER_NAMES + } +} + +function Get-Ps1FullPath{ + [CmdletBinding()] + param( + [Parameter(Mandatory,Position = 0)][string]$Name, + [Parameter(Position = 1)][ValidateSet([ValidFolderNames])][string]$FolderName, + [Parameter(Position = 0)][string]$ModuleRootPath + ) + + # If folderName is not empty + if($FolderName -ne $null){ + $folder = Get-ModuleFolder -FolderName $FolderName -ModuleRootPath $ModuleRootPath + $path = $folder | Join-Path -ChildPath $Name + } else { + $path = $Name + } + + # Check if file exists + if(-Not (Test-Path $path)){ + throw "File $path not found" + } + + # Get Path item + $item = Get-item -Path $path + + return $item +} +function Get-ModuleRootPath{ + [CmdletBinding()] + param( + [Parameter(Position = 0)][string]$ModuleRootPath + ) + + # if ModuleRootPath is not provided, default to local module path + if([string]::IsNullOrWhiteSpace($ModuleRootPath)){ + $ModuleRootPath = $MODULE_ROOT_PATH + } + + # Convert to full path + $ModuleRootPath = Convert-Path -Path $ModuleRootPath + + return $ModuleRootPath +} + +function Get-ModuleName{ + [CmdletBinding()] + param( + [Parameter(Position = 0)] [string]$ModuleRootPath + ) + + $ModuleRootPath = Get-ModuleRootPath -ModuleRootPath $ModuleRootPath + + $MODULE_NAME = (Get-ChildItem -Path $MODULE_ROOT_PATH -Filter *.psd1 | Select-Object -First 1).BaseName + + + return $MODULE_NAME +} + +function Get-ModuleFolder{ + [CmdletBinding()] + param( + [Parameter(Mandatory,Position = 0)][ValidateSet([ValidFolderNames])][string]$FolderName, + [Parameter(Position = 1)][string]$ModuleRootPath + ) + + $ModuleRootPath = Get-ModuleRootPath -ModuleRootPath $ModuleRootPath + + # TestRootPath + $testRootPath = $ModuleRootPath | Join-Path -ChildPath "Test" + + switch ($FolderName){ + 'Public'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath "public" + } + 'Private'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath "private" + } + 'Include'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath "include" + } + 'TestInclude'{ + $moduleFolder = $testRootPath | Join-Path -ChildPath "include" + } + 'TestPrivate'{ + $moduleFolder = $testRootPath | Join-Path -ChildPath "private" + } + 'TestPublic'{ + $moduleFolder = $testRootPath | Join-Path -ChildPath "public" + } + 'Root'{ + $moduleFolder = $ModuleRootPath + } + 'TestRoot'{ + $moduleFolder = $testRootPath + } + 'Tools'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath "tools" + } + 'DevContainer'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath ".devcontainer" + } + 'WorkFlows'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath ".github/workflows" + } + 'GitHub'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath ".github" + } + 'Helper'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath "helper" + } + 'Config'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath "config" + } + 'TestHelper'{ + $moduleFolder = $testRootPath | Join-Path -ChildPath "helper" + } + 'TestConfig'{ + $moduleFolder = $testRootPath | Join-Path -ChildPath "config" + } + default{ + throw "Folder [$FolderName] is unknown" + } + } + return $moduleFolder +} Export-ModuleMember -Function Get-ModuleFolder diff --git a/Test/include/database.mock.ps1 b/Test/include/database.mock.ps1 index 801f637..85ff32c 100644 --- a/Test/include/database.mock.ps1 +++ b/Test/include/database.mock.ps1 @@ -3,26 +3,19 @@ # This file is used to mock the database path and the database file # for the tests. It creates a mock database path and a mock database file # and sets the database path to the mock database path. -# We need to define variables for this include to work -# $MOCK_DATABASE_PATH : The path used as the mock database folder -# $DB_INVOKE_GET_ROOT_PATH_CMD : Invoke command that is needed to be mocked -# $DB_INVOKE_GET_ROOT_PATH_ALIAS : Invoke function to retreive the root path of the database -# -# Sample file -# # DATABASE MOCK VARIABLES -# # This file is required for DATABASE MOCK to work -# $DB_INVOKE_GET_ROOT_PATH_ALIAS = "MyModuleGetDbRootPath" # +# THIS INCLUDE REQURED module.helper.ps1 +if(-not $MODULE_NAME){ throw "Missing MODULE_NAME varaible initialization. Check for module.helerp.ps1 file." } -$DB_INVOKE_GET_ROOT_PATH_CMD = "Invoke-$DB_INVOKE_GET_ROOT_PATH_ALIAS" +$DB_INVOKE_GET_ROOT_PATH_CMD = "Invoke-$($MODULE_NAME)GetDbRootPath" $MOCK_DATABASE_PATH = "test_database_path" function Mock_Database([switch]$ResetDatabase){ - MockCallToString $DB_INVOKE_GET_ROOT_PATH_CMD -OutString "test_database_path" + MockCallToString $DB_INVOKE_GET_ROOT_PATH_CMD -OutString $MOCK_DATABASE_PATH $dbstore = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_CMD - Assert-AreEqual -Expected "test_database_path" -Presented $dbstore + Assert-AreEqual -Expected $MOCK_DATABASE_PATH -Presented $dbstore if($ResetDatabase){ Reset-DatabaseStore @@ -39,10 +32,10 @@ function Reset-DatabaseStore{ [CmdletBinding()] param() - $databaseRoot = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_ALIAS + $databaseRoot = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_CMD Remove-Item -Path $databaseRoot -Recurse -Force -ErrorAction SilentlyContinue New-Item -Path $databaseRoot -ItemType Directory -} \ No newline at end of file +} diff --git a/Test/include/invokeCommand.Mock.ps1 b/Test/include/invokeCommand.Mock.ps1 index cd9cf19..8daeaca 100644 --- a/Test/include/invokeCommand.Mock.ps1 +++ b/Test/include/invokeCommand.Mock.ps1 @@ -2,17 +2,18 @@ # INVOKE COMMAND MOCK # # This includes help commands to mock invokes in a test module -# You need to set the following variables -# $MODULE_INVOKATION_TAG : name of the module that you are testing. This needs to match with the Tag used in the module you are testing. -# $MODULE_INVOKATION_TAG_MOCK : Tag for the mock functions on the testing moodule you are loading this include in -# MOCK_PATH : path to the mocks folder. This is where the mock files will be saved and loaded from. # -# Sample: -# $MODULE_INVOKATION_TAG = "SfHelperModule" -# $MODULE_INVOKATION_TAG_MOCK = "SfHelperModule-Mock" -# $MOCK_PATH = $PSScriptRoot | Split-Path -Parent | Join-Path -ChildPath 'private' -AdditionalChildPath 'mocks' +# THIS INCLUDE REQURED module.helper.ps1 +if(-not $MODULE_NAME){ throw "Missing MODULE_NAME varaible initialization. Check for module.helerp.ps1 file." } +if(-not $MODULE_ROOT_PATH){ throw "Missing MODULE_ROOT_PATH varaible initialization. Check for module.helerp.ps1 file." } +$testRootPath = $MODULE_ROOT_PATH | Join-Path -ChildPath 'Test' +$MOCK_PATH = $testRootPath | Join-Path -ChildPath 'private' -AdditionalChildPath 'mocks' + +$MODULE_INVOKATION_TAG = "$($MODULE_NAME)Module" +$MODULE_INVOKATION_TAG_MOCK = "$($MODULE_INVOKATION_TAG)_Mock" + function Set-InvokeCommandMock{ [CmdletBinding()] param( @@ -21,7 +22,7 @@ function Set-InvokeCommandMock{ ) InvokeHelper\Set-InvokeCommandAlias -Alias $Alias -Command $Command -Tag $MODULE_INVOKATION_TAG_MOCK -} +} function Reset-InvokeCommandMock{ [CmdletBinding()] @@ -32,6 +33,10 @@ function Reset-InvokeCommandMock{ # Disable all dependecies of the library Disable-InvokeCommandAlias -Tag $MODULE_INVOKATION_TAG + + # Clear Enviroment variables used + Get-Variable -scope Global -Name "$($MODULE_INVOKATION_TAG_MOCK)_*" | Remove-Variable -Force -Scope Global + } Export-ModuleMember -Function Reset-InvokeCommandMock function Enable-InvokeCommandAliasModule{ @@ -52,6 +57,19 @@ function MockCall{ Set-InvokeCommandMock -Alias $command -Command "Get-MockFileContent -filename $filename" } +function MockCallAsync{ + param( + [Parameter(Position=0)][string] $command, + [Parameter(Position=1)][string] $filename + ) + + Assert-MockFileNotfound $fileName + + $moduleTest = $PSScriptRoot | Split-Path -Parent | Convert-Path + + Set-InvokeCommandMock -Alias $command -Command "Import-Module $moduleTest ; Get-MockFileContent -filename $filename" +} + function MockCallJson{ param( [Parameter(Position=0)][string] $command, @@ -64,6 +82,20 @@ function MockCallJson{ Set-InvokeCommandMock -Alias $command -Command "Get-MockFileContentJson -filename $filename" } +function MockCallJsonAsync{ + param( + [Parameter(Position=0)][string] $command, + [Parameter(Position=1)][string] $filename + + ) + + Assert-MockFileNotfound $fileName + + $moduleTest = $PSScriptRoot | Split-Path -Parent | Convert-Path + + Set-InvokeCommandMock -Alias $command -Command "Import-Module $moduleTest ; Get-MockFileContentJson -filename $filename" +} + function Get-MockFileFullPath{ param( [parameter(Mandatory,Position=0)][string] $fileName @@ -112,6 +144,21 @@ function MockCallToString{ Set-InvokeCommandMock -Alias $command -Command $outputstring } + +function MockCallToObject{ + param( + [Parameter(Position=0)][string] $command, + [Parameter(Position=1)][object] $OutObject + ) + + $random = [System.Guid]::NewGuid().ToString() + $varName = "$MODULE_INVOKATION_TAG_MOCK" + "_$random" + + Set-Variable -Name $varName -Value $OutObject -Scope Global + + Set-InvokeCommandMock -Alias $command -Command "(Get-Variable -Name $varName -Scope Global).Value" +} + function MockCallToNull{ param( [Parameter(Position=0)][string] $command @@ -133,6 +180,21 @@ function MockCallThrow{ Set-InvokeCommandMock -Alias $command -Command $mockCommand } +function MockCallExpression{ + param( + [Parameter(Position=0)][string] $command, + [Parameter(Position=1)][string] $expression + ) + + $mockCommand = @' + Invoke-Expression -Command '{expression}' +'@ + $mockCommand = $mockCommand -replace "{expression}", $expression + + Set-InvokeCommandMock -Alias $command -Command $expression +} + + function Save-InvokeAsMockFile{ param( [Parameter(Mandatory=$true)] [string]$Command, @@ -144,8 +206,8 @@ function Save-InvokeAsMockFile{ $result = Invoke-Expression -Command $Command - $json = $result | ConvertTo-Json -Depth 100 - + $json = $result | ConvertTo-Json -Depth 100 + $json | Out-File -FilePath $filePath Write-Host $FileName @@ -185,4 +247,5 @@ function Assert-MockFileNotfound{ Wait-Debugger throw "File not found or wrong case name. Expected[ $filename ] - Found[$( $file.name )]" } -} \ No newline at end of file +} + diff --git a/deploy.ps1 b/deploy.ps1 index bf66cfb..c6a5c21 100644 --- a/deploy.ps1 +++ b/deploy.ps1 @@ -79,3 +79,4 @@ if ( [string]::IsNullOrWhiteSpace($NuGetApiKey) ) { # Deploy module to PSGallery Invoke-DeployModuleToPSGallery -NuGetApiKey $NuGetApiKey -Force -ModuleManifestPath $MODULE_PSD1 + diff --git a/helper/module.helper.ps1 b/helper/module.helper.ps1 new file mode 100644 index 0000000..7fd1786 --- /dev/null +++ b/helper/module.helper.ps1 @@ -0,0 +1,171 @@ +# Helper for module variables + +function Find-ModuleRootPath{ + [CmdletBinding()] + param( + [Parameter(Mandatory,ValueFromPipeline,Position = 0)] + [string]$Path + ) + + $path = Convert-Path -Path $Path + + while (-not [string]::IsNullOrWhiteSpace($Path)){ + $psd1 = Get-ChildItem -Path $Path -Filter *.psd1 | Select-Object -First 1 + + if ($psd1 | Test-Path) { + + if($psd1.BaseName -eq "Test"){ + #foudn testing module. Continue + $path = $path | Split-Path -Parent + continue + } + + # foudn module + return $path + } + # folder without psd1 file + $path = $path | Split-Path -Parent + } + + # Path is null. Reached driver root. Module not found + return $null +} + +$MODULE_ROOT_PATH = $PSScriptRoot | Find-ModuleRootPath +$MODULE_NAME = (Get-ChildItem -Path $MODULE_ROOT_PATH -Filter *.psd1 | Select-Object -First 1).BaseName + +# Helper for module variables + + +$VALID_FOLDER_NAMES = @('Include', 'Private', 'Public', 'Root', 'TestInclude', 'TestPrivate', 'TestPublic', 'TestRoot', 'Tools', 'DevContainer', 'WorkFlows', 'GitHub', 'Helper', 'Config', 'TestHelper', 'TestConfig') + +class ValidFolderNames : System.Management.Automation.IValidateSetValuesGenerator { + [String[]] GetValidValues() { + return $script:VALID_FOLDER_NAMES + } +} + +function Get-Ps1FullPath{ + [CmdletBinding()] + param( + [Parameter(Mandatory,Position = 0)][string]$Name, + [Parameter(Position = 1)][ValidateSet([ValidFolderNames])][string]$FolderName, + [Parameter(Position = 0)][string]$ModuleRootPath + ) + + # If folderName is not empty + if($FolderName -ne $null){ + $folder = Get-ModuleFolder -FolderName $FolderName -ModuleRootPath $ModuleRootPath + $path = $folder | Join-Path -ChildPath $Name + } else { + $path = $Name + } + + # Check if file exists + if(-Not (Test-Path $path)){ + throw "File $path not found" + } + + # Get Path item + $item = Get-item -Path $path + + return $item +} +function Get-ModuleRootPath{ + [CmdletBinding()] + param( + [Parameter(Position = 0)][string]$ModuleRootPath + ) + + # if ModuleRootPath is not provided, default to local module path + if([string]::IsNullOrWhiteSpace($ModuleRootPath)){ + $ModuleRootPath = $MODULE_ROOT_PATH + } + + # Convert to full path + $ModuleRootPath = Convert-Path -Path $ModuleRootPath + + return $ModuleRootPath +} + +function Get-ModuleName{ + [CmdletBinding()] + param( + [Parameter(Position = 0)] [string]$ModuleRootPath + ) + + $ModuleRootPath = Get-ModuleRootPath -ModuleRootPath $ModuleRootPath + + $MODULE_NAME = (Get-ChildItem -Path $MODULE_ROOT_PATH -Filter *.psd1 | Select-Object -First 1).BaseName + + + return $MODULE_NAME +} + +function Get-ModuleFolder{ + [CmdletBinding()] + param( + [Parameter(Mandatory,Position = 0)][ValidateSet([ValidFolderNames])][string]$FolderName, + [Parameter(Position = 1)][string]$ModuleRootPath + ) + + $ModuleRootPath = Get-ModuleRootPath -ModuleRootPath $ModuleRootPath + + # TestRootPath + $testRootPath = $ModuleRootPath | Join-Path -ChildPath "Test" + + switch ($FolderName){ + 'Public'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath "public" + } + 'Private'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath "private" + } + 'Include'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath "include" + } + 'TestInclude'{ + $moduleFolder = $testRootPath | Join-Path -ChildPath "include" + } + 'TestPrivate'{ + $moduleFolder = $testRootPath | Join-Path -ChildPath "private" + } + 'TestPublic'{ + $moduleFolder = $testRootPath | Join-Path -ChildPath "public" + } + 'Root'{ + $moduleFolder = $ModuleRootPath + } + 'TestRoot'{ + $moduleFolder = $testRootPath + } + 'Tools'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath "tools" + } + 'DevContainer'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath ".devcontainer" + } + 'WorkFlows'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath ".github/workflows" + } + 'GitHub'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath ".github" + } + 'Helper'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath "helper" + } + 'Config'{ + $moduleFolder = $ModuleRootPath | Join-Path -ChildPath "config" + } + 'TestHelper'{ + $moduleFolder = $testRootPath | Join-Path -ChildPath "helper" + } + 'TestConfig'{ + $moduleFolder = $testRootPath | Join-Path -ChildPath "config" + } + default{ + throw "Folder [$FolderName] is unknown" + } + } + return $moduleFolder +} Export-ModuleMember -Function Get-ModuleFolder diff --git a/include/databaseV2.ps1 b/include/databaseV2.ps1 index 4fa8d1c..8be4f0e 100644 --- a/include/databaseV2.ps1 +++ b/include/databaseV2.ps1 @@ -11,38 +11,16 @@ # to use the store path, mocked or not, to create the final store file name. # All functions of this ps1 will depend on `GetDatabaseFile` for functionality. # -# TODO : Create a related public ps1 -# 1. define $DB_INVOKE_GET_ROOT_PATH_ALIAS. Make it unique. -# 2. define $DB_INVOKE_GET_ROOT_PATH_CMD. Point to the invoke function that calls GetConfigRootPath to get the store path -# -# Sample code (replace "MyModule" with a unique module prefix): -# $DB_INVOKE_GET_ROOT_PATH_ALIAS = "SfGetDbRootPath" -# $DB_INVOKE_GET_ROOT_PATH_CMD = "Invoke-SfGetDbRootPath" -# -# Set-MyInvokeCommandAlias -Alias $DB_INVOKE_GET_ROOT_PATH_ALIAS -Command $DB_INVOKE_GET_ROOT_PATH_CMD -# -# function Invoke-SfGetDbRootPath{ -# [CmdletBinding()] -# param() -# -# $databaseRoot = GetDatabaseRootPath -# return $databaseRoot -# -# } Export-ModuleMember -Function Invoke-SfGetDbRootPath - -$moduleRootPath = $PSScriptRoot | Split-Path -Parent -$moduleName = (Get-ChildItem -Path $moduleRootPath -Filter *.psd1 | Select-Object -First 1).BaseName -$DATABASE_ROOT = [System.Environment]::GetFolderPath('UserProfile') | Join-Path -ChildPath ".helpers" -AdditionalChildPath $moduleName, "databaseCache" - -# Create the database root if it does not exist -if(-Not (Test-Path $DATABASE_ROOT)){ - New-Item -Path $DATABASE_ROOT -ItemType Directory -} -Set-MyInvokeCommandAlias -Alias $DB_INVOKE_GET_ROOT_PATH_ALIAS -Command "Invoke-$DB_INVOKE_GET_ROOT_PATH_ALIAS" +$MODULE_ROOT_PATH = $PSScriptRoot | Split-Path -Parent +$MODULE_NAME = (Get-ChildItem -Path $MODULE_ROOT_PATH -Filter *.psd1 | Select-Object -First 1).BaseName +$DATABASE_ROOT = [System.Environment]::GetFolderPath('UserProfile') | Join-Path -ChildPath ".helpers" -AdditionalChildPath $MODULE_NAME, "databaseCache" -if(-not (Test-Path -Path function:"Invoke-$DB_INVOKE_GET_ROOT_PATH_ALIAS")){ +$DB_INVOKE_GET_ROOT_PATH_ALIAS = "$($MODULE_NAME)GetDbRootPath" +$function = "Invoke-$($MODULE_NAME)GetDbRootPath" +if(-not (Test-Path -Path function:$function)){ + # PUBLIC FUNCTION function Invoke-MyModuleGetDbRootPath{ [CmdletBinding()] @@ -52,13 +30,14 @@ if(-not (Test-Path -Path function:"Invoke-$DB_INVOKE_GET_ROOT_PATH_ALIAS")){ return $databaseRoot } - Rename-Item -path Function:Invoke-MyModuleGetDbRootPath -NewName "Invoke-$DB_INVOKE_GET_ROOT_PATH_ALIAS" - Export-ModuleMember -Function "Invoke-$DB_INVOKE_GET_ROOT_PATH_ALIAS" + Rename-Item -path Function:Invoke-MyModuleGetDbRootPath -NewName $function + Export-ModuleMember -Function $function + Set-MyInvokeCommandAlias -Alias $DB_INVOKE_GET_ROOT_PATH_ALIAS -Command $function } # Extra functions not needed by INCLUDE DATABASE V2 - -if(-not (Test-Path -Path function:"Reset-$DB_INVOKE_GET_ROOT_PATH_ALIAS")){ +$function = "Reset-$($MODULE_NAME)DatabaseStore" +if(-not (Test-Path -Path function:$function)){ function Reset-MyModuleDatabaseStore{ [CmdletBinding()] param() @@ -70,8 +49,9 @@ if(-not (Test-Path -Path function:"Reset-$DB_INVOKE_GET_ROOT_PATH_ALIAS")){ New-Item -Path $databaseRoot -ItemType Directory } - Rename-Item -path Function:Reset-MyModuleDatabaseStore -NewName "Reset-$($moduleName)DatabaseStore" - Export-ModuleMember -Function "Reset-$($moduleName)DatabaseStore" + + Rename-Item -path Function:Reset-MyModuleDatabaseStore -NewName $function + Export-ModuleMember -Function $function } # PRIVATE FUNCTIONS @@ -89,11 +69,6 @@ function GetDatabaseFile{ [Parameter(Position = 0)][string]$Key ) - # throw if DB_INVOKE_GET_ROOT_PATH_ALIAS is not set - if (-not $DB_INVOKE_GET_ROOT_PATH_ALIAS) { - throw "DB_INVOKE_GET_ROOT_PATH_ALIAS is not set. Please set it before calling GetDatabaseFile." - } - $databaseRoot = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_ALIAS $path = $databaseRoot | Join-Path -ChildPath "$Key.json" @@ -156,4 +131,4 @@ function Test-DatabaseKey{ # TODO: Return $false if cache has expired return $true -} \ No newline at end of file +} diff --git a/include/getHashCode.ps1 b/include/getHashCode.ps1 index 830a869..4b5f0bb 100644 --- a/include/getHashCode.ps1 +++ b/include/getHashCode.ps1 @@ -23,4 +23,4 @@ function Get-HashCode { return $hashString } -} \ No newline at end of file +} diff --git a/release.ps1 b/release.ps1 index a97de5f..1a35f1a 100644 --- a/release.ps1 +++ b/release.ps1 @@ -67,3 +67,4 @@ if ($PSCmdlet.ShouldProcess($VersionTag, "gh release create")) { gh release create $VersionTag --generate-notes --verify-tag --title "Release $VersionTag (PreRelease)" --prerelease } } + diff --git a/sync.ps1 b/sync.ps1 index 09f7fd3..82ebdbf 100644 --- a/sync.ps1 +++ b/sync.ps1 @@ -31,3 +31,4 @@ Save-UrlContentToFile -File 'sync.ps1' -Folder $MODULE_PATH Save-UrlContentToFile -File 'release.ps1' -Folder $MODULE_PATH -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.release.ps1' Save-UrlContentToFile -File 'test.ps1' -Folder $MODULE_PATH -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.test.ps1' + diff --git a/test.ps1 b/test.ps1 index d34447b..b477ff6 100644 --- a/test.ps1 +++ b/test.ps1 @@ -141,4 +141,4 @@ if($TestName){ Invoke-TestingHelper -TestName $TestName -ShowTestErrors:$ShowTestErrors } else { Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -} \ No newline at end of file +} diff --git a/tools/deploy.Helper.ps1 b/tools/deploy.Helper.ps1 index 16eeea5..0d5aefd 100644 --- a/tools/deploy.Helper.ps1 +++ b/tools/deploy.Helper.ps1 @@ -145,3 +145,4 @@ function Get-DeployModulePreRelease { $preRelease } + diff --git a/tools/sync.Helper.ps1 b/tools/sync.Helper.ps1 index d7ea2e1..e953ac1 100644 --- a/tools/sync.Helper.ps1 +++ b/tools/sync.Helper.ps1 @@ -59,3 +59,4 @@ function Save-UrlContentToFile { Set-Content -Path $filePath -Value $fileContent Write-Information -MessageData "Saved content to [$filePath] from [$url]" } + From 2c0efd35cb540a7b8ef95eb9f5fa7f863161967e Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 8 Aug 2025 12:21:05 +0200 Subject: [PATCH 2/2] chore: remove sync scripts for TestingHelper templates --- sync.ps1 | 34 ------------------------ tools/sync.Helper.ps1 | 62 ------------------------------------------- 2 files changed, 96 deletions(-) delete mode 100644 sync.ps1 delete mode 100644 tools/sync.Helper.ps1 diff --git a/sync.ps1 b/sync.ps1 deleted file mode 100644 index 82ebdbf..0000000 --- a/sync.ps1 +++ /dev/null @@ -1,34 +0,0 @@ -<# -.SYNOPSIS - Synchronizes with TestingHelper templates files - -.DESCRIPTION - Synchronizes with TestingHelper templates to the local repo. - TestingHelper uses templates to create a new module. - This script will update the local module with the latest templates. -.LINK - https://raw.githubusercontent.com/rulasg/TestingHelper/main/sync.ps1 -#> - -[cmdletbinding(SupportsShouldProcess)] -param() - -$MODULE_PATH = $PSScriptRoot -$TOOLS_PATH = $MODULE_PATH | Join-Path -ChildPath "tools" -$WORKFLOW_PATH = $MODULE_PATH | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" - -. ($TOOLS_PATH | Join-Path -ChildPath "sync.Helper.ps1") - -Save-UrlContentToFile -File 'deploy_module_on_release.yml' -Folder $WORKFLOW_PATH -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy_module_on_release.yml' -Save-UrlContentToFile -File 'powershell.yml' -Folder $WORKFLOW_PATH -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.powershell.yml' -Save-UrlContentToFile -File 'test_with_TestingHelper.yml' -Folder $WORKFLOW_PATH -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.test_with_TestingHelper.yml' - -Save-UrlContentToFile -File 'deploy.Helper.ps1' -Folder $TOOLS_PATH -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy.Helper.ps1' -Save-UrlContentToFile -File 'deploy.ps1' -Folder $MODULE_PATH -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy.ps1' - -Save-UrlContentToFile -File 'sync.Helper.ps1' -Folder $TOOLS_PATH -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.sync.Helper.ps1' -Save-UrlContentToFile -File 'sync.ps1' -Folder $MODULE_PATH -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.sync.ps1' - -Save-UrlContentToFile -File 'release.ps1' -Folder $MODULE_PATH -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.release.ps1' -Save-UrlContentToFile -File 'test.ps1' -Folder $MODULE_PATH -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.test.ps1' - diff --git a/tools/sync.Helper.ps1 b/tools/sync.Helper.ps1 deleted file mode 100644 index e953ac1..0000000 --- a/tools/sync.Helper.ps1 +++ /dev/null @@ -1,62 +0,0 @@ -<# -.SYNOPSIS - Helper functions to Synchronize TestingHelper templates files - -.DESCRIPTION - Helper functions Synchronize TestingHelper templates to the local repo. - TestingHelper uses templates to create a new module. - This script will update the local module with the latest templates. -.LINK - https://raw.githubusercontent.com/rulasg/DemoPsModule/main/sync.ps1 -#> - -[cmdletbinding()] -param() - -function Get-UrlContent { - [cmdletbinding()] - param( - [Parameter(Mandatory=$true)][string]$url - ) - $wc = New-Object -TypeName System.Net.WebClient - $fileContent = $wc.DownloadString($url) - - return $fileContent -} - -function Out-ContentToFile { - [cmdletbinding(SupportsShouldProcess)] - param( - [Parameter(ValueFromPipeline)][string]$content, - [Parameter(Mandatory=$true)][string]$filePath - ) - - process{ - - if ($PSCmdlet.ShouldProcess($filePath, "Save content [{0}] to file" -f $content.Length)) { - $content | Out-File -FilePath $filePath -Force - } - } -} - -function Save-UrlContentToFile { - [cmdletbinding(SupportsShouldProcess)] - param( - [Parameter(Mandatory=$true)][string]$Url, - [Parameter(Mandatory=$true)][string]$File, - [Parameter()][string]$Folder - ) - - $fileContent = Get-UrlContent -Url $url - - if ([string]::IsNullOrWhiteSpace($fileContent)) { - Write-Error -Message "Content from [$url] is empty" - return - } - - $filePath = $Folder ? (Join-Path -Path $Folder -ChildPath $File) : $File - - Set-Content -Path $filePath -Value $fileContent - Write-Information -MessageData "Saved content to [$filePath] from [$url]" -} -