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
4 changes: 2 additions & 2 deletions Test/public/getNotes.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions Test/public/newNotes.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
212 changes: 212 additions & 0 deletions include/config.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
# 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_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." }

$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











12 changes: 10 additions & 2 deletions private/resolveNotesPath.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

Set-MyInvokeCommandAlias -Alias GetNotesRoot 'echo $NOTES_ROOT'
Set-MyInvokeCommandAlias -Alias GetNotesRoot 'Invoke-NotesHelperNotesRoot'

function Resolve-NotesPath {
[CmdletBinding()]
Expand Down Expand Up @@ -67,4 +67,12 @@ function New-FolderIfNotExists{
}

return $Path
}
}

function Invoke-NotesHelperNotesRoot{
[CmdletBinding()]
param()

$config = Get-NotesHelperConfig
return $config.NotesRoot
} Export-ModuleMember -Function Invoke-NotesHelperNotesRoot