Public/Initialize-BudgetWorkspace.ps1
|
function Initialize-BudgetWorkspace { <# .SYNOPSIS Initializes the budget workspace structure. .DESCRIPTION Creates the budget workspace directory structure and preferences file. Default location is %USERPROFILE%\Documents\.mybudget Creates: - Workspace root directory - budgets\ subdirectory - preferences.json with default settings - Optionally creates an initial budget if -DefaultBudget is specified .PARAMETER WorkspacePath Custom workspace location. Defaults to Documents\.mybudget .PARAMETER DefaultBudget Name of a default budget to create during initialization. .PARAMETER Force Reinitialize workspace even if it already exists. .EXAMPLE Initialize-BudgetWorkspace Creates workspace at default location (Documents\.mybudget) .EXAMPLE Initialize-BudgetWorkspace -DefaultBudget "daily-expenses" Creates workspace and a default budget named "daily-expenses" .EXAMPLE Initialize-BudgetWorkspace -WorkspacePath "D:\MyBudgets" Creates workspace at custom location .OUTPUTS PSCustomObject - Workspace information #> [CmdletBinding(SupportsShouldProcess)] param( [Parameter()] [string]$WorkspacePath, [Parameter()] [ValidatePattern('^[a-zA-Z0-9\-_]+$')] [string]$DefaultBudget, [Parameter()] [switch]$Force ) # Determine workspace path if (-not $WorkspacePath) { $documentsPath = [Environment]::GetFolderPath('MyDocuments') $WorkspacePath = Join-Path $documentsPath '.mybudget' } # Check if already initialized $preferencesPath = Join-Path $WorkspacePath 'preferences.json' $budgetsPath = Join-Path $WorkspacePath 'budgets' if ((Test-Path $preferencesPath) -and -not $Force) { Write-Warning "Workspace already initialized at: $WorkspacePath" Write-Warning "Use -Force to reinitialize" $existingPrefs = Get-Preferences -WorkspacePath $WorkspacePath return $existingPrefs } if ($PSCmdlet.ShouldProcess($WorkspacePath, "Initialize budget workspace")) { # Create workspace directory structure Write-Verbose "Creating workspace at: $WorkspacePath" New-Item -Path $WorkspacePath -ItemType Directory -Force | Out-Null New-Item -Path $budgetsPath -ItemType Directory -Force | Out-Null # Create default preferences $preferences = [PSCustomObject]@{ workspacePath = $WorkspacePath defaultBudget = if ($DefaultBudget) { $DefaultBudget } else { $null } activeBudget = if ($DefaultBudget) { $DefaultBudget } else { $null } dateFormat = 'yyyy-MM-dd' currencySymbol = '$' version = '0.4.0' } # Save preferences if (Set-Preferences -Preferences $preferences -WorkspacePath $WorkspacePath) { Write-Verbose "Created preferences.json" } # Create default budget if specified if ($DefaultBudget) { Write-Verbose "Creating default budget: $DefaultBudget" $budgetPath = Join-Path $budgetsPath $DefaultBudget New-Item -Path $budgetPath -ItemType Directory -Force | Out-Null # Create budget metadata $metadata = [PSCustomObject]@{ name = $DefaultBudget description = "Default budget" created = (Get-Date).ToString('yyyy-MM-dd') lastModified = (Get-Date).ToString('yyyy-MM-dd') initialBalance = 0.00 tags = @('default') } Set-BudgetMetadata -Metadata $metadata -BudgetPath $budgetPath | Out-Null # Create empty entity files @('Billers.json', 'Earnings.json', 'Accounts.json') | ForEach-Object { $filePath = Join-Path $budgetPath $_ '[]' | Set-Content -Path $filePath -Encoding UTF8 } Write-Verbose "Created default budget: $DefaultBudget" } Write-Host "Budget workspace initialized at: $WorkspacePath" -ForegroundColor Green if ($DefaultBudget) { Write-Host "Default budget created: $DefaultBudget" -ForegroundColor Green } return $preferences } } |