-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.ps1
194 lines (176 loc) · 7.14 KB
/
run.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
#! pwsh
<#
.SYNOPSIS
Helper for running various tasks
.DESCRIPTION
This script is a wrapper for snippets of dot net commands. It is useful to keep in the root of a project for running various tasks.
.PARAMETER Tasks
One or more tasks to run. Use tab completion to see available tasks.
.PARAMETER Version
For pack task, the version to use. e.g. 1.0.0
.PARAMETER Prerelease
For pack task, if specified, will add --version-suffix prerelease to pack command
.PARAMETER appName
The name of the app folder. Defaults to OptionToStringGenerator
.PARAMETER BuildLogFolder
The folder to write build logs to. Defaults to the system temp folder
.EXAMPLE
./run.ps1 build
#>
[CmdletBinding()]
param (
[ArgumentCompleter({
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$runFile = (Join-Path (Split-Path $commandAst -Parent) run.ps1)
if (Test-Path $runFile) {
Get-Content $runFile |
Where-Object { $_ -match "^\s+'([\w+-]+)'\s*{" } |
ForEach-Object {
if ( !($fakeBoundParameters[$parameterName]) -or
(($matches[1] -notin $fakeBoundParameters.$parameterName) -and
($matches[1] -like "$wordToComplete*"))
)
{
$matches[1]
}
}
}
})]
[string[]] $Tasks,
[string] $Version,
[switch] $Prerelease,
[string] $appName = "OptionToStringGenerator",
[string] $BuildLogFolder,
[switch] $SuppressApiCompat
)
$currentTask = ""
if (!$BuildLogFolder) {
$BuildLogFolder = [System.IO.Path]::GetTempPath()
}
# execute a script, checking lastexit code
function executeSB
{
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[scriptblock] $ScriptBlock,
[string] $RelativeDir,
[string] $Name = $currentTask
)
if ($RelativeDir) {
Push-Location (Join-Path $PSScriptRoot $RelativeDir)
} else {
Push-Location $PSScriptRoot
}
try {
$global:LASTEXITCODE = 0
Invoke-Command -ScriptBlock $ScriptBlock
if ($LASTEXITCODE -ne 0) {
throw "Error executing command '$Name', last exit $LASTEXITCODE"
}
} finally {
Pop-Location
}
}
foreach ($currentTask in $Tasks) {
try {
$prevPref = $ErrorActionPreference
$ErrorActionPreference = "Stop"
"-------------------------------"
"Starting $currentTask"
"-------------------------------"
switch ($currentTask) {
'build' {
executeSB -RelativeDir "src/$appName" {
dotnet build /warnaserror
}
}
'testUnit' {
executeSB -RelativeDir "src/tests/unit" {
Remove-Item TestResults -Recurse -Force -ErrorAction Ignore
dotnet test --logger "trx;LogFileName=test-results.trx" --collect:"XPlat Code Coverage"
Get-ChildItem *coverage.cobertura.xml -R | Select-Object -First 1 | ForEach-Object {
"Move-Item $($_.FullName) ."
Remove-Item $_.Name -ErrorAction Ignore
Move-Item $_.FullName .
}
}
}
'testIntegration' {
executeSB -RelativeDir "src/tests/integration" {
Remove-Item TestResults -Recurse -Force -ErrorAction Ignore
dotnet test --collect:"XPlat Code Coverage" --logger "trx;LogFileName=test-results.trx"
Get-ChildItem *coverage.cobertura.xml -R | Select-Object -First 1 | ForEach-Object {
"Move-Item $($_.FullName) ."
Remove-Item $_.Name -ErrorAction Ignore
Move-Item $_.FullName .
}
}
}
'run' {
executeSB -RelativeDir "src/${appName}" {
dotnet run
}
}
'watch' {
executeSB -RelativeDir "src/${appName}" {
dotnet watch
}
}
'createLocalNuget' {
executeSB -Name 'CreateNuget' {
$localNuget = dotnet nuget list source | Select-String "Local \[Enabled" -Context 0,1
if (!$localNuget) {
if (!$LocalNugetFolder) {
$LocalNugetFolder = (Join-Path $PSScriptRoot 'packages')
$null = New-Item 'packages' -ItemType Directory -ErrorAction Ignore
}
dotnet nuget add source $LocalNugetFolder --name Local
}
}
}
'pack' {
if ($Version) {
"Packing with version $Version"
$localNuget = dotnet nuget list source | Select-String "Local \[Enabled" -Context 0,1
if ($localNuget) {
$AppName | ForEach-Object {
$params = @()
if ($Prerelease) {
$params += '--version-suffix'
$params += 'prerelease'
$params += '--include-source'
$params += '--include-symbols'
}
if ($SuppressApiCompat) {
$params += "/p:ApiCompatGenerateSuppressionFile=true" `
}
executeSB -RelativeDir "src/$_" -Name "$currentTask $_" {
$logFile = Join-Path $BuildLogFolder Build.log
$justVersion = $Version.Split('-')[0]
# pack directly to local nuget folder since may not be able to push
# NOTE in Azure/GitHub Package version must be used, locally VersionPrefix
dotnet pack -o ($localNuget.Context.PostContext.Trim()) `
-p:Version=$Version `
-p:PackageVersion=$Version `
-p:AssemblyVersion=$justVersion `
/warnaserror `
"/flp:logfile=`"$logFile`";Append" `
@params
Write-Information "Packed. Logfile is $logFile" -InformationAction Continue
}
}
} else {
throw "Must have a 'Local' NuGet source for testing. e.g. dotnet nuget sources add -name Local -source c:\nupkgs"
}
} else {
throw "Must supply Version for pack"
}
} Default {
Write-Warning "Unknown task $currentTask"
}
}
} finally {
$ErrorActionPreference = $prevPref
}
}