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

Commit

Permalink
Merge pull request #1 from actions/features/getRubyVersion
Browse files Browse the repository at this point in the history
First attempt at find ruby action
  • Loading branch information
dmarcey authored Jun 27, 2019
2 parents d2fa3b8 + 22cf5cb commit edbe0e4
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 8 deletions.
59 changes: 59 additions & 0 deletions __tests__/find-ruby.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
3 changes: 0 additions & 3 deletions __tests__/main.test.ts

This file was deleted.

3 changes: 2 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 40 additions & 0 deletions lib/find-ruby.js
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 11 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 26 additions & 0 deletions src/find-ruby.ts
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit edbe0e4

Please sign in to comment.