Add and configure ESLint and update configuration for Prettier (#391)

* Apply ESLint config and update Prettier

* Update dependencies and rebuild

* Update docs
This commit is contained in:
Ivan 2023-03-09 14:43:05 +02:00 committed by GitHub
parent 926f442022
commit c82240598b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 3145 additions and 967 deletions

6
.eslintignore Normal file
View File

@ -0,0 +1,6 @@
# Ignore list
/*
# Do not ignore these folders:
!__tests__/
!src/

49
.eslintrc.js Normal file
View File

@ -0,0 +1,49 @@
module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:eslint-plugin-jest/recommended',
'eslint-config-prettier'
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'eslint-plugin-jest'],
rules: {
'@typescript-eslint/no-require-imports': 'error',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/ban-ts-comment': [
'error',
{
'ts-ignore': 'allow-with-description'
}
],
'no-console': 'error',
'yoda': 'error',
'prefer-const': [
'error',
{
destructuring: 'all'
}
],
'no-control-regex': 'off',
'no-constant-condition': ['error', {checkLoops: false}]
},
overrides: [
{
files: ['**/*{test,spec}.ts'],
rules: {
'@typescript-eslint/no-unused-vars': 'off',
'jest/no-standalone-expect': 'off',
'jest/no-conditional-expect': 'off',
'no-console': 'off',
}
}
],
env: {
node: true,
es6: true,
'jest/globals': true
}
};

1
.gitattributes vendored
View File

