Skip to content

Commit 9f4784f

Browse files
committed
Install .NET SDKs during build.
1 parent 7fe98f6 commit 9f4784f

4 files changed

Lines changed: 321 additions & 9 deletions

File tree

build.ps1

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
1-
dotnet build .\Examples.sln
2-
if ($NULL -eq $env:APPVEYOR_BUILD_NUMBER -and $IsWindows) {
3-
# Only build the Service Fabric demo when it's not AppVeyor and the current environment is Windows.
4-
nuget restore .\src\ServiceFabricDemo\AutofacServiceFabricDemo.sln
5-
msbuild .\src\ServiceFabricDemo\AutofacServiceFabricDemo.sln /t:Rebuild /verbosity:Minimal /p:Configuration=Release /p:Platform=x64
6-
}
1+
########################
2+
# THE BUILD!
3+
########################
4+
5+
Push-Location $PSScriptRoot
6+
try {
7+
Import-Module $PSScriptRoot/build/Autofac.Build.psd1 -Force
8+
9+
$globalJson = (Get-Content "$PSScriptRoot/global.json" | ConvertFrom-Json -NoEnumerate);
10+
11+
$sdkVersion = $globalJson.sdk.version
12+
13+
# Install dotnet SDK versions during CI. In a local build we assume you have
14+
# everything installed; on CI we'll force the install. If you install _any_
15+
# SDKs, you have to install _all_ of them because you can't install SDKs in
16+
# two different locations. dotnet CLI locates SDKs relative to the
17+
# executable.
18+
if ($Null -ne $env:APPVEYOR_BUILD_NUMBER) {
19+
Install-DotNetCli -Version $sdkVersion
20+
foreach ($additional in $globalJson.additionalSdks)
21+
{
22+
Install-DotNetCli -Version $additional;
23+
}
24+
}
25+
26+
# Write out dotnet information
27+
& dotnet --info
28+
29+
# Build/package
30+
Write-Message "Building projects"
31+
&dotnet build $PSScriptRoot\Examples.sln
32+
if ($NULL -eq $env:APPVEYOR_BUILD_NUMBER -and $IsWindows) {
33+
# Only build the Service Fabric demo when it's not AppVeyor and the current environment is Windows.
34+
nuget restore $PSScriptRoot\src\ServiceFabricDemo\AutofacServiceFabricDemo.sln
35+
msbuild $PSScriptRoot\src\ServiceFabricDemo\AutofacServiceFabricDemo.sln /t:Rebuild /verbosity:Minimal /p:Configuration=Release /p:Platform=x64
36+
}
37+
38+
# Finished
39+
Write-Message "Build finished"
40+
}
41+
finally {
42+
Pop-Location
43+
}

build/Autofac.Build.psd1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@{
2+
RootModule = '.\Autofac.Build.psm1'
3+
ModuleVersion = '0.3.0'
4+
GUID = '55d3f738-f48f-4497-9b2c-ecd90ec1f978'
5+
Author = 'Autofac Contributors'
6+
CompanyName = 'Autofac'
7+
Description = 'Build support for Autofac projects.'
8+
FunctionsToExport = '*'
9+
CmdletsToExport = '*'
10+
VariablesToExport = '*'
11+
AliasesToExport = '*'
12+
ModuleList = @()
13+
FileList = @()
14+
PrivateData = ''
15+
}

