forked from EventStore/EventStore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
176 lines (151 loc) · 6.46 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
[CmdletBinding()]
Param(
[Parameter(HelpMessage="Assembly version number (x.y.z.0 where x.y.z is the semantic version)")]
[string]$Version = "0.0.0.0",
[Parameter(HelpMessage="Configuration (Debug, Release)")]
[ValidateSet("Debug","Release")]
[string]$Configuration = "Release",
[Parameter(HelpMessage="The runtime identifier")]
[string]$Runtime = "win10-x64",
[Parameter(HelpMessage="Build UI (yes,no)")]
[ValidateSet("yes","no")]
[string]$BuildUI = "no",
[Parameter(HelpMessage="Run Tests (yes,no)")]
[ValidateSet("yes","no")]
[string]$RunTests = "no"
)
$NetFramework = "netcoreapp3.1"
Function Write-Info {
Param([string]$message)
Process {
Write-Host $message -ForegroundColor Cyan
}
}
Function Patch-VersionInfo {
Param(
[Parameter(Mandatory=$true)]
[string]$versionInfoFilePath,
[Parameter(Mandatory=$true)]
[string]$version,
[Parameter(Mandatory=$true)]
[string]$branch,
[Parameter(Mandatory=$true)]
[string]$commitHash,
[Parameter(Mandatory=$true)]
[string]$timestamp
)
Process {
$newVersion = 'public static readonly string Version = "' + $version + '";'
$newBranch = 'public static readonly string Branch = "' + $branch + '";'
$newCommitHash = 'public static readonly string Hashtag = "' + $commitHash + '";'
$newTimestamp = 'public static readonly string Timestamp = "' + $timestamp + '";'
$versionPattern = 'public static readonly string Version = ".*";'
$branchPattern = 'public static readonly string Branch = ".*";'
$commitHashPattern = 'public static readonly string Hashtag = ".*";'
$timestampPattern = 'public static readonly string Timestamp = ".*";'
$edited = (Get-Content $versionInfoFilePath) | ForEach-Object {
% {$_ -replace "\/\*+.*\*+\/", "" } |
% {$_ -replace "\/\/+.*$", "" } |
% {$_ -replace "\/\*+.*$", "" } |
% {$_ -replace "^.*\*+\/\b*$", "" } |
% {$_ -replace $versionPattern, $newVersion} |
% {$_ -replace $branchPattern, $newBranch} |
% {$_ -replace $commitHashPattern, $newCommitHash } |
% {$_ -replace $timestampPattern, $newTimestamp}
}
Set-Content -Path $versionInfoFilePath -Value $edited
}
}
#Borrowed from psake
Function Exec
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=0)][scriptblock]$Command,
[Parameter(Mandatory=$false, Position=1)][string]$ErrorMessage = ("Failed executing {0}" -F $Command)
)
& $Command
if ($LASTEXITCODE -ne 0) {
throw ("Exec: " + $ErrorMessage)
}
}
Function Get-GitCommitHash
{
$lastCommitLog = Exec { git log --max-count=1 --pretty=format:%H HEAD } "Cannot execute git log. Ensure that the current directory is a git repository and that git is available on PATH."
return $lastCommitLog
}
Function Get-GitTimestamp
{
$lastCommitLog = Exec { git log --max-count=1 --pretty=format:%aD HEAD } "Cannot execute git log. Ensure that the current directory is a git repository and that git is available on PATH."
return $lastCommitLog
}
Function Get-GitBranchOrTag
{
$revParse = Exec { git rev-parse --abbrev-ref HEAD } "Cannot execute git rev-parse. Ensure that the current directory is a git repository and that git is available on PATH."
if ($revParse -ne "HEAD") {
return $revParse
}
$describeTags = Exec { git describe --tags } "Cannot execute git describe. Ensure that the current directory is a git repository and that git is available on PATH."
return $describeTags
}
Function Start-Build{
#Make compatible with Powershell 2
if(!$PSScriptRoot) { $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent }
#Configuration
$platform = "x64"
$baseDirectory = $PSScriptRoot
$srcDirectory = Join-Path $baseDirectory "src"
$binDirectory = Join-Path $baseDirectory "bin"
$libsDirectory = Join-Path $srcDirectory "libs"
$eventStoreSolution = Join-Path $srcDirectory "EventStore.sln"
$uiSrcDirectory = Join-Path $srcDirectory "EventStore.UI\"
$uiDistDirectory = Join-Path $srcDirectory "EventStore.ClusterNode.Web\clusternode-web\"
Write-Info "Build Configuration"
Write-Info "-------------------"
Write-Info "Version: $Version"
Write-Info "Platform: $platform"
Write-Info "Configuration: $Configuration"
Write-Info "Runtime: $Runtime"
Write-Info "Build UI: $BuildUI"
Write-Info "Run Tests: $RunTests"
#Build Event Store UI
if ($BuildUI -eq "yes") {
#Build the UI
if (Test-Path $uiDistDirectory) {
Remove-Item -Recurse -Force $uiDistDirectory
}
Push-Location $uiSrcDirectory
if(-Not (Test-Path (Join-Path $uiSrcDirectory "package.json"))) {
Exec { git submodule update --init ./ }
}
Exec { npm install bower@~1.8.4 -g }
Exec { bower install --allow-root }
Exec { npm install gulp@~3.8.8 -g --force}
Exec { npm install }
Exec { gulp dist }
Exec { mv es-dist $uiDistDirectory }
Pop-Location
}
#Build Event Store (Patch AssemblyInfo, Build, Revert AssemblyInfo)
Remove-Item -Force -Recurse $binDirectory -ErrorAction SilentlyContinue > $null
$commitHash = Get-GitCommitHash
$timestamp = Get-GitTimestamp
$branchName = Get-GitBranchOrTag
$versionInfoFile = Resolve-Path (Join-Path $srcDirectory (Join-Path "EventStore.Common" (Join-Path "Utils" "VersionInfo.cs"))) -Relative
try {
Write-Info "Patching $versionInfoFile with product information."
Patch-VersionInfo -versionInfoFilePath $versionInfoFile -version $Version -commitHash $commitHash -timestamp $timestamp -branch $branchName
Exec { dotnet build -c $configuration --runtime=$Runtime --framework=$NetFramework /p:Version=$Version /p:Platform=x64 $eventStoreSolution }
} finally {
Write-Info "Reverting $versionInfoFile to original state."
& { git checkout --quiet $versionInfoFile }
}
if($RunTests -eq "yes"){
(Get-ChildItem -Attributes Directory src | % FullName) -Match '.Tests' | `
ForEach-Object {
dotnet test -v normal -c $Configuration --no-build --logger trx --results-directory testResults $_ -- RunConfiguration.TargetPlatform=x64
if (-Not $?) { throw "Exit code is $?" }
}
}
}
Start-Build