From 91aec57b900088684bd271295233f3de9d017197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Thu, 15 Aug 2019 20:12:08 +0200 Subject: [PATCH] Draft initial implementation --- lib/main.js | 8 +++-- lib/rustup.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.ts | 9 ++++-- src/rustup.ts | 79 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+), 4 deletions(-) create mode 100644 lib/rustup.js create mode 100644 src/rustup.ts diff --git a/lib/main.js b/lib/main.js index 19fb6a1..8d23233 100644 --- a/lib/main.js +++ b/lib/main.js @@ -16,11 +16,15 @@ var __importStar = (this && this.__importStar) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); const core = __importStar(require("@actions/core")); +const rustup = __importStar(require("./rustup")); +const os = __importStar(require("os")); function run() { return __awaiter(this, void 0, void 0, function* () { try { - const myInput = core.getInput('myInput'); - core.debug(`Hello ${myInput}`); + const version = core.getInput('rust-version'); + if (version && os.platform() != 'win32') { + yield rustup.install(version); + } } catch (error) { core.setFailed(error.message); diff --git a/lib/rustup.js b/lib/rustup.js new file mode 100644 index 0000000..a11c348 --- /dev/null +++ b/lib/rustup.js @@ -0,0 +1,88 @@ +"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 io = __importStar(require("@actions/io")); +const toolCache = __importStar(require("@actions/tool-cache")); +const path = __importStar(require("path")); +const os = __importStar(require("os")); +const fs_1 = require("fs"); +let osPlat = os.platform(); +let osArch = os.arch(); +let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || ''; +const WINDOWS = osPlat == 'win32'; +function install(version) { + return __awaiter(this, void 0, void 0, function* () { + let toolPath = toolCache.find('rust', version); + if (!toolPath) { + toolPath = yield acquireRust(version); + core.debug('Rust toolchain is cached under ' + toolPath); + } + toolPath = path.join(toolPath, 'bin'); + core.addPath(toolPath); + yield exec.exec('rustup', ['update']); + }); +} +exports.install = install; +function acquireRust(version) { + return __awaiter(this, void 0, void 0, function* () { + let script; + try { + script = yield toolCache.downloadTool(rustupUrl()); + } + catch (error) { + core.debug(error); + throw `Failed to download rustup: ${error}`; + } + if (WINDOWS) { + // TODO: Fix this + // It currently fails with: + // error: 'rustup-init.exe' is not installed for the toolchain 'stable-x86_64-pc-windows-msvc' + yield io.cp(script, script + '.exe'); + script += '.exe'; + const powershell = yield io.which('powershell', true); + yield exec.exec(`"${script}"`, [ + '--default-host', + 'gnu', + '--default-toolchain', + version, + ]); + } + else { + fs_1.chmodSync(script, '777'); + yield exec.exec(`"${script}"`, ['-y', '--default-toolchain', version]); + } + return yield toolCache.cacheDir(binRoot(), 'rust', version); + }); +} +function rustupUrl() { + if (WINDOWS) { + return "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-gnu/rustup-init.exe"; + } + else { + return "https://sh.rustup.rs"; + } +} +function binRoot() { + if (WINDOWS) { + return path.join(process.env['USERPROFILE'] || '', '.cargo'); + } + else { + return path.join(process.env['HOME'] || '', '.cargo'); + } +} diff --git a/src/main.ts b/src/main.ts index fae8b12..6eaf68f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,14 @@ import * as core from '@actions/core'; +import * as rustup from './rustup'; +import * as os from 'os'; async function run() { try { - const myInput = core.getInput('myInput'); - core.debug(`Hello ${myInput}`); + const version = core.getInput('rust-version'); + + if(version && os.platform() != 'win32') { + await rustup.install(version); + } } catch (error) { core.setFailed(error.message); } diff --git a/src/rustup.ts b/src/rustup.ts new file mode 100644 index 0000000..1881878 --- /dev/null +++ b/src/rustup.ts @@ -0,0 +1,79 @@ +import * as core from '@actions/core'; +import * as exec from '@actions/exec'; +import * as io from '@actions/io'; +import * as toolCache from '@actions/tool-cache'; +import * as path from 'path'; +import * as os from 'os'; +import {chmodSync} from 'fs'; + +let osPlat: string = os.platform(); +let osArch: string = os.arch(); +let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || ''; +const WINDOWS: boolean = osPlat == 'win32'; + +export async function install(version: string) { + let toolPath = toolCache.find('rust', version); + + if (!toolPath) { + toolPath = await acquireRust(version); + core.debug('Rust toolchain is cached under ' + toolPath); + } + + toolPath = path.join(toolPath, 'bin'); + core.addPath(toolPath); + + await exec.exec('rustup', ['update']); +} + +async function acquireRust(version: string): Promise { + let script: string; + + try { + script = await toolCache.downloadTool(rustupUrl()); + } catch (error) { + core.debug(error); + throw `Failed to download rustup: ${error}`; + } + + if(WINDOWS) { + // TODO: Fix this + // It currently fails with: + // error: 'rustup-init.exe' is not installed for the toolchain 'stable-x86_64-pc-windows-msvc' + await io.cp(script, script + '.exe'); + script += '.exe'; + + const powershell = await io.which('powershell', true); + + await exec.exec( + `"${script}"`, + [ + '--default-host', + 'gnu', + '--default-toolchain', + version, + ] + ); + } else { + chmodSync(script, '777'); + + await exec.exec(`"${script}"`, ['-y', '--default-toolchain', version]); + } + + return await toolCache.cacheDir(binRoot(), 'rust', version); +} + +function rustupUrl(): string { + if(WINDOWS) { + return "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-gnu/rustup-init.exe" + } else { + return "https://sh.rustup.rs" + } +} + +function binRoot(): string { + if(WINDOWS) { + return path.join(process.env['USERPROFILE'] || '', '.cargo'); + } else { + return path.join(process.env['HOME'] || '', '.cargo'); + } +}