From f078482971ee996a7da79a0b59e838f5049eb263 Mon Sep 17 00:00:00 2001 From: Owen Smith Date: Mon, 18 Apr 2022 08:25:56 -0400 Subject: [PATCH] add global-json-file input (#276) * support specifying global.json location with global-json-file input * add test workflow jobs for global.json usage * fix typo in global-json-file description Co-authored-by: Brian Cristante <33549821+brcrista@users.noreply.github.com> Co-authored-by: Brian Cristante <33549821+brcrista@users.noreply.github.com> --- .github/workflows/workflow.yml | 26 ++++++++++++++++++++++++++ README.md | 10 ++++++++++ action.yml | 2 ++ dist/index.js | 10 ++++++++++ src/setup-dotnet.ts | 14 ++++++++++++++ 5 files changed, 62 insertions(+) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 34b7183..d2dcede 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -163,6 +163,32 @@ jobs: shell: pwsh run: __tests__/verify-dotnet.ps1 3.1 2.2 + test-setup-global-json-specified-and-version: + runs-on: ${{ matrix.operating-system }} + strategy: + fail-fast: false + matrix: + operating-system: [ubuntu-latest, windows-latest, macOS-latest] + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Clear toolcache + shell: pwsh + run: __tests__/clear-toolcache.ps1 ${{ runner.os }} + - name: Write global.json + shell: bash + run: | + mkdir subdirectory + echo '{"sdk":{"version": "2.2","rollForward": "latestFeature"}}' > ./subdirectory/global.json + - name: Setup dotnet + uses: ./ + with: + dotnet-version: 3.1 + global-json-file: ./subdirectory/global.json + - name: Verify dotnet + shell: pwsh + run: __tests__/verify-dotnet.ps1 2.2 3.1 + test-proxy: runs-on: ubuntu-latest container: diff --git a/README.md b/README.md index 5891a7b..a8656e0 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,16 @@ steps: include-prerelease: true - run: dotnet build ``` +global.json in a subdirectory: +```yml +steps: +- uses: actions/checkout@v3 +- uses: actions/setup-dotnet@v2 + with: + global-json-file: csharp/global.json +- run: dotnet build + working-directory: csharp +``` Matrix Testing: ```yaml diff --git a/action.yml b/action.yml index 7a8471e..3c0ecb0 100644 --- a/action.yml +++ b/action.yml @@ -7,6 +7,8 @@ branding: inputs: dotnet-version: 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' + global-json-file: + description: 'Optional global.json location, if your global.json isn''t located in the root of the repo.' source-url: 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' owner: diff --git a/dist/index.js b/dist/index.js index 3d48ec6..724cad8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -9115,11 +9115,21 @@ function run() { // // dotnet-version is optional, but needs to be provided for most use cases. // If supplied, install / use from the tool cache. + // global-version-file may be specified to point to a specific global.json + // and will be used to install an additional version. // If not supplied, look for version in ./global.json. // If a valid version still can't be identified, nothing will be installed. // Proxy, auth, (etc) are still set up, even if no version is identified // let versions = core.getMultilineInput('dotnet-version'); + const globalJsonFileInput = core.getInput('global-json-file'); + if (globalJsonFileInput) { + const globalJsonPath = path.join(process.cwd(), globalJsonFileInput); + if (!fs.existsSync(globalJsonPath)) { + throw new Error(`The specified global.json file '${globalJsonFileInput}' does not exist`); + } + versions.push(getVersionFromGlobalJson(globalJsonPath)); + } if (!versions.length) { // Try to fall back to global.json core.debug('No version found, trying to find version from global.json'); diff --git a/src/setup-dotnet.ts b/src/setup-dotnet.ts index 65c9893..64f3149 100644 --- a/src/setup-dotnet.ts +++ b/src/setup-dotnet.ts @@ -9,11 +9,25 @@ export async function run() { // // dotnet-version is optional, but needs to be provided for most use cases. // If supplied, install / use from the tool cache. + // global-version-file may be specified to point to a specific global.json + // and will be used to install an additional version. // If not supplied, look for version in ./global.json. // If a valid version still can't be identified, nothing will be installed. // Proxy, auth, (etc) are still set up, even if no version is identified // let versions = core.getMultilineInput('dotnet-version'); + + const globalJsonFileInput = core.getInput('global-json-file'); + if (globalJsonFileInput) { + const globalJsonPath = path.join(process.cwd(), globalJsonFileInput); + if (!fs.existsSync(globalJsonPath)) { + throw new Error( + `The specified global.json file '${globalJsonFileInput}' does not exist` + ); + } + versions.push(getVersionFromGlobalJson(globalJsonPath)); + } + if (!versions.length) { // Try to fall back to global.json core.debug('No version found, trying to find version from global.json');