Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
Output rustc, cargo and rustup versions as Action outputs (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
svartalf authored Jan 26, 2020
1 parent ad7f1a0 commit 5d50a12
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ See [additional recipes here](https://github.com/actions-rs/meta).
| `profile` | | Execute `rustup set profile {value}` before installing the toolchain, ex. `minimal` | string | |
| `components` | | Comma-separated list of the additional components to install, ex. `clippy, rustfmt` | string | |

## Outputs

Installed `rustc`, `cargo` and `rustup` versions can be fetched from the Action outputs:

| Name | Description | Example |
| ------------ | --------------------- | ------------------------------- |
| `rustc` | Rustc version | `1.40.0 (73528e339 2019-12-16)` |
| `rustc-hash` | Rustc version hash | `73528e339` |
| `cargo` | Cargo version | `1.40.0 (bc8e4c8be 2019-11-22)` |
| `rustup` | rustup version | `1.21.1 (7832b2ebe 2019-12-20)` |

Note: `rustc-hash` output value can be used with [actions/cache](https://github.com/actions/cache) Action
to store cache for different Rust versions, as it is unique across different Rust versions and builds (including `nightly`).

## Profiles

This Action supports rustup [profiles](https://blog.rust-lang.org/2019/10/15/Rustup-1.20.0.html#profiles),
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as io from '@actions/io';
import path from "path";

import * as args from './args';
import * as versions from './versions';
import {RustUp, ToolchainOptions} from '@actions-rs/core';

async function run() {
Expand Down Expand Up @@ -52,6 +53,8 @@ async function run() {
if (opts.target) {
await rustup.addTarget(opts.target, opts.name);
}

await versions.gatherInstalledVersions();
}

async function main() {
Expand Down
77 changes: 77 additions & 0 deletions src/versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as exec from '@actions/exec';
import * as core from '@actions/core';

export async function gatherInstalledVersions(): Promise<void> {
try {
core.startGroup('Gathering installed versions');

await rustc();
await cargo();
await rustup();
} finally {
core.endGroup();
}
}

/**
* Fetch currently used `rustc` version
*/
async function rustc(): Promise<void> {
const stdout = await getStdout('rustc', ['-V']);
const version = parse(stdout);

core.setOutput('rustc', version.long);
core.setOutput('rustc-hash', version.hash);
}

/**
* Fetch currently used `cargo` version
*/
async function cargo(): Promise<void> {
const stdout = await getStdout('cargo', ['-V']);
const version = parse(stdout);

core.setOutput('cargo', version.long);
// core.setOutput('cargo_short', version.short);
}

async function rustup(): Promise<void> {
const stdout = await getStdout('rustup', ['-V']);
const version = parse(stdout);

core.setOutput('rustup', version.long);
// core.setOutput('rustup_short', version.short);
}

interface Version {
long: string,
hash: string,
}

function parse(stdout: string): Version {
stdout = stdout.trim();
const matches = stdout.match(/\S+\s((\S+)\s\((\S+)\s(\S+)\))/m);
if (matches == null) {
throw new Error(`Unable to parse version from the "${stdout}" string`);
}

return {
long: matches[1],
hash: matches[3],
}
}

async function getStdout(exe: string, args: string[], options?: {}): Promise<string> {
let stdout = '';
const resOptions = Object.assign({}, options, {
listeners: {
stdout: (buffer: Buffer) => {
stdout += buffer.toString();
},
},
});

await exec.exec(exe, args, resOptions);

return stdout;
}

0 comments on commit 5d50a12

Please sign in to comment.