@ -1 +1,2 @@
* text=auto eol=lf
.licenses/** -diff linguist-generated=true .licenses/** -diff linguist-generated=true

View File

@ -14,4 +14,4 @@ on:
jobs: jobs:
call-basic-validation: call-basic-validation:
name: Basic validation name: Basic validation
uses: actions/reusable-workflows/.github/workflows/basic-validation.yml@main uses: actions/reusable-workflows/.github/workflows/basic-validation.yml@main

View File

@ -14,4 +14,4 @@ on:
jobs: jobs:
call-check-dist: call-check-dist:
name: Check dist/ name: Check dist/
uses: actions/reusable-workflows/.github/workflows/check-dist.yml@main uses: actions/reusable-workflows/.github/workflows/check-dist.yml@main

View File

@ -2,13 +2,13 @@ name: CodeQL analysis
on: on:
push: push:
branches: [ main ] branches: [main]
pull_request: pull_request:
branches: [ main ] branches: [main]
schedule: schedule:
- cron: '0 3 * * 0' - cron: '0 3 * * 0'
jobs: jobs:
call-codeQL-analysis: call-codeQL-analysis:
name: CodeQL analysis name: CodeQL analysis
uses: actions/reusable-workflows/.github/workflows/codeql-analysis.yml@main uses: actions/reusable-workflows/.github/workflows/codeql-analysis.yml@main

View File

@ -192,83 +192,83 @@ jobs:
run: __tests__/verify-dotnet.ps1 2.2 3.1 run: __tests__/verify-dotnet.ps1 2.2 3.1
test-setup-with-dotnet-quality: test-setup-with-dotnet-quality:
runs-on: ${{ matrix.operating-system }} runs-on: ${{ matrix.operating-system }}
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
operating-system: [ubuntu-latest, windows-latest, macOS-latest] operating-system: [ubuntu-latest, windows-latest, macOS-latest]
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Clear toolcache - name: Clear toolcache
shell: pwsh shell: pwsh
run: __tests__/clear-toolcache.ps1 ${{ runner.os }} run: __tests__/clear-toolcache.ps1 ${{ runner.os }}
- name: Setup dotnet 7.0 with preview quality - name: Setup dotnet 7.0 with preview quality
uses: ./ uses: ./
with: with:
dotnet-version: "7.0" dotnet-version: '7.0'
dotnet-quality: "preview" dotnet-quality: 'preview'
- name: Verify preview version - name: Verify preview version
shell: pwsh shell: pwsh
run: | run: |
$version = & dotnet --version $version = & dotnet --version
Write-Host "Installed version: $version" Write-Host "Installed version: $version"
if (-not ($version.Contains("preview") -or $version.Contains("rc"))) { throw "Unexpected version" } if (-not ($version.Contains("preview") -or $version.Contains("rc"))) { throw "Unexpected version" }
test-dotnet-version-output-during-single-version-installation: test-dotnet-version-output-during-single-version-installation:
runs-on: ${{ matrix.operating-system }} runs-on: ${{ matrix.operating-system }}
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
operating-system: [ubuntu-latest, windows-latest, macOS-latest] operating-system: [ubuntu-latest, windows-latest, macOS-latest]
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Clear toolcache - name: Clear toolcache
shell: pwsh shell: pwsh
run: __tests__/clear-toolcache.ps1 ${{ runner.os }} run: __tests__/clear-toolcache.ps1 ${{ runner.os }}
- name: Setup dotnet 6.0.401 - name: Setup dotnet 6.0.401
uses: ./ uses: ./
id: step1 id: step1
with: with:
dotnet-version: "6.0.401" dotnet-version: '6.0.401'
- name: Verify value of the dotnet-version output
shell: pwsh
run: |
$version = & dotnet --version
Write-Host "Installed version: $version"
if (-not ($version -eq '${{steps.step1.outputs.dotnet-version}}')) { throw "Unexpected output value" }
- name: Verify value of the dotnet-version output
shell: pwsh
run: |
$version = & dotnet --version
Write-Host "Installed version: $version"
if (-not ($version -eq '${{steps.step1.outputs.dotnet-version}}')) { throw "Unexpected output value" }
test-dotnet-version-output-during-multiple-version-installation: test-dotnet-version-output-during-multiple-version-installation:
runs-on: ${{ matrix.operating-system }} runs-on: ${{ matrix.operating-system }}
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
operating-system: [ubuntu-latest, windows-latest, macOS-latest] operating-system: [ubuntu-latest, windows-latest, macOS-latest]
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Clear toolcache - name: Clear toolcache
shell: pwsh shell: pwsh
run: __tests__/clear-toolcache.ps1 ${{ runner.os }} run: __tests__/clear-toolcache.ps1 ${{ runner.os }}
- name: Setup dotnet 6.0.401, 5.0.408, 7.0.100-rc.1.22431.12
uses: ./
id: step2
with:
dotnet-version: |
7.0.100-rc.1.22431.12
6.0.401
5.0.408
- name: Verify value of the dotnet-version output - name: Setup dotnet 6.0.401, 5.0.408, 7.0.100-rc.1.22431.12
shell: pwsh uses: ./
run: | id: step2
$version = "7.0.100-rc.1.22431.12" with:
if (-not ($version -eq '${{steps.step2.outputs.dotnet-version}}')) { throw "Unexpected output value" } dotnet-version: |
7.0.100-rc.1.22431.12
6.0.401
5.0.408
- name: Verify value of the dotnet-version output
shell: pwsh
run: |
$version = "7.0.100-rc.1.22431.12"
if (-not ($version -eq '${{steps.step2.outputs.dotnet-version}}')) { throw "Unexpected output value" }
test-proxy: test-proxy:
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -320,4 +320,4 @@ jobs:
env: env:
NUGET_AUTH_TOKEN: NOTATOKEN NUGET_AUTH_TOKEN: NOTATOKEN
- name: Verify dotnet - name: Verify dotnet
run: __tests__/verify-dotnet.sh 3.1.201 run: __tests__/verify-dotnet.sh 3.1.201

View File

@ -12,4 +12,4 @@ on:
jobs: jobs:
call-licensed: call-licensed:
name: Licensed name: Licensed
uses: actions/reusable-workflows/.github/workflows/licensed.yml@main uses: actions/reusable-workflows/.github/workflows/licensed.yml@main

View File

@ -21,9 +21,9 @@ jobs:
name: releaseNewActionVersion name: releaseNewActionVersion
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Update the ${{ env.TAG_NAME }} tag - name: Update the ${{ env.TAG_NAME }} tag
id: update-major-tag id: update-major-tag
uses: actions/publish-action@v0.2.2 uses: actions/publish-action@v0.2.2
with: with:
source-tag: ${{ env.TAG_NAME }} source-tag: ${{ env.TAG_NAME }}
slack-webhook: ${{ secrets.SLACK_WEBHOOK }} slack-webhook: ${{ secrets.SLACK_WEBHOOK }}

View File

@ -34,4 +34,4 @@ jobs:
run: | run: |
$version = & dotnet --version $version = & dotnet --version
Write-Host "Installed version: $version" Write-Host "Installed version: $version"
if (-not $version.StartsWith("${{ matrix.dotnet-version }}")) { throw "Unexpected version" } if (-not $version.StartsWith("${{ matrix.dotnet-version }}")) { throw "Unexpected version" }

View File

@ -2,3 +2,4 @@
. "$(dirname -- "$0")/_/husky.sh" . "$(dirname -- "$0")/_/husky.sh"
npm run format npm run format
npm run lint:fix

View File

@ -3,3 +3,4 @@
# Tests are not run at push time since they can take 2-4 minutes to complete # Tests are not run at push time since they can take 2-4 minutes to complete
npm run format-check npm run format-check
npm run lint

7
.prettierignore Normal file
View File

@ -0,0 +1,7 @@
# Ignore list
/*
# Do not ignore these folders:
!__tests__/
!.github/
!src/

10
.prettierrc.js Normal file
View File

@ -0,0 +1,10 @@
module.exports = {
printWidth: 80,
tabWidth: 2,
useTabs: false,
semi: true,
singleQuote: true,
trailingComma: 'none',
bracketSpacing: false,
arrowParens: 'avoid'
};

View File

@ -1,12 +0,0 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": false,
"arrowParens": "avoid",
"parser": "typescript",
"endOfLine": "auto"
}

View File

@ -1,336 +1,336 @@
import * as io from '@actions/io'; import * as io from '@actions/io';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
const fakeSourcesDirForTesting = path.join( const fakeSourcesDirForTesting = path.join(
__dirname, __dirname,
'runner', 'runner',
path.join(Math.random().toString(36).substring(7)), path.join(Math.random().toString(36).substring(7)),
's' 's'
); );
const invalidNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>`; const invalidNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>`;
const emptyNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?> const emptyNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
</configuration>`; </configuration>`;
const nugetorgNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?> const nugetorgNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<packageSources> <packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources> </packageSources>
</configuration>`; </configuration>`;
const gprnugetorgNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?> const gprnugetorgNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<packageSources> <packageSources>
<add key="GPR" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" /> <add key="GPR" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources> </packageSources>
</configuration>`; </configuration>`;
const gprNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?> const gprNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<packageSources> <packageSources>
<add key="GPR" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" /> <add key="GPR" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
</packageSources> </packageSources>
</configuration>`; </configuration>`;
const twogprNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?> const twogprNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<packageSources> <packageSources>
<add key="GPR-GitHub" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" /> <add key="GPR-GitHub" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
<add key="GPR-Actions" value="https://nuget.pkg.github.com/actions/index.json" protocolVersion="3" /> <add key="GPR-Actions" value="https://nuget.pkg.github.com/actions/index.json" protocolVersion="3" />
</packageSources> </packageSources>
</configuration>`; </configuration>`;
const spaceNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?> const spaceNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<packageSources> <packageSources>
<add key="GPR GitHub" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" /> <add key="GPR GitHub" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
</packageSources> </packageSources>
</configuration>`; </configuration>`;
const azureartifactsNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?> const azureartifactsNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<packageSources> <packageSources>
<add key="AzureArtifacts" value="https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json" protocolVersion="3" /> <add key="AzureArtifacts" value="https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json" protocolVersion="3" />
</packageSources> </packageSources>
</configuration>`; </configuration>`;
const azureartifactsnugetorgNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?> const azureartifactsnugetorgNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<packageSources> <packageSources>
<add key="AzureArtifacts" value="https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json" protocolVersion="3" /> <add key="AzureArtifacts" value="https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json" protocolVersion="3" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources> </packageSources>
</configuration>`; </configuration>`;
// We want a NuGet.config one level above the sources directory, so it doesn't trample a user's NuGet.config but is still picked up by NuGet/dotnet. // We want a NuGet.config one level above the sources directory, so it doesn't trample a user's NuGet.config but is still picked up by NuGet/dotnet.
const nugetConfigFile = path.join(fakeSourcesDirForTesting, '../nuget.config'); const nugetConfigFile = path.join(fakeSourcesDirForTesting, '../nuget.config');
process.env['GITHUB_REPOSITORY'] = 'OwnerName/repo'; process.env['GITHUB_REPOSITORY'] = 'OwnerName/repo';
import * as auth from '../src/authutil'; import * as auth from '../src/authutil';
describe('authutil tests', () => { describe('authutil tests', () => {
beforeEach(async () => { beforeEach(async () => {
await io.rmRF(fakeSourcesDirForTesting); await io.rmRF(fakeSourcesDirForTesting);
await io.mkdirP(fakeSourcesDirForTesting); await io.mkdirP(fakeSourcesDirForTesting);
}, 30000); }, 30000);
afterAll(async () => { afterAll(async () => {
await io.rmRF(fakeSourcesDirForTesting); await io.rmRF(fakeSourcesDirForTesting);
}, 30000); }, 30000);
beforeEach(() => { beforeEach(() => {
if (fs.existsSync(nugetConfigFile)) { if (fs.existsSync(nugetConfigFile)) {
fs.unlinkSync(nugetConfigFile); fs.unlinkSync(nugetConfigFile);
} }
process.env['INPUT_OWNER'] = ''; process.env['INPUT_OWNER'] = '';
process.env['NUGET_AUTH_TOKEN'] = ''; process.env['NUGET_AUTH_TOKEN'] = '';
}); });
it('No existing config, sets up a full NuGet.config with URL and user/PAT for GPR', async () => { it('No existing config, sets up a full NuGet.config with URL and user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN'; process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
await auth.configAuthentication( await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json', 'https://nuget.pkg.github.com/OwnerName/index.json',
'', '',
fakeSourcesDirForTesting fakeSourcesDirForTesting
); );
expect(fs.existsSync(nugetConfigFile)).toBe(true); expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect( expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'}) fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
it('No existing config, auth token environment variable not provided, throws', async () => { it('No existing config, auth token environment variable not provided, throws', async () => {
let thrown = false; let thrown = false;
try { try {
await auth.configAuthentication( await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json', 'https://nuget.pkg.github.com/OwnerName/index.json',
'', '',
fakeSourcesDirForTesting fakeSourcesDirForTesting
); );
} catch { } catch {
thrown = true; thrown = true;
} }
expect(thrown).toBe(true); expect(thrown).toBe(true);
}); });
it('No existing config, sets up a full NuGet.config with URL and other owner/PAT for GPR', async () => { it('No existing config, sets up a full NuGet.config with URL and other owner/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN'; process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
process.env['INPUT_OWNER'] = 'otherorg'; process.env['INPUT_OWNER'] = 'otherorg';
await auth.configAuthentication( await auth.configAuthentication(
'https://nuget.pkg.github.com/otherorg/index.json', 'https://nuget.pkg.github.com/otherorg/index.json',
'', '',
fakeSourcesDirForTesting fakeSourcesDirForTesting
); );
expect(fs.existsSync(nugetConfigFile)).toBe(true); expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect( expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'}) fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
it('Existing config (invalid), tries to parse an invalid NuGet.config and throws', async () => { it('Existing config (invalid), tries to parse an invalid NuGet.config and throws', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN'; process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join( const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting, fakeSourcesDirForTesting,
'nuget.config' 'nuget.config'
); );
fs.writeFileSync(inputNuGetConfigPath, invalidNuGetConfig); fs.writeFileSync(inputNuGetConfigPath, invalidNuGetConfig);
let thrown = false; let thrown = false;
try { try {
await auth.configAuthentication( await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json', 'https://nuget.pkg.github.com/OwnerName/index.json',
'', '',
fakeSourcesDirForTesting fakeSourcesDirForTesting
); );
} catch { } catch {
thrown = true; thrown = true;
} }
expect(thrown).toBe(true); expect(thrown).toBe(true);
}); });
it('Existing config w/ no sources, sets up a full NuGet.config with URL and user/PAT for GPR', async () => { it('Existing config w/ no sources, sets up a full NuGet.config with URL and user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN'; process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join( const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting, fakeSourcesDirForTesting,
'nuget.config' 'nuget.config'
); );
fs.writeFileSync(inputNuGetConfigPath, emptyNuGetConfig); fs.writeFileSync(inputNuGetConfigPath, emptyNuGetConfig);
await auth.configAuthentication( await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json', 'https://nuget.pkg.github.com/OwnerName/index.json',
'', '',
fakeSourcesDirForTesting fakeSourcesDirForTesting
); );
expect(fs.existsSync(nugetConfigFile)).toBe(true); expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect( expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'}) fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
it('Existing config w/ no GPR sources, sets up a full NuGet.config with URL and user/PAT for GPR', async () => { it('Existing config w/ no GPR sources, sets up a full NuGet.config with URL and user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN'; process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join( const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting, fakeSourcesDirForTesting,
'nuget.config' 'nuget.config'
); );
fs.writeFileSync(inputNuGetConfigPath, nugetorgNuGetConfig); fs.writeFileSync(inputNuGetConfigPath, nugetorgNuGetConfig);
await auth.configAuthentication( await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json', 'https://nuget.pkg.github.com/OwnerName/index.json',
'', '',
fakeSourcesDirForTesting fakeSourcesDirForTesting
); );
expect(fs.existsSync(nugetConfigFile)).toBe(true); expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect( expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'}) fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
it('Existing config w/ only GPR source, sets up a partial NuGet.config user/PAT for GPR', async () => { it('Existing config w/ only GPR source, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN'; process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join( const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting, fakeSourcesDirForTesting,
'nuget.config' 'nuget.config'
); );
fs.writeFileSync(inputNuGetConfigPath, gprNuGetConfig); fs.writeFileSync(inputNuGetConfigPath, gprNuGetConfig);
await auth.configAuthentication( await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json', 'https://nuget.pkg.github.com/OwnerName/index.json',
'', '',
fakeSourcesDirForTesting fakeSourcesDirForTesting
); );
expect(fs.existsSync(nugetConfigFile)).toBe(true); expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect( expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'}) fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
it('Existing config w/ GPR source and NuGet.org, sets up a partial NuGet.config user/PAT for GPR', async () => { it('Existing config w/ GPR source and NuGet.org, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN'; process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join( const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting, fakeSourcesDirForTesting,
'nuget.config' 'nuget.config'
); );
fs.writeFileSync(inputNuGetConfigPath, gprnugetorgNuGetConfig); fs.writeFileSync(inputNuGetConfigPath, gprnugetorgNuGetConfig);
await auth.configAuthentication( await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json', 'https://nuget.pkg.github.com/OwnerName/index.json',
'', '',
fakeSourcesDirForTesting fakeSourcesDirForTesting
); );
expect(fs.existsSync(nugetConfigFile)).toBe(true); expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect( expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'}) fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
it('Existing config w/ two GPR sources, sets up a partial NuGet.config user/PAT for GPR', async () => { it('Existing config w/ two GPR sources, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN'; process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join( const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting, fakeSourcesDirForTesting,
'nuget.config' 'nuget.config'
); );
fs.writeFileSync(inputNuGetConfigPath, twogprNuGetConfig); fs.writeFileSync(inputNuGetConfigPath, twogprNuGetConfig);
await auth.configAuthentication( await auth.configAuthentication(
'https://nuget.pkg.github.com', 'https://nuget.pkg.github.com',
'', '',
fakeSourcesDirForTesting fakeSourcesDirForTesting
); );
expect(fs.existsSync(nugetConfigFile)).toBe(true); expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect( expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'}) fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
it('Existing config w/ spaces in key, throws for now', async () => { it('Existing config w/ spaces in key, throws for now', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN'; process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join( const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting, fakeSourcesDirForTesting,
'nuget.config' 'nuget.config'
); );
fs.writeFileSync(inputNuGetConfigPath, spaceNuGetConfig); fs.writeFileSync(inputNuGetConfigPath, spaceNuGetConfig);
let thrown = false; let thrown = false;
try { try {
await auth.configAuthentication( await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json', 'https://nuget.pkg.github.com/OwnerName/index.json',
'', '',
fakeSourcesDirForTesting fakeSourcesDirForTesting
); );
} catch { } catch {
thrown = true; thrown = true;
} }
expect(thrown).toBe(true); expect(thrown).toBe(true);
}); });
it('Existing config not in repo root, sets up a partial NuGet.config user/PAT for GPR', async () => { it('Existing config not in repo root, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN'; process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigDirectory: string = path.join( const inputNuGetConfigDirectory: string = path.join(
fakeSourcesDirForTesting, fakeSourcesDirForTesting,
'subfolder' 'subfolder'
); );
const inputNuGetConfigPath: string = path.join( const inputNuGetConfigPath: string = path.join(
inputNuGetConfigDirectory, inputNuGetConfigDirectory,
'nuget.config' 'nuget.config'
); );
fs.mkdirSync(inputNuGetConfigDirectory, {recursive: true}); fs.mkdirSync(inputNuGetConfigDirectory, {recursive: true});
fs.writeFileSync(inputNuGetConfigPath, gprNuGetConfig); fs.writeFileSync(inputNuGetConfigPath, gprNuGetConfig);
await auth.configAuthentication( await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json', 'https://nuget.pkg.github.com/OwnerName/index.json',
'subfolder/nuget.config', 'subfolder/nuget.config',
fakeSourcesDirForTesting fakeSourcesDirForTesting
); );
expect(fs.existsSync(nugetConfigFile)).toBe(true); expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect( expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'}) fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
it('Existing config w/ only Azure Artifacts source, sets up a partial NuGet.config user/PAT for GPR', async () => { it('Existing config w/ only Azure Artifacts source, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN'; process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join( const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting, fakeSourcesDirForTesting,
'nuget.config' 'nuget.config'
); );
fs.writeFileSync(inputNuGetConfigPath, azureartifactsNuGetConfig); fs.writeFileSync(inputNuGetConfigPath, azureartifactsNuGetConfig);
await auth.configAuthentication( await auth.configAuthentication(
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json', 'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json',
'', '',
fakeSourcesDirForTesting fakeSourcesDirForTesting
); );
expect(fs.existsSync(nugetConfigFile)).toBe(true); expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect( expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'}) fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
it('Existing config w/ Azure Artifacts source and NuGet.org, sets up a partial NuGet.config user/PAT for GPR', async () => { it('Existing config w/ Azure Artifacts source and NuGet.org, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN'; process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join( const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting, fakeSourcesDirForTesting,
'nuget.config' 'nuget.config'
); );
fs.writeFileSync(inputNuGetConfigPath, azureartifactsnugetorgNuGetConfig); fs.writeFileSync(inputNuGetConfigPath, azureartifactsnugetorgNuGetConfig);
await auth.configAuthentication( await auth.configAuthentication(
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json', 'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json',
'', '',
fakeSourcesDirForTesting fakeSourcesDirForTesting
); );
expect(fs.existsSync(nugetConfigFile)).toBe(true); expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect( expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'}) fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
it('No existing config, sets up a full NuGet.config with URL and token for other source', async () => { it('No existing config, sets up a full NuGet.config with URL and token for other source', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN'; process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
await auth.configAuthentication( await auth.configAuthentication(
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json', 'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json',
'', '',
fakeSourcesDirForTesting fakeSourcesDirForTesting
); );
expect(fs.existsSync(nugetConfigFile)).toBe(true); expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect( expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'}) fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
}); });

View File

@ -1,19 +1,19 @@
import cscFile from '../.github/csc.json';
describe('csc tests', () => { describe('csc tests', () => {
it('Valid regular expression', async () => { it('Valid regular expression', async () => {
var cscFile = require('../.github/csc.json'); const regex = cscFile['problemMatcher'][0]['pattern'][0]['regexp'];
var regex = cscFile['problemMatcher'][0]['pattern'][0]['regexp'];
console.log(regex); console.log(regex);
var re = new RegExp(regex); const re = new RegExp(regex);
// Ideally we would verify that this // Ideally we would verify that this
var stringsToMatch = [ const stringsToMatch = [
'Program.cs(10,79): error CS1002: ; expected [/Users/zacharyeisinger/Documents/repo/setup-dotnet/__tests__/sample-broken-csproj/sample.csproj]', 'Program.cs(10,79): error CS1002: ; expected [/Users/zacharyeisinger/Documents/repo/setup-dotnet/__tests__/sample-broken-csproj/sample.csproj]',
"S:\\Msbuild\\src\\Build\\Evaluation\\ExpressionShredder.cs(33,7): error CS1003: Syntax error, ',' expected [S:\\msbuild\\src\\Build\\Microsoft.Build.csproj > Properties:prop]" "S:\\Msbuild\\src\\Build\\Evaluation\\ExpressionShredder.cs(33,7): error CS1003: Syntax error, ',' expected [S:\\msbuild\\src\\Build\\Microsoft.Build.csproj > Properties:prop]"
]; ];
stringsToMatch.forEach(string => { stringsToMatch.forEach(string => {
var matchStr = string.match(re); const matchStr = string.match(re);
console.log(matchStr); console.log(matchStr);
expect(matchStr).toEqual(expect.anything()); expect(matchStr).toEqual(expect.anything());
}); });

View File

@ -68,8 +68,8 @@ describe('DotnetCoreInstaller tests', () => {
expect(fs.existsSync(path.join(toolDir, 'dotnet'))).toBe(true); expect(fs.existsSync(path.join(toolDir, 'dotnet'))).toBe(true);
} }
expect(process.env.DOTNET_ROOT).toBeDefined; expect(process.env.DOTNET_ROOT).toBeDefined();
expect(process.env.PATH).toBeDefined; expect(process.env.PATH).toBeDefined();
expect(process.env.DOTNET_ROOT).toBe(toolDir); expect(process.env.DOTNET_ROOT).toBe(toolDir);
expect(process.env.PATH?.startsWith(toolDir)).toBe(true); expect(process.env.PATH?.startsWith(toolDir)).toBe(true);
}, 600000); }, 600000);
@ -83,15 +83,15 @@ describe('DotnetCoreInstaller tests', () => {
expect(fs.existsSync(path.join(toolDir, 'dotnet'))).toBe(true); expect(fs.existsSync(path.join(toolDir, 'dotnet'))).toBe(true);
} }
expect(process.env.DOTNET_ROOT).toBeDefined; expect(process.env.DOTNET_ROOT).toBeDefined();
expect(process.env.PATH).toBeDefined; expect(process.env.PATH).toBeDefined();
expect(process.env.DOTNET_ROOT).toBe(toolDir); expect(process.env.DOTNET_ROOT).toBe(toolDir);
expect(process.env.PATH?.startsWith(toolDir)).toBe(true); expect(process.env.PATH?.startsWith(toolDir)).toBe(true);
}, 600000); //This needs some time to download on "slower" internet connections }, 600000); //This needs some time to download on "slower" internet connections
it('Acquires generic version of dotnet if no matching version is installed', async () => { it('Acquires generic version of dotnet if no matching version is installed', async () => {
await getDotnet('3.1'); await getDotnet('3.1');
var directory = fs const directory = fs
.readdirSync(path.join(toolDir, 'sdk')) .readdirSync(path.join(toolDir, 'sdk'))
.filter(fn => fn.startsWith('3.1.')); .filter(fn => fn.startsWith('3.1.'));
expect(directory.length > 0).toBe(true); expect(directory.length > 0).toBe(true);
@ -101,17 +101,16 @@ describe('DotnetCoreInstaller tests', () => {
expect(fs.existsSync(path.join(toolDir, 'dotnet'))).toBe(true); expect(fs.existsSync(path.join(toolDir, 'dotnet'))).toBe(true);
} }
expect(process.env.DOTNET_ROOT).toBeDefined; expect(process.env.DOTNET_ROOT).toBeDefined();
expect(process.env.PATH).toBeDefined; expect(process.env.PATH).toBeDefined();
expect(process.env.DOTNET_ROOT).toBe(toolDir); expect(process.env.DOTNET_ROOT).toBe(toolDir);
expect(process.env.PATH?.startsWith(toolDir)).toBe(true); expect(process.env.PATH?.startsWith(toolDir)).toBe(true);
}, 600000); //This needs some time to download on "slower" internet connections }, 600000); //This needs some time to download on "slower" internet connections
it('Returns string with installed SDK version', async () => { it('Returns string with installed SDK version', async () => {
const version = '3.1.120'; const version = '3.1.120';
let installedVersion: string;
installedVersion = await getDotnet(version); const installedVersion = await getDotnet(version);
expect(installedVersion).toBe('3.1.120'); expect(installedVersion).toBe('3.1.120');
}, 600000); }, 600000);
@ -143,7 +142,7 @@ describe('DotnetCoreInstaller tests', () => {
}, 30000); }, 30000);
it('Uses an up to date powershell download script', async () => { it('Uses an up to date powershell download script', async () => {
var httpCallbackClient = new hc.HttpClient('setup-dotnet-test', [], { const httpCallbackClient = new hc.HttpClient('setup-dotnet-test', [], {
allowRetries: true, allowRetries: true,
maxRetries: 3 maxRetries: 3
}); });
@ -180,7 +179,7 @@ describe('DotnetVersionResolver tests', () => {
); );
const versionObject = await dotnetVersionResolver.createDotNetVersion(); const versionObject = await dotnetVersionResolver.createDotNetVersion();
expect(!!versionObject.value).toBeTruthy; expect(!!versionObject.value).toBe(true);
} }
); );
@ -223,15 +222,27 @@ describe('DotnetVersionResolver tests', () => {
); );
each(['3.1', '3.1.x', '3.1.*', '3.1.X']).test( each(['3.1', '3.1.x', '3.1.*', '3.1.X']).test(
"if version: '%s' that can be resolved to 'channel' option is supplied, it should set quality flag to 'true' and type to 'channel' in version object", "if version: '%s' that can be resolved to 'channel' option is supplied, it should set type to 'channel' in version object",
async version => { async version => {
const dotnetVersionResolver = new installer.DotnetVersionResolver( const dotnetVersionResolver = new installer.DotnetVersionResolver(
version version
); );
const versionObject = await dotnetVersionResolver.createDotNetVersion(); const versionObject = await dotnetVersionResolver.createDotNetVersion();
expect(versionObject.type.toLowerCase().includes('channel')).toBeTruthy; expect(versionObject.type.toLowerCase().includes('channel')).toBe(true);
expect(versionObject.qualityFlag).toBeTruthy; }
);
each(['6.0', '6.0.x', '6.0.*', '6.0.X']).test(
"if version: '%s' that can be resolved to 'channel' option is supplied and its major tag is >= 6, it should set type to 'channel' and qualityFlag to 'true' in version object",
async version => {
const dotnetVersionResolver = new installer.DotnetVersionResolver(
version
);
const versionObject = await dotnetVersionResolver.createDotNetVersion();
expect(versionObject.type.toLowerCase().includes('channel')).toBe(true);
expect(versionObject.qualityFlag).toBe(true);
} }
); );
@ -243,8 +254,8 @@ describe('DotnetVersionResolver tests', () => {
); );
const versionObject = await dotnetVersionResolver.createDotNetVersion(); const versionObject = await dotnetVersionResolver.createDotNetVersion();
expect(versionObject.type.toLowerCase().includes('version')).toBeTruthy; expect(versionObject.type.toLowerCase().includes('version')).toBe(true);
expect(versionObject.qualityFlag).toBeFalsy; expect(versionObject.qualityFlag).toBe(false);
} }
); );
@ -259,11 +270,11 @@ describe('DotnetVersionResolver tests', () => {
const nonWindowsRegEx = new RegExp(/^--[vc]/); const nonWindowsRegEx = new RegExp(/^--[vc]/);
if (IS_WINDOWS) { if (IS_WINDOWS) {
expect(windowsRegEx.test(versionObject.type)).toBeTruthy; expect(windowsRegEx.test(versionObject.type)).toBe(true);
expect(nonWindowsRegEx.test(versionObject.type)).toBeFalsy; expect(nonWindowsRegEx.test(versionObject.type)).toBe(false);
} else { } else {
expect(nonWindowsRegEx.test(versionObject.type)).toBeTruthy; expect(nonWindowsRegEx.test(versionObject.type)).toBe(true);
expect(windowsRegEx.test(versionObject.type)).toBeFalsy; expect(windowsRegEx.test(versionObject.type)).toBe(false);
} }
} }
); );
@ -276,10 +287,7 @@ function normalizeFileContents(contents: string): string {
.replace(new RegExp('\r', 'g'), '\n'); .replace(new RegExp('\r', 'g'), '\n');
} }
async function getDotnet( async function getDotnet(version: string, quality = ''): Promise<string> {
version: string,
quality: string = ''
): Promise<string> {
const dotnetInstaller = new installer.DotnetCoreInstaller( const dotnetInstaller = new installer.DotnetCoreInstaller(
version, version,
quality as QualityOptions quality as QualityOptions

View File

@ -21,11 +21,11 @@ if (IS_WINDOWS) {
const tempDir = path.join(__dirname, 'runner', 'temp2'); const tempDir = path.join(__dirname, 'runner', 'temp2');
describe('setup-dotnet tests', () => { describe('setup-dotnet tests', () => {
let getInputSpy = jest.spyOn(core, 'getInput'); const getInputSpy = jest.spyOn(core, 'getInput');
let getMultilineInputSpy = jest.spyOn(core, 'getMultilineInput'); const getMultilineInputSpy = jest.spyOn(core, 'getMultilineInput');
let setOutputSpy = jest.spyOn(core, 'setOutput'); const setOutputSpy = jest.spyOn(core, 'setOutput');
let inputs = {} as any; const inputs = {} as any;
beforeAll(async () => { beforeAll(async () => {
process.env.RUNNER_TOOL_CACHE = toolDir; process.env.RUNNER_TOOL_CACHE = toolDir;
@ -74,7 +74,7 @@ describe('setup-dotnet tests', () => {
await setup.run(); await setup.run();
expect(setOutputSpy).toBeCalledWith('dotnet-version', '6.0.401'); expect(setOutputSpy).toHaveBeenCalledWith('dotnet-version', '6.0.401');
}, 400000); }, 400000);
it("Sets output with the version specified in global.json, if it's present", async () => { it("Sets output with the version specified in global.json, if it's present", async () => {
@ -93,6 +93,6 @@ describe('setup-dotnet tests', () => {
await setup.run(); await setup.run();
expect(setOutputSpy).toBeCalledWith('dotnet-version', '3.0.103'); expect(setOutputSpy).toHaveBeenCalledWith('dotnet-version', '3.0.103');
}, 400000); }, 400000);
}); });

18
dist/index.js vendored
View File

@ -45,7 +45,7 @@ function configAuthentication(feedUrl, existingFileLocation = '', processRoot =
} }
exports.configAuthentication = configAuthentication; exports.configAuthentication = configAuthentication;
function isValidKey(key) { function isValidKey(key) {
return /^[\w\-\.]+$/i.test(key); return /^[\w\-.]+$/i.test(key);
} }
function getExistingNugetConfig(processRoot) { function getExistingNugetConfig(processRoot) {
const defaultConfigName = 'nuget.config'; const defaultConfigName = 'nuget.config';
@ -60,9 +60,9 @@ function getExistingNugetConfig(processRoot) {
function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) { function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) {
var _a, _b; var _a, _b;
core.info(`dotnet-auth: Finding any source references in ${existingFileLocation}, writing a new temporary configuration file with credentials to ${tempFileLocation}`); core.info(`dotnet-auth: Finding any source references in ${existingFileLocation}, writing a new temporary configuration file with credentials to ${tempFileLocation}`);
let sourceKeys = []; const sourceKeys = [];
let owner = core.getInput('owner'); let owner = core.getInput('owner');
let sourceUrl = feedUrl; const sourceUrl = feedUrl;
if (!owner) { if (!owner) {
owner = github.context.repo.owner; owner = github.context.repo.owner;
} }
@ -132,7 +132,7 @@ function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) {
} }
]; ];
if (!sourceKeys.length) { if (!sourceKeys.length) {
let keystring = 'Source'; const keystring = 'Source';
xmlSource[1].configuration.push({ xmlSource[1].configuration.push({
packageSources: [ packageSources: [
{ {
@ -300,9 +300,9 @@ class DotnetVersionResolver {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const response = yield httpClient.getJson(DotnetVersionResolver.DotNetCoreIndexUrl); const response = yield httpClient.getJson(DotnetVersionResolver.DotNetCoreIndexUrl);
const result = response.result || {}; const result = response.result || {};
let releasesInfo = result['releases-index']; const releasesInfo = result['releases-index'];
let releaseInfo = releasesInfo.find(info => { const releaseInfo = releasesInfo.find(info => {
let sdkParts = info['channel-version'].split('.'); const sdkParts = info['channel-version'].split('.');
return sdkParts[0] === versionParts[0]; return sdkParts[0] === versionParts[0];
}); });
if (!releaseInfo) { if (!releaseInfo) {
@ -408,8 +408,8 @@ class DotnetCoreInstaller {
outputDotnetVersion(version) { outputDotnetVersion(version) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const installationPath = process.env['DOTNET_INSTALL_DIR']; const installationPath = process.env['DOTNET_INSTALL_DIR'];
let versionsOnRunner = yield (0, promises_1.readdir)(path_1.default.join(installationPath.replace(/'/g, ''), 'sdk')); const versionsOnRunner = yield (0, promises_1.readdir)(path_1.default.join(installationPath.replace(/'/g, ''), 'sdk'));
let installedVersion = semver_1.default.maxSatisfying(versionsOnRunner, version, { const installedVersion = semver_1.default.maxSatisfying(versionsOnRunner, version, {
includePrerelease: true includePrerelease: true
}); });
return installedVersion; return installedVersion;

View File

@ -61,6 +61,7 @@ Pull requests are the easiest way to contribute changes to git repos at GitHub.
- To implement new features or fix bugs, you need to make changes to the `.ts` files, which are located in the `src` folder - To implement new features or fix bugs, you need to make changes to the `.ts` files, which are located in the `src` folder
- To comply with the code style, **you need to run the `format` script** - To comply with the code style, **you need to run the `format` script**
- To lint the code, **you need to run the `lint:fix` script**
- To transpile source code to `javascript` we use [NCC](https://github.com/vercel/ncc). **It is very important to run the `build` script after making changes**, otherwise your changes will not get into the final `javascript` build - To transpile source code to `javascript` we use [NCC](https://github.com/vercel/ncc). **It is very important to run the `build` script after making changes**, otherwise your changes will not get into the final `javascript` build
**Learn more about how to implement tests:** **Learn more about how to implement tests:**

2137
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,9 +6,10 @@
"main": "lib/setup-dotnet.js", "main": "lib/setup-dotnet.js",
"scripts": { "scripts": {
"build": "tsc && ncc build", "build": "tsc && ncc build",
"format": "prettier --write **/*.ts", "format": "prettier --no-error-on-unmatched-pattern --config ./.prettierrc.js --write **/*.{ts,yml,yaml}",
"format-check": "prettier --check **/*.ts", "format-check": "prettier --no-error-on-unmatched-pattern --config ./.prettierrc.js --check **/*.{ts,yml,yaml}",
"lint": "echo \"Fake command that does nothing. It is used in reusable workflows\"", "lint": "eslint --config ./.eslintrc.js **/*.ts",
"lint:fix": "eslint --config ./.eslintrc.js **/*.ts --fix",
"prepare": "husky install", "prepare": "husky install",
"test": "jest --coverage --config ./jest.config.js", "test": "jest --coverage --config ./jest.config.js",
"update-installers": "nwget https://dot.net/v1/dotnet-install.ps1 -O externals/install-dotnet.ps1 && nwget https://dot.net/v1/dotnet-install.sh -O externals/install-dotnet.sh" "update-installers": "nwget https://dot.net/v1/dotnet-install.ps1 -O externals/install-dotnet.ps1 && nwget https://dot.net/v1/dotnet-install.sh -O externals/install-dotnet.sh"
@ -37,11 +38,16 @@
"@types/jest": "^27.0.2", "@types/jest": "^27.0.2",
"@types/node": "^16.11.25", "@types/node": "^16.11.25",
"@types/semver": "^6.2.2", "@types/semver": "^6.2.2",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"@vercel/ncc": "^0.33.4", "@vercel/ncc": "^0.33.4",
"eslint": "^8.35.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-jest": "^27.2.1",
"husky": "^8.0.1", "husky": "^8.0.1",
"jest": "^27.2.5", "jest": "^27.2.5",
"jest-circus": "^27.2.5", "jest-circus": "^27.2.5",
"prettier": "^2.7.1", "prettier": "^2.8.4",
"ts-jest": "^27.0.5", "ts-jest": "^27.0.5",
"typescript": "^4.8.4", "typescript": "^4.8.4",
"wget-improved": "^3.2.1" "wget-improved": "^3.2.1"

