Merge pull request #1 from actions/features/getRubyVersion

First attempt at find ruby action
This commit is contained in:
Derrick Marcey
2019-06-27 14:31:03 -04:00
committed by GitHub
7 changed files with 145 additions and 8 deletions

View File

@@ -0,0 +1,59 @@
import * as io from '@actions/io';
import fs = require('fs');
import os = require('os');
import path = require('path');
const toolDir = path.join(__dirname, 'runner', 'tools');
const tempDir = path.join(__dirname, 'runner', 'temp');
process.env['RUNNER_TOOLSDIRECTORY'] = toolDir;
process.env['RUNNER_TEMPDIRECTORY'] = tempDir;
import findRubyVersion from '../src/find-ruby';
describe('find-ruby', () => {
beforeAll(async () => {
await io.rmRF(toolDir);
await io.rmRF(tempDir);
});
afterAll(async () => {
try {
await io.rmRF(toolDir);
await io.rmRF(tempDir);
} catch {
console.log('Failed to remove test directories');
}
}, 100000);
it('findRubyVersion throws if cannot find any version of ruby', async () => {
let thrown = false;
try {
await findRubyVersion('>= 2.4');
} catch {
thrown = true;
}
expect(thrown).toBe(true);
});
it('findRubyVersion throws version of ruby is not complete', async () => {
let thrown = false;
const rubyDir: string = path.join(toolDir, 'Ruby', '2.4.6', os.arch());
await io.mkdirP(rubyDir);
try {
await findRubyVersion('>= 2.4');
} catch {
thrown = true;
}
expect(thrown).toBe(true);
});
it('findRubyVersion adds ruby bin to PATH', async () => {
const rubyDir: string = path.join(toolDir, 'Ruby', '2.4.6', os.arch());
await io.mkdirP(rubyDir);
fs.writeFileSync(`${rubyDir}.complete`, 'hello');
await findRubyVersion('>= 2.4');
const binDir = path.join(rubyDir, 'bin');
expect(process.env['PATH']!.startsWith(`${binDir};`)).toBe(true);
});
});

View File

@@ -1,3 +0,0 @@
describe('TODO - Add a test suite', () => {
it('TODO - Add a test', async () => {});
});

View File

@@ -3,8 +3,9 @@ description: 'Setup a Ruby environment and add it to the PATH'
author: 'GitHub'
inputs:
version:
description: 'The Ruby version to use. Defaults to >= 2.4'
description: 'Version range or exact version of a Ruby version to use.'
default: '>= 2.4'
runs:
using: 'node'
main: 'lib/main.js'

40
lib/find-ruby.js Normal file
View File

@@ -0,0 +1,40 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const exec = __importStar(require("@actions/exec"));
const tc = __importStar(require("@actions/tool-cache"));
const path = __importStar(require("path"));
const IS_WINDOWS = process.platform === 'win32';
function default_1(version) {
return __awaiter(this, void 0, void 0, function* () {
const installDir = tc.find('Ruby', version);
if (!installDir) {
throw new Error(`Version ${version} not found`);
}
const toolPath = path.join(installDir, 'bin');
if (!IS_WINDOWS) {
// Ruby / Gem heavily use the '#!/usr/bin/ruby' to find ruby, so this task needs to
// replace that version of ruby so all the correct version of ruby gets selected
// replace the default
const dest = '/usr/bin/ruby';
exec.exec('sudo ln', ['-sf', path.join(toolPath, 'ruby'), dest]); // replace any existing
}
core.addPath(toolPath);
});
}
exports.default = default_1;

View File

@@ -14,12 +14,21 @@ var __importStar = (this && this.__importStar) || function (mod) {
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const find_ruby_1 = __importDefault(require("./find-ruby"));
function run() {
return __awaiter(this, void 0, void 0, function* () {
const myInput = core.getInput('myInput');
core.debug(`Hello ${myInput}`);
try {
const version = core.getInput('version');
yield find_ruby_1.default(version);
}
catch (error) {
core.setFailed(error.message);
}
});
}
run();

26
src/find-ruby.ts Normal file
View File

@@ -0,0 +1,26 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as tc from '@actions/tool-cache';
import * as path from 'path';
const IS_WINDOWS = process.platform === 'win32';
export default async function(version: string) {
const installDir: string | null = tc.find('Ruby', version);
if (!installDir) {
throw new Error(`Version ${version} not found`);
}
const toolPath: string = path.join(installDir, 'bin');
if (!IS_WINDOWS) {
// Ruby / Gem heavily use the '#!/usr/bin/ruby' to find ruby, so this task needs to
// replace that version of ruby so all the correct version of ruby gets selected
// replace the default
const dest: string = '/usr/bin/ruby';
exec.exec('sudo ln', ['-sf', path.join(toolPath, 'ruby'), dest]); // replace any existing
}
core.addPath(toolPath);
}

View File

@@ -1,8 +1,13 @@
import * as core from '@actions/core';
import findRubyVersion from './find-ruby';
async function run() {
const myInput = core.getInput('myInput');
core.debug(`Hello ${myInput}`);
try {
const version = core.getInput('version');
await findRubyVersion(version);
} catch (error) {
core.setFailed(error.message);
}
}
run();