forked from PowerShell/PSScriptAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ps1
205 lines (175 loc) · 5.4 KB
/
build.ps1
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
[CmdletBinding()]
param(
[Parameter(ParameterSetName='Build')]
[ValidateSet('PSV3 Debug','PSV3 Release','Debug','Release')]
[string] $Configuration = 'Debug',
[Parameter(ParameterSetName='Build')]
[switch] $BuildSolution = $false,
[Parameter(ParameterSetName='Build')]
[switch] $CleanSolution = $false,
[Parameter(ParameterSetName='Build')]
[switch] $BuildDocs = $false,
[Parameter(ParameterSetName='Build')]
[switch] $CleanOutput = $false,
[Parameter(ParameterSetName='Build')]
[switch] $Install = $false,
[Parameter(ParameterSetName='Build')]
[switch] $Uninstall = $false,
[Parameter(ParameterSetName='Test')]
[switch] $Test = $false,
[Parameter(ParameterSetName='Test')]
[switch] $Engine = $false,
[Parameter(ParameterSetName='Test')]
[switch] $Rules = $false,
[Parameter(ParameterSetName='Test')]
[switch] $RunInDifferentProcess = $false
)
# Some cmdlets like copy-item do not respond to the $verbosepreference variable
# hence we set it explicitly
$verbosity = $false
if ($VerbosePreference.Equals([System.Management.Automation.ActionPreference]'Continue'))
{
$verbosity = $true
}
Function CreateIfNotExists([string] $folderPath)
{
if (-not (Test-Path $folderPath))
{
New-Item -Path $folderPath -ItemType Directory -Verbose:$verbosity
}
}
$projectRoot = Resolve-path (Split-Path $MyInvocation.InvocationName)
$solutionPath = Join-Path $projectRoot 'PSScriptAnalyzer.sln'
$outPath = Join-Path $projectRoot 'out'
$destinationPath = Join-Path $outPath PSScriptAnalyzer
if (-not (Test-Path $solutionPath))
{
$errMsg = "{0} not the right directory" -f $solutionPath
throw $errMsg
}
$buildCmd = Join-Path $projectRoot "build.cmd"
if (-not (Test-Path $buildCmd))
{
throw "cannot find build.cmd"
}
if ($CleanOutput)
{
Remove-Item -Recurse $outPath\* -Force -Verbose:$verbosity
}
if ($CleanSolution)
{
& $buildCmd $solutionPath $Configuration 'clean'
}
if ($BuildSolution)
{
& $buildCmd $solutionPath $Configuration
}
if ($BuildDocs)
{
$docsPath = Join-Path $projectRoot 'docs'
$markdownDocsPath = Join-Path $docsPath 'markdown'
$outputDocsPath = Join-Path $destinationPath en-US
CreateIfNotExists($outputDocsPath)
# copy the about help file
Copy-Item -Path $docsPath\about_PSScriptAnalyzer.help.txt -Destination $outputDocsPath -Force -Verbose:$verbosity
# Build documentation using platyPS
if ((Get-Module PlatyPS -ListAvailable -Verbose:$verbosity) -eq $null)
{
throw "Cannot find PlatyPS. Please install it from https://www.powershellgallery.com."
}
if ((Get-Module PlatyPS -Verbose:$verbosity) -eq $null)
{
Import-Module PlatyPS -Verbose:$verbosity
}
if (-not (Test-Path $markdownDocsPath -Verbose:$verbosity))
{
throw "Cannot find markdown documentation folder."
}
New-ExternalHelp -Path $markdownDocsPath -OutputPath $outputDocsPath -Force -Verbose:$verbosity
}
# Appveyor errors out due to $profile being null. Hence...
$moduleRootPath = "$HOME/Documents/WindowsPowerShell/Modules"
if ($profile -ne $null)
{
$moduleRootPath = Join-Path (Split-Path $profile) 'Modules'
}
$modulePSSAPath = Join-Path $moduleRootPath 'PSScriptAnalyzer'
if ($Install)
{
if (-not (Test-Path $moduleRootPath))
{
New-Item -Path $moduleRootPath -ItemType Directory -Force -Verbose:$verbosity
}
if (-not (Test-Path -Path $destinationPath))
{
throw "Please build the module first."
}
Copy-Item -Path $destinationPath -Destination $modulePSSAPath -Recurse -Verbose:$verbosity
}
if ($Test)
{
Import-Module -Name Pester -MinimumVersion 3.4.0 -ErrorAction Stop
Function GetTestRunnerScriptContent($testPath)
{
$x = @"
cd $testPath
Invoke-Pester
"@
return $x
}
Function CreateTestRunnerScript($testPath)
{
$tmptmpFilePath = [System.IO.Path]::GetTempFileName()
$tmpFilePath = $tmptmpFilePath + '.ps1'
Move-Item $tmptmpFilePath $tmpFilePath -Verbose:$verbosity
$content = GetTestRunnerScriptContent $testPath
Set-Content -Path $tmpFilePath -Value $content -Verbose:$verbosity
return $tmpFilePath
}
Function GetTestPath($TestType)
{
if ($TestType -eq "engine")
{
$testPath = Join-Path $projectRoot "Tests/Engine"
}
else
{
$testPath = Join-Path $projectRoot "Tests/Rules"
}
return $testPath
}
Function RunTest($TestType, [Boolean] $DifferentProcess)
{
$testPath = GetTestPath($TestType)
if ($DifferentProcess)
{
$testScriptFilePath = CreateTestRunnerScript $testPath
Start-Process powershell -ArgumentList "-NoExit","-File $testScriptFilePath" -Verb runas
# clean up the test file
}
else
{
try
{
Push-Location .
([scriptblock]::Create((GetTestRunnerScriptContent $testPath))).Invoke()
}
finally
{
Pop-Location
}
}
}
if ($Engine -or (-not ($Engine -or $Rules)))
{
RunTest 'engine' $RunInDifferentProcess
}
if ($Rules -or (-not ($Engine -or $Rules)))
{
RunTest 'rules' $RunInDifferentProcess
}
}
if ($Uninstall)
{
Remove-Item -Path $modulePSSAPath -Force -Verbose:$verbosity -Recurse
}