Skip to content

Commit 5507021

Browse files
authored
Support multiple versions in single invocation (#240)
1 parent e3ce416 commit 5507021

14 files changed

+1606
-321
lines changed

.github/workflows/workflow.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ jobs:
3232
- name: Verify no unstaged changes
3333
if: runner.os != 'windows'
3434
run: __tests__/verify-no-unstaged-changes.sh
35+
36+
test-setup-multiple-versions:
37+
runs-on: ${{ matrix.operating-system }}
38+
strategy:
39+
fail-fast: false
40+
matrix:
41+
operating-system: [ubuntu-latest, windows-latest, macOS-latest]
42+
steps:
43+
- name: Checkout
44+
uses: actions/checkout@v2
45+
- name: Clear toolcache
46+
shell: pwsh
47+
run: __tests__/clear-toolcache.ps1 ${{ runner.os }}
48+
- name: Setup dotnet 2.2.402 and 3.1.404
49+
uses: ./
50+
with:
51+
dotnet-version: |
52+
2.2.402
53+
3.1.404
54+
3.0.x
55+
- name: Verify dotnet
56+
shell: pwsh
57+
run: __tests__/verify-dotnet.ps1 2.2.402 3.1.404 '3.0'
3558

3659
test-setup-full-version:
3760
runs-on: ${{ matrix.operating-system }}

.licenses/npm/@actions/core.dep.yml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.licenses/npm/@actions/http-client-1.0.11.dep.yml

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
This action sets up a [.NET CLI](https://github.com/dotnet/sdk) environment for use in actions by:
88

9-
- optionally downloading and caching a version of dotnet by SDK version and adding to PATH
9+
- optionally downloading and caching a version(s) of dotnet by SDK version(s) and adding to PATH
1010
- registering problem matchers for error output
1111
- setting up authentication to private package sources like GitHub Packages
1212

@@ -29,7 +29,20 @@ steps:
2929
dotnet-version: '3.1.x' # SDK Version to use; x will use the latest version of the 3.1 channel
3030
- run: dotnet build <my project>
3131
```
32+
Multiple versions:
33+
> Note: In case multiple versions are installed, the latest .NET version will be used by default unless another version is specified in the `global.json` file.
3234

35+
```yml
36+
steps:
37+
- name: Setup dotnet
38+
- uses: actions/checkout@v2
39+
- uses: actions/setup-dotnet@v1
40+
with:
41+
dotnet-version: |
42+
3.1.x
43+
5.0.x
44+
- run: dotnet build <my project>
45+
```
3346
Preview version:
3447
```yml
3548
steps:

__tests__/installer.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ describe('installer tests', () => {
3232
}
3333
}, 30000);
3434

35+
it('Aquires multiple versions of dotnet', async () => {
36+
const versions = ['2.2.207', '3.1.120'];
37+
38+
for (const version of versions) {
39+
await getDotnet(version);
40+
}
41+
expect(fs.existsSync(path.join(toolDir, 'sdk', '2.2.207'))).toBe(true);
42+
expect(fs.existsSync(path.join(toolDir, 'sdk', '3.1.120'))).toBe(true);
43+
44+
if (IS_WINDOWS) {
45+
expect(fs.existsSync(path.join(toolDir, 'dotnet.exe'))).toBe(true);
46+
} else {
47+
expect(fs.existsSync(path.join(toolDir, 'dotnet'))).toBe(true);
48+
}
49+
50+
expect(process.env.DOTNET_ROOT).toBeDefined;
51+
expect(process.env.PATH).toBeDefined;
52+
expect(process.env.DOTNET_ROOT).toBe(toolDir);
53+
expect(process.env.PATH?.startsWith(toolDir)).toBe(true);
54+
}, 600000);
55+
3556
it('Acquires version of dotnet if no matching version is installed', async () => {
3657
await getDotnet('3.1.201');
3758
expect(fs.existsSync(path.join(toolDir, 'sdk', '3.1.201'))).toBe(true);
@@ -126,4 +147,5 @@ function normalizeFileContents(contents: string): string {
126147
async function getDotnet(version: string): Promise<void> {
127148
const dotnetInstaller = new installer.DotnetCoreInstaller(version);
128149
await dotnetInstaller.installDotnet();
150+
installer.DotnetCoreInstaller.addToPath();
129151
}

__tests__/sample-csproj/sample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.1;netcoreapp2.2</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp3.1;netcoreapp3.0;netcoreapp2.2</TargetFrameworks>
55
<RootNamespace>sample_csproj</RootNamespace>
66

77
<IsPackable>false</IsPackable>

__tests__/verify-dotnet.ps1

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,33 @@ if (!$args[0])
66
$dotnet = Get-Command dotnet | Select-Object -First 1 | ForEach-Object { $_.Path }
77
Write-Host "Found '$dotnet'"
88

9-
$version = & $dotnet --version | Out-String | ForEach-Object { $_.Trim() }
10-
Write-Host "Version $version"
11-
if (-not ($version.StartsWith($args[0].ToString())))
9+
if($args.count -eq 1)
1210
{
13-
Write-Host "PATH='$env:PATH'"
14-
throw "Unexpected version"
11+
$version = & $dotnet --version | Out-String | ForEach-Object { $_.Trim() }
12+
Write-Host "Version $version"
13+
if (-not ($version.StartsWith($args[0].ToString())))
14+
{
15+
Write-Host "PATH='$env:PATH'"
16+
throw "Unexpected version"
17+
}
1518
}
1619

1720
if ($args[1])
1821
{
1922
# SDKs are listed on multiple lines with the path afterwards in square brackets
2023
$versions = & $dotnet --list-sdks | ForEach-Object { $_.SubString(0, $_.IndexOf('[')).Trim() }
2124
Write-Host "Installed versions: $versions"
22-
$isInstalledVersion = $false
23-
foreach ($version in $versions)
24-
{
25-
if ($version.StartsWith($args[1].ToString()))
25+
$InstalledVersionCount = 0
26+
foreach($arg in $args){
27+
foreach ($version in $versions)
2628
{
27-
$isInstalledVersion = $true
28-
break
29+
if ($version.StartsWith($arg.ToString()))
30+
{
31+
$InstalledVersionCount++
32+
}
2933
}
30-
}
31-
if (-not $isInstalledVersion)
34+
}
35+
if ( $InstalledVersionCount -ne $args.Count)
3236
{
3337
Write-Host "PATH='$env:PATH'"
3438
throw "Unexpected version"
@@ -53,6 +57,13 @@ if ($args[1])
5357
throw "Unexpected output"
5458
}
5559
}
60+
if ($args[2])
61+
{
62+
if ($sample_output -notlike "*Test Run Successful.*Test Run Successful.*Test Run Successful.*")
63+
{
64+
throw "Unexpected output"
65+
}
66+
}
5667
else
5768
{
5869
if ($sample_output -notlike "*Test Run Successful.*")

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ branding:
66
color: green
77
inputs:
88
dotnet-version:
9-
description: 'Optional SDK version to use. If not provided, will install global.json version when available. Examples: 2.2.104, 3.1, 3.1.x'
9+
description: 'Optional SDK version(s) to use. If not provided, will install global.json version when available. Examples: 2.2.104, 3.1, 3.1.x'
1010
source-url:
1111
description: 'Optional package source for which to set up authentication. Will consult any existing NuGet.config in the root of the repo and provide a temporary NuGet.config using the NUGET_AUTH_TOKEN environment variable as a ClearTextPassword'
1212
owner:

0 commit comments

Comments
 (0)