Skip to content

Commit

Permalink
make some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-shibanov committed Feb 18, 2022
1 parent 5b0ae0e commit 4594943
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 60 deletions.
40 changes: 16 additions & 24 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,7 @@ function run() {
let token = core.getInput('token');
let auth = !token || isGhes() ? undefined : `token ${token}`;
const checkLatest = core.getBooleanInput('check-latest');
const installDir = yield installer.getGo(versionSpec, stable, checkLatest, auth);
const installDir = yield installer.getGo(versionSpec, checkLatest, auth);
core.exportVariable('GOROOT', installDir);
core.addPath(path_1.default.join(installDir, 'bin'));
core.info('Added go to the path');
Expand Down Expand Up @@ -5718,7 +5718,7 @@ module.exports = bytesToUuid;

Object.defineProperty(exports, "__esModule", { value: true });
exports.getArch = exports.getPlatform = void 0;
let os = __webpack_require__(87);
const os = __webpack_require__(87);
function getPlatform() {
// darwin and linux match already
// freebsd not supported yet but future proofed.
Expand Down Expand Up @@ -5890,13 +5890,13 @@ const semver = __importStar(__webpack_require__(280));
const httpm = __importStar(__webpack_require__(539));
const sys = __importStar(__webpack_require__(737));
const os_1 = __importDefault(__webpack_require__(87));
function getGo(versionSpec, stable, checkLatest, auth) {
function getGo(versionSpec, checkLatest, auth) {
return __awaiter(this, void 0, void 0, function* () {
let osPlat = os_1.default.platform();
let osArch = os_1.default.arch();
if (checkLatest) {
core.info('Attempting to resolve the latest version from the manifest...');
const resolvedVersion = yield resolveVersionFromManifest(versionSpec, stable, auth);
const resolvedVersion = yield resolveVersionFromManifest(versionSpec, true, auth);
if (resolvedVersion) {
versionSpec = resolvedVersion;
core.info(`Resolved as '${versionSpec}'`);
Expand All @@ -5920,7 +5920,7 @@ function getGo(versionSpec, stable, checkLatest, auth) {
// Try download from internal distribution (popular versions only)
//
try {
info = yield getInfoFromManifest(versionSpec, stable, auth);
info = yield getInfoFromManifest(versionSpec, true, auth);
if (info) {
downloadPath = yield installGoVersion(info, auth);
}
Expand All @@ -5943,7 +5943,7 @@ function getGo(versionSpec, stable, checkLatest, auth) {
// Download from storage.googleapis.com
//
if (!downloadPath) {
info = yield getInfoFromDist(versionSpec, stable);
info = yield getInfoFromDist(versionSpec);
if (!info) {
throw new Error(`Unable to find Go version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`);
}
Expand Down Expand Up @@ -6018,10 +6018,10 @@ function getInfoFromManifest(versionSpec, stable, auth) {
});
}
exports.getInfoFromManifest = getInfoFromManifest;
function getInfoFromDist(versionSpec, stable) {
function getInfoFromDist(versionSpec) {
return __awaiter(this, void 0, void 0, function* () {
let version;
version = yield findMatch(versionSpec, stable);
version = yield findMatch(versionSpec);
if (!version) {
return null;
}
Expand All @@ -6034,7 +6034,7 @@ function getInfoFromDist(versionSpec, stable) {
};
});
}
function findMatch(versionSpec, stable) {
function findMatch(versionSpec) {
return __awaiter(this, void 0, void 0, function* () {
let archFilter = sys.getArch();
let platFilter = sys.getPlatform();
Expand All @@ -6049,15 +6049,8 @@ function findMatch(versionSpec, stable) {
for (let i = 0; i < candidates.length; i++) {
let candidate = candidates[i];
let version = makeSemver(candidate.version);
// 1.13.0 is advertised as 1.13 preventing being able to match exactly 1.13.0
// since a semver of 1.13 would match latest 1.13
let parts = version.split('.');
if (parts.length == 2) {
version = version + '.0';
}
core.debug(`check ${version} satisfies ${versionSpec}`);
if (semver.satisfies(version, versionSpec) &&
(!stable || candidate.stable === stable)) {
if (semver.satisfies(version, versionSpec)) {
goFile = candidate.files.find(file => {
core.debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`);
return file.arch === archFilter && file.os === platFilter;
Expand Down Expand Up @@ -6097,15 +6090,14 @@ exports.getVersionsDist = getVersionsDist;
// 1.8.5beta1 => 1.8.5-beta1, 1.8.5rc1 => 1.8.5-rc1
function makeSemver(version) {
version = version.replace('go', '');
version = version.replace('beta', '-beta').replace('rc', '-rc');
version = version.replace('beta', '-beta.').replace('rc', '-rc.');
let parts = version.split('-');
let verPart = parts[0];
let prereleasePart = parts.length > 1 ? `-${parts[1]}` : '';
let verParts = verPart.split('.');
if (verParts.length == 2) {
verPart += '.0';
let semVersion = semver.coerce(version).version;
if (!parts[1]) {
return semVersion;
}
return `${verPart}${prereleasePart}`;
semVersion = new semver.SemVer(`${semVersion}-${parts[1]}`).version;
return semVersion;
}
exports.makeSemver = makeSemver;
//# sourceMappingURL=installer.js.map
Expand Down
42 changes: 13 additions & 29 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export interface IGoVersionInfo {

export async function getGo(
versionSpec: string,
stable: boolean,
checkLatest: boolean,
auth: string | undefined
) {
Expand All @@ -41,7 +40,7 @@ export async function getGo(
core.info('Attempting to resolve the latest version from the manifest...');
const resolvedVersion = await resolveVersionFromManifest(
versionSpec,
stable,
true,
auth
);
if (resolvedVersion) {
Expand All @@ -68,7 +67,7 @@ export async function getGo(
// Try download from internal distribution (popular versions only)
//
try {
info = await getInfoFromManifest(versionSpec, stable, auth);
info = await getInfoFromManifest(versionSpec, true, auth);
if (info) {
downloadPath = await installGoVersion(info, auth);
} else {
Expand All @@ -95,7 +94,7 @@ export async function getGo(
// Download from storage.googleapis.com
//
if (!downloadPath) {
info = await getInfoFromDist(versionSpec, stable);
info = await getInfoFromDist(versionSpec);
if (!info) {
throw new Error(
`Unable to find Go version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
Expand Down Expand Up @@ -191,11 +190,10 @@ export async function getInfoFromManifest(
}

async function getInfoFromDist(
versionSpec: string,
stable: boolean
versionSpec: string
): Promise<IGoVersionInfo | null> {
let version: IGoVersion | undefined;
version = await findMatch(versionSpec, stable);
version = await findMatch(versionSpec);
if (!version) {
return null;
}
Expand All @@ -211,8 +209,7 @@ async function getInfoFromDist(
}

export async function findMatch(
versionSpec: string,
stable: boolean
versionSpec: string
): Promise<IGoVersion | undefined> {
let archFilter = sys.getArch();
let platFilter = sys.getPlatform();
Expand All @@ -233,18 +230,8 @@ export async function findMatch(
let candidate: IGoVersion = candidates[i];
let version = makeSemver(candidate.version);

// 1.13.0 is advertised as 1.13 preventing being able to match exactly 1.13.0
// since a semver of 1.13 would match latest 1.13
let parts: string[] = version.split('.');
if (parts.length == 2) {
version = version + '.0';
}

core.debug(`check ${version} satisfies ${versionSpec}`);
if (
semver.satisfies(version, versionSpec) &&
(!stable || candidate.stable === stable)
) {
if (semver.satisfies(version, versionSpec)) {
goFile = candidate.files.find(file => {
core.debug(
`${file.arch}===${archFilter} && ${file.os}===${platFilter}`
Expand Down Expand Up @@ -288,16 +275,13 @@ export async function getVersionsDist(
// 1.8.5beta1 => 1.8.5-beta1, 1.8.5rc1 => 1.8.5-rc1
export function makeSemver(version: string): string {
version = version.replace('go', '');
version = version.replace('beta', '-beta').replace('rc', '-rc');
version = version.replace('beta', '-beta.').replace('rc', '-rc.');
let parts = version.split('-');

let verPart: string = parts[0];
let prereleasePart = parts.length > 1 ? `-${parts[1]}` : '';

let verParts: string[] = verPart.split('.');
if (verParts.length == 2) {
verPart += '.0';
let semVersion = semver.coerce(version)!.version;
if (!parts[1]) {
return semVersion;
}

return `${verPart}${prereleasePart}`;
semVersion = new semver.SemVer(`${semVersion}-${parts[1]}`).version;
return semVersion;
}
7 changes: 1 addition & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ export async function run() {
let auth = !token || isGhes() ? undefined : `token ${token}`;

const checkLatest = core.getBooleanInput('check-latest');
const installDir = await installer.getGo(
versionSpec,
stable,
checkLatest,
auth
);
const installDir = await installer.getGo(versionSpec, checkLatest, auth);

core.exportVariable('GOROOT', installDir);
core.addPath(path.join(installDir, 'bin'));
Expand Down
2 changes: 1 addition & 1 deletion src/system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let os = require('os');
const os = require('os');

export function getPlatform(): string {
// darwin and linux match already
Expand Down

0 comments on commit 4594943

Please sign in to comment.