Simplify installer logic

This commit is contained in:
Imran Ismail
2020-09-25 12:53:36 +08:00
parent 399ab9d4d6
commit 51957eba2f
3 changed files with 75 additions and 203 deletions

124
dist/index.js vendored
View File

@@ -9082,16 +9082,16 @@ exports.getKustomize = void 0;
// Load tempDirectory before it gets wiped by tool-cache // Load tempDirectory before it gets wiped by tool-cache
const rest_1 = __webpack_require__(889); const rest_1 = __webpack_require__(889);
const core = __importStar(__webpack_require__(470)); const core = __importStar(__webpack_require__(470));
const tc = __importStar(__webpack_require__(533)); const cache = __importStar(__webpack_require__(533));
const os = __importStar(__webpack_require__(87));
const path = __importStar(__webpack_require__(622)); const path = __importStar(__webpack_require__(622));
const semver = __importStar(__webpack_require__(876)); const semver = __importStar(__webpack_require__(876));
const fs = __importStar(__webpack_require__(747)); const fs = __importStar(__webpack_require__(747));
let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || ''; let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || '';
const osPlat = os.platform();
const osArch = os.arch();
const octokit = new rest_1.Octokit(); const octokit = new rest_1.Octokit();
const versionRegex = /\d+\.?\d*\.?\d*/; const versionRegex = /\d+\.?\d*\.?\d*/;
const toolName = 'kustomize';
const platform = process.platform;
const arch = process.arch === 'x64' ? 'amd64' : process.arch;
if (!tempDirectory) { if (!tempDirectory) {
let baseLocation; let baseLocation;
if (process.platform === 'win32') { if (process.platform === 'win32') {
@@ -9110,55 +9110,20 @@ if (!tempDirectory) {
} }
function getKustomize(versionSpec) { function getKustomize(versionSpec) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
// check cache let toolPath = cache.find('kustomize', versionSpec);
let toolPath;
toolPath = tc.find('kustomize', versionSpec);
// If not found in cache, download
if (!toolPath) { if (!toolPath) {
let version; const version = yield getMaxSatisfyingVersion(versionSpec);
const c = semver.clean(versionSpec) || ''; toolPath = yield acquireVersion(version);
// If explicit version
if (semver.valid(c) != null) {
// version to download
version = versionSpec;
}
else {
// query kustomize for a matching version
const match = yield queryLatestMatch(versionSpec);
if (!match) {
throw new Error(`Unable to find Kustomize version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`);
}
version = match;
}
if (!toolPath) {
// download, extract, cache
toolPath = yield acquireKustomize(version);
}
} }
core.addPath(toolPath); return core.addPath(toolPath);
}); });
} }
exports.getKustomize = getKustomize; exports.getKustomize = getKustomize;
function queryLatestMatch(versionSpec) { function getMaxSatisfyingVersion(versionSpec) {
var e_1, _a; var e_1, _a;
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let dataFileName; const versionRange = semver.validRange(versionSpec);
switch (osPlat) { const downloadUrls = new Map();
case 'linux':
case 'darwin':
case 'win32':
dataFileName = osPlat;
break;
default:
throw new Error(`Unexpected OS '${osPlat}'`);
}
switch (osArch) {
case 'x64':
dataFileName = `${dataFileName}_amd64`;
break;
default:
dataFileName = `${dataFileName}_${osArch}`;
}
const versions = []; const versions = [];
try { try {
for (var _b = __asyncValues(octokit.paginate.iterator(octokit.repos.listReleases, { for (var _b = __asyncValues(octokit.paginate.iterator(octokit.repos.listReleases, {
@@ -9167,10 +9132,13 @@ function queryLatestMatch(versionSpec) {
})), _c; _c = yield _b.next(), !_c.done;) { })), _c; _c = yield _b.next(), !_c.done;) {
const response = _c.value; const response = _c.value;
for (const release of response.data) { for (const release of response.data) {
if (release.assets.some(asset => asset.name.includes(dataFileName) && const matchingAsset = release.assets.find(asset => asset.name.includes('kustomize') &&
asset.name.includes('kustomize'))) { asset.name.includes(platform) &&
asset.name.includes(arch));
if (matchingAsset) {
const version = (versionRegex.exec(release.name) || []).shift(); const version = (versionRegex.exec(release.name) || []).shift();
if (version != null) { if (version != null) {
downloadUrls.set(version, matchingAsset.browser_download_url);
versions.push(version); versions.push(version);
} }
} }
@@ -9184,68 +9152,36 @@ function queryLatestMatch(versionSpec) {
} }
finally { if (e_1) throw e_1.error; } finally { if (e_1) throw e_1.error; }
} }
return semver.maxSatisfying(versions, versionSpec); const versionToDownload = semver.maxSatisfying(versions, versionRange);
if (!versionToDownload) {
throw new Error(`Unable to find Kustomize version '${versionSpec}' for platform '${platform}' and architecture ${arch}.`);
}
const downloadUrl = downloadUrls.get(versionToDownload);
return { name: versionToDownload, url: downloadUrl };
}); });
} }
function acquireKustomize(version) { function acquireVersion(version) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
version = semver.clean(version) || ''; const toolFilename = process.platform === 'win32' ? `${toolName}.exe` : toolName;
let downloadUrl;
let toolPath; let toolPath;
let toolFilename = 'kustomize';
const toolName = 'kustomize';
if (osPlat === 'win32') {
toolFilename = `${toolFilename}.exe`;
}
if (semver.gte(version, '3.3.0')) {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v${version}/kustomize_v${version}_%{os}_%{arch}.tar.gz`;
}
else if (semver.gte(version, '3.2.1')) {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v${version}/kustomize_kustomize.v${version}_%{os}_%{arch}`;
}
else {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/v${version}/kustomize_${version}_%{os}_%{arch}`;
}
switch (osPlat) {
case 'win32':
if (semver.lte(version, '3.2.1'))
throw new Error(`Unexpected OS '${osPlat}'`);
downloadUrl = downloadUrl.replace('%{os}', 'windows');
if (semver.lt(version, '3.3.0'))
downloadUrl = `${downloadUrl}.exe`;
break;
case 'linux':
case 'darwin':
downloadUrl = downloadUrl.replace('%{os}', osPlat);
break;
default:
throw new Error(`Unexpected OS '${osPlat}'`);
}
switch (osArch) {
case 'x64':
downloadUrl = downloadUrl.replace('%{arch}', 'amd64');
break;
default:
throw new Error(`Unexpected Arch '${osArch}'`);
}
try { try {
toolPath = yield tc.downloadTool(downloadUrl); toolPath = yield cache.downloadTool(version.url);
} }
catch (err) { catch (err) {
core.debug(err); core.debug(err);
throw new Error(`Failed to download version ${version}: ${err}`); throw new Error(`Failed to download version ${version.name}: ${err}`);
} }
if (downloadUrl.endsWith('.tar.gz')) { if (version.url.endsWith('.tar.gz')) {
toolPath = yield tc.extractTar(toolPath); toolPath = yield cache.extractTar(toolPath);
toolPath = path.join(toolPath, toolFilename); toolPath = path.join(toolPath, toolFilename);
} }
switch (osPlat) { switch (process.platform) {
case 'linux': case 'linux':
case 'darwin': case 'darwin':
fs.chmodSync(toolPath, 0o755); fs.chmodSync(toolPath, 0o755);
break; break;
} }
return yield tc.cacheFile(toolPath, toolFilename, toolName, version); return yield cache.cacheFile(toolPath, toolFilename, toolName, version.name);
}); });
} }

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@@ -1,17 +1,17 @@
// Load tempDirectory before it gets wiped by tool-cache // Load tempDirectory before it gets wiped by tool-cache
import {Octokit} from '@octokit/rest' import {Octokit} from '@octokit/rest'
import * as core from '@actions/core' import * as core from '@actions/core'
import * as tc from '@actions/tool-cache' import * as cache from '@actions/tool-cache'
import * as os from 'os'
import * as path from 'path' import * as path from 'path'
import * as semver from 'semver' import * as semver from 'semver'
import * as fs from 'fs' import * as fs from 'fs'
let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || '' let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || ''
const osPlat: string = os.platform()
const osArch: string = os.arch()
const octokit = new Octokit() const octokit = new Octokit()
const versionRegex = /\d+\.?\d*\.?\d*/ const versionRegex = /\d+\.?\d*\.?\d*/
const toolName = 'kustomize'
const platform = process.platform
const arch = process.arch === 'x64' ? 'amd64' : process.arch
if (!tempDirectory) { if (!tempDirectory) {
let baseLocation let baseLocation
@@ -29,62 +29,24 @@ if (!tempDirectory) {
} }
export async function getKustomize(versionSpec: string): Promise<void> { export async function getKustomize(versionSpec: string): Promise<void> {
// check cache let toolPath = cache.find('kustomize', versionSpec)
let toolPath: string
toolPath = tc.find('kustomize', versionSpec)
// If not found in cache, download
if (!toolPath) { if (!toolPath) {
let version: string const version = await getMaxSatisfyingVersion(versionSpec)
const c = semver.clean(versionSpec) || '' toolPath = await acquireVersion(version)
// If explicit version
if (semver.valid(c) != null) {
// version to download
version = versionSpec
} else {
// query kustomize for a matching version
const match = await queryLatestMatch(versionSpec)
if (!match) {
throw new Error(
`Unable to find Kustomize version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
)
}
version = match
}
if (!toolPath) {
// download, extract, cache
toolPath = await acquireKustomize(version)
}
} }
core.addPath(toolPath) return core.addPath(toolPath)
} }
async function queryLatestMatch(versionSpec: string): Promise<string | null> { interface Version {
let dataFileName: string name: string
url: string
switch (osPlat) { }
case 'linux':
case 'darwin':
case 'win32':
dataFileName = osPlat
break
default:
throw new Error(`Unexpected OS '${osPlat}'`)
}
switch (osArch) {
case 'x64':
dataFileName = `${dataFileName}_amd64`
break
default:
dataFileName = `${dataFileName}_${osArch}`
}
async function getMaxSatisfyingVersion(versionSpec: string): Promise<Version> {
const versionRange = semver.validRange(versionSpec)
const downloadUrls: Map<string, string> = new Map()
const versions: string[] = [] const versions: string[] = []
for await (const response of octokit.paginate.iterator( for await (const response of octokit.paginate.iterator(
@@ -95,86 +57,60 @@ async function queryLatestMatch(versionSpec: string): Promise<string | null> {
} }
)) { )) {
for (const release of response.data) { for (const release of response.data) {
if ( const matchingAsset = release.assets.find(
release.assets.some( asset =>
asset => asset.name.includes('kustomize') &&
asset.name.includes(dataFileName) && asset.name.includes(platform) &&
asset.name.includes('kustomize') asset.name.includes(arch)
) )
) {
if (matchingAsset) {
const version = (versionRegex.exec(release.name) || []).shift() const version = (versionRegex.exec(release.name) || []).shift()
if (version != null) { if (version != null) {
downloadUrls.set(version, matchingAsset.browser_download_url)
versions.push(version) versions.push(version)
} }
} }
} }
} }
return semver.maxSatisfying(versions, versionSpec) const versionToDownload = semver.maxSatisfying(versions, versionRange)
if (!versionToDownload) {
throw new Error(
`Unable to find Kustomize version '${versionSpec}' for platform '${platform}' and architecture ${arch}.`
)
}
const downloadUrl = downloadUrls.get(versionToDownload) as string
return {name: versionToDownload, url: downloadUrl}
} }
async function acquireKustomize(version: string): Promise<string> { async function acquireVersion(version: Version): Promise<string> {
version = semver.clean(version) || '' const toolFilename =
process.platform === 'win32' ? `${toolName}.exe` : toolName
let downloadUrl: string
let toolPath: string let toolPath: string
let toolFilename = 'kustomize'
const toolName = 'kustomize'
if (osPlat === 'win32') {
toolFilename = `${toolFilename}.exe`
}
if (semver.gte(version, '3.3.0')) {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v${version}/kustomize_v${version}_%{os}_%{arch}.tar.gz`
} else if (semver.gte(version, '3.2.1')) {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v${version}/kustomize_kustomize.v${version}_%{os}_%{arch}`
} else {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/v${version}/kustomize_${version}_%{os}_%{arch}`
}
switch (osPlat) {
case 'win32':
if (semver.lte(version, '3.2.1'))
throw new Error(`Unexpected OS '${osPlat}'`)
downloadUrl = downloadUrl.replace('%{os}', 'windows')
if (semver.lt(version, '3.3.0')) downloadUrl = `${downloadUrl}.exe`
break
case 'linux':
case 'darwin':
downloadUrl = downloadUrl.replace('%{os}', osPlat)
break
default:
throw new Error(`Unexpected OS '${osPlat}'`)
}
switch (osArch) {
case 'x64':
downloadUrl = downloadUrl.replace('%{arch}', 'amd64')
break
default:
throw new Error(`Unexpected Arch '${osArch}'`)
}
try { try {
toolPath = await tc.downloadTool(downloadUrl) toolPath = await cache.downloadTool(version.url)
} catch (err) { } catch (err) {
core.debug(err) core.debug(err)
throw new Error(`Failed to download version ${version}: ${err}`) throw new Error(`Failed to download version ${version.name}: ${err}`)
} }
if (downloadUrl.endsWith('.tar.gz')) { if (version.url.endsWith('.tar.gz')) {
toolPath = await tc.extractTar(toolPath) toolPath = await cache.extractTar(toolPath)
toolPath = path.join(toolPath, toolFilename) toolPath = path.join(toolPath, toolFilename)
} }
switch (osPlat) { switch (process.platform) {
case 'linux': case 'linux':
case 'darwin': case 'darwin':
fs.chmodSync(toolPath, 0o755) fs.chmodSync(toolPath, 0o755)
break break
} }
return await tc.cacheFile(toolPath, toolFilename, toolName, version) return await cache.cacheFile(toolPath, toolFilename, toolName, version.name)
} }