Files
sf-cli-wrapper/sf-dry-run.ps1
2025-08-28 15:51:17 +08:00

147 lines
4.1 KiB
PowerShell

param(
[string]$o = "",
[string]$s = "",
[string]$d = "",
[string]$t = "",
[switch]$h
)
function Show-Help {
@"
sf-dry-run.ps1 PowerShell wrapper for ``sf project deploy start --dry-run``
USAGE:
sf-dry-run.ps1 -o <ORG_ALIAS_OR_USERNAME> (-s "<src1>,<src2>[,...]" | -d <DIRECTORY>) [-t "<Test1>,<Test2>[,...]"]
sf-dry-run.ps1 -h
OPTIONS:
-o Org alias or username for --target-org
-s Comma-separated list of --source-dir paths
-d Single directory path to validate (alternative to -s)
-t Comma-separated list of --tests (enables --test-level RunSpecifiedTests)
-h Show this help
EXAMPLES:
1) Basic dry-run with multiple flexipages (specific files):
sf-dry-run.ps1 -o "DEMO-ORG" ``
-s "force-app/main/default/flexipages/Sample_Page.flexipage-meta.xml,force-app/main/default/flexipages/Sample_SalesNavigator.flexipage-meta.xml,force-app/main/default/flexipages/Sample_Role_Record_Page.flexipage-meta.xml"
2) Dry-run validation for entire directory:
sf-dry-run.ps1 -o "DEMO-ORG" -d "force-app/main/default/classes"
3) Dry-run with specified tests:
sf-dry-run.ps1 -o "DEMO-ORG" ``
-s "force-app/main/default/flexipages/Demo_Page.flexipage-meta.xml,force-app/main/default/flexipages/Demo_Page_Backup_With_SalesNavigator.flexipage-meta.xml" ``
-t "SelectorOpportunity_Test,SelectorOpportunity2_Test"
Notes:
- Use -s for specific files (comma-separated) OR -d for entire directories (not both).
- Pass absolute or repo-relative paths.
- Multiple tests are comma-separated in -t; they will be expanded to multiple --tests flags.
"@
}
# Show help if requested or no parameters provided
if ($h -or ($o -eq "" -and $s -eq "" -and $d -eq "" -and $t -eq "")) {
Show-Help
exit 0
}
# Validate that either -s or -d is provided, but not both
if ($s -ne "" -and $d -ne "") {
Write-Error "Cannot use both -s and -d options. Use -s for specific files or -d for directories."
Write-Host ""
Show-Help
exit 1
}
# Validate required parameters
if ($o -eq "" -or ($s -eq "" -and $d -eq "")) {
Write-Error "Must provide -o (org) and either -s (specific files) or -d (directory path)."
Write-Host ""
Show-Help
exit 1
}
# Silent environment check - verify SF CLI is available
try {
Get-Command sf -ErrorAction Stop | Out-Null
}
catch {
Write-Host "❌ Salesforce CLI (sf) not found!" -ForegroundColor Red
Write-Host ""
Write-Host "Running environment check to help you get started..."
Write-Host ""
# Try to find and run sf-check.ps1 in the same directory as this script
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$sfCheckPath = Join-Path $scriptDir "sf-check.ps1"
if (Test-Path $sfCheckPath) {
& $sfCheckPath
}
elseif (Get-Command sf-check.ps1 -ErrorAction SilentlyContinue) {
sf-check.ps1
}
else {
Write-Host "sf-check.ps1 not found. Please install the Salesforce CLI from:"
Write-Host "https://developer.salesforce.com/tools/sfdxcli"
}
exit 1
}
# Build the command array
$cmd = @("sf", "project", "deploy", "start", "--dry-run")
# Add source directories (specific files)
if ($s -ne "") {
$sourcesArray = $s -split ","
foreach ($src in $sourcesArray) {
$src = $src.Trim()
if ($src -ne "") {
$cmd += "--source-dir"
$cmd += $src
}
}
}
# Add directory path
if ($d -ne "") {
$cmd += "--source-dir"
$cmd += $d
}
# Add target org
if ($o -ne "") {
$cmd += "--target-org"
$cmd += $o
}
# Add tests if specified
if ($t -ne "") {
$testsArray = $t -split ","
$cmd += "--test-level"
$cmd += "RunSpecifiedTests"
foreach ($test in $testsArray) {
$test = $test.Trim()
if ($test -ne "") {
$cmd += "--tests"
$cmd += $test
}
}
}
# Display the command being executed
Write-Host ">>> Running: $($cmd -join ' ')" -ForegroundColor Yellow
# Execute the command
try {
& $cmd[0] $cmd[1..($cmd.Length-1)]
exit $LASTEXITCODE
}
catch {
Write-Error "Failed to execute sf command: $_"
exit 1
}