refactor: setup context (#325)

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2022-02-27 17:22:06 +01:00
committed by GitHub
parent a80c8fd82d
commit 39419c3fac
9 changed files with 208 additions and 152 deletions
+23
View File
@@ -0,0 +1,23 @@
import * as os from 'os';
import * as core from '@actions/core';
export const osPlat: string = os.platform();
export const osArch: string = os.arch();
export interface Inputs {
distribution: string;
version: string;
args: string;
workdir: string;
installOnly: boolean;
}
export async function getInputs(): Promise<Inputs> {
return {
distribution: core.getInput('distribution') || 'goreleaser',
version: core.getInput('version'),
args: core.getInput('args'),
workdir: core.getInput('workdir') || '.',
installOnly: core.getBooleanInput('install-only')
};
}
+6 -8
View File
@@ -1,7 +1,7 @@
import * as httpm from '@actions/http-client';
import * as core from '@actions/core';
import * as goreleaser from './goreleaser';
import * as semver from 'semver';
import * as pro from './pro';
import * as core from '@actions/core';
import * as httpm from '@actions/http-client';
export interface GitHubRelease {
id: number;
@@ -22,13 +22,13 @@ const resolveVersion = async (distribution: string, version: string): Promise<st
}
core.debug(`Found ${allTags.length} tags in total`);
if (version === 'latest' || !pro.isPro(distribution)) {
if (version === 'latest' || !goreleaser.isPro(distribution)) {
return semver.maxSatisfying(allTags, version);
}
const cleanTags: Array<string> = allTags.map(tag => cleanTag(tag));
const cleanVersion: string = cleanTag(version);
return semver.maxSatisfying(cleanTags, cleanVersion) + pro.suffix(distribution);
return semver.maxSatisfying(cleanTags, cleanVersion) + goreleaser.distribSuffix(distribution);
};
interface GitHubTag {
@@ -37,15 +37,13 @@ interface GitHubTag {
const getAllTags = async (distribution: string): Promise<Array<string>> => {
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
const suffix: string = pro.suffix(distribution);
const suffix: string = goreleaser.distribSuffix(distribution);
const url: string = `https://goreleaser.com/static/releases${suffix}.json`;
const getTags = http.getJson<Array<GitHubTag>>(url);
return getTags.then(response => {
if (response.result == null) {
return [];
}
return response.result.map(obj => obj.tag_name);
});
};
+18 -14
View File
@@ -1,15 +1,11 @@
import * as os from 'os';
import * as path from 'path';
import * as util from 'util';
import * as context from './context';
import * as github from './github';
import * as pro from './pro';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
const osPlat: string = os.platform();
const osArch: string = os.arch();
export async function getGoReleaser(distribution: string, version: string): Promise<string> {
export async function install(distribution: string, version: string): Promise<string> {
const release: github.GitHubRelease | null = await github.getRelease(distribution, version);
if (!release) {
throw new Error(`Cannot find GoReleaser ${version} release`);
@@ -29,7 +25,7 @@ export async function getGoReleaser(distribution: string, version: string): Prom
core.info('Extracting GoReleaser');
let extPath: string;
if (osPlat == 'win32') {
if (context.osPlat == 'win32') {
extPath = await tc.extractZip(downloadPath);
} else {
extPath = await tc.extractTar(downloadPath);
@@ -39,15 +35,23 @@ export async function getGoReleaser(distribution: string, version: string): Prom
const cachePath: string = await tc.cacheDir(extPath, 'goreleaser-action', release.tag_name.replace(/^v/, ''));
core.debug(`Cached to ${cachePath}`);
const exePath: string = path.join(cachePath, osPlat == 'win32' ? 'goreleaser.exe' : 'goreleaser');
const exePath: string = path.join(cachePath, context.osPlat == 'win32' ? 'goreleaser.exe' : 'goreleaser');
core.debug(`Exe path is ${exePath}`);
return exePath;
}
export const distribSuffix = (distribution: string): string => {
return isPro(distribution) ? '-pro' : '';
};
export const isPro = (distribution: string): boolean => {
return distribution === 'goreleaser-pro';
};
const getFilename = (distribution: string): string => {
let arch: string;
switch (osArch) {
switch (context.osArch) {
case 'x64': {
arch = 'x86_64';
break;
@@ -62,15 +66,15 @@ const getFilename = (distribution: string): string => {
break;
}
default: {
arch = osArch;
arch = context.osArch;
break;
}
}
if (osPlat == 'darwin') {
if (context.osPlat == 'darwin') {
arch = 'all';
}
const platform: string = osPlat == 'win32' ? 'Windows' : osPlat == 'darwin' ? 'Darwin' : 'Linux';
const ext: string = osPlat == 'win32' ? 'zip' : 'tar.gz';
const suffix: string = pro.suffix(distribution);
const platform: string = context.osPlat == 'win32' ? 'Windows' : context.osPlat == 'darwin' ? 'Darwin' : 'Linux';
const ext: string = context.osPlat == 'win32' ? 'zip' : 'tar.gz';
const suffix: string = distribSuffix(distribution);
return util.format('goreleaser%s_%s_%s.%s', suffix, platform, arch, ext);
};
+21 -21
View File
@@ -1,32 +1,29 @@
import * as path from 'path';
import * as context from './context';
import * as git from './git';
import * as installer from './installer';
import * as goreleaser from './goreleaser';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import {dirname} from 'path';
async function run(): Promise<void> {
try {
const distribution = core.getInput('distribution') || 'goreleaser';
const version = core.getInput('version') || 'latest';
const args = core.getInput('args');
const workdir = core.getInput('workdir') || '.';
const isInstallOnly = /^true$/i.test(core.getInput('install-only'));
const goreleaser = await installer.getGoReleaser(distribution, version);
core.info(`GoReleaser ${version} installed successfully`);
const inputs: context.Inputs = await context.getInputs();
const bin = await goreleaser.install(inputs.distribution, inputs.version);
core.info(`GoReleaser ${inputs.version} installed successfully`);
if (isInstallOnly) {
const goreleaserDir = dirname(goreleaser);
if (inputs.installOnly) {
const goreleaserDir = path.dirname(bin);
core.addPath(goreleaserDir);
core.debug(`Added ${goreleaserDir} to PATH`);
return;
} else if (!args) {
} else if (!inputs.args) {
core.setFailed('args input required');
return;
}
if (workdir && workdir !== '.') {
core.info(`Using ${workdir} as working directory`);
process.chdir(workdir);
if (inputs.workdir && inputs.workdir !== '.') {
core.info(`Using ${inputs.workdir} as working directory`);
process.chdir(inputs.workdir);
}
const commit = await git.getShortCommit();
@@ -34,9 +31,9 @@ async function run(): Promise<void> {
const isTagDirty = await git.isTagDirty(tag);
let snapshot = '';
if (args.split(' ').indexOf('release') > -1) {
if (inputs.args.split(' ').indexOf('release') > -1) {
if (isTagDirty) {
if (!args.includes('--snapshot') && !args.includes('--nightly')) {
if (!inputs.args.includes('--snapshot') && !inputs.args.includes('--nightly')) {
core.info(`No tag found for commit ${commit}. Snapshot forced`);
snapshot = ' --snapshot';
}
@@ -45,10 +42,13 @@ async function run(): Promise<void> {
}
}
if (!('GORELEASER_CURRENT_TAG' in process.env)) {
process.env.GORELEASER_CURRENT_TAG = tag;
}
await exec.exec(`${goreleaser} ${args}${snapshot}`);
await exec.exec(`${bin} ${inputs.args}${snapshot}`, undefined, {
env: Object.assign({}, process.env, {
GORELEASER_CURRENT_TAG: tag || process.env.GORELEASER_CURRENT_TAG || ''
}) as {
[key: string]: string;
}
});
} catch (error) {
core.setFailed(error.message);
}
-7
View File
@@ -1,7 +0,0 @@
export const suffix = (distribution: string): string => {
return isPro(distribution) ? '-pro' : '';
};
export const isPro = (distribution: string): boolean => {
return distribution === 'goreleaser-pro';
};