-
Notifications
You must be signed in to change notification settings - Fork 0
feat(database): implement project and repository management functions #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4647fbf
1dd79a7
5a9f068
d00c340
b97f85a
b5bad3f
615a99b
0905ae8
984f54d
af97a8f
d98700d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # DATABASE MOCK | ||
| # | ||
| # 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. | ||
| # | ||
| # 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-$($MODULE_NAME)GetDbRootPath" | ||
| $MOCK_DATABASE_PATH = "test_database_path" | ||
|
|
||
| function Mock_Database([switch]$ResetDatabase){ | ||
|
|
||
| MockCallToString $DB_INVOKE_GET_ROOT_PATH_CMD -OutString $MOCK_DATABASE_PATH | ||
|
|
||
| $dbstore = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_CMD | ||
| Assert-AreEqual -Expected $MOCK_DATABASE_PATH -Presented $dbstore | ||
|
|
||
| if($ResetDatabase){ | ||
| Reset-DatabaseStore | ||
| } | ||
|
|
||
| } | ||
|
|
||
| function Get-Mock_DatabaseStore{ | ||
| $dbstore = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_CMD | ||
| return $dbstore | ||
| } | ||
|
|
||
| function Reset-DatabaseStore{ | ||
| [CmdletBinding()] | ||
| param() | ||
|
|
||
| # Get actual store path | ||
| $databaseRoot = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_CMD | ||
|
|
||
| # Remove the database root directory | ||
| Remove-Item -Path $databaseRoot -Recurse -Force -ErrorAction SilentlyContinue | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| # DATABASE V2 | ||
| # | ||
| # Database driver to store the cache | ||
| # | ||
| # 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 `GetDatabaseRootPath` to use production root path | ||
| # Mock this Invoke function with Set-MyInvokeCommandAlias to set the Store elsewhere | ||
| # This ps1 has function `GetDatabaseFile` that will call `Invoke-MyCommand -Command $DB_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 `GetDatabaseFile` for functionality. | ||
| # | ||
|
|
||
| $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" | ||
|
|
||
| $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()] | ||
| param() | ||
|
|
||
| $databaseRoot = GetDatabaseRootPath | ||
| return $databaseRoot | ||
|
|
||
| } | ||
| 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 | ||
| $function = "Reset-$($MODULE_NAME)DatabaseStore" | ||
| if(-not (Test-Path -Path function:$function)){ | ||
| function Reset-MyModuleDatabaseStore{ | ||
Check warningCode scanning / PSScriptAnalyzer Function 'Reset-MyModuleDatabaseStore' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'. Warning
Function 'Reset-MyModuleDatabaseStore' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.
|
||
| [CmdletBinding()] | ||
| param() | ||
|
|
||
| $databaseRoot = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_ALIAS | ||
|
|
||
| Remove-Item -Path $databaseRoot -Recurse -Force -ErrorAction SilentlyContinue | ||
|
|
||
| New-Item -Path $databaseRoot -ItemType Directory | ||
|
|
||
| } | ||
|
|
||
| Rename-Item -path Function:Reset-MyModuleDatabaseStore -NewName $function | ||
| Export-ModuleMember -Function $function | ||
| } | ||
|
|
||
| # PRIVATE FUNCTIONS | ||
| function GetDatabaseRootPath { | ||
| [CmdletBinding()] | ||
| param() | ||
|
|
||
| $databaseRoot = $DATABASE_ROOT | ||
| return $databaseRoot | ||
| } | ||
|
|
||
| function GetDatabaseFile{ | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Mandatory, Position = 0)][string]$Key, | ||
| [Parameter(Position = 1)][ValidateSet("JSON","XML","TXT")][string]$DBFormat = "JSON" | ||
| ) | ||
|
|
||
| $databaseRoot = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_ALIAS | ||
|
|
||
| if(-not (Test-Path -Path $databaseRoot)){ | ||
| New-Item -Path $databaseRoot -ItemType Directory -Force | Out-Null | ||
| } | ||
|
|
||
| $ext = GetFileExtension -DbFormat $DBFormat | ||
|
|
||
| $path = $databaseRoot | Join-Path -ChildPath "$Key$ext" | ||
|
|
||
| return $path | ||
| } | ||
|
|
||
| function GetFileExtension{ | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Mandatory)][string]$DbFormat | ||
| ) | ||
|
|
||
| switch ($DbFormat.ToUpper()){ | ||
| "JSON" { $ret = ".json" ; Break } | ||
| "XML" { $ret = ".xml" ; Break } | ||
| "TXT" { $ret = ".txt" ; Break } | ||
| default { throw "Unsupported database format $DbFormat" } | ||
| } | ||
| return $ret | ||
| } | ||
|
|
||
| function Get-DatabaseKey{ | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Mandatory, Position = 0)][string]$Key, | ||
| [Parameter(Position = 1)][ValidateSet("JSON","XML","TXT")][string]$DBFormat = "JSON", | ||
| [Parameter()][switch]$AsHashtable | ||
| ) | ||
|
|
||
| if(-Not (Test-DatabaseKey $Key -DBFormat $DBFormat)){ | ||
| return $null | ||
| } | ||
|
|
||
| $path = GetDatabaseFile $Key -DBFormat $DBFormat | ||
|
|
||
| switch ($DBFormat) { | ||
| "JSON" { $ret = Get-Content $path | ConvertFrom-Json -AsHashtable:$AsHashtable ; Break } | ||
| "XML" { $ret = Import-Clixml -Path $path ; Break } | ||
| "TXT" { $ret = Get-Content $path ; Break } | ||
| default { throw "Unsupported database format $DbFormat" } | ||
| } | ||
|
|
||
| return $ret | ||
| } | ||
|
|
||
| function Reset-DatabaseKey{ | ||
Check warningCode scanning / PSScriptAnalyzer Function 'Reset-DatabaseKey' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'. Warning
Function 'Reset-DatabaseKey' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.
|
||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Mandatory, Position = 0)][string]$Key, | ||
| [Parameter(Position = 1)][ValidateSet("JSON","XML","TXT")][string]$DBFormat = "JSON" | ||
| ) | ||
| $path = GetDatabaseFile -Key $Key -DBFormat $DBFormat | ||
| Remove-Item -Path $path -Force -ErrorAction SilentlyContinue | ||
| return | ||
| } | ||
|
|
||
| function Save-DatabaseKey{ | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Mandatory, Position = 0)][string]$Key, | ||
| [Parameter(Mandatory, Position = 2)][Object]$Value, | ||
| [Parameter(Position = 3)][ValidateSet("JSON","XML","TXT")][string]$DbFormat = "JSON" | ||
| ) | ||
|
|
||
| $path = GetDatabaseFile -Key $Key -DBFormat $DbFormat | ||
|
|
||
| switch ($DbFormat) { | ||
| "JSON" { $Value | ConvertTo-Json -Depth 10 | Set-Content $path -Encoding UTF8 -Force ; Break } | ||
| "XML" { $Value | Export-Clixml -Path $path -Force ; Break } | ||
| "TXT" { $Value | Set-Content -Path $path -Encoding UTF8 -Force ; Break } | ||
| default { throw "Unsupported database format $DbFormat" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function Test-DatabaseKey{ | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Mandatory, Position = 0)][string]$Key, | ||
| [Parameter(Position = 1)][ValidateSet("JSON","XML","TXT")][string]$DBFormat = "JSON" | ||
| ) | ||
|
|
||
| $path = GetDatabaseFile -Key $Key -DBFormat $DBFormat | ||
|
|
||
| # Key file not exists | ||
| if(-Not (Test-Path $path)){ | ||
| return $false | ||
Check noticeCode scanning / PSScriptAnalyzer The cmdlet 'Test-DatabaseKey' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute. Note
The cmdlet 'Test-DatabaseKey' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute.
|
||
| } | ||
|
|
||
| # TODO: Return $false if cache has expired | ||
|
|
||
| return $true | ||
Check noticeCode scanning / PSScriptAnalyzer The cmdlet 'Test-DatabaseKey' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute. Note
The cmdlet 'Test-DatabaseKey' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute.
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
|
|
||
| # Include openFilesUrls.ps1 | ||
| # Provides controls to open files and URLs in the default system applications. | ||
| # Use $MODULE_NAME variable to set up functions names | ||
|
|
||
| Set-MyInvokeCommandAlias -Alias OpenUrl -Command $('Invoke-{modulename}OpenUrl -Url "{url}"' -replace "{modulename}", $MODULE_NAME) | ||
|
|
||
| function Invoke-ModuleNameOpenUrl{ | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Mandatory = $true)][string]$Url | ||
| ) | ||
|
|
||
| try { | ||
| # Determine the operating system | ||
| if ($IsWindows -or $env:OS -match "Windows") { | ||
| # Windows - use Start-Process | ||
| Start-Process $Url | ||
| } | ||
| elseif ($IsMacOS) { | ||
| # macOS - use open command | ||
| Start-Process "open" -ArgumentList $Url | ||
| } | ||
| elseif ($IsLinux) { | ||
| # Linux - try xdg-open | ||
| Start-Process "xdg-open" -ArgumentList $Url | ||
| } | ||
| else { | ||
| # Fallback for older PowerShell versions without OS variables | ||
| switch ([System.Environment]::OSVersion.Platform) { | ||
| "Win32NT" { | ||
| Start-Process $Url | ||
| } | ||
| "Unix" { | ||
| # Try to determine if macOS or Linux | ||
| if (Test-Path "/System/Library/CoreServices/Finder.app") { | ||
| # macOS | ||
| Start-Process "open" -ArgumentList $Url | ||
| } | ||
| else { | ||
| # Assume Linux | ||
| Start-Process "xdg-open" -ArgumentList $Url | ||
| } | ||
| } | ||
| default { | ||
| throw "Unsupported operating system" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| catch { | ||
| Write-Error "Failed to open URL: $_" | ||
| } | ||
| } | ||
| Copy-Item -path Function:Invoke-ModuleNameOpenUrl -Destination Function:"Invoke-$($MODULE_NAME)OpenUrl" | ||
| Export-ModuleMember -Function "Invoke-$($MODULE_NAME)OpenUrl" | ||
|
|
||
|
|
||
| function Open-Url { | ||
| [CmdletBinding()] | ||
| param ( | ||
| [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] | ||
| [ValidateNotNullOrEmpty()] | ||
| [string]$Url | ||
| ) | ||
|
|
||
| process { | ||
| Invoke-MyCommand -Command OpenUrl -Parameters @{url = $Url} | ||
| } | ||
| } | ||
|
|
||
| function Open-File { | ||
| [CmdletBinding()] | ||
| param ( | ||
| [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] | ||
| [ValidateNotNullOrEmpty()] | ||
| [string]$Path | ||
| ) | ||
|
|
||
| process { | ||
| try { | ||
| # Ensure the file exists | ||
| if (-not (Test-Path -Path $Path)) { | ||
| throw "File not found: $Path" | ||
| } | ||
|
|
||
| # Get absolute path | ||
| $absolutePath = (Resolve-Path -Path $Path).Path | ||
|
|
||
| # Determine the operating system | ||
| if ($IsWindows -or $env:OS -match "Windows") { | ||
| # Windows - use Invoke-Item | ||
| Invoke-Item -Path $absolutePath | ||
| } | ||
| elseif ($IsMacOS) { | ||
| # macOS - use open command | ||
| Start-Process "open" -ArgumentList $absolutePath | ||
| } | ||
| elseif ($IsLinux) { | ||
| # Linux - try xdg-open | ||
| Start-Process "xdg-open" -ArgumentList $absolutePath | ||
| } | ||
| else { | ||
| # Fallback for older PowerShell versions without OS variables | ||
| switch ([System.Environment]::OSVersion.Platform) { | ||
| "Win32NT" { | ||
| Invoke-Item -Path $absolutePath | ||
| } | ||
| "Unix" { | ||
| # Try to determine if macOS or Linux | ||
| if (Test-Path "/System/Library/CoreServices/Finder.app") { | ||
| # macOS | ||
| Start-Process "open" -ArgumentList $absolutePath | ||
| } | ||
| else { | ||
| # Assume Linux | ||
| Start-Process "xdg-open" -ArgumentList $absolutePath | ||
| } | ||
| } | ||
| default { | ||
| throw "Unsupported operating system" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| catch { | ||
| Write-Error "Failed to open file: $_" | ||
| } | ||
| } | ||
| } |
Check warning
Code scanning / PSScriptAnalyzer
Function 'Reset-DatabaseStore' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'. Warning