Skip to content

Commit

Permalink
automatically resolve latest version of CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
repolevedavaj committed May 24, 2022
1 parent 6c2a2ed commit 309a76c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This action sets up the runner environment with the Project-Env shell. See [Proj

## `cli-version`

**Required** Version of [Project-Env CLI](https://github.com/Project-Env/project-env-cli) to use.
Version of [Project-Env CLI](https://github.com/Project-Env/project-env-cli) to use. If not specified, the latest version will be used.

## `cli-debug`

Expand Down
2 changes: 0 additions & 2 deletions __tests__/project-env-github-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import * as os from "os";
describe('Project-Env Github Action', () => {

const ENV_BACKUP = process.env;
const CLI_VERSION = '3.4.0';

beforeEach(() => {
process.env = {
...{
'INPUT_CONFIG-FILE': '__tests__/project-env.toml',
'INPUT_CLI-VERSION': CLI_VERSION,
'INPUT_CLI-DEBUG': 'true',
'RUNNER_TEMP': os.tmpdir()
},
Expand Down
32 changes: 23 additions & 9 deletions src/project-env-github-action.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as core from '@actions/core'
import * as httpClient from '@actions/http-client'
import * as toolCache from '@actions/tool-cache';
import * as exec from '@actions/exec';
import * as os from "os";
Expand All @@ -15,13 +16,13 @@ type AllToolInfos = { [key: string]: ToolInfo[] };
export default class ProjectEnvGithubAction {

async run() {
this.fixRunnerEnvironment();
try {
this.fixRunnerEnvironment();

const configFile = core.getInput('config-file') || 'project-env.toml';
const cliVersion = core.getInput('cli-version', {required: true});
const cliDebug = core.getInput('cli-debug') === 'true'
const configFile = core.getInput('config-file') || 'project-env.toml';
const cliVersion = core.getInput('cli-version') || (await this.resolveLatestProjectEnvCliVersion());
const cliDebug = core.getInput('cli-debug') === 'true'

try {
const archiveUrl = await this.resolveProjectEnvCliArchiveUrl(cliVersion);
const archive = await toolCache.downloadTool(archiveUrl);

Expand All @@ -46,6 +47,23 @@ export default class ProjectEnvGithubAction {
}
}

private async resolveLatestProjectEnvCliVersion() {
const response = await new httpClient.HttpClient(undefined, undefined, {allowRedirects: false}).get("https://github.com/Project-Env/project-env-cli/releases/latest")

const statusCode = response.message.statusCode;
const location = response.message.headers.location;
if (statusCode !== 302 || !location) {
throw new Error("failed to resolve latest Project-Env CLI version");
}

const version = location.match(/.+\/v(.+)$/)?.[1]
if (!version) {
throw new Error("failed to resolve latest Project-Env CLI version");
}

return version;
}

private async resolveProjectEnvCliArchiveUrl(cliVersion: string) {
const archiveBaseUrl = this.createProjectEnvCliArchiveBaseUrl(cliVersion);
const archiveFilename = this.createProjectEnvCliArchiveFilename(cliVersion);
Expand Down Expand Up @@ -140,10 +158,6 @@ export default class ProjectEnvGithubAction {
return JSON.parse(stdOutput.stdout);
}

private getExecutableName() {
return `project-env-cli${this.getExecutableExtension()}`;
}

private getExecutableExtension() {
return os.platform() === 'win32' ? '.exe' : '';
}
Expand Down

0 comments on commit 309a76c

Please sign in to comment.