1
+ Param () # No parameters for now
2
+
3
+ Set-StrictMode - Version Latest
4
+ $ErrorActionPreference = ' Stop'
5
+
6
+ # Ensure the "generated" folder exists
7
+ if (-not (Test-Path - LiteralPath ' generated' )) {
8
+ New-Item - ItemType Directory - Path ' generated' | Out-Null
9
+ }
10
+
11
+ if (Test-Path ' generated\build_id.h' ) {
12
+ # ------------------------------------------------------------------------------
13
+ # 1) CAPTURE SIGNATURE A (LIST OF UNCOMMITTED CHANGES WITH LINES ADDED/REMOVED)
14
+ # ------------------------------------------------------------------------------
15
+ Write-Host " [build_id_gen] Gathering diff info (Signature A)..."
16
+
17
+ # 1A) Normal 'numstat' lines for tracked changes
18
+ $signatureA_current_lines = git diff -- numstat
19
+
20
+ # 1B) Collect untracked files so we can treat them like "newly added"
21
+ # (i.e., lines = total lines, removed = 0)
22
+ $untrackedFiles = git ls- files -- others -- exclude- standard
23
+
24
+ foreach ($uf in $untrackedFiles ) {
25
+ # Ensure file still exists (and isn't a folder)
26
+ if (Test-Path $uf ) {
27
+ # Count lines. If the file is huge or binary, consider try/catch or skip.
28
+ $lineCount = (Get-Content $uf - ErrorAction SilentlyContinue | Measure-Object - Line).Lines
29
+
30
+ # Append path to make relative to solution (repo) root
31
+ $uf = ' IntelPresentMon/Versioning/' + $uf
32
+
33
+ # Append a "numstat-like" line: "<lines> 0 <filePath>"
34
+ # Use backtick-t (`t) for tabs, to mirror git diff --numstat output.
35
+ $signatureA_current_lines += " $lineCount `t 0`t $uf "
36
+ }
37
+ }
38
+
39
+ # 1C) Build a single signature string (space-separated or however you like).
40
+ # If you want each record separated by a newline instead, use "-join [Environment]::NewLine"
41
+ $signatureA_current = $signatureA_current_lines -join ' '
42
+
43
+ # Retrieve previous signature A if it exists
44
+ $signatureA_prev = ' '
45
+ if (Test-Path ' generated\signature_a_prev.txt' ) {
46
+ $signatureA_prev = Get-Content ' generated\signature_a_prev.txt' - Raw
47
+ }
48
+
49
+ # ------------------------------------------------------------------------------
50
+ # 2) CAPTURE SIGNATURE B (PER-FILE MD5 FOR ALL UNCOMMITTED FILES)
51
+ # ------------------------------------------------------------------------------
52
+ Write-Host " [build_id_gen] Calculating per-file hashes (Signature B)..."
53
+ $signatureB_current = ' '
54
+
55
+ foreach ($line in $signatureA_current_lines ) {
56
+ # Each line is something like: 10 2 src\main.cpp
57
+ # Split on whitespace
58
+ $fields = $line -split ' \s+'
59
+ if ($fields.Count -ge 3 ) {
60
+ $linesAdded = $fields [0 ]
61
+ $linesRemoved = $fields [1 ]
62
+ $changedFile = ' ../../' + $fields [2 ]
63
+
64
+ # Edge case: file might be deleted or not present
65
+ if (-not (Test-Path $changedFile )) {
66
+ # Write-Host "[build_id_gen] File '$changedFile' does not exist (possibly deleted). Skipping..."
67
+ continue
68
+ }
69
+
70
+ # Get MD5 hash of the file
71
+ # (You can change -Algorithm to e.g. SHA256 if desired)
72
+ $hash = (Get-FileHash $changedFile - Algorithm MD5).Hash
73
+ $signatureB_current += $hash
74
+ }
75
+ }
76
+
77
+ # Retrieve previous signature B if it exists s
78
+ $signatureB_prev = ' '
79
+ if (Test-Path ' generated\signature_b_prev.txt' ) {
80
+ $signatureB_prev = Get-Content ' generated\signature_b_prev.txt' - Raw
81
+ }
82
+
83
+ # Store the updated Signature B
84
+ $signatureB_current | Set-Content ' generated\signature_b_prev.txt'
85
+
86
+ # If Signature A is unchanged, we must check Signature B
87
+ if ($signatureA_current.Trim () -eq $signatureA_prev.Trim ()) {
88
+ # If Signature B is unchanged, skip generating build_id.h
89
+ if ($signatureB_current.Trim () -eq $signatureB_prev.Trim ()) {
90
+ Write-Host " [build_id_gen] Signature B is unchanged. Skipping Versioning\generated\build_id.h regeneration."
91
+ return
92
+ }
93
+ Write-Host " [build_id_gen] Signature B mismatch detected."
94
+ } else {
95
+ # Store the updated Signature A
96
+ $signatureA_current | Set-Content ' generated\signature_a_prev.txt'
97
+ Write-Host " [build_id_gen] Signature A mismatch detected."
98
+ }
99
+ } else {
100
+ Write-Host " [build_id_gen] Versioning\generated\build_id.h not present; generating..."
101
+ }
102
+
103
+
104
+ # ------------------------------------------------------------------------------
105
+ # 3) GENERATE/UPDATE BUILD_ID.H
106
+ # ------------------------------------------------------------------------------
107
+ Write-Host " [build_id_gen] Generating new build_id.h..."
108
+
109
+ # Get the current git commit hash
110
+ $gitHash = (git rev- parse HEAD) | Out-String
111
+ $gitHashShort = (git rev- parse -- short HEAD)| Out-String
112
+ $gitHash = $gitHash.Trim ()
113
+ $gitHashShort = $gitHashShort.Trim ()
114
+
115
+ # Format: yyyy.M.d.HH:mm:ss (e.g., 2025.1.8.12:20:11)
116
+ $dateTime = Get-Date - Format ' yyyy.M.d.HH:mm:ss'
117
+
118
+ # Check if there are uncommitted changes (sets $LASTEXITCODE = 0 if none)
119
+ git diff -- quiet
120
+ if ($LASTEXITCODE -eq 0 ) {
121
+ $uncommittedChanges = ' false'
122
+ } else {
123
+ $uncommittedChanges = ' true'
124
+ }
125
+
126
+ @"
127
+ #pragma once
128
+ // Auto-generated by build_id_gen.ps1
129
+
130
+ #define PM_BID_GIT_HASH "$gitHash "
131
+ #define PM_BID_GIT_HASH_SHORT "$gitHashShort "
132
+ #define PM_BID_TIME "$dateTime "
133
+ #define PM_BID_UID "$gitHash -$dateTime "
134
+ #define PM_BID_DIRTY $uncommittedChanges
135
+ "@ | Set-Content ' generated\build_id.h'
0 commit comments