forked from AnthonySteele/JustEat.StatsD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuild.ps1
107 lines (88 loc) · 3.57 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
param(
[Parameter(Mandatory=$false)][string] $Configuration = "Release",
[Parameter(Mandatory=$false)][string] $OutputPath = "",
[Parameter(Mandatory=$false)][bool] $RunTests = $true,
[Parameter(Mandatory=$false)][bool] $CreatePackages = $true
)
$ErrorActionPreference = "Stop"
$solutionPath = Split-Path $MyInvocation.MyCommand.Definition
$sdkFile = Join-Path $solutionPath "global.json"
$dotnetVersion = (Get-Content $sdkFile | ConvertFrom-Json).sdk.version
if ($OutputPath -eq "") {
$OutputPath = "$(Convert-Path "$PSScriptRoot")\artifacts"
}
$installDotNetSdk = $false;
if (($null -eq (Get-Command "dotnet.exe" -ErrorAction SilentlyContinue)) -and ($null -eq (Get-Command "dotnet" -ErrorAction SilentlyContinue))) {
Write-Host "The .NET Core SDK is not installed."
$installDotNetSdk = $true
}
else {
Try {
$installedDotNetVersion = (dotnet --version 2>&1 | Out-String).Trim()
}
Catch {
$installedDotNetVersion = "?"
}
if ($installedDotNetVersion -ne $dotnetVersion) {
Write-Host "The required version of the .NET Core SDK is not installed. Expected $dotnetVersion."
$installDotNetSdk = $true
}
}
if ($installDotNetSdk -eq $true) {
$env:DOTNET_INSTALL_DIR = "$(Convert-Path "$PSScriptRoot")\.dotnetcli"
if (!(Test-Path $env:DOTNET_INSTALL_DIR)) {
mkdir $env:DOTNET_INSTALL_DIR | Out-Null
$installScript = Join-Path $env:DOTNET_INSTALL_DIR "install.ps1"
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor "Tls12"
Invoke-WebRequest "https://dot.net/v1/dotnet-install.ps1" -OutFile $installScript -UseBasicParsing
& $installScript -Version "$dotnetVersion" -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath
}
$env:PATH = "$env:DOTNET_INSTALL_DIR;$env:PATH"
$dotnet = "$env:DOTNET_INSTALL_DIR\dotnet"
} else {
$dotnet = "dotnet"
}
function DotNetBuild { param([string]$Project, [string]$Configuration, [string]$Framework)
& $dotnet build $Project --output (Join-Path $OutputPath $Framework) --framework $Framework --configuration $Configuration
if ($LASTEXITCODE -ne 0) {
throw "dotnet build failed with exit code $LASTEXITCODE"
}
}
function DotNetTest { param([string]$Project)
& $dotnet test $Project
if ($LASTEXITCODE -ne 0) {
throw "dotnet test failed with exit code $LASTEXITCODE"
}
}
function DotNetPack { param([string]$Project, [string]$Configuration)
& $dotnet pack $Project --output $OutputPath --configuration $Configuration --include-symbols --include-source
if ($LASTEXITCODE -ne 0) {
throw "dotnet pack failed with exit code $LASTEXITCODE"
}
}
$projects = @(
(Join-Path $solutionPath "src\JustEat.StatsD\JustEat.StatsD.csproj")
)
$testProjects = @(
(Join-Path $solutionPath "tests\JustEat.StatsD.Tests\JustEat.StatsD.Tests.csproj")
)
$packageProjects = @(
(Join-Path $solutionPath "src\JustEat.StatsD\JustEat.StatsD.csproj")
)
Write-Host "Building $($projects.Count) projects..." -ForegroundColor Green
ForEach ($project in $projects) {
DotNetBuild $project $Configuration "netstandard2.0"
DotNetBuild $project $Configuration "net451"
}
if ($RunTests -eq $true) {
Write-Host "Testing $($testProjects.Count) project(s)..." -ForegroundColor Green
ForEach ($project in $testProjects) {
DotNetTest $project
}
}
if ($CreatePackages -eq $true) {
Write-Host "Creating $($packageProjects.Count) package(s)..." -ForegroundColor Green
ForEach ($project in $packageProjects) {
DotNetPack $project $Configuration
}
}