forked from dotnet/docfx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
236 lines (201 loc) · 8.16 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
param(
[string] $configuration = "Release",
[switch] $raw = $false,
[switch] $prod = $false
)
################################################################################################
# Usage:
# Run build.ps1
# [-configuration Configuration]: Default to be Release
# [-raw]: If it's set, the build process will skip updating template
# [-prod]: If it's set, the build process will update version
################################################################################################
$ErrorActionPreference = 'Stop'
$releaseBranch = "master"
$dotnetCommand = "dotnet"
$nugetCommand = "$env:LOCALAPPDATA\Nuget\Nuget.exe"
$gitCommand = "git"
$framework = "net461"
$packageVersion = "1.0.0"
$assemblyVersion = "1.0.0.0"
$scriptPath = $MyInvocation.MyCommand.Path
$scriptHome = Split-Path $scriptPath
$versionCsFolderPath = $scriptHome + "\TEMP\"
$versionCsFilePath = $versionCsFolderPath + "version.cs"
$global:LASTEXITCODE = $null
Push-Location $scriptHome
function NugetPack {
param($basepath, $nuspec, $version)
if (Test-Path $nuspec) {
& $nugetCommand pack $nuspec -Version $version -OutputDirectory artifacts\$configuration -BasePath $basepath
ProcessLastExitCode $lastexitcode "$nugetCommand pack $nuspec -Version $version -OutputDirectory artifacts\$configuration -BasePath $basepath"
}
}
function ProcessLastExitCode {
param($exitCode, $msg)
if ($exitCode -eq 0) {
Write-Host "Success: $msg
" -ForegroundColor Green
}
else {
Write-Host "Error $($exitCode): $msg
" -ForegroundColor Red
Pop-Location
Exit 1
}
}
function ValidateCommand {
param($command)
return (Get-Command $command -ErrorAction SilentlyContinue) -ne $null
}
# Check if dotnet cli exists globally
if (-not(ValidateCommand("dotnet"))) {
ProcessLastExitCode 1 "Dotnet CLI is not successfully configured. Please follow https://www.microsoft.com/net/core to install .NET Core."
}
# Check if nuget.exe exists
if (-not(ValidateCommand($nugetCommand))) {
Write-Host "Downloading NuGet.exe..."
mkdir -Path "$env:LOCALAPPDATA\Nuget" -Force
$ProgressPreference = 'SilentlyContinue'
[Net.WebRequest]::DefaultWebProxy.Credentials = [Net.CredentialCache]::DefaultCredentials
Invoke-WebRequest 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe' -OutFile $nugetCommand
}
if ($raw -eq $false) {
& ".\UpdateTemplate.cmd"
ProcessLastExitCode $lastexitcode "Update template"
}
else {
Write-Host "Skip updating template"
}
if ($prod -eq $true) {
Write-Host "Updating version from ReleaseNote.md and GIT commit info"
if (-not(ValidateCommand($gitCommand))) {
ProcessLastExitCode 1 "Git is required however it is not installed."
}
$branch = & $gitCommand rev-parse --abbrev-ref HEAD
ProcessLastExitCode $lastexitcode "Get GIT branch name $branch"
$version = "v1", "0", "0"
$firstLine = Get-Content ReleaseNote.md | Select-Object -First 1
if ($firstLine -match ".*(v[0-9.]+)") {
$mainVersion = ($matches[1] -split '\.')
for ($i = 0; $i -lt $mainVersion.length -and $i -lt 3; $i++) {
$version[$i] = $mainVersion[$i]
}
}
$commitInfo = (& $gitCommand describe --tags) -split '-'
ProcessLastExitCode $lastexitcode "Get GIT commit information $commitInfo"
if ($commitInfo.length -gt 1) {
$revision = $commitInfo[1].PadLeft(4, '0')
} else {
$revision = '0000'
}
$assemblyVersion = (($version + '0') -join '.').Substring(1)
$assemblyFileVersion = (($version + $revision) -join '.').Substring(1)
if ($branch -ne $releaseBranch) {
$abbrev = $commitInfo[2]
$packageVersion = ((($version -join '.'), "alpha", $revision, $abbrev) -join '-').Substring(1)
}
else {
$packageVersion = ($version -join ".").Substring(1)
}
if (-not(Test-Path -Path $versionCsFolderPath)) {
New-Item -ItemType directory -Path $versionCsFolderPath
}
"
[assembly: System.Reflection.AssemblyVersionAttribute(""$assemblyVersion"")]
[assembly: System.Reflection.AssemblyFileVersionAttribute(""$assemblyFileVersion"")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute(""$assemblyFileVersion"")]
" | Out-File -FilePath $versionCsFilePath
Write-Host "Version file saved to $versionCsFilePath" -ForegroundColor Green
}
Write-Host "Using package version $packageVersion, and assembly version $assemblyVersion, assembly file version $assemblyFileVersion"
foreach ($sln in (Get-ChildItem *.sln)) {
Write-Host "Start building $($sln.FullName)"
& dotnet restore $sln.FullName /p:Version=$packageVersion
ProcessLastExitCode $lastexitcode "dotnet restore $($sln.FullName) /p:Version=$packageVersion"
& dotnet build $sln.FullName -c $configuration -v n /m:1
ProcessLastExitCode $lastexitcode "dotnet build $($sln.FullName) -c $configuration -v n /m:1"
}
# Run unit test cases
Write-Host "Start running unit test"
foreach ($proj in (Get-ChildItem "test" -Include "*.csproj" -Recurse)) {
if (($proj.Name) -ne "docfx.E2E.Tests.csproj") {
& dotnet test $proj.FullName --no-build -c $configuration
ProcessLastExitCode $lastexitcode "dotnet test $($proj.FullName) --no-build -c $configuration"
}
}
# dotnet pack first
foreach ($proj in (Get-ChildItem -Path ("src","plugins") -Include *.csproj -Exclude 'docfx.msbuild.csproj' -Recurse)) {
& dotnet pack $proj.FullName -c $configuration -o $scriptHome\artifacts\$configuration --no-build /p:Version=$packageVersion
ProcessLastExitCode $lastexitcode "dotnet pack $($proj.FullName) -c $configuration -o $scriptHome\artifacts\$configuration --no-build /p:Version=$packageVersion"
}
# Pack docfx.console
$docfxTarget = "target\$configuration\docfx";
if (-not(Test-Path -path $docfxTarget)) {
New-Item $docfxTarget -Type Directory
}
Copy-Item -Path "src\nuspec\docfx.console\build" -Destination $docfxTarget -Force -Recurse
Copy-Item -Path "src\nuspec\docfx.console\content" -Destination $docfxTarget -Force -Recurse
$packages = @{
"docfx" = @{
"proj" = $null;
"nuspecs" = @("src\nuspec\docfx.console\docfx.console.nuspec");
};
"AzureMarkdownRewriterTool" = @{
"proj" = $null;
"nuspecs" = @("src\nuspec\AzureMarkdownRewriterTool\AzureMarkdownRewriterTool.nuspec");
};
"DfmHttpService" = @{
"proj" = $null;
"nuspecs" = @("src\nuspec\DfmHttpService\DfmHttpService.nuspec");
};
"MergeDeveloperComments" = @{
"proj" = $null;
"nuspecs" = @("src\nuspec\MergeDeveloperComments\MergeDeveloperComments.nuspec");
};
"MergeSourceInfo" = @{
"proj" = $null;
"nuspecs" = @("src\nuspec\MergeSourceInfo\MergeSourceInfo.nuspec");
};
"TocConverter" = @{
"proj" = $null;
"nuspecs" = @("src\nuspec\TocConverter\TocConverter.nuspec");
};
}
# Pack plugins and tools
# TODO: clean up docfx.msbuild.csproj
foreach ($proj in (Get-ChildItem -Path ("src", "plugins", "tools") -Include *.csproj -Exclude 'docfx.msbuild.csproj' -Recurse)) {
$name = $proj.BaseName
if ($packages.ContainsKey($name)) {
$packages[$name].proj = $proj
}
$nuspecs = Join-Path $proj.DirectoryName "*.nuspec" -Resolve
if ($nuspecs -ne $null) {
if ($packages.ContainsKey($name)) {
$packages[$name].nuspecs = $packages[$name].nuspecs + $nuspecs
} else {
$packages[$name] = @{
nuspecs = $nuspecs;
proj = $proj;
}
}
}
}
foreach ($name in $packages.Keys) {
$val = $packages[$name]
$proj = $val.proj
if ($proj -eq $null) {
Write-Host $package
ProcessLastExitCode 1 "$name does not have project found"
}
$outputFolder = "$scriptHome\target\$configuration\$name"
# publish to target folder before pack
& dotnet publish $proj.FullName -c $configuration -f $framework -o $outputFolder
ProcessLastExitCode $lastexitcode "dotnet publish $($proj.FullName) -c $configuration -f $framework -o $outputFolder"
$nuspecs = $val.nuspecs
foreach ($nuspec in $nuspecs) {
NugetPack $outputFolder $nuspec $packageVersion
}
}
Write-Host "Build succeeds." -ForegroundColor Green
Pop-Location