Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Test/include/database.mock.ps1
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{

Check warning

Code scanning / PSScriptAnalyzer

Function 'Reset-DatabaseStore' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'. Warning

Function 'Reset-DatabaseStore' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.
[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
}
7 changes: 1 addition & 6 deletions Test/private/run_BeforeAfter.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
function Run_BeforeEach{
# Write-Verbose "Run_BeforeEach"
Reset-InvokeCommandMock

Invoke-PrivateContext {
# Clear the repo list cache to ensure tests are isolated
$script:projectlist = $null
$script:repoList = @{}
}
Mock_Database
}

# function Run_AfterEach{
Expand Down
4 changes: 2 additions & 2 deletions Test/public/projects/GcProjects.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ function Test_GetGcProjects {
Assert-Count -Expected 16 -Presented $projects

# Pick one random and check the structure
$testProject = $projects."bit21"
Assert-AreEqual -Expected "bit21" -Presented $testProject.Title
$testProject = $projects."BiT21"
Assert-AreEqual -Expected "BiT21" -Presented $testProject.Title
Assert-AreEqual -Expected "githubcustomers" -Presented $testProject.Owner
Assert-AreEqual -Expected 2683 -Presented $testProject.ProjectNumber
Assert-AreEqual -Expected "https://github.com/orgs/githubcustomers/projects/2683" -Presented $testProject.Url
Expand Down
172 changes: 172 additions & 0 deletions include/databaseV2.ps1
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 warning

Code 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 warning

Code 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 notice

Code 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 notice

Code 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.
}
130 changes: 130 additions & 0 deletions include/openFilesUrls.ps1
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: $_"
}
}
}
Loading
Loading