build/Autofac.Build.psm1

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
# EXIT CODES
2+
# 1: dotnet packaging failure
3+
# 2: dotnet publishing failure
4+
# 3: Unit test failure
5+
# 4: dotnet / NuGet package restore failure
6+
7+
<#
8+
.SYNOPSIS
9+
Gets the set of directories in which projects are available for compile/processing.
10+
11+
.PARAMETER RootPath
12+
Path where searching for project directories should begin.
13+
#>
14+
function Get-DotNetProjectDirectory {
15+
[CmdletBinding()]
16+
Param(
17+
[Parameter(Mandatory = $True, ValueFromPipeline = $False, ValueFromPipelineByPropertyName = $False)]
18+
[ValidateNotNullOrEmpty()]
19+
[string]
20+
$RootPath
21+
)
22+
23+
Get-ChildItem -Path $RootPath -Recurse -Include "*.csproj" | Select-Object @{ Name = "ParentFolder"; Expression = { $_.Directory.FullName.TrimEnd("\") } } | Select-Object -ExpandProperty ParentFolder
24+
}
25+
26+
<#
27+
.SYNOPSIS
28+
Runs the dotnet CLI install script from GitHub to install a project-local
29+
copy of the CLI.
30+
#>
31+
function Install-DotNetCli {
32+
[CmdletBinding()]
33+
Param(
34+
[string]
35+
$Version = "Latest"
36+
)
37+
Write-Message "Installing .NET SDK version $Version"
38+
39+
$callerPath = Split-Path $MyInvocation.PSCommandPath
40+
$installDir = Join-Path -Path $callerPath -ChildPath ".dotnet/cli"
41+
if (!(Test-Path $installDir)) {
42+
New-Item -ItemType Directory -Path "$installDir" | Out-Null
43+
}
44+
45+
# Download the dotnet CLI install script
46+
if ($IsWindows) {
47+
if (!(Test-Path ./.dotnet/dotnet-install.ps1)) {
48+
Invoke-WebRequest "https://dot.net/v1/dotnet-install.ps1" -OutFile "./.dotnet/dotnet-install.ps1"
49+
}
50+
51+
& ./.dotnet/dotnet-install.ps1 -InstallDir "$installDir" -Version $Version
52+
} else {
53+
if (!(Test-Path ./.dotnet/dotnet-install.sh)) {
54+
Invoke-WebRequest "https://dot.net/v1/dotnet-install.sh" -OutFile "./.dotnet/dotnet-install.sh"
55+
}
56+
57+
& bash ./.dotnet/dotnet-install.sh --install-dir "$installDir" --version $Version
58+
}
59+
60+
Add-Path "$installDir"
61+
}
62+
63+
<#
64+
.SYNOPSIS
65+
Appends a given value to the path but only if the value does not yet exist within the path.
66+
.PARAMETER Path
67+
The path to append.
68+
#>
69+
function Add-Path {
70+
[CmdletBinding()]
71+
Param(
72+
[ValidateNotNullOrEmpty()]
73+
[string]
74+
$Path
75+
)
76+
77+
$pathSeparator = ":";
78+
79+
if ($IsWindows) {
80+
$pathSeparator = ";";
81+
}
82+
83+
$pathValues = $env:PATH.Split($pathSeparator);
84+
if ($pathValues -Contains $Path) {
85+
return;
86+
}
87+
88+
$env:PATH = "${Path}${pathSeparator}$env:PATH"
89+
}
90+
91+
<#
92+
.SYNOPSIS
93+
Builds a project using dotnet cli.
94+
.DESCRIPTION
95+
Builds a project in a specified directory using the dotnet cli.
96+
.PARAMETER DirectoryName
97+
The path to the directory containing the project to build.
98+
#>
99+
function Invoke-DotNetBuild {
100+
[CmdletBinding()]
101+
Param(
102+
[Parameter(Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)]
103+
[ValidateNotNull()]
104+
[System.IO.DirectoryInfo[]]
105+
$ProjectDirectory
106+
)
107+
Process {
108+
foreach ($Project in $ProjectDirectory) {
109+
& dotnet build ("""" + $Project.FullName + """") --configuration Release
110+
if ($LASTEXITCODE -ne 0) {
111+
exit 1
112+
}
113+
}
114+
}
115+
}
116+
117+
<#
118+
.SYNOPSIS
119+
Invokes the dotnet utility to package a project.
120+
121+
.PARAMETER ProjectDirectory
122+
Path to the directory containing the project to package.
123+
124+
.PARAMETER PackagesPath
125+
Path to the "artifacts/packages" folder where packages should go.
126+
127+
.PARAMETER VersionSuffix
128+
The version suffix to use for the NuGet package version.
129+
#>
130+
function Invoke-DotNetPack {
131+
[CmdletBinding()]
132+
Param(
133+
[Parameter(Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)]
134+
[ValidateNotNull()]
135+
[System.IO.DirectoryInfo[]]
136+
$ProjectDirectory,
137+
138+
[Parameter(Mandatory = $True, ValueFromPipeline = $False)]
139+
[ValidateNotNull()]
140+
[System.IO.DirectoryInfo]
141+
$PackagesPath,
142+
143+
[Parameter(Mandatory = $True, ValueFromPipeline = $False)]
144+
[AllowEmptyString()]
145+
[string]
146+
$VersionSuffix
147+
)
148+
Begin {
149+
New-Item -Path $PackagesPath -ItemType Directory -Force | Out-Null
150+
}
151+
Process {
152+
foreach ($Project in $ProjectDirectory) {
153+
if ($VersionSuffix -eq "") {
154+
& dotnet build ("""" + $Project.FullName + """") --configuration Release
155+
}
156+
else {
157+
& dotnet build ("""" + $Project.FullName + """") --configuration Release --version-suffix $VersionSuffix
158+
}
159+
if ($LASTEXITCODE -ne 0) {
160+
exit 1
161+
}
162+
163+
if ($VersionSuffix -eq "") {
164+
& dotnet pack ("""" + $Project.FullName + """") --configuration Release --output $PackagesPath
165+
}
166+
else {
167+
& dotnet pack ("""" + $Project.FullName + """") --configuration Release --version-suffix $VersionSuffix --output $PackagesPath
168+
}
169+
if ($LASTEXITCODE -ne 0) {
170+
exit 1
171+
}
172+
}
173+
}
174+
}
175+
176+
<#
177+
.SYNOPSIS
178+
Invokes dotnet test command.
179+
180+
.PARAMETER ProjectDirectory
181+
Path to the directory containing the project to package.
182+
#>
183+
function Invoke-Test {
184+
[CmdletBinding()]
185+
Param(
186+
[Parameter(Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)]
187+
[ValidateNotNull()]
188+
[System.IO.DirectoryInfo[]]
189+
$ProjectDirectory
190+
)
191+
Process {
192+
foreach ($Project in $ProjectDirectory) {
193+
Push-Location $Project
194+
195+
& dotnet test `
196+
--configuration Release `
197+
--logger:trx `
198+
/p:CollectCoverage=true `
199+
/p:CoverletOutput="../../artifacts/coverage/$($Project.Name)/" `
200+
/p:CoverletOutputFormat="json%2clcov" `
201+
/p:ExcludeByAttribute=CompilerGeneratedAttribute `
202+
/p:ExcludeByAttribute=GeneratedCodeAttribute `
203+
/p:Exclude="[Autofac.Test.Scenarios.ScannedAssembly]*"
204+
205+
if ($LASTEXITCODE -ne 0) {
206+
Pop-Location
207+
exit 3
208+
}
209+
210+
Pop-Location
211+
}
212+
}
213+
}
214+
215+
<#
216+
.SYNOPSIS
217+
Restores dependencies using the dotnet utility.
218+
219+
.PARAMETER ProjectDirectory
220+
Path to the directory containing the project with dependencies to restore.
221+
#>
222+
function Restore-DependencyPackages {
223+
[CmdletBinding()]
224+
Param(
225+
[Parameter(Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)]
226+
[ValidateNotNull()]
227+
[System.IO.DirectoryInfo[]]
228+
$ProjectDirectory
229+
)
230+
Process {
231+
foreach ($Project in $ProjectDirectory) {
232+
& dotnet restore ("""" + $Project.FullName + """") --no-cache
233+
if ($LASTEXITCODE -ne 0) {
234+
exit 4
235+
}
236+
}
237+
}
238+
}
239+
240+
<#
241+
.SYNOPSIS
242+
Writes a build progress message to the host.
243+
244+
.PARAMETER Message
245+
The message to write.
246+
#>
247+
function Write-Message {
248+
[CmdletBinding()]
249+
Param(
250+
[Parameter(Mandatory = $True, ValueFromPipeline = $False, ValueFromPipelineByPropertyName = $False)]
251+
[ValidateNotNullOrEmpty()]
252+
[string]
253+
$Message
254+
)
255+
256+
Write-Host "[BUILD] $Message" -ForegroundColor Cyan
257+
}

global.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"sdk": {
3-
"version": "6.0.403",
4-
"rollForward": "latestFeature"
5-
}
3+
"rollForward": "latestFeature",
4+
"version": "6.0.403"
5+
},
6+
"additionalSdks": [
7+
"3.1.425"
8+
]
69
}

0 commit comments

Comments
 (0)