Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' $script:LoaderRoot = Split-Path -Parent $MyInvocation.MyCommand.Path $script:ConfigPath = Join-Path -Path $script:LoaderRoot -ChildPath 'config.json' $script:ConfigCache = $null function Get-ProjectStructureConfig { if ($null -ne $script:ConfigCache) { return $script:ConfigCache } if (Test-Path -LiteralPath $script:ConfigPath) { try { $raw = Get-Content -LiteralPath $script:ConfigPath -Raw -ErrorAction Stop if ($raw.Trim().Length -gt 0) { $script:ConfigCache = $raw | ConvertFrom-Json return $script:ConfigCache } } catch { Write-Warning "Failed to parse config.json: $($_.Exception.Message)" } } $script:ConfigCache = [pscustomobject]@{} return $script:ConfigCache } function Get-ConfigValue { param( [Parameter(Mandatory)] [string]$Name, $Default = $null ) $config = Get-ProjectStructureConfig if ($config.PSObject.Properties.Name -contains $Name) { $value = $config.$Name if ($null -ne $value -and ($value -isnot [string] -or $value.Trim().Length -gt 0)) { return $value } } return $Default } function Get-StructDirectory { $value = Get-ConfigValue -Name 'structDir' if ($null -eq $value -or [string]::IsNullOrWhiteSpace($value)) { return $script:LoaderRoot } if ([System.IO.Path]::IsPathRooted($value)) { $resolved = Resolve-Path -LiteralPath $value -ErrorAction SilentlyContinue if ($null -ne $resolved) { return $resolved.Path } return $value } $candidate = Join-Path -Path $script:LoaderRoot -ChildPath $value $resolvedCandidate = Resolve-Path -LiteralPath $candidate -ErrorAction SilentlyContinue if ($null -ne $resolvedCandidate) { return $resolvedCandidate.Path } return $candidate } function Get-ProjectsRoot { $value = Get-ConfigValue -Name 'projectsRoot' if ($null -eq $value -or [string]::IsNullOrWhiteSpace($value)) { $structDir = Get-StructDirectory $parent = Split-Path -Parent $structDir if ($null -eq $parent -or $parent.Length -eq 0 -or $parent -eq $structDir) { return $structDir } return $parent } if ([System.IO.Path]::IsPathRooted($value)) { $resolved = Resolve-Path -LiteralPath $value -ErrorAction SilentlyContinue if ($null -ne $resolved) { return $resolved.Path } return $value } $candidate = Join-Path -Path $script:LoaderRoot -ChildPath $value $resolvedCandidate = Resolve-Path -LiteralPath $candidate -ErrorAction SilentlyContinue if ($null -ne $resolvedCandidate) { return $resolvedCandidate.Path } return $candidate } function Get-ProjectPathFromUser { param( [string]$Prompt = "Paste the project path to deploy in" ) do { $inputPath = Read-Host -Prompt $Prompt if ([string]::IsNullOrWhiteSpace($inputPath)) { Write-Warning "Path cannot be empty. Please try again." continue } # Remove quotes if present $inputPath = $inputPath.Trim('"', "'") # Try to resolve the path try { if (Test-Path -LiteralPath $inputPath -PathType Container) { $resolved = Resolve-Path -LiteralPath $inputPath -ErrorAction Stop return $resolved.Path } else { Write-Warning "Path does not exist or is not a directory: $inputPath" $retry = Read-Host "Try again? (Y/N)" if ($retry -notmatch '^[Yy]') { return $null } } } catch { Write-Warning "Invalid path: $($_.Exception.Message)" $retry = Read-Host "Try again? (Y/N)" if ($retry -notmatch '^[Yy]') { return $null } } } while ($true) } function Use-IsoDailyFormat { $dailyFormat = Get-ConfigValue -Name 'dailyFormat' -Default $true # Handle backward compatibility with boolean values if ($dailyFormat -is [bool]) { return $dailyFormat } # Handle string values if ($dailyFormat -is [string]) { $formatStr = $dailyFormat.Trim() # ISO format contains YYYY-MM-DD pattern if ($formatStr -match 'YYYY-MM-DD' -or $formatStr -match '\d{4}-\d{2}-\d{2}') { return $true } # daily_YYMMDD or other formats are not ISO return $false } # Default to false (not ISO format) for unknown types return $false } function Use-7Zip { $zipper = Get-ConfigValue -Name 'zipper' -Default $true # Handle backward compatibility with boolean values if ($zipper -is [bool]) { return $zipper } # Handle string values if ($zipper -is [string]) { $zipperStr = $zipper.Trim().ToLower() return ($zipperStr -eq '7z') } # Default to false (use zip) for unknown types return $false } function Get-ZipCompressionLevel { $value = Get-ConfigValue -Name 'compression' -Default 9 if ($value -is [string]) { $parsed = 0 if ([int]::TryParse($value, [ref]$parsed)) { $value = $parsed } } if ($value -isnot [int]) { return 9 } return [Math]::Min(9, [Math]::Max(0, $value)) } # If script is run directly (not dot-sourced), prompt for project path and deploy # When dot-sourced, InvocationName is '.'; when run directly, it's the script path or name if ($MyInvocation.InvocationName -ne '.') { $projectPath = Get-ProjectPathFromUser if ($null -ne $projectPath) { Write-Host "`nDeploying to: $projectPath" -ForegroundColor Cyan # Find UpdateProjectBatches.ps1 in the same directory as ConfigLoader.ps1 $updateScript = Join-Path -Path $script:LoaderRoot -ChildPath 'UpdateProjectBatches.ps1' if (Test-Path -LiteralPath $updateScript) { Write-Host "Running UpdateProjectBatches.ps1...`n" -ForegroundColor Yellow & powershell -NoProfile -ExecutionPolicy Bypass -File $updateScript -ProjectPath $projectPath $exitCode = $LASTEXITCODE if ($exitCode -eq 0) { Write-Host "`nDeployment completed successfully." -ForegroundColor Green } else { Write-Host "`nDeployment completed with errors (exit code: $exitCode)." -ForegroundColor Red } exit $exitCode } else { Write-Warning "UpdateProjectBatches.ps1 not found in $script:LoaderRoot" Write-Host "Project path: $projectPath" -ForegroundColor Green return $projectPath } } else { Write-Host "No project path provided." -ForegroundColor Yellow exit 1 } }