-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMake.ps1
179 lines (159 loc) · 4.62 KB
/
Make.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
<#
.SYNOPSIS
Invokes various build commands.
.DESCRIPTION
This script is similar to a makefile.
Copyright Jeffrey Sharp
SPDX-License-Identifier: ISC
#>
[CmdletBinding(DefaultParameterSetName="Test")]
param (
# Clean.
[Parameter(Mandatory, ParameterSetName="Clean")]
[switch] $Clean
,
# Build.
[Parameter(Mandatory, ParameterSetName="Build")]
[switch] $Build
,
# Build, run tests.
[Parameter(ParameterSetName="Test")]
[switch] $Test
,
# Build, run tests, produce code coverage report.
[Parameter(Mandatory, ParameterSetName="Coverage")]
[switch] $Coverage
,
# Do not build before running tests.
[Parameter(ParameterSetName="Test")]
[Parameter(ParameterSetName="Coverage")]
[switch] $NoBuild
,
# The configuration to build: Debug or Release. The default is Debug.
[Parameter(ParameterSetName="Build")]
[Parameter(ParameterSetName="Test")]
[Parameter(ParameterSetName="Coverage")]
[ValidateSet("Debug", "Release")]
[string] $Configuration = "Debug"
,
# Update .NET CLI 'local tool' plugins.
[Parameter(Mandatory, ParameterSetName="UpdateLocalTools")]
[switch] $UpdateLocalTools
)
#Requires -Version 5
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$Command = $PSCmdlet.ParameterSetName
if ($Command -eq "Test") { $Test = $true }
# http://patorjk.com/software/taag/#p=display&f=Slant
Write-Host -ForegroundColor Cyan @'
____ _____ __ ___ ______ _ __
/ __ \/ ___// |/ /___ __________/_ __/________ _____ _____(_) /_
/ /_/ /\__ \/ /|_/ / __ `/ ___/ ___// / / ___/ __ `/ __ \/ ___/ / __/
/ _, _/___/ / / / / /_/ (__ |__ )/ / / / / /_/ / / / (__ ) / /_
/_/ |_|/____/_/ /_/\__,_/____/____//_/ /_/ \__,_/_/ /_/____/_/\__/
'@
function Main {
if ($UpdateLocalTools) {
Update-LocalTools
return
}
if ($Clean) {
Invoke-Clean
return
}
if (!$NoBuild) {
Invoke-Build
}
if ($Test -or $Coverage) {
Invoke-Test
}
if ($Coverage) {
Export-CoverageReport
}
}
function Update-LocalTools {
Write-Phase "Update Local Tools"
Invoke-DotNet tool update dotnet-reportgenerator-globaltool
}
function Invoke-Clean {
Write-Phase "Clean"
Invoke-Git -Arguments @(
"clean", "-fxd", # Delete all untracked files in directory tree
"-e", "*.suo", # Keep Visual Studio < 2015 local options
"-e", "*.user", # Keep Visual Studio < 2015 local options
"-e", ".vs/" # Keep Visual Studio >= 2015 local options
)
}
function Invoke-Build {
Write-Phase "Build"
Invoke-DotNet build --configuration:$Configuration
}
function Invoke-Test {
Write-Phase "Test$(if ($Coverage) {" + Coverage"})"
Remove-Item coverage\raw -Recurse -ErrorAction SilentlyContinue
Invoke-DotNet -Arguments @(
"test"
"--nologo"
"--no-build"
"--configuration:$Configuration"
if ($Coverage) {
"--settings:Coverlet.runsettings"
"--results-directory:coverage\raw"
}
)
}
function Export-CoverageReport {
Write-Phase "Coverage Report"
Invoke-DotNet -Arguments "tool", "restore"
Invoke-DotNet -Arguments @(
"reportgenerator"
"-reports:coverage\raw\**\coverage.opencover.xml"
"-targetdir:coverage"
"-reporttypes:Cobertura;JsonSummary;Html_Dark;Badges"
"-verbosity:Warning"
)
$Summary = (Get-Content coverage\Summary.json -Raw | ConvertFrom-Json).summary
@(
""
"Coverage:"
" Methods: {0,7:F3}%" -f $Summary.methodcoverage
" Lines: {0,7:F3}%" -f $Summary.linecoverage
" Branches: {0,7:F3}%" -f $Summary.branchcoverage
""
) | Write-Host
if ($Summary.methodcoverage + $Summary.linecoverage + $Summary.branchcoverage -lt 300) {
Write-Warning "Coverage is below 100%."
}
}
function Invoke-DotNet {
param (
[Parameter(Mandatory, ValueFromRemainingArguments)]
[string[]] $Arguments
)
& dotnet $Arguments
if ($LASTEXITCODE -ne 0) { throw "dotnet exited with an error." }
}
function Invoke-Git {
param (
[Parameter(Mandatory, ValueFromRemainingArguments)]
[string[]] $Arguments
)
& git $Arguments
if ($LASTEXITCODE -ne 0) { throw "git exited with an error." }
}
function Write-Phase {
param (
[Parameter(Mandatory)]
[string] $Name
)
Write-Host "`n===== $Name =====`n" -ForegroundColor Cyan
}
# Invoke Main
try {
Push-Location $PSScriptRoot
Main
}
finally {
Pop-Location
}