-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.build.ps1
More file actions
338 lines (285 loc) · 11.3 KB
/
Copy path.build.ps1
File metadata and controls
338 lines (285 loc) · 11.3 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#Requires -Version 5.1
<#
.SYNOPSIS
Build script for Daily Motivation Brain Helper
.DESCRIPTION
Modern PowerShell build script using Invoke-Build pattern.
Supports clean, analyze, test, and build tasks.
.EXAMPLE
Invoke-Build
Runs default build: Clean -> Analyze -> Test -> Build
.EXAMPLE
Invoke-Build Test
Runs only tests
.EXAMPLE
Invoke-Build Build -Verbose
Builds EXE with verbose output
.NOTES
Requires: Invoke-Build, PSScriptAnalyzer, Pester, ps2exe modules
Install: Install-Module InvokeBuild, PSScriptAnalyzer, Pester, ps2exe -Scope CurrentUser
#>
# Build configuration
$script:Config = @{
ProjectRoot = $PSScriptRoot
SourcePath = Join-Path $PSScriptRoot 'src'
TestsPath = Join-Path $PSScriptRoot 'Tests'
OutputPath = Join-Path $PSScriptRoot 'Output'
DocsPath = Join-Path $PSScriptRoot 'docs'
MainAppScript = Join-Path $PSScriptRoot 'src\MainApp.ps1'
PopupScript = Join-Path $PSScriptRoot 'src\DailyMotivation.ps1'
MainAppExe = Join-Path $PSScriptRoot 'Output\DailyMotivationBrainHelper.exe'
PopupExe = Join-Path $PSScriptRoot 'Output\DailyMotivation.exe'
}
# Synopsis: Default build task
task . Clean, Analyze, Test, Build
# Synopsis: Clean output directory
task Clean {
if (Test-Path $script:Config.OutputPath) {
Write-Build Green "Cleaning output directory: $($script:Config.OutputPath)"
Remove-Item -Path $script:Config.OutputPath -Recurse -Force
}
New-Item -ItemType Directory -Path $script:Config.OutputPath -Force | Out-Null
}
# Synopsis: Run PSScriptAnalyzer
task Analyze {
Write-Build Green "Running PSScriptAnalyzer..."
# Check if PSScriptAnalyzer is installed
if (-not (Get-Module -ListAvailable -Name PSScriptAnalyzer)) {
Write-Build Yellow "PSScriptAnalyzer not found. Install with: Install-Module PSScriptAnalyzer"
return
}
Import-Module PSScriptAnalyzer
# Analyze PowerShell files
$filesToAnalyze = @(
Get-ChildItem -Path $script:Config.SourcePath -Filter *.ps1 -Recurse
Get-ChildItem -Path $script:Config.SourcePath -Filter *.psm1 -Recurse
).FullName
$settingsPath = Join-Path $script:Config.ProjectRoot '.PSScriptAnalyzerSettings.psd1'
$analyzerParams = @{
Path = $filesToAnalyze
Recurse = $false
}
if (Test-Path $settingsPath) {
$analyzerParams['Settings'] = $settingsPath
Write-Build Cyan "Using PSScriptAnalyzer settings: $settingsPath"
}
$results = Invoke-ScriptAnalyzer @analyzerParams
# Export results to SARIF format for GitHub Code Scanning
$sarifPath = Join-Path $script:Config.ProjectRoot 'scriptanalyzer-results.sarif'
if ($results) {
Write-Build Yellow "PSScriptAnalyzer found $($results.Count) issue(s):"
$results | Format-Table -AutoSize | Out-String | Write-Build
# Convert results to SARIF format
$sarif = @{
version = "2.1.0"
runs = @(
@{
tool = @{
driver = @{
name = "PSScriptAnalyzer"
version = (Get-Module PSScriptAnalyzer | Select-Object -First 1).Version.ToString()
informationUri = "https://github.com/PowerShell/PSScriptAnalyzer"
}
}
results = @($results | ForEach-Object {
@{
ruleId = $_.RuleName
message = @{
text = $_.Message
}
locations = @(
@{
physicalLocation = @{
artifactLocation = @{
uri = $_.ScriptPath.Replace((Get-Location).Path, "").TrimStart("\")
}
region = @{
startLine = $_.Line
startColumn = $_.Column
}
}
}
)
level = if ($_.Severity -eq 'Error') { "error" } elseif ($_.Severity -eq 'Warning') { "warning" } else { "note" }
}
})
}
)
}
$sarif | ConvertTo-Json -Depth 10 | Set-Content -Path $sarifPath -Encoding UTF8
Write-Build Cyan "SARIF results exported to: $sarifPath"
$errors = $results | Where-Object Severity -eq 'Error'
if ($errors) {
throw "PSScriptAnalyzer found $($errors.Count) error(s). Build failed."
}
} else {
Write-Build Green "✅ PSScriptAnalyzer: No issues found"
# Create empty SARIF file for consistency
$emptySarif = @{
version = "2.1.0"
runs = @(
@{
tool = @{
driver = @{
name = "PSScriptAnalyzer"
version = (Get-Module PSScriptAnalyzer | Select-Object -First 1).Version.ToString()
informationUri = "https://github.com/PowerShell/PSScriptAnalyzer"
}
}
results = @()
}
)
}
$emptySarif | ConvertTo-Json -Depth 10 | Set-Content -Path $sarifPath -Encoding UTF8
Write-Build Cyan "Empty SARIF results exported to: $sarifPath"
}
}
# Synopsis: Run Pester tests
task Test {
Write-Build Green "Running Pester tests..."
# Check if Pester is installed
if (-not (Get-Module -ListAvailable -Name Pester)) {
Write-Build Yellow "Pester not found. Install with: Install-Module Pester -MinimumVersion 5.0"
return
}
# Run test script
$testScript = Join-Path $script:Config.ProjectRoot 'Invoke-Tests.ps1'
if (Test-Path $testScript) {
& $testScript -CI -Coverage $true
} else {
Write-Build Yellow "Invoke-Tests.ps1 not found"
}
}
# Synopsis: Build executables with PS2EXE
task Build {
Write-Build Green "Building executables with PS2EXE..."
# Check if ps2exe is installed
if (-not (Get-Module -ListAvailable -Name ps2exe)) {
Write-Build Yellow "ps2exe not found. Install with: Install-Module ps2exe"
Write-Build Yellow "Skipping EXE build"
return
}
Import-Module ps2exe
# Build MainApp.exe
Write-Build Cyan "Building MainApp executable..."
$mainParams = @{
InputFile = $script:Config.MainAppScript
OutputFile = $script:Config.MainAppExe
NoConsole = $true
NoOutput = $true
NoError = $false
RequireAdmin = $false
STA = $true
Title = "Daily Motivation Brain Helper"
Product = "Daily Motivation Brain Helper"
Version = "1.0.0.0"
Verbose = $false
}
Invoke-PS2EXE @mainParams
if (Test-Path $script:Config.MainAppExe) {
Write-Build Green "✅ Built: $($script:Config.MainAppExe)"
} else {
throw "Failed to build MainApp executable"
}
# Build DailyMotivation.exe
Write-Build Cyan "Building Popup executable..."
$popupParams = @{
InputFile = $script:Config.PopupScript
OutputFile = $script:Config.PopupExe
NoConsole = $true
NoOutput = $true
NoError = $false
RequireAdmin = $false
STA = $true
Title = "Daily Motivation Brain Helper - Popup"
Product = "Daily Motivation Brain Helper"
Version = "1.0.0.0"
Verbose = $false
}
Invoke-PS2EXE @popupParams
if (Test-Path $script:Config.PopupExe) {
Write-Build Green "✅ Built: $($script:Config.PopupExe)"
} else {
throw "Failed to build Popup executable"
}
# Copy dependencies
Write-Build Cyan "Copying dependencies..."
# Copy Modules
Copy-Item -Path (Join-Path $script:Config.SourcePath 'Modules') `
-Destination $script:Config.OutputPath -Recurse -Force
# Copy data
if (Test-Path (Join-Path $script:Config.SourcePath 'data')) {
Copy-Item -Path (Join-Path $script:Config.SourcePath 'data') `
-Destination $script:Config.OutputPath -Recurse -Force
}
# Copy LaunchMotivation.bat
Copy-Item -Path (Join-Path $script:Config.SourcePath 'LaunchMotivation.bat') `
-Destination $script:Config.OutputPath -Force
Write-Build Green "✅ Build completed successfully"
Write-Build Cyan "Output directory: $($script:Config.OutputPath)"
}
# Synopsis: Run only unit tests
task TestUnit {
Write-Build Green "Running unit tests only..."
$testScript = Join-Path $script:Config.ProjectRoot 'Invoke-Tests.ps1'
if (Test-Path $testScript) {
& $testScript -Tag 'Unit' -Coverage $false
}
}
# Synopsis: Run only integration tests
task TestIntegration {
Write-Build Green "Running integration tests only..."
$testScript = Join-Path $script:Config.ProjectRoot 'Invoke-Tests.ps1'
if (Test-Path $testScript) {
& $testScript -Tag 'Integration' -Coverage $false
}
}
# Synopsis: Quick build without tests
task QuickBuild Clean, Build
# Synopsis: Full release build
task Release Clean, Analyze, Test, Build, Package
# Synopsis: Create release package
task Package {
Write-Build Green "Creating release package..."
$packagePath = Join-Path $script:Config.OutputPath 'DailyMotivationBrainHelper_Release.zip'
# Create ZIP package
$filesToPackage = @(
$script:Config.MainAppExe
$script:Config.PopupExe
(Join-Path $script:Config.OutputPath 'LaunchMotivation.bat')
(Join-Path $script:Config.OutputPath 'src')
)
Compress-Archive -Path $filesToPackage -DestinationPath $packagePath -Force
Write-Build Green "✅ Package created: $packagePath"
}
# Synopsis: Install required build modules
task InstallDependencies {
Write-Build Green "Installing build dependencies..."
$modules = @(
@{ Name = 'InvokeBuild'; MinimumVersion = '5.0' }
@{ Name = 'Pester'; MinimumVersion = '5.0' }
@{ Name = 'PSScriptAnalyzer'; MinimumVersion = '1.20' }
@{ Name = 'ps2exe'; MinimumVersion = '1.0' }
)
foreach ($module in $modules) {
$installed = Get-Module -ListAvailable -Name $module.Name |
Where-Object { $_.Version -ge $module.MinimumVersion } |
Select-Object -First 1
if (-not $installed) {
Write-Build Yellow "Installing $($module.Name) >= $($module.MinimumVersion)..."
Install-Module -Name $module.Name -MinimumVersion $module.MinimumVersion `
-Scope CurrentUser -Force -SkipPublisherCheck
Write-Build Green "✅ Installed $($module.Name)"
} else {
Write-Build Cyan "$($module.Name) already installed: $($installed.Version)"
}
}
Write-Build Green "✅ All dependencies installed"
}
# Synopsis: Show build configuration
task ShowConfig {
Write-Build Cyan "Build Configuration:"
$script:Config.GetEnumerator() | Sort-Object Key | ForEach-Object {
Write-Build White " $($_.Key): $($_.Value)"
}
}