Skip to content

Commit

Permalink
Release v0.0.8
Browse files Browse the repository at this point in the history
Signed-off-by: Radu M <[email protected]>
  • Loading branch information
Radu M committed Jun 4, 2021
1 parent 94e10ec commit 479197c
Show file tree
Hide file tree
Showing 557 changed files with 101,684 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bower_components
build/Release

# Dependency directories
node_modules/
# node_modules/
jspm_packages/

# TypeScript v1 declaration files
Expand Down Expand Up @@ -90,5 +90,5 @@ typings/
# DynamoDB Local files
.dynamodb/

lib/
test/tmp
# lib/
test/tmp
203 changes: 203 additions & 0 deletions lib/configurator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArchiveType = exports.binPath = exports.getArchiveType = exports.Configurator = exports.getConfig = void 0;
const core = __importStar(require("@actions/core"));
const tc = __importStar(require("@actions/tool-cache"));
const exec = __importStar(require("@actions/exec"));
const io = __importStar(require("@actions/io"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const release_1 = require("./release");
const mustache_1 = __importDefault(require("mustache"));
const uuid_1 = require("uuid");
const NameInput = "name";
const URLInput = "url";
const PathInArchiveInput = "pathInArchive";
const FromGitHubReleases = "fromGitHubReleases";
const Token = "token";
const Repo = "repo";
const Version = "version";
const IncludePrereleases = "includePrereleases";
const URLTemplate = "urlTemplate";
function getConfig() {
return new Configurator(core.getInput(NameInput), core.getInput(URLInput), core.getInput(PathInArchiveInput), core.getInput(FromGitHubReleases), core.getInput(Token), core.getInput(Repo), core.getInput(Version), core.getInput(IncludePrereleases), core.getInput(URLTemplate));
}
exports.getConfig = getConfig;
class Configurator {
constructor(name, url, pathInArchive, fromGitHubRelease, token, repo, version, includePrereleases, urlTemplate) {
this.name = name;
this.url = url;
this.pathInArchive = pathInArchive;
this.fromGitHubReleases = fromGitHubRelease == "true";
this.token = token;
this.repo = repo;
this.version = version;
this.includePrereleases = includePrereleases == "true";
this.urlTemplate = urlTemplate;
}
configure() {
return __awaiter(this, void 0, void 0, function* () {
this.validate();
let downloadURL;
if (this.fromGitHubReleases) {
let tag = yield release_1.getTag(this.token, this.repo, this.version, this.includePrereleases);
const rawVersion = tag.startsWith("v") ? tag.substr(1) : tag;
downloadURL = mustache_1.default.render(this.urlTemplate, {
version: tag,
rawVersion: rawVersion,
});
}
else {
downloadURL = this.url;
}
console.log(`Downloading tool from ${downloadURL}`);
let downloadPath = null;
let archivePath = null;
let randomDir = uuid_1.v4();
const tempDir = path.join(os.tmpdir(), "tmp", "runner", randomDir);
console.log(`Creating tempdir ${tempDir}`);
yield io.mkdirP(tempDir);
downloadPath = yield tc.downloadTool(downloadURL);
switch (getArchiveType(downloadURL)) {
case ArchiveType.None:
yield this.moveToPath(downloadPath);
break;
case ArchiveType.TarGz:
archivePath = yield tc.extractTar(downloadPath, tempDir);
yield this.moveToPath(path.join(archivePath, this.pathInArchive));
break;
case ArchiveType.TarXz:
archivePath = yield tc.extractTar(downloadPath, tempDir, "x");
yield this.moveToPath(path.join(archivePath, this.pathInArchive));
break;
case ArchiveType.Tgz:
archivePath = yield tc.extractTar(downloadPath, tempDir);
yield this.moveToPath(path.join(archivePath, this.pathInArchive));
break;
case ArchiveType.Zip:
archivePath = yield tc.extractZip(downloadPath, tempDir);
yield this.moveToPath(path.join(archivePath, this.pathInArchive));
break;
case ArchiveType.SevenZ:
archivePath = yield tc.extract7z(downloadPath, tempDir);
yield this.moveToPath(path.join(archivePath, this.pathInArchive));
break;
}
// Clean up the tempdir when done (this step is important for self-hosted runners)
return io.rmRF(tempDir);
});
}
moveToPath(downloadPath) {
return __awaiter(this, void 0, void 0, function* () {
let toolPath = binPath();
yield io.mkdirP(toolPath);
yield io.mv(downloadPath, path.join(toolPath, this.name));
if (process.platform !== "win32") {
yield exec.exec("chmod", ["+x", path.join(toolPath, this.name)]);
}
core.addPath(toolPath);
});
}
validate() {
if (!this.name) {
throw new Error(`"name" is required. This is used to set the executable name of the tool.`);
}
if (!this.fromGitHubReleases && !this.url) {
throw new Error(`"url" is required when downloading a tool directly.`);
}
if (!this.fromGitHubReleases && !matchesUrlRegex(this.url)) {
throw new Error(`"url" supplied as input is not a valid URL.`);
}
if (this.fromGitHubReleases && !matchesUrlRegex(this.urlTemplate)) {
throw new Error(`"urlTemplate" supplied as input is not a valid URL.`);
}
if (getArchiveType(this.url) !== ArchiveType.None && !this.pathInArchive) {
throw new Error(`"pathInArchive" is required when "url" points to an archive file`);
}
if (this.fromGitHubReleases &&
getArchiveType(this.urlTemplate) !== ArchiveType.None &&
!this.pathInArchive) {
throw new Error(`"pathInArchive" is required when "urlTemplate" points to an archive file.`);
}
if (this.fromGitHubReleases &&
(!this.token || !this.repo || !this.version || !this.urlTemplate)) {
throw new Error(`if trying to fetch version from GitHub releases, "token", "repo", "version", and "urlTemplate" are required.`);
}
}
}
exports.Configurator = Configurator;
function getArchiveType(downloadURL) {
if (downloadURL.endsWith(ArchiveType.TarGz))
return ArchiveType.TarGz;
if (downloadURL.endsWith(ArchiveType.TarXz))
return ArchiveType.TarXz;
if (downloadURL.endsWith(ArchiveType.Tgz))
return ArchiveType.Tgz;
if (downloadURL.endsWith(ArchiveType.Zip))
return ArchiveType.Zip;
if (downloadURL.endsWith(ArchiveType.SevenZ))
return ArchiveType.SevenZ;
return ArchiveType.None;
}
exports.getArchiveType = getArchiveType;
function binPath() {
let baseLocation;
if (process.platform === "win32") {
// On windows use the USERPROFILE env variable
baseLocation = process.env["USERPROFILE"] || "C:\\";
}
else {
if (process.platform === "darwin") {
baseLocation = "/Users";
}
else {
baseLocation = "/home";
}
}
return path.join(baseLocation, os.userInfo().username, "configurator", "bin");
}
exports.binPath = binPath;
var ArchiveType;
(function (ArchiveType) {
ArchiveType["None"] = "";
ArchiveType["TarGz"] = ".tar.gz";
ArchiveType["TarXz"] = ".tar.xz";
ArchiveType["Tgz"] = ".tgz";
ArchiveType["Zip"] = ".zip";
ArchiveType["SevenZ"] = ".7z";
})(ArchiveType = exports.ArchiveType || (exports.ArchiveType = {}));
function matchesUrlRegex(input) {
var reg = new RegExp("^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$");
return reg.test(input);
}
43 changes: 43 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const cfg = __importStar(require("./configurator"));
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield cfg.getConfig().configure();
}
catch (error) {
core.setFailed(error.message);
}
});
}
run();
94 changes: 94 additions & 0 deletions lib/release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTag = void 0;
const github = __importStar(require("@actions/github"));
const semver = __importStar(require("semver"));
function getTag(token, repo, version, includePrereleases) {
return __awaiter(this, void 0, void 0, function* () {
// the `version` input field can either be a valid semver version, or a
// version range. If none of them are valid, throw an error.
if (semver.valid(version) === null &&
semver.validRange(version) === null &&
version !== "latest") {
throw new Error(`version input: ${version} is not a valid semver version or range.`);
}
let client = github.getOctokit(token);
let owner = repo.split("/")[0];
let repoName = repo.split("/")[1];
// by default, the GitHub releases API returns the first 30 releases.
// while we haven't found a suitable release, increment the page index
// and iterate through the results.
let result;
let pageIndex = 1;
while (result === undefined) {
let releases = yield client.repos.listReleases({
owner: owner,
repo: repoName,
page: pageIndex,
});
// if result is still undefined and there are no more releases it means there are actually
// no releases that satisty the version constrained. If this happens, throw an error.
if (releases.data.length === 0 && result === undefined) {
throw new Error(`cannot find suitable release for version constraint ${version} for repo ${owner}/${repo}`);
}
result = searchSatisfyingRelease(releases.data, version, includePrereleases);
pageIndex++;
}
console.log(`selected release version ${result.tag_name}, which can be viewed at ${result.url}`);
return result.tag_name;
});
}
exports.getTag = getTag;
function searchSatisfyingRelease(releases, version, includePrereleases) {
// normally the release array returned by the API should be ordered by the release date, so
// we iterate through it until we find the latest version that satisfies the version constraint.
// The initial assumption is that, given that users _should_ want to use recently released versions,
// and the number of releases is not (normally) very large, the complexity of this loop should not
// be an issue.
// However, given that this is an ordered array, we could explore a binary search here.
for (let i = 0; i < releases.length; i++) {
if (version === "latest") {
if ((includePrereleases && releases[i].prerelease) ||
!releases[i].prerelease) {
return releases[i];
}
}
if (includePrereleases === false && releases[i].prerelease === true) {
continue;
}
if (semver.satisfies(releases[i].tag_name, version, {
includePrerelease: includePrereleases,
})) {
return releases[i];
}
}
return undefined;
}
1 change: 1 addition & 0 deletions node_modules/.bin/mustache

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/uuid

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions node_modules/@actions/core/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 479197c

Please sign in to comment.