Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to global.json if no version specified #15

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ node_modules/.bin
node_modules/typescript
node_modules/@types
node_modules/prettier
__tests__/runner/*
__tests__/runner/*
global.json
20 changes: 20 additions & 0 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const tempDir = path.join(__dirname, 'runner', 'temp');

process.env['RUNNER_TOOL_CACHE'] = toolDir;
process.env['RUNNER_TEMP'] = tempDir;
import * as setup from '../src/setup-dotnet';
import * as installer from '../src/installer';

const IS_WINDOWS = process.platform === 'win32';
Expand Down Expand Up @@ -40,6 +41,25 @@ describe('installer tests', () => {
}
}, 100000);

it('Acquires version of dotnet if no matching version is installed', async () => {
const dotnetDir = path.join(toolDir, 'dncs', '2.2.105', os.arch());

const globalJsonPath = path.join(process.cwd(), 'global.json');
const jsonContents = `{${os.EOL}"sdk": {${os.EOL}"version": "2.2.105"${os.EOL}}${os.EOL}}`;
if (!fs.existsSync(globalJsonPath)) {
fs.writeFileSync(globalJsonPath, jsonContents);
}
await setup.run();

expect(fs.existsSync(`${dotnetDir}.complete`)).toBe(true);
if (IS_WINDOWS) {
expect(fs.existsSync(path.join(dotnetDir, 'dotnet.exe'))).toBe(true);
} else {
expect(fs.existsSync(path.join(dotnetDir, 'dotnet'))).toBe(true);
}
fs.unlinkSync(globalJsonPath);
}, 100000);

it('Throws if no location contains correct dotnet version', async () => {
let thrown = false;
try {
Expand Down
15 changes: 14 additions & 1 deletion lib/setup-dotnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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* () {
Expand All @@ -25,7 +26,18 @@ function run() {
// Version is optional. If supplied, install / use from the tool cache
// If not supplied then task is still used to setup proxy, auth, etc...
//
const version = core.getInput('version');
let version = core.getInput('version');
if (!version) {
// Try to fall back to global.json
core.debug('No version found, trying to find version from global.json');
const globalJsonPath = path.join(process.cwd(), 'global.json');
if (fs.existsSync(globalJsonPath)) {
const globalJson = JSON.parse(fs.readFileSync(globalJsonPath, { encoding: 'utf8' }));
if (globalJson.sdk && globalJson.sdk.version) {
version = globalJson.sdk.version;
}
}
}
if (version) {
const dotnetInstaller = new installer.DotnetCoreInstaller(version);
yield dotnetInstaller.installDotnet();
Expand All @@ -39,4 +51,5 @@ function run() {
}
});
}
exports.run = run;
run();
19 changes: 17 additions & 2 deletions src/setup-dotnet.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import * as core from '@actions/core';
damccorm marked this conversation as resolved.
Show resolved Hide resolved
import * as installer from './installer';
import * as fs from 'fs';
import * as path from 'path';

async function run() {
export async function run() {
try {
//
// Version is optional. If supplied, install / use from the tool cache
// If not supplied then task is still used to setup proxy, auth, etc...
//
const version = core.getInput('version');
let version: string = core.getInput('version');
if (!version) {
// Try to fall back to global.json
damccorm marked this conversation as resolved.
Show resolved Hide resolved
core.debug('No version found, trying to find version from global.json');
const globalJsonPath = path.join(process.cwd(), 'global.json');
damccorm marked this conversation as resolved.
Show resolved Hide resolved
if (fs.existsSync(globalJsonPath)) {
const globalJson = JSON.parse(
fs.readFileSync(globalJsonPath, {encoding: 'utf8'})
);
if (globalJson.sdk && globalJson.sdk.version) {
version = globalJson.sdk.version;
}
}
}

if (version) {
const dotnetInstaller = new installer.DotnetCoreInstaller(version);
await dotnetInstaller.installDotnet();
Expand Down