View File

@ -1,196 +1,196 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as github from '@actions/github'; import * as github from '@actions/github';
import {XMLParser, XMLBuilder} from 'fast-xml-parser'; import {XMLParser, XMLBuilder} from 'fast-xml-parser';
export function configAuthentication( export function configAuthentication(
feedUrl: string, feedUrl: string,
existingFileLocation: string = '', existingFileLocation = '',
processRoot: string = process.cwd() processRoot: string = process.cwd()
) { ) {
const existingNuGetConfig: string = path.resolve( const existingNuGetConfig: string = path.resolve(
processRoot, processRoot,
existingFileLocation === '' existingFileLocation === ''
? getExistingNugetConfig(processRoot) ? getExistingNugetConfig(processRoot)
: existingFileLocation : existingFileLocation
); );
const tempNuGetConfig: string = path.resolve( const tempNuGetConfig: string = path.resolve(
processRoot, processRoot,
'../', '../',
'nuget.config' 'nuget.config'
); );
writeFeedToFile(feedUrl, existingNuGetConfig, tempNuGetConfig); writeFeedToFile(feedUrl, existingNuGetConfig, tempNuGetConfig);
} }
function isValidKey(key: string): boolean { function isValidKey(key: string): boolean {
return /^[\w\-\.]+$/i.test(key); return /^[\w\-.]+$/i.test(key);
} }
function getExistingNugetConfig(processRoot: string) { function getExistingNugetConfig(processRoot: string) {
const defaultConfigName = 'nuget.config'; const defaultConfigName = 'nuget.config';
const configFileNames = fs const configFileNames = fs
.readdirSync(processRoot) .readdirSync(processRoot)
.filter(filename => filename.toLowerCase() === defaultConfigName); .filter(filename => filename.toLowerCase() === defaultConfigName);
if (configFileNames.length) { if (configFileNames.length) {
return configFileNames[0]; return configFileNames[0];
} }
return defaultConfigName; return defaultConfigName;
} }
function writeFeedToFile( function writeFeedToFile(
feedUrl: string, feedUrl: string,
existingFileLocation: string, existingFileLocation: string,
tempFileLocation: string tempFileLocation: string
) { ) {
core.info( core.info(
`dotnet-auth: Finding any source references in ${existingFileLocation}, writing a new temporary configuration file with credentials to ${tempFileLocation}` `dotnet-auth: Finding any source references in ${existingFileLocation}, writing a new temporary configuration file with credentials to ${tempFileLocation}`
); );
let sourceKeys: string[] = []; const sourceKeys: string[] = [];
let owner: string = core.getInput('owner'); let owner: string = core.getInput('owner');
let sourceUrl: string = feedUrl; const sourceUrl: string = feedUrl;
if (!owner) { if (!owner) {
owner = github.context.repo.owner; owner = github.context.repo.owner;
} }
if (!process.env.NUGET_AUTH_TOKEN) { if (!process.env.NUGET_AUTH_TOKEN) {
throw new Error( throw new Error(
'The NUGET_AUTH_TOKEN environment variable was not provided. In this step, add the following: \r\nenv:\r\n NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}' 'The NUGET_AUTH_TOKEN environment variable was not provided. In this step, add the following: \r\nenv:\r\n NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}'
); );
} }
if (fs.existsSync(existingFileLocation)) { if (fs.existsSync(existingFileLocation)) {
// get key from existing NuGet.config so NuGet/dotnet can match credentials // get key from existing NuGet.config so NuGet/dotnet can match credentials
const curContents: string = fs.readFileSync(existingFileLocation, 'utf8'); const curContents: string = fs.readFileSync(existingFileLocation, 'utf8');
const parserOptions = { const parserOptions = {
ignoreAttributes: false ignoreAttributes: false
}; };
const parser = new XMLParser(parserOptions); const parser = new XMLParser(parserOptions);
const json = parser.parse(curContents); const json = parser.parse(curContents);
if (typeof json.configuration === 'undefined') { if (typeof json.configuration === 'undefined') {
throw new Error(`The provided NuGet.config seems invalid.`); throw new Error(`The provided NuGet.config seems invalid.`);
} }
if (json.configuration?.packageSources?.add) { if (json.configuration?.packageSources?.add) {
const packageSources = json.configuration.packageSources.add; const packageSources = json.configuration.packageSources.add;
if (Array.isArray(packageSources)) { if (Array.isArray(packageSources)) {
packageSources.forEach(source => { packageSources.forEach(source => {
const value = source['@_value']; const value = source['@_value'];
core.debug(`source '${value}'`); core.debug(`source '${value}'`);
if (value.toLowerCase().includes(feedUrl.toLowerCase())) { if (value.toLowerCase().includes(feedUrl.toLowerCase())) {
const key = source['@_key']; const key = source['@_key'];
sourceKeys.push(key); sourceKeys.push(key);
core.debug(`Found a URL with key ${key}`); core.debug(`Found a URL with key ${key}`);
} }
}); });
} else { } else {
if ( if (
packageSources['@_value'] packageSources['@_value']
.toLowerCase() .toLowerCase()
.includes(feedUrl.toLowerCase()) .includes(feedUrl.toLowerCase())
) { ) {
const key = packageSources['@_key']; const key = packageSources['@_key'];
sourceKeys.push(key); sourceKeys.push(key);
core.debug(`Found a URL with key ${key}`); core.debug(`Found a URL with key ${key}`);
} }
} }
} }
} }
const xmlSource: any[] = [ const xmlSource: any[] = [
{ {
'?xml': [ '?xml': [
{ {
'#text': '' '#text': ''
} }
], ],
':@': { ':@': {
'@_version': '1.0' '@_version': '1.0'
} }
}, },
{ {
configuration: [ configuration: [
{ {
config: [ config: [
{ {
add: [], add: [],
':@': { ':@': {
'@_key': 'defaultPushSource', '@_key': 'defaultPushSource',
'@_value': sourceUrl '@_value': sourceUrl
} }
} }
] ]
} }
] ]
} }
]; ];
if (!sourceKeys.length) { if (!sourceKeys.length) {
let keystring = 'Source'; const keystring = 'Source';
xmlSource[1].configuration.push({ xmlSource[1].configuration.push({
packageSources: [ packageSources: [
{ {
add: [], add: [],
':@': { ':@': {
'@_key': keystring, '@_key': keystring,
'@_value': sourceUrl '@_value': sourceUrl
} }
} }
] ]
}); });
sourceKeys.push(keystring); sourceKeys.push(keystring);
} }
const packageSourceCredentials: any[] = []; const packageSourceCredentials: any[] = [];
sourceKeys.forEach(key => { sourceKeys.forEach(key => {
if (!isValidKey(key)) { if (!isValidKey(key)) {
throw new Error( throw new Error(
"Source name can contain letters, numbers, and '-', '_', '.' symbols only. Please, fix source name in NuGet.config and try again." "Source name can contain letters, numbers, and '-', '_', '.' symbols only. Please, fix source name in NuGet.config and try again."
); );
} }
packageSourceCredentials.push({ packageSourceCredentials.push({
[key]: [ [key]: [
{ {
add: [], add: [],
':@': { ':@': {
'@_key': 'Username', '@_key': 'Username',
'@_value': owner '@_value': owner
} }
}, },
{ {
add: [], add: [],
':@': { ':@': {
'@_key': 'ClearTextPassword', '@_key': 'ClearTextPassword',
'@_value': process.env.NUGET_AUTH_TOKEN '@_value': process.env.NUGET_AUTH_TOKEN
} }
} }
] ]
}); });
}); });
xmlSource[1].configuration.push({ xmlSource[1].configuration.push({
packageSourceCredentials packageSourceCredentials
}); });
const xmlBuilderOptions = { const xmlBuilderOptions = {
format: true, format: true,
ignoreAttributes: false, ignoreAttributes: false,
preserveOrder: true, preserveOrder: true,
allowBooleanAttributes: true, allowBooleanAttributes: true,
suppressBooleanAttributes: true, suppressBooleanAttributes: true,
suppressEmptyNode: true suppressEmptyNode: true
}; };
const builder = new XMLBuilder(xmlBuilderOptions); const builder = new XMLBuilder(xmlBuilderOptions);
const output = builder.build(xmlSource).trim(); const output = builder.build(xmlSource).trim();
fs.writeFileSync(tempFileLocation, output); fs.writeFileSync(tempFileLocation, output);
} }

