-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathbazel-diff-example.ps1
More file actions
116 lines (97 loc) · 3.99 KB
/
bazel-diff-example.ps1
File metadata and controls
116 lines (97 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# PowerShell version of bazel-diff-example.sh for Windows compatibility
param(
[Parameter(Mandatory=$true)]
[string]$WorkspacePath,
[Parameter(Mandatory=$true)]
[string]$BazelPath,
[Parameter(Mandatory=$true)]
[string]$PreviousRevision,
[Parameter(Mandatory=$true)]
[string]$FinalRevision
)
$ErrorActionPreference = "Stop"
# Use temp directory for intermediate files
$TempDir = $env:TEMP
$StartingHashesJson = Join-Path $TempDir "starting_hashes.json"
$FinalHashesJson = Join-Path $TempDir "final_hashes.json"
$ImpactedTargetsPath = Join-Path $TempDir "impacted_targets.txt"
# Set appropriate flags based on environment variables
$BazelDiffFlags = @()
if ($env:BAZEL_DIFF_DISABLE_WORKSPACE -eq "true") {
Write-Host "Disabling workspace for bazel-diff commands (BAZEL_DIFF_DISABLE_WORKSPACE=true)"
$BazelDiffFlags += "-co"
$BazelDiffFlags += "--enable_workspace=false"
}
if ($env:BAZEL_DIFF_EXTRA_FLAGS) {
Write-Host "Injecting extra bazel-diff flags: $env:BAZEL_DIFF_EXTRA_FLAGS"
$BazelDiffFlags += $env:BAZEL_DIFF_EXTRA_FLAGS.Split(" ")
}
# Bazel command options applied to both top-level bazel invocations and generate-hashes (via -co)
if ($env:BAZEL_EXTRA_COMMAND_OPTIONS) {
Write-Host "Applying extra Bazel command options to top-level and generate-hashes: $env:BAZEL_EXTRA_COMMAND_OPTIONS"
$BazelDiffFlags += "-co"
$BazelDiffFlags += $env:BAZEL_EXTRA_COMMAND_OPTIONS.Split(" ")
}
# Set git checkout flags based on environment variable
$GitCheckoutFlags = @("--quiet")
if ($env:BAZEL_DIFF_FORCE_CHECKOUT -eq "true") {
Write-Host "Force checkout enabled (BAZEL_DIFF_FORCE_CHECKOUT=true) - will discard uncommitted changes"
$GitCheckoutFlags = @("--force", "--quiet")
}
# Build bazel-diff first to ensure it's ready
Write-Host "Building bazel-diff..."
$BazelExtraOptions = @()
if ($env:BAZEL_EXTRA_COMMAND_OPTIONS) {
$BazelExtraOptions = $env:BAZEL_EXTRA_COMMAND_OPTIONS.Split(" ")
}
& $BazelPath build @BazelExtraOptions //:bazel-diff
if ($LASTEXITCODE -ne 0) {
throw "Failed to build bazel-diff"
}
# Helper function to run bazel-diff commands
function Run-BazelDiff {
param(
[string[]]$Arguments
)
& $BazelPath run @BazelExtraOptions //:bazel-diff -- @Arguments
return $LASTEXITCODE
}
# Checkout previous revision
Write-Host "Checking out revision '$PreviousRevision'"
& git -C $WorkspacePath checkout @GitCheckoutFlags $PreviousRevision
if ($LASTEXITCODE -ne 0) {
throw "Failed to checkout revision $PreviousRevision"
}
# Generate hashes for previous revision
Write-Host "Generating Hashes for Revision '$PreviousRevision'"
$exitCode = Run-BazelDiff (@("generate-hashes", "-w", $WorkspacePath, "-b", $BazelPath) + $BazelDiffFlags + @($StartingHashesJson))
if ($exitCode -ne 0) {
throw "Failed to generate hashes for revision $PreviousRevision"
}
# Checkout final revision
Write-Host "Checking out revision '$FinalRevision'"
& git -C $WorkspacePath checkout @GitCheckoutFlags $FinalRevision
if ($LASTEXITCODE -ne 0) {
throw "Failed to checkout revision $FinalRevision"
}
# Generate hashes for final revision
Write-Host "Generating Hashes for Revision '$FinalRevision'"
$exitCode = Run-BazelDiff (@("generate-hashes", "-w", $WorkspacePath, "-b", $BazelPath) + $BazelDiffFlags + @($FinalHashesJson))
if ($exitCode -ne 0) {
throw "Failed to generate hashes for revision $FinalRevision"
}
# Determine impacted targets
Write-Host "Determining Impacted Targets"
$exitCode = Run-BazelDiff @("get-impacted-targets", "-w", $WorkspacePath, "-b", $BazelPath, "-sh", $StartingHashesJson, "-fh", $FinalHashesJson, "-o", $ImpactedTargetsPath)
if ($exitCode -ne 0) {
throw "Failed to get impacted targets"
}
# Read and display impacted targets
if (Test-Path $ImpactedTargetsPath) {
$ImpactedTargets = Get-Content $ImpactedTargetsPath
Write-Host "Impacted Targets between ${PreviousRevision} and ${FinalRevision}:"
$ImpactedTargets | ForEach-Object { Write-Host $_ }
Write-Host ""
} else {
Write-Host "No impacted targets file found"
}