-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
139 lines (127 loc) · 4.79 KB
/
build.ps1
File metadata and controls
139 lines (127 loc) · 4.79 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
# spell-checker:ignore nologo psake
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSReviewUnusedParameter',
'Command',
Justification = 'false positive'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSReviewUnusedParameter',
'Parameter',
Justification = 'false positive'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSReviewUnusedParameter',
'CommandAst',
Justification = 'false positive'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSReviewUnusedParameter',
'FakeBoundParams',
Justification = 'false positive'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSReviewUnusedParameter',
'Help',
Justification = 'false positive'
)]
[CmdletBinding(DefaultParameterSetName = 'Task')]
param(
# Build task(s) to execute
[Parameter(ParameterSetName = 'task', Position = 0)]
[ArgumentCompleter( {
param($Command, $Parameter, $WordToComplete, $CommandAst, $FakeBoundParams)
try {
Get-PSakeScriptTasks -BuildFile './build.psake.ps1' -ErrorAction 'Stop' |
Where-Object { $_.Name -like "$WordToComplete*" } |
Select-Object -ExpandProperty 'Name'
}
catch {
# Silently fail if psake tasks can't be retrieved
@()
}
})]
[ValidateNotNullOrEmpty()]
[string[]]$Task = 'default',
# Bootstrap dependencies
[switch]$Bootstrap,
# List available build tasks
[Parameter(ParameterSetName = 'Help')]
[switch]$Help,
# Optional properties to pass to psake
[hashtable]$Properties,
# Optional parameters to pass to psake
[hashtable]$Parameters
)
$ErrorActionPreference = 'Stop'
# Define dependency file pattern
$dependencyFilePattern = '*.depend.psd1'
$dependencyFilePath = Join-Path -Path $PSScriptRoot -ChildPath $dependencyFilePattern
# Bootstrap dependencies
if ($Bootstrap) {
$null = PackageManagement\Get-PackageProvider -Name 'NuGet' -ForceBootstrap
if ((Test-Path -Path $dependencyFilePath)) {
# Ensure PSGallery is registered and trusted
if (-not (Get-PSRepository -Name 'PSGallery' -ErrorAction 'SilentlyContinue')) {
Register-PSRepository -Default
}
Set-PSRepository -Name 'PSGallery' -InstallationPolicy 'Trusted'
if (-not (Get-Module -Name 'PSDepend' -ListAvailable)) {
Install-Module -Name 'PSDepend' -Scope 'CurrentUser' -Repository 'PSGallery' -Force
}
Import-Module -Name 'PSDepend' -Verbose:$false
# Try to import existing modules first to avoid installation locks
# Only install if import fails (missing modules or wrong versions)
$psDependParameters = @{
Path = $PSScriptRoot
Recurse = $False
WarningAction = 'SilentlyContinue'
Import = $True
Force = $True
ErrorAction = 'Stop'
}
$importSucceeded = $false
try {
Invoke-PSDepend @psDependParameters
$importSucceeded = $true
Write-Verbose 'Successfully imported existing modules.' -Verbose
}
catch {
Write-Verbose "Could not import all required modules: $_" -Verbose
Write-Verbose 'Attempting to install missing or outdated dependencies...' -Verbose
}
# If import failed, install the dependencies
if (-not $importSucceeded) {
try {
Invoke-PSDepend @psDependParameters -Install
}
catch {
Write-Error "Failed to install and import required dependencies: $_"
Write-Error 'This may be due to locked module files. Please restart the build environment or clear module locks.'
if ($_.Exception.InnerException) {
Write-Error "Inner exception: $($_.Exception.InnerException.Message)"
}
throw
}
}
}
else {
Write-Warning "No dependency file ($dependencyFilePattern) found. Skipping build dependency installation."
}
}
else {
if (-not (Get-Module -Name 'PSDepend' -ListAvailable)) {
throw 'Missing dependencies. Please run with the "-Bootstrap" flag to install dependencies.'
}
Invoke-PSDepend -Path $PSScriptRoot -Recurse $False -WarningAction 'SilentlyContinue' -Import -Force
}
# Execute psake task(s)
$psakeFile = './build.psake.ps1'
if ($PSCmdlet.ParameterSetName -eq 'Help') {
Get-PSakeScriptTasks -BuildFile $psakeFile |
Format-Table -Property 'Name', 'Description', 'Alias', 'DependsOn'
}
else {
Set-BuildEnvironment -Force
Invoke-psake -BuildFile $psakeFile -TaskList $Task -NoLogo -Properties $Properties -Parameters $Parameters
exit ([int](-not $psake.build_success))
}