First attempt at find ruby action

This commit is contained in:
Derrick Marcey
2019-06-27 11:50:36 -04:00
parent d2fa3b8435
commit c39a9768cf
7 changed files with 148 additions and 6 deletions

View File

@@ -1,3 +1,7 @@
import findRuby, {Platform} from '../src/find-ruby';
describe('TODO - Add a test suite', () => {
it('TODO - Add a test', async () => {});
it('TODO - Add a test', async () => {
findRuby({addToPath: 'true', version: '>= 2.4'}, Platform.Windows);
});
});

View File

@@ -3,8 +3,15 @@ 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'
add-to-path:
description: 'Prepend the retrieved Ruby version to the PATH environment variable to make it available in subsequent actions or scripts without using the output variable.'
default: 'true'
outputs:
ruby-location:
description: "The resolved folder of the Ruby distribution."
runs:
using: 'node'
main: 'lib/main.js'

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

@@ -0,0 +1,62 @@
"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 os = __importStar(require("os"));
const path = __importStar(require("path"));
var Platform;
(function (Platform) {
Platform[Platform["Windows"] = 0] = "Windows";
Platform[Platform["MacOS"] = 1] = "MacOS";
Platform[Platform["Linux"] = 2] = "Linux";
})(Platform = exports.Platform || (exports.Platform = {}));
function getPlatform() {
switch (os.platform()) {
case 'win32':
return Platform.Windows;
case 'darwin':
return Platform.MacOS;
case 'linux':
return Platform.Linux;
default:
throw Error('Platform not recognized');
}
}
exports.getPlatform = getPlatform;
function default_1(inputs, platform) {
return __awaiter(this, void 0, void 0, function* () {
const installDir = tc.find('Ruby', inputs.version);
if (!installDir) {
throw new Error(`Version ${inputs.version} not found`);
}
const toolPath = path.join(installDir, 'bin');
if (platform !== Platform.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.setOutput('ruby-location', toolPath);
if (inputs.addToPath === 'true') {
core.addPath(toolPath);
}
});
}
exports.default = default_1;

View File

@@ -16,10 +16,17 @@ var __importStar = (this && this.__importStar) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const find_ruby_1 = __importStar(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');
const addToPath = core.getInput('add-to-path');
yield find_ruby_1.default({ version, addToPath }, find_ruby_1.getPlatform());
}
catch (error) {
core.setFailed(error.message);
}
});
}
run();

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

@@ -0,0 +1,53 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as tc from '@actions/tool-cache';
import * as os from 'os';
import * as path from 'path';
export enum Platform {
Windows,
MacOS,
Linux
}
export function getPlatform(): Platform {
switch (os.platform()) {
case 'win32':
return Platform.Windows;
case 'darwin':
return Platform.MacOS;
case 'linux':
return Platform.Linux;
default:
throw Error('Platform not recognized');
}
}
interface ActionInputs {
version: string;
addToPath: string;
}
export default async function(inputs: ActionInputs, platform: Platform) {
const installDir: string | null = tc.find('Ruby', inputs.version);
if (!installDir) {
throw new Error(`Version ${inputs.version} not found`);
}
const toolPath: string = path.join(installDir, 'bin');
if (platform !== Platform.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.setOutput('ruby-location', toolPath);
if (inputs.addToPath === 'true') {
core.addPath(toolPath);
}
}

View File

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

View File

@@ -4,6 +4,9 @@
// "incremental": true, /* Enable incremental compilation */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": [
"es6"
],
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */