Skip to content

Commit fde13e5

Browse files
committed
Add .build.ps1 logic to automatically download dotnet cli if missing
1 parent aa6cae1 commit fde13e5

File tree

4 files changed

+74
-13
lines changed

4 files changed

+74
-13
lines changed

.build.ps1

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,93 @@ param(
55

66
#Requires -Modules @{ModuleName="InvokeBuild";ModuleVersion="3.2.1"}
77

8-
task EnsureDotNet -Before Clean, Build, BuildHost, Test, TestPowerShellApi {
9-
# TODO: Download if it doesn't exist in known paths
10-
if (!(Test-Path 'c:\Program Files\dotnet\dotnet.exe')) {
11-
Write-Error "dotnet is not installed"
8+
if ($env:APPVEYOR -ne $null) {
9+
dotnet --info
10+
}
11+
12+
task SetupDotNet -Before Restore, Clean, Build, BuildHost, Test, TestPowerShellApi {
13+
14+
# Bail out early if we've already found the exe path
15+
if ($script:dotnetExe -ne $null) { return }
16+
17+
$requiredDotnetVersion = "1.0.0-preview4-004233"
18+
$needsInstall = $true
19+
$dotnetPath = "$PSScriptRoot/.dotnet"
20+
$dotnetExePath = "$dotnetPath/dotnet.exe"
21+
22+
if (Test-Path $dotnetExePath) {
23+
$script:dotnetExe = $dotnetExePath
1224
}
25+
else {
26+
$installedDotnet = Get-Command dotnet -ErrorAction Ignore
27+
if ($installedDotnet) {
28+
$dotnetExePath = $installedDotnet.Source
29+
30+
exec {
31+
if ((& $dotnetExePath --version) -eq $requiredDotnetVersion) {
32+
$script:dotnetExe = $dotnetExePath
33+
}
34+
}
35+
}
36+
37+
if ($script:dotnetExe -eq $null) {
1338

14-
exec {
15-
if (!((& dotnet --version) -like "*preview4*")) {
16-
Write-Error "You must have at least preview4 of the dotnet tools installed."
39+
Write-Host "`n### Installing .NET CLI $requiredDotnetVersion...`n" -ForegroundColor Green
40+
41+
# Download the official installation script and run it
42+
$installScriptPath = "$($env:TEMP)\dotnet-install.ps1"
43+
Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0-preview4/scripts/obtain/dotnet-install.ps1" -OutFile $installScriptPath
44+
$env:DOTNET_INSTALL_DIR = "$PSScriptRoot\.dotnet"
45+
& $installScriptPath -Version $requiredDotnetVersion -InstallDir "$env:DOTNET_INSTALL_DIR"
46+
47+
Write-Host "`n### Installation complete." -ForegroundColor Green
48+
$script:dotnetExe = $dotnetExePath
1749
}
1850
}
51+
52+
# This variable is used internally by 'dotnet' to know where it's installed
53+
$script:dotnetExe = Resolve-Path $script:dotnetExe
54+
if (!$env:DOTNET_INSTALL_DIR)
55+
{
56+
$dotnetExeDir = [System.IO.Path]::GetDirectoryName($script:dotnetExe)
57+
$env:PATH = $dotnetExeDir + [System.IO.Path]::PathSeparator + $env:PATH
58+
$env:DOTNET_INSTALL_DIR = $dotnetExeDir
59+
}
60+
61+
Write-Host "`n### Using dotnet at path $script:dotnetExe`n" -ForegroundColor Green
62+
}
63+
64+
task Restore {
65+
exec { & dotnet restore }
1966
}
2067

2168
task Clean {
22-
exec { & dotnet clean .\PowerShellEditorServices.sln }
69+
exec { & dotnet clean }
2370
}
2471

2572
function BuildForPowerShellVersion($version) {
73+
# Restore packages for the specified version
2674
exec { & dotnet restore .\src\PowerShellEditorServices\PowerShellEditorServices.csproj -- /p:PowerShellVersion=$version }
2775

2876
Write-Host -ForegroundColor Green "`n### Testing API usage for PowerShell $version...`n"
29-
exec { & dotnet build -f net451 .\src\PowerShellEditorServices\PowerShellEditorServices.csproj -- /p:PowerShellVersion=$version}
77+
exec { & dotnet build -f net451 .\src\PowerShellEditorServices\PowerShellEditorServices.csproj -- /p:PowerShellVersion=$version }
3078
}
3179

3280
task TestPowerShellApi {
3381
BuildForPowerShellVersion v3
3482
BuildForPowerShellVersion v4
3583
BuildForPowerShellVersion v5r1
36-
BuildForPowerShellVersion v5r2
84+
85+
# Do a final restore to put everything back to normal
86+
exec { & dotnet restore .\src\PowerShellEditorServices\PowerShellEditorServices.csproj }
3787
}
3888

39-
task BuildHost EnsureDotNet, {
89+
task BuildHost {
4090
# This task is meant to be used in a quick dev cycle so no 'restore' is done first
4191
exec { & dotnet build -c $Configuration .\src\PowerShellEditorServices.Host\PowerShellEditorServices.Host.csproj }
4292
}
4393

4494
task Build {
45-
exec { & dotnet restore -v:m .\PowerShellEditorServices.sln }
4695
exec { & dotnet build -c $Configuration .\PowerShellEditorServices.sln }
4796
}
4897

@@ -67,4 +116,5 @@ task LayoutModule -After Build, BuildHost {
67116
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\netstandard1.6\* -Filter Microsoft.PowerShell.EditorServices*.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Core\
68117
}
69118

70-
task . Clean, Build, Test, TestPowerShellApi
119+
# The default task is to run the entire CI build
120+
task . Restore, Clean, Build, TestPowerShellApi, Test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ lock
2626
.corext/gen
2727
registered_data.ini
2828
.vs/
29+
.dotnet/
2930
module/
3031

3132
docs/_site/

.nuget/NuGet.Config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@
33
<solution>
44
<add key="disableSourceControlIntegration" value="true" />
55
</solution>
6+
<packageSources>
7+
<clear />
8+
<add key="CI Builds (dotnet-core)" value="https://www.myget.org/F/dotnet-core/api/v3/index.json" />
9+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
10+
<add key="powershell-core" value="https://powershell.myget.org/F/powershell-core/api/v3/index.json" />
11+
</packageSources>
612
</configuration>

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Place your settings in this file to overwrite default and user settings.
2+
{
3+
"files.trimTrailingWhitespace": true
4+
}

0 commit comments

Comments
 (0)