Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

First attempt at find ruby action #1

Merged
merged 4 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ 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'

runs:
using: 'node'
main: 'lib/main.js'
61 changes: 61 additions & 0 deletions lib/find-ruby.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"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
}
if (inputs.addToPath === 'true') {
core.addPath(toolPath);
}
});
}
exports.default = default_1;
11 changes: 9 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
51 changes: 51 additions & 0 deletions src/find-ruby.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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
}

if (inputs.addToPath === 'true') {
core.addPath(toolPath);
}
}
10 changes: 8 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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();