This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.ps1
153 lines (127 loc) · 4.67 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
# ----------------------------------------------
# Build script
# ----------------------------------------------
param
(
[switch] $Release,
[switch] $ExcludeTests,
[switch] $Pack,
[switch] $OnlyNetStandard,
[switch] $ClearOnly
)
$ErrorActionPreference = "Stop"
# ----------------------------------------------
# Helper functions
# ----------------------------------------------
function Test-IsWindows
{
[environment]::OSVersion.Platform -ne "Unix"
}
function Invoke-Cmd ($cmd)
{
Write-Host $cmd -ForegroundColor DarkCyan
if (Test-IsWindows) { $cmd = "cmd.exe /C $cmd" }
Invoke-Expression -Command $cmd
if ($LastExitCode -ne 0) { Write-Error "An error occured when executing '$cmd'."; return }
}
function Write-DotnetVersion
{
$dotnetVersion = Invoke-Cmd "dotnet --version"
Write-Host ".NET Core runtime version: $dotnetVersion" -ForegroundColor Cyan
}
function dotnet-restore ($project, $argv) { Invoke-Cmd "dotnet restore $project $argv" }
function dotnet-build ($project, $argv) { Invoke-Cmd "dotnet build $project $argv" }
function dotnet-run ($project, $argv) { Invoke-Cmd "dotnet run --project $project $argv" }
function dotnet-test ($project, $argv) { Invoke-Cmd "dotnet test $project $argv" }
function dotnet-pack ($project, $argv) { Invoke-Cmd "dotnet pack $project $argv" }
function Test-Version ($project)
{
if ($env:APPVEYOR_REPO_TAG -eq $true)
{
Write-Host "Matching version against git tag..." -ForegroundColor Magenta
[xml] $xml = Get-Content $project
[string] $version = $xml.Project.PropertyGroup.Version
[string] $gitTag = $env:APPVEYOR_REPO_TAG_NAME
Write-Host "Project version: $version" -ForegroundColor Cyan
Write-Host "Git tag version: $gitTag" -ForegroundColor Cyan
if (!$gitTag.EndsWith($version))
{
Write-Error "Version and Git tag do not match."
}
}
}
function Update-AppVeyorBuildVersion ($project)
{
if ($env:APPVEYOR -eq $true)
{
Write-Host "Updating AppVeyor build version..." -ForegroundColor Magenta
[xml]$xml = Get-Content $project
$version = $xml.Project.PropertyGroup.Version
$buildVersion = "$version-$env:APPVEYOR_BUILD_NUMBER"
Write-Host "Setting AppVeyor build version to $buildVersion."
Update-AppveyorBuild -Version $buildVersion
}
}
function Remove-OldBuildArtifacts
{
Write-Host "Deleting old build artifacts..." -ForegroundColor Magenta
Get-ChildItem -Include "bin", "obj" -Recurse -Directory `
| ForEach-Object {
Write-Host "Removing folder $_" -ForegroundColor DarkGray
Remove-Item $_ -Recurse -Force }
}
function Get-TargetFrameworks ($projFile)
{
[xml]$proj = Get-Content $projFile
($proj.Project.PropertyGroup.TargetFrameworks).Split(";")
}
function Get-NetCoreTargetFramework ($projFile)
{
Get-TargetFrameworks $projFile | where { $_ -like "netstandard*" -or $_ -like "netcoreapp*" }
}
function Get-FrameworkArg ($projFile)
{
if ($OnlyNetStandard.IsPresent) {
$fw = Get-NetCoreTargetFramework $projFile
"-f $fw"
}
else { "" }
}
# ----------------------------------------------
# Main
# ----------------------------------------------
if ($ClearOnly.IsPresent) {
Remove-OldBuildArtifacts
return
}
$giraffeTasks = ".\src\Giraffe.Tasks\Giraffe.Tasks.fsproj"
$giraffeTasksTests = ".\tests\Giraffe.Tasks.Tests\Giraffe.Tasks.Tests.fsproj"
Update-AppVeyorBuildVersion $giraffeTasks
Test-Version $giraffeTasks
Write-DotnetVersion
Remove-OldBuildArtifacts
$configuration = if ($Release.IsPresent) { "Release" } else { "Debug" }
Write-Host "Building Giraffe.Tasks..." -ForegroundColor Magenta
$framework = Get-FrameworkArg $giraffeTasks
dotnet-restore $giraffeTasks
dotnet-build $giraffeTasks "-c $configuration $framework"
if (!$ExcludeTests.IsPresent)
{
Write-Host "Building and running tests..." -ForegroundColor Magenta
$framework = Get-FrameworkArg $giraffeTasksTests
# Currently dotnet test does not work for net461 on Linux/Mac
# See: https://github.com/Microsoft/vstest/issues/1318
if (!(Test-IsWindows)) {
Write-Warning "Running tests only for .NET Core build, because dotnet test does not support net4x tests on Linux/Mac at the moment (see: https://github.com/Microsoft/vstest/issues/1318)."
$fw = Get-NetCoreTargetFramework $giraffeTasksTests
$framework = "-f $fw"
}
dotnet-restore $giraffeTasksTests
dotnet-build $giraffeTasksTests $framework
dotnet-test $giraffeTasksTests $framework
}
if ($Pack.IsPresent)
{
Write-Host "Packaging all NuGet packages..." -ForegroundColor Magenta
dotnet-pack $giraffeTasks "-c $configuration"
}