diff --git a/lib/installer.js b/lib/installer.js index 5b30c6e..60f960d 100644 --- a/lib/installer.js +++ b/lib/installer.js @@ -23,11 +23,12 @@ const path = __importStar(require("path")); const semver = __importStar(require("semver")); const IS_WINDOWS = process.platform === 'win32'; class DotnetCoreInstaller { - constructor(version) { + constructor(version = "", jsonfile = "") { if (semver.valid(semver.clean(version) || '') == null) { throw 'Implicit version not permitted'; } this.version = version; + this.jsonfile = jsonfile; } installDotnet() { return __awaiter(this, void 0, void 0, function* () { @@ -37,7 +38,13 @@ class DotnetCoreInstaller { let escapedScript = path .join(__dirname, '..', 'externals', 'install-dotnet.ps1') .replace(/'/g, "''"); - let command = `& '${escapedScript}' -Version ${this.version}`; + let command = `& '${escapedScript}'`; + if (this.version) { + command += ` -Version ${this.version}`; + } + if (this.jsonfile) { + command += ` -jsonfile ${this.jsonfile}`; + } const powershellPath = yield io.which('powershell', true); resultCode = yield exec.exec(`"${powershellPath}"`, [ '-NoLogo', @@ -62,7 +69,14 @@ class DotnetCoreInstaller { .replace(/'/g, "''"); fs_1.chmodSync(escapedScript, '777'); const scriptPath = yield io.which(escapedScript, true); - resultCode = yield exec.exec(`"${scriptPath}"`, ['--version', this.version], { + let scriptArguments = []; + if (this.version) { + scriptArguments.concat(['--version', this.version]); + } + if (this.jsonfile) { + scriptArguments.concat(['--jsonfile', this.jsonfile]); + } + resultCode = yield exec.exec(`"${scriptPath}"`, scriptArguments, { listeners: { stdout: (data) => { output += data.toString(); diff --git a/lib/setup-dotnet.js b/lib/setup-dotnet.js index 8822872..477f94a 100644 --- a/lib/setup-dotnet.js +++ b/lib/setup-dotnet.js @@ -17,6 +17,7 @@ var __importStar = (this && this.__importStar) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); const core = __importStar(require("@actions/core")); const installer = __importStar(require("./installer")); +const fs = __importStar(require("fs")); const path = __importStar(require("path")); function run() { return __awaiter(this, void 0, void 0, function* () { @@ -31,6 +32,15 @@ function run() { const dotnetInstaller = new installer.DotnetCoreInstaller(version); yield dotnetInstaller.installDotnet(); } + else { + // Try to fall back to global.json + core.debug('No version found, falling back to global.json'); + const globalJsonPath = path.join(process.cwd(), 'global.json'); + if (fs.existsSync(globalJsonPath)) { + const dotnetInstaller = new installer.DotnetCoreInstaller(undefined, globalJsonPath); + yield dotnetInstaller.installDotnet(); + } + } // TODO: setup proxy from runner proxy config const matchersPath = path.join(__dirname, '..', '.github'); console.log(`##[add-matcher]${path.join(matchersPath, 'csc.json')}`); diff --git a/src/installer.ts b/src/installer.ts index 35cb617..8ffba22 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -9,11 +9,12 @@ import * as semver from 'semver'; const IS_WINDOWS = process.platform === 'win32'; export class DotnetCoreInstaller { - constructor(version: string) { + constructor(version: string = "", jsonfile: string = "") { if (semver.valid(semver.clean(version) || '') == null) { throw 'Implicit version not permitted'; } this.version = version; + this.jsonfile = jsonfile; } public async installDotnet() { @@ -24,7 +25,13 @@ export class DotnetCoreInstaller { let escapedScript = path .join(__dirname, '..', 'externals', 'install-dotnet.ps1') .replace(/'/g, "''"); - let command = `& '${escapedScript}' -Version ${this.version}`; + let command = `& '${escapedScript}'`; + if (this.version) { + command += ` -Version ${this.version}`; + } + if (this.jsonfile) { + command += ` -jsonfile ${this.jsonfile}`; + } const powershellPath = await io.which('powershell', true); resultCode = await exec.exec( @@ -54,9 +61,18 @@ export class DotnetCoreInstaller { chmodSync(escapedScript, '777'); const scriptPath = await io.which(escapedScript, true); + + let scriptArguments: string[] = []; + if (this.version) { + scriptArguments.concat(['--version', this.version]); + } + if (this.jsonfile) { + scriptArguments.concat(['--jsonfile', this.jsonfile]); + } + resultCode = await exec.exec( `"${scriptPath}"`, - ['--version', this.version], + scriptArguments, { listeners: { stdout: (data: Buffer) => { @@ -73,4 +89,5 @@ export class DotnetCoreInstaller { } private version: string; + private jsonfile: string; } diff --git a/src/setup-dotnet.ts b/src/setup-dotnet.ts index 08c817f..ffb5acf 100644 --- a/src/setup-dotnet.ts +++ b/src/setup-dotnet.ts @@ -1,5 +1,6 @@ import * as core from '@actions/core'; import * as installer from './installer'; +import * as fs from 'fs'; import * as path from 'path'; async function run() { @@ -12,11 +13,19 @@ async function run() { `::warning::Use the v1 tag to get the last version, master may contain breaking changes and will not contain any required packages in the future. i.e. actions/setup-dotnet@v1` ); - let version = core.getInput('dotnet-version'); + let version: string = core.getInput('dotnet-version'); if (version) { const dotnetInstaller = new installer.DotnetCoreInstaller(version); await dotnetInstaller.installDotnet(); + } else { + // Try to fall back to global.json + core.debug('No version found, falling back to global.json'); + const globalJsonPath = path.join(process.cwd(), 'global.json'); + if (fs.existsSync(globalJsonPath)) { + const dotnetInstaller = new installer.DotnetCoreInstaller(undefined, globalJsonPath); + await dotnetInstaller.installDotnet(); + } } // TODO: setup proxy from runner proxy config