View File

@ -1,265 +1,265 @@
// Load tempDirectory before it gets wiped by tool-cache // Load tempDirectory before it gets wiped by tool-cache
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import * as io from '@actions/io'; import * as io from '@actions/io';
import * as hc from '@actions/http-client'; import * as hc from '@actions/http-client';
import {chmodSync} from 'fs'; import {chmodSync} from 'fs';
import {readdir} from 'fs/promises'; import {readdir} from 'fs/promises';
import path from 'path'; import path from 'path';
import os from 'os'; import os from 'os';
import semver from 'semver'; import semver from 'semver';
import {IS_LINUX, IS_WINDOWS} from './utils'; import {IS_LINUX, IS_WINDOWS} from './utils';
import {QualityOptions} from './setup-dotnet'; import {QualityOptions} from './setup-dotnet';
export interface DotnetVersion { export interface DotnetVersion {
type: string; type: string;
value: string; value: string;
qualityFlag: boolean; qualityFlag: boolean;
} }
export class DotnetVersionResolver { export class DotnetVersionResolver {
private inputVersion: string; private inputVersion: string;
private resolvedArgument: DotnetVersion; private resolvedArgument: DotnetVersion;
constructor(version: string) { constructor(version: string) {
this.inputVersion = version.trim(); this.inputVersion = version.trim();
this.resolvedArgument = {type: '', value: '', qualityFlag: false}; this.resolvedArgument = {type: '', value: '', qualityFlag: false};
} }
private async resolveVersionInput(): Promise<void> { private async resolveVersionInput(): Promise<void> {
if (!semver.validRange(this.inputVersion)) { if (!semver.validRange(this.inputVersion)) {
throw new Error( throw new Error(
`'dotnet-version' was supplied in invalid format: ${this.inputVersion}! Supported syntax: A.B.C, A.B, A.B.x, A, A.x` `'dotnet-version' was supplied in invalid format: ${this.inputVersion}! Supported syntax: A.B.C, A.B, A.B.x, A, A.x`
); );
} }
if (semver.valid(this.inputVersion)) { if (semver.valid(this.inputVersion)) {
this.resolvedArgument.type = 'version'; this.resolvedArgument.type = 'version';
this.resolvedArgument.value = this.inputVersion; this.resolvedArgument.value = this.inputVersion;
} else { } else {
const [major, minor] = this.inputVersion.split('.'); const [major, minor] = this.inputVersion.split('.');
if (this.isNumericTag(major)) { if (this.isNumericTag(major)) {
this.resolvedArgument.type = 'channel'; this.resolvedArgument.type = 'channel';
if (this.isNumericTag(minor)) { if (this.isNumericTag(minor)) {
this.resolvedArgument.value = `${major}.${minor}`; this.resolvedArgument.value = `${major}.${minor}`;
} else { } else {
const httpClient = new hc.HttpClient('actions/setup-dotnet', [], { const httpClient = new hc.HttpClient('actions/setup-dotnet', [], {
allowRetries: true, allowRetries: true,
maxRetries: 3 maxRetries: 3
}); });
this.resolvedArgument.value = await this.getLatestVersion( this.resolvedArgument.value = await this.getLatestVersion(
httpClient, httpClient,
[major, minor] [major, minor]
); );
} }
} }
this.resolvedArgument.qualityFlag = +major >= 6 ? true : false; this.resolvedArgument.qualityFlag = +major >= 6 ? true : false;
} }
} }
private isNumericTag(versionTag): boolean { private isNumericTag(versionTag): boolean {
return /^\d+$/.test(versionTag); return /^\d+$/.test(versionTag);
} }
public async createDotNetVersion(): Promise<{ public async createDotNetVersion(): Promise<{
type: string; type: string;
value: string; value: string;
qualityFlag: boolean; qualityFlag: boolean;
}> { }> {
await this.resolveVersionInput(); await this.resolveVersionInput();
if (!this.resolvedArgument.type) { if (!this.resolvedArgument.type) {
return this.resolvedArgument; return this.resolvedArgument;
} }
if (IS_WINDOWS) { if (IS_WINDOWS) {
this.resolvedArgument.type = this.resolvedArgument.type =
this.resolvedArgument.type === 'channel' ? '-Channel' : '-Version'; this.resolvedArgument.type === 'channel' ? '-Channel' : '-Version';
} else { } else {
this.resolvedArgument.type = this.resolvedArgument.type =
this.resolvedArgument.type === 'channel' ? '--channel' : '--version'; this.resolvedArgument.type === 'channel' ? '--channel' : '--version';
} }
return this.resolvedArgument; return this.resolvedArgument;
} }
private async getLatestVersion( private async getLatestVersion(
httpClient: hc.HttpClient, httpClient: hc.HttpClient,
versionParts: string[] versionParts: string[]
): Promise<string> { ): Promise<string> {
const response = await httpClient.getJson<any>( const response = await httpClient.getJson<any>(
DotnetVersionResolver.DotNetCoreIndexUrl DotnetVersionResolver.DotNetCoreIndexUrl
); );
const result = response.result || {}; const result = response.result || {};
let releasesInfo: any[] = result['releases-index']; const releasesInfo: any[] = result['releases-index'];
let releaseInfo = releasesInfo.find(info => { const releaseInfo = releasesInfo.find(info => {
let sdkParts: string[] = info['channel-version'].split('.'); const sdkParts: string[] = info['channel-version'].split('.');
return sdkParts[0] === versionParts[0]; return sdkParts[0] === versionParts[0];
}); });
if (!releaseInfo) { if (!releaseInfo) {
throw new Error( throw new Error(
`Could not find info for version ${versionParts.join('.')} at ${ `Could not find info for version ${versionParts.join('.')} at ${
DotnetVersionResolver.DotNetCoreIndexUrl DotnetVersionResolver.DotNetCoreIndexUrl
}` }`
); );
} }
return releaseInfo['channel-version']; return releaseInfo['channel-version'];
} }
static DotNetCoreIndexUrl: string = static DotNetCoreIndexUrl =
'https://dotnetcli.azureedge.net/dotnet/release-metadata/releases-index.json'; 'https://dotnetcli.azureedge.net/dotnet/release-metadata/releases-index.json';
} }
export class DotnetCoreInstaller { export class DotnetCoreInstaller {
private version: string; private version: string;
private quality: QualityOptions; private quality: QualityOptions;
static { static {
const installationDirectoryWindows = path.join( const installationDirectoryWindows = path.join(
process.env['PROGRAMFILES'] + '', process.env['PROGRAMFILES'] + '',
'dotnet' 'dotnet'
); );
const installationDirectoryLinux = '/usr/share/dotnet'; const installationDirectoryLinux = '/usr/share/dotnet';
const installationDirectoryMac = path.join( const installationDirectoryMac = path.join(
process.env['HOME'] + '', process.env['HOME'] + '',
'.dotnet' '.dotnet'
); );
const dotnetInstallDir: string | undefined = const dotnetInstallDir: string | undefined =
process.env['DOTNET_INSTALL_DIR']; process.env['DOTNET_INSTALL_DIR'];
if (dotnetInstallDir) { if (dotnetInstallDir) {
process.env['DOTNET_INSTALL_DIR'] = process.env['DOTNET_INSTALL_DIR'] =
this.convertInstallPathToAbsolute(dotnetInstallDir); this.convertInstallPathToAbsolute(dotnetInstallDir);
} else { } else {
if (IS_WINDOWS) { if (IS_WINDOWS) {
process.env['DOTNET_INSTALL_DIR'] = installationDirectoryWindows; process.env['DOTNET_INSTALL_DIR'] = installationDirectoryWindows;
} else { } else {
process.env['DOTNET_INSTALL_DIR'] = IS_LINUX process.env['DOTNET_INSTALL_DIR'] = IS_LINUX
? installationDirectoryLinux ? installationDirectoryLinux
: installationDirectoryMac; : installationDirectoryMac;
} }
} }
} }
constructor(version: string, quality: QualityOptions) { constructor(version: string, quality: QualityOptions) {
this.version = version; this.version = version;
this.quality = quality; this.quality = quality;
} }
private static convertInstallPathToAbsolute(installDir: string): string { private static convertInstallPathToAbsolute(installDir: string): string {
let transformedPath; let transformedPath;
if (path.isAbsolute(installDir)) { if (path.isAbsolute(installDir)) {
transformedPath = installDir; transformedPath = installDir;
} else { } else {
transformedPath = installDir.startsWith('~') transformedPath = installDir.startsWith('~')
? path.join(os.homedir(), installDir.slice(1)) ? path.join(os.homedir(), installDir.slice(1))
: (transformedPath = path.join(process.cwd(), installDir)); : (transformedPath = path.join(process.cwd(), installDir));
} }
return path.normalize(transformedPath); return path.normalize(transformedPath);
} }
static addToPath() { static addToPath() {
core.addPath(process.env['DOTNET_INSTALL_DIR']!); core.addPath(process.env['DOTNET_INSTALL_DIR']!);
core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']); core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']);
} }
private setQuality( private setQuality(
dotnetVersion: DotnetVersion, dotnetVersion: DotnetVersion,
scriptArguments: string[] scriptArguments: string[]
): void { ): void {
const option = IS_WINDOWS ? '-Quality' : '--quality'; const option = IS_WINDOWS ? '-Quality' : '--quality';
if (dotnetVersion.qualityFlag) { if (dotnetVersion.qualityFlag) {
scriptArguments.push(option, this.quality); scriptArguments.push(option, this.quality);
} else { } else {
core.warning( core.warning(
`'dotnet-quality' input can be used only with .NET SDK version in A.B, A.B.x, A and A.x formats where the major tag is higher than 5. You specified: ${this.version}. 'dotnet-quality' input is ignored.` `'dotnet-quality' input can be used only with .NET SDK version in A.B, A.B.x, A and A.x formats where the major tag is higher than 5. You specified: ${this.version}. 'dotnet-quality' input is ignored.`
); );
} }
} }
public async installDotnet(): Promise<string> { public async installDotnet(): Promise<string> {
const windowsDefaultOptions = [ const windowsDefaultOptions = [
'-NoLogo', '-NoLogo',
'-Sta', '-Sta',
'-NoProfile', '-NoProfile',
'-NonInteractive', '-NonInteractive',
'-ExecutionPolicy', '-ExecutionPolicy',
'Unrestricted', 'Unrestricted',
'-Command' '-Command'
]; ];
const scriptName = IS_WINDOWS ? 'install-dotnet.ps1' : 'install-dotnet.sh'; const scriptName = IS_WINDOWS ? 'install-dotnet.ps1' : 'install-dotnet.sh';
const escapedScript = path const escapedScript = path
.join(__dirname, '..', 'externals', scriptName) .join(__dirname, '..', 'externals', scriptName)
.replace(/'/g, "''"); .replace(/'/g, "''");
let scriptArguments: string[]; let scriptArguments: string[];
let scriptPath = ''; let scriptPath = '';
const versionResolver = new DotnetVersionResolver(this.version); const versionResolver = new DotnetVersionResolver(this.version);
const dotnetVersion = await versionResolver.createDotNetVersion(); const dotnetVersion = await versionResolver.createDotNetVersion();
if (IS_WINDOWS) { if (IS_WINDOWS) {
scriptArguments = ['&', `'${escapedScript}'`]; scriptArguments = ['&', `'${escapedScript}'`];
if (dotnetVersion.type) { if (dotnetVersion.type) {
scriptArguments.push(dotnetVersion.type, dotnetVersion.value); scriptArguments.push(dotnetVersion.type, dotnetVersion.value);
} }
if (this.quality) { if (this.quality) {
this.setQuality(dotnetVersion, scriptArguments); this.setQuality(dotnetVersion, scriptArguments);
} }
if (process.env['https_proxy'] != null) { if (process.env['https_proxy'] != null) {
scriptArguments.push(`-ProxyAddress ${process.env['https_proxy']}`); scriptArguments.push(`-ProxyAddress ${process.env['https_proxy']}`);
} }
// This is not currently an option // This is not currently an option
if (process.env['no_proxy'] != null) { if (process.env['no_proxy'] != null) {
scriptArguments.push(`-ProxyBypassList ${process.env['no_proxy']}`); scriptArguments.push(`-ProxyBypassList ${process.env['no_proxy']}`);
} }
scriptPath = scriptPath =
(await io.which('pwsh', false)) || (await io.which('powershell', true)); (await io.which('pwsh', false)) || (await io.which('powershell', true));
scriptArguments = windowsDefaultOptions.concat(scriptArguments); scriptArguments = windowsDefaultOptions.concat(scriptArguments);
} else { } else {
chmodSync(escapedScript, '777'); chmodSync(escapedScript, '777');
scriptPath = await io.which(escapedScript, true); scriptPath = await io.which(escapedScript, true);
scriptArguments = []; scriptArguments = [];
if (dotnetVersion.type) { if (dotnetVersion.type) {
scriptArguments.push(dotnetVersion.type, dotnetVersion.value); scriptArguments.push(dotnetVersion.type, dotnetVersion.value);
} }
if (this.quality) { if (this.quality) {
this.setQuality(dotnetVersion, scriptArguments); this.setQuality(dotnetVersion, scriptArguments);
} }
} }
// process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used // process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used
const getExecOutputOptions = { const getExecOutputOptions = {
ignoreReturnCode: true, ignoreReturnCode: true,
env: process.env as {string: string} env: process.env as {string: string}
}; };
const {exitCode, stderr} = await exec.getExecOutput( const {exitCode, stderr} = await exec.getExecOutput(
`"${scriptPath}"`, `"${scriptPath}"`,
scriptArguments, scriptArguments,
getExecOutputOptions getExecOutputOptions
); );
if (exitCode) { if (exitCode) {
throw new Error( throw new Error(
`Failed to install dotnet, exit code: ${exitCode}. ${stderr}` `Failed to install dotnet, exit code: ${exitCode}. ${stderr}`
); );
} }
return this.outputDotnetVersion(dotnetVersion.value); return this.outputDotnetVersion(dotnetVersion.value);
} }
private async outputDotnetVersion(version): Promise<string> { private async outputDotnetVersion(version): Promise<string> {
const installationPath = process.env['DOTNET_INSTALL_DIR']!; const installationPath = process.env['DOTNET_INSTALL_DIR']!;
let versionsOnRunner: string[] = await readdir( const versionsOnRunner: string[] = await readdir(
path.join(installationPath.replace(/'/g, ''), 'sdk') path.join(installationPath.replace(/'/g, ''), 'sdk')
); );
let installedVersion = semver.maxSatisfying(versionsOnRunner, version, { const installedVersion = semver.maxSatisfying(versionsOnRunner, version, {
includePrerelease: true includePrerelease: true
})!; })!;
return installedVersion; return installedVersion;
} }
} }

View File

@ -13,7 +13,7 @@ const qualityOptions = [
'ga' 'ga'
] as const; ] as const;
export type QualityOptions = typeof qualityOptions[number]; export type QualityOptions = (typeof qualityOptions)[number];
export async function run() { export async function run() {
try { try {
@ -100,7 +100,7 @@ export async function run() {
} }
function getVersionFromGlobalJson(globalJsonPath: string): string { function getVersionFromGlobalJson(globalJsonPath: string): string {
let version: string = ''; let version = '';
const globalJson = JSON.parse( const globalJson = JSON.parse(
// .trim() is necessary to strip BOM https://github.com/nodejs/node/issues/20649 // .trim() is necessary to strip BOM https://github.com/nodejs/node/issues/20649
fs.readFileSync(globalJsonPath, {encoding: 'utf8'}).trim() fs.readFileSync(globalJsonPath, {encoding: 'utf8'}).trim()

View File

@ -49,7 +49,8 @@
// "typeRoots": [], /* List of folders to include type definitions from. */ // "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */ // "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"resolveJsonModule": true, /* Allows importing modules with a '.json' extension, which is a common practice in node projects. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */