Skip to content

Commit 3de0fe2

Browse files
committed
Add .build.ps1 logic to automatically download dotnet cli if missing
1 parent de1b549 commit 3de0fe2

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

.build.ps1

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,56 @@ 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+
task SetupDotNet -Before Clean, Build, BuildHost, Test, TestPowerShellApi {
9+
10+
# Bail out early if we've already found the exe path
11+
if ($script:dotnetExe -ne $null) { return }
12+
13+
$requiredDotnetVersion = "1.0.0-preview4-004233"
14+
$needsInstall = $true
15+
$dotnetPath = "$PSScriptRoot/.dotnet"
16+
$dotnetExePath = "$dotnetPath/dotnet.exe"
17+
18+
if (Test-Path $dotnetExePath) {
19+
$script:dotnetExe = $dotnetExePath
1220
}
21+
else {
22+
$installedDotnet = Get-Command dotnet -ErrorAction Ignore
23+
if ($installedDotnet) {
24+
$dotnetExePath = $installedDotnet.Source
1325

14-
exec {
15-
if (!((& dotnet --version) -like "*preview4*")) {
16-
Write-Error "You must have at least preview4 of the dotnet tools installed."
26+
exec {
27+
if ((& $dotnetExePath --version) -eq $requiredDotnetVersion) {
28+
$script:dotnetExe = $dotnetExePath
29+
}
30+
}
31+
}
32+
33+
if ($script:dotnetExe -eq $null) {
34+
35+
Write-Host "`n### Installing .NET CLI $requiredDotnetVersion...`n" -ForegroundColor Green
36+
37+
# Download the official installation script and run it
38+
$installScriptPath = "$($env:TEMP)\dotnet-install.ps1"
39+
Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0-preview4/scripts/obtain/dotnet-install.ps1" -OutFile $installScriptPath
40+
$env:DOTNET_INSTALL_DIR = "$PSScriptRoot\.dotnet"
41+
& $installScriptPath -Version $requiredDotnetVersion -InstallDir "$env:DOTNET_INSTALL_DIR"
42+
43+
Write-Host "`n### Installation complete." -ForegroundColor Green
44+
$script:dotnetExe = $dotnetExePath
1745
}
1846
}
47+
48+
# This variable is used internally by 'dotnet' to know where it's installed
49+
$script:dotnetExe = Resolve-Path $script:dotnetExe
50+
if (!$env:DOTNET_INSTALL_DIR)
51+
{
52+
$dotnetExeDir = [System.IO.Path]::GetDirectoryName($script:dotnetExe)
53+
$env:PATH = $dotnetExeDir + [System.IO.Path]::PathSeparator + $env:PATH
54+
$env:DOTNET_INSTALL_DIR = $dotnetExeDir
55+
}
56+
57+
Write-Host "`n### Using dotnet at path $script:dotnetExe`n" -ForegroundColor Green
1958
}
2059

2160
task Clean {
@@ -36,7 +75,7 @@ task TestPowerShellApi {
3675
BuildForPowerShellVersion v5r2
3776
}
3877

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

.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)