diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 48f15ff..2c750df 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -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); + }); }); diff --git a/action.yml b/action.yml index 10638c4..ae043fc 100644 --- a/action.yml +++ b/action.yml @@ -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' \ No newline at end of file diff --git a/lib/find-ruby.js b/lib/find-ruby.js new file mode 100644 index 0000000..1ecdba0 --- /dev/null +++ b/lib/find-ruby.js @@ -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; diff --git a/lib/main.js b/lib/main.js index 072b3f2..cace5cf 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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(); diff --git a/src/find-ruby.ts b/src/find-ruby.ts new file mode 100644 index 0000000..038e454 --- /dev/null +++ b/src/find-ruby.ts @@ -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); + } +} diff --git a/src/main.ts b/src/main.ts index a461915..1448289 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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(); diff --git a/tsconfig.json b/tsconfig.json index 9ecff86..25dc099 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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'. */