diff --git a/Test/include/config.mock.ps1 b/Test/include/config.mock.ps1 index 53536fe..4a131ef 100644 --- a/Test/include/config.mock.ps1 +++ b/Test/include/config.mock.ps1 @@ -1,4 +1,5 @@ +$CONFIG_INVOKE_GET_ROOT_PATH_CMD = "Invoke-SfGetConfigRootPath" function Mock_Config{ param( @@ -23,6 +24,6 @@ function Mock_Config{ } # Mock invoke call - MockCallToString "Invoke-GetConfigRootPath" -OutString $MOCK_CONFIG_PATH + MockCallToString $CONFIG_INVOKE_GET_ROOT_PATH_CMD -OutString $MOCK_CONFIG_PATH } \ No newline at end of file diff --git a/Test/include/database.mock.ps1 b/Test/include/database.mock.ps1 index 3a0e927..5c5ed6f 100644 --- a/Test/include/database.mock.ps1 +++ b/Test/include/database.mock.ps1 @@ -1,6 +1,9 @@ + +$DB_INVOKE_GET_ROOT_PATH_CMD = "Invoke-SfGetDbRootPath" + function Mock_Database([switch]$ResetDatabase){ - MockCallToString "Invoke-GetDatabaseStorePath" -OutString "test_database_path" + MockCallToString $DB_INVOKE_GET_ROOT_PATH_CMD -OutString "test_database_path" if($ResetDatabase){ Reset-DatabaseStore diff --git a/Test/public/sfDataQuery.test.ps1 b/Test/public/sfDataQuery.test.ps1 index 54dd671..8fd9530 100644 --- a/Test/public/sfDataQuery.test.ps1 +++ b/Test/public/sfDataQuery.test.ps1 @@ -5,7 +5,7 @@ function Test_GetSfAccount{ $mockAttrib = @{attributes = @("Potential_Seats_Manual__c","Website","PhotoUrl")} Mock_Config -Config $mockAttrib - $dbstore = Invoke-MyCommand -Command GetDatabaseStorePath + $dbstore = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_CMD Assert-AreEqual -Expected "test_database_path" -Presented $dbstore $attrib = "Id,Name,OwnerId,Industry,Account_Owner__c,Account_Segment__c,Account_Owner_Role__c,Account_Tier__c,Potential_Seats__c,Country_Name__c,Current_Seats__c,Current_ARR_10__c,Salesforce_Record_URL__c,Potential_Seats_Manual__c,Website,PhotoUrl" diff --git a/include/config.ps1 b/include/config.ps1 index 19d2d30..f446200 100644 --- a/include/config.ps1 +++ b/include/config.ps1 @@ -1,6 +1,31 @@ # Configuration management module -Set-MyInvokeCommandAlias -Alias GetConfigRootPath -Command "Invoke-GetConfigRootPath" +# 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. +# +# TODO : Create a related public ps1 + +# Create a related public ps1 +# 1. define $CONFIG_INVOKE_GET_ROOT_PATH_ALIAS. Make it unique. +# 2. define $CONFIG_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): +# $CONFIG_INVOKE_GET_ROOT_PATH_ALIAS = "MyModuleGetConfigRootPath" +# $CONFIG_INVOKE_GET_ROOT_PATH_CMD = "Invoke-MyModuleGetConfigRootPath" +# +# Set-MyInvokeCommandAlias -Alias $CONFIG_INVOKE_GET_ROOT_PATH_ALIAS -Command $CONFIG_INVOKE_GET_ROOT_PATH_CMD +# +# function Invoke-MyModuleGetConfigRootPath{ +# $configRoot = GetConfigRootPath +# return $configRoot +# } Export-ModuleMember -Function Invoke-MyModuleGetConfigRootPath + $moduleName = Get-ModuleName $CONFIG_ROOT = [System.Environment]::GetFolderPath('UserProfile') | Join-Path -ChildPath ".helpers" -AdditionalChildPath $moduleName, "config" @@ -10,13 +35,13 @@ if(-Not (Test-Path $CONFIG_ROOT)){ New-Item -Path $CONFIG_ROOT -ItemType Directory } -function Invoke-GetConfigRootPath { +function GetConfigRootPath { [CmdletBinding()] param() $configRoot = $CONFIG_ROOT return $configRoot -} Export-ModuleMember -Function Invoke-GetConfigRootPath +} function GetConfigFile { [CmdletBinding()] @@ -24,7 +49,7 @@ function GetConfigFile { [Parameter(Mandatory = $true, Position = 0)][string]$Key ) - $configRoot = Invoke-MyCommand -Command GetConfigRootPath + $configRoot = Invoke-MyCommand -Command $CONFIG_INVOKE_GET_ROOT_PATH_ALIAS $path = Join-Path -Path $configRoot -ChildPath "$Key.json" return $path } diff --git a/private/dependencies/databaseV2.ps1 b/include/databaseV2.ps1 similarity index 54% rename from private/dependencies/databaseV2.ps1 rename to include/databaseV2.ps1 index 305ae37..02a009c 100644 --- a/private/dependencies/databaseV2.ps1 +++ b/include/databaseV2.ps1 @@ -1,7 +1,34 @@ # 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. +# +# 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 + # Invoke to allow mockig the store path on testing -Set-MyInvokeCommandAlias -Alias GetDatabaseStorePath -Command "Invoke-GetDatabaseStorePath" $moduleName = Get-ModuleName $DATABASE_ROOT = [System.Environment]::GetFolderPath('UserProfile') | Join-Path -ChildPath ".helpers" -AdditionalChildPath $moduleName, "databaseCache" @@ -11,27 +38,26 @@ if(-Not (Test-Path $DATABASE_ROOT)){ New-Item -Path $DATABASE_ROOT -ItemType Directory } -function Reset-DatabaseStore{ +function GetDatabaseRootPath { [CmdletBinding()] param() - $databaseRoot = Invoke-MyCommand -Command GetDatabaseStorePath - - Remove-Item -Path $databaseRoot -Recurse -Force -ErrorAction SilentlyContinue - - New-Item -Path $databaseRoot -ItemType Directory - -} Export-ModuleMember -Function Reset-DatabaseStore + $databaseRoot = $DATABASE_ROOT + return $databaseRoot +} -function Get-DatabaseStore{ +function GetDatabaseFile{ [CmdletBinding()] - param() + param( + [Parameter(Position = 0)][string]$Key + ) - $databaseRoot = Invoke-MyCommand -Command GetDatabaseStorePath - - return $databaseRoot + $databaseRoot = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_ALIAS + + $path = $databaseRoot | Join-Path -ChildPath "$Key.json" -} Export-ModuleMember -Function Get-DatabaseStore + return $path +} function Get-Database{ [CmdletBinding()] @@ -90,24 +116,3 @@ function Test-Database{ return $true } -function GetDatabaseFile{ - [CmdletBinding()] - param( - [Parameter(Position = 0)][string]$Key - ) - - $databaseRoot = Invoke-MyCommand -Command GetDatabaseStorePath - - $path = $databaseRoot | Join-Path -ChildPath "$Key.json" - - return $path -} - -function Invoke-GetDatabaseStorePath{ - [CmdletBinding()] - param() - - $databaseRoot = $DATABASE_ROOT - - return $databaseRoot -} Export-ModuleMember -Function Invoke-GetDatabaseStorePath \ No newline at end of file diff --git a/public/sfDatabase.ps1 b/public/sfDatabase.ps1 new file mode 100644 index 0000000..6bb48ad --- /dev/null +++ b/public/sfDatabase.ps1 @@ -0,0 +1,28 @@ + + +$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 + + +function Reset-DatabaseStore{ + [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 + +} Export-ModuleMember -Function Reset-DatabaseStore \ No newline at end of file diff --git a/public/sfconfig.ps1 b/public/sfconfig.ps1 new file mode 100644 index 0000000..0a496fd --- /dev/null +++ b/public/sfconfig.ps1 @@ -0,0 +1,74 @@ + +$CONFIG_INVOKE_GET_ROOT_PATH_ALIAS = "SfGetConfigRootPath" +$CONFIG_INVOKE_GET_ROOT_PATH_CMD = "Invoke-SfGetConfigRootPath" + +Set-MyInvokeCommandAlias -Alias $CONFIG_INVOKE_GET_ROOT_PATH_ALIAS -Command $CONFIG_INVOKE_GET_ROOT_PATH_CMD + +function Invoke-SfGetConfigRootPath{ + $configRoot = GetConfigRootPath + return $configRoot +} Export-ModuleMember -Function Invoke-SfGetConfigRootPath + + +function Get-SfConfig{ + [CmdletBinding()] + param() + + $config = Get-Configuration + + return $config +} Export-ModuleMember -Function Get-SfConfig + +function Save-SfConfig{ + [CmdletBinding()] + param( + [Parameter(Mandatory, ValueFromPipeline, Position = 0)][Object]$Config + ) + + return Save-Configuration -Config $Config +} Export-ModuleMember -Function Save-SfConfig + +function Open-SfConfig{ + [CmdletBinding()] + param() + + $path = GetConfigFile -Key "config" + + code $path + +} Export-ModuleMember -Function Open-SfConfig + +function Add-SfConfigAttribute{ + [CmdletBinding()] + param( + [Parameter(Mandatory, ValueFromPipeline, Position = 0)][string]$Attribute + ) + + begin{ + $config = Get-Configuration + + if(-Not $config){ + $config = @{} + } + + if(-Not $config.attributes){ + $config.attributes = @() + } + } + + process{ + $config.attributes += $Attribute + } + + End{ + $ret = Save-Configuration -Config $config + if(-Not $ret){ + throw "Error saving configuration" + } + + $config = Get-SfConfig + Write-Output $config.attributes + + } + +} Export-ModuleMember -Function Add-SfConfigAttribute