Skip to content

Commit

Permalink
Merge pull request #54 from Cyberbeni/refactor
Browse files Browse the repository at this point in the history
separate github action input and installer flow
  • Loading branch information
Cyberbeni authored Oct 5, 2020
2 parents 9e6152c + 9462fef commit 9d61b57
Show file tree
Hide file tree
Showing 6 changed files with 326 additions and 239 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

174 changes: 174 additions & 0 deletions lib/installer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
"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.prototype.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.SwiftToolInstaller = void 0;
const cache = __importStar(require("@actions/cache"));
const core = __importStar(require("@actions/core"));
const os = __importStar(require("os"));
const semver = __importStar(require("semver"));
const helpers_1 = require("./helpers");
class SwiftToolInstaller {
constructor(url, branch, version, useCache) {
this.uuid = '';
this.cacheKey = '';
this.workingDirectory = '';
this.productDirectory = '';
this.cacheDirectory = '';
this.didRestore = false;
this.url = url;
this.branch = branch;
this.version = version;
this.useCache = useCache;
}
// Steps
resolve_version() {
return __awaiter(this, void 0, void 0, function* () {
yield core.group('Resolving version requirement', () => __awaiter(this, void 0, void 0, function* () {
let versions = (yield helpers_1.exec('git', ['ls-remote', '--refs', '--tags', this.url]))
.split('\n')
.map(function (value) {
var _a;
return (_a = value.split('/').pop()) !== null && _a !== void 0 ? _a : '';
});
let targetVersion = semver.maxSatisfying(versions, this.version);
if (targetVersion) {
core.info(`Resolved version: ${targetVersion}`);
this.branch = targetVersion;
}
else {
throw Error(`No version satisfying '${this.version}' found.`);
}
}));
});
}
updateDirectoryNames(newUuid) {
this.uuid = newUuid;
this.cacheKey = `installswifttool-${this.uuid}`;
this.workingDirectory = `${os.homedir()}/install-swift-tool-${this.uuid}`;
this.productDirectory = `${this.workingDirectory}/.build/release`;
this.cacheDirectory = `${this.workingDirectory}/.build/*/release`;
}
create_working_directory() {
return __awaiter(this, void 0, void 0, function* () {
yield core.group('Creating working directory', () => __awaiter(this, void 0, void 0, function* () {
let commitHash = '';
if (this.branch) {
commitHash = yield helpers_1.exec('git', ['ls-remote', '-ht', this.url, `refs/heads/${this.branch}`, `refs/tags/${this.branch}`]);
}
else {
commitHash = yield helpers_1.exec('git', ['ls-remote', this.url, `HEAD`]);
}
commitHash = commitHash.substring(0, 40);
this.updateDirectoryNames(yield helpers_1.getUuid(this.url, commitHash));
yield helpers_1.exec('mkdir', ['-p', this.workingDirectory]);
}));
});
}
try_to_restore() {
return __awaiter(this, void 0, void 0, function* () {
yield core.group('Trying to restore from cache', () => __awaiter(this, void 0, void 0, function* () {
this.didRestore = (yield cache.restoreCache([this.cacheDirectory, this.productDirectory], this.cacheKey)) !== undefined;
core.setOutput('cache-hit', `${this.didRestore}`);
}));
});
}
clone_git() {
return __awaiter(this, void 0, void 0, function* () {
yield core.group('Cloning repo', () => __awaiter(this, void 0, void 0, function* () {
if (this.branch) {
yield helpers_1.exec('git', ['clone', '--depth', '1', '--branch', this.branch, this.url, this.workingDirectory]);
}
else {
yield helpers_1.exec('git', ['clone', '--depth', '1', this.url, this.workingDirectory]);
}
const commitHash = yield helpers_1.exec('git', ['-C', this.workingDirectory, 'rev-parse', 'HEAD']);
const newUuid = yield helpers_1.getUuid(this.url, commitHash);
if (this.uuid != newUuid) {
const oldWorkingDirectory = this.workingDirectory;
this.updateDirectoryNames(newUuid);
yield helpers_1.exec('mv', [oldWorkingDirectory, this.workingDirectory]);
}
}));
});
}
build_tool() {
return __awaiter(this, void 0, void 0, function* () {
yield core.group('Building tool', () => __awaiter(this, void 0, void 0, function* () {
yield helpers_1.exec('swift', ['build', '--package-path', this.workingDirectory, '--configuration', 'release', '--disable-sandbox']);
}));
});
}
try_to_cache() {
return __awaiter(this, void 0, void 0, function* () {
yield core.group('Trying to save to cache', () => __awaiter(this, void 0, void 0, function* () {
try {
yield cache.saveCache([this.cacheDirectory, this.productDirectory], this.cacheKey);
}
catch (error) {
core.info(error.message);
}
}));
});
}
export_path() {
return __awaiter(this, void 0, void 0, function* () {
yield core.group('Exporting path', () => __awaiter(this, void 0, void 0, function* () {
core.addPath(this.productDirectory);
}));
});
}
// Run
install() {
return __awaiter(this, void 0, void 0, function* () {
if (this.version) {
yield this.resolve_version();
}
yield this.create_working_directory();
if (this.useCache) {
yield this.try_to_restore();
}
if (!this.didRestore) {
yield this.clone_git();
yield this.build_tool();
if (this.useCache) {
yield this.try_to_cache();
}
}
yield this.export_path();
});
}
static install(url, branch, version, useCache) {
return __awaiter(this, void 0, void 0, function* () {
const installer = new SwiftToolInstaller(url, branch, version, useCache);
yield installer.install();
});
}
}
exports.SwiftToolInstaller = SwiftToolInstaller;
127 changes: 4 additions & 123 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,136 +28,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const cache = __importStar(require("@actions/cache"));
const core = __importStar(require("@actions/core"));
const os = __importStar(require("os"));
const semver = __importStar(require("semver"));
const helpers_1 = require("./helpers");
const installer_1 = require("./installer");
// Inputs
const url = core.getInput('url');
let branch = core.getInput('branch');
const branch = core.getInput('branch');
const version = core.getInput('version');
const useCache = core.getInput('use-cache') == 'true';
// Steps
function resolve_version() {
return __awaiter(this, void 0, void 0, function* () {
yield core.group('Resolving version requirement', () => __awaiter(this, void 0, void 0, function* () {
let versions = (yield helpers_1.exec('git', ['ls-remote', '--refs', '--tags', url]))
.split('\n')
.map(function (value) {
var _a;
return (_a = value.split('/').pop()) !== null && _a !== void 0 ? _a : '';
});
let targetVersion = semver.maxSatisfying(versions, version);
if (targetVersion) {
core.info(`Resolved version: ${targetVersion}`);
branch = targetVersion;
}
else {
throw Error(`No version satisfying '${version}' found.`);
}
}));
});
}
let uuid = '';
let workingDirectory = '';
let productDirectory = '';
let cacheDirectory = '';
function updateDirectoryNames(newUuid) {
uuid = newUuid;
workingDirectory = `${os.homedir()}/install-swift-tool-${uuid}`;
productDirectory = `${workingDirectory}/.build/release`;
cacheDirectory = `${workingDirectory}/.build/*/release`;
}
function create_working_directory() {
return __awaiter(this, void 0, void 0, function* () {
yield core.group('Creating working directory', () => __awaiter(this, void 0, void 0, function* () {
let commitHash = '';
if (branch) {
commitHash = yield helpers_1.exec('git', ['ls-remote', '-ht', url, `refs/heads/${branch}`, `refs/tags/${branch}`]);
}
else {
commitHash = yield helpers_1.exec('git', ['ls-remote', url, `HEAD`]);
}
commitHash = commitHash.substring(0, 40);
updateDirectoryNames(yield helpers_1.getUuid(url, commitHash));
yield helpers_1.exec('mkdir', ['-p', workingDirectory]);
}));
});
}
let cacheKey = '';
let didRestore = false;
function try_to_restore() {
return __awaiter(this, void 0, void 0, function* () {
yield core.group('Trying to restore from cache', () => __awaiter(this, void 0, void 0, function* () {
cacheKey = `installswifttool-${uuid}`;
didRestore = (yield cache.restoreCache([cacheDirectory, productDirectory], cacheKey)) !== undefined;
core.setOutput('cache-hit', `${didRestore}`);
}));
});
}
function clone_git() {
return __awaiter(this, void 0, void 0, function* () {
yield core.group('Cloning repo', () => __awaiter(this, void 0, void 0, function* () {
if (branch) {
yield helpers_1.exec('git', ['clone', '--depth', '1', '--branch', branch, url, workingDirectory]);
}
else {
yield helpers_1.exec('git', ['clone', '--depth', '1', url, workingDirectory]);
}
const commitHash = yield helpers_1.exec('git', ['-C', workingDirectory, 'rev-parse', 'HEAD']);
const newUuid = yield helpers_1.getUuid(url, commitHash);
if (uuid != newUuid) {
const oldWorkingDirectory = workingDirectory;
updateDirectoryNames(newUuid);
yield helpers_1.exec('mv', [oldWorkingDirectory, workingDirectory]);
}
}));
});
}
function build_tool() {
return __awaiter(this, void 0, void 0, function* () {
yield core.group('Building tool', () => __awaiter(this, void 0, void 0, function* () {
yield helpers_1.exec('swift', ['build', '--package-path', workingDirectory, '--configuration', 'release', '--disable-sandbox']);
}));
});
}
function try_to_cache() {
return __awaiter(this, void 0, void 0, function* () {
yield core.group('Trying to save to cache', () => __awaiter(this, void 0, void 0, function* () {
try {
yield cache.saveCache([cacheDirectory, productDirectory], cacheKey);
}
catch (error) {
core.info(error.message);
}
}));
});
}
function export_path() {
return __awaiter(this, void 0, void 0, function* () {
yield core.group('Exporting path', () => __awaiter(this, void 0, void 0, function* () {
core.addPath(productDirectory);
}));
});
}
// Run
function main() {
return __awaiter(this, void 0, void 0, function* () {
if (version) {
yield resolve_version();
}
yield create_working_directory();
if (useCache) {
yield try_to_restore();
}
if (!didRestore) {
yield clone_git();
yield build_tool();
if (useCache) {
yield try_to_cache();
}
}
yield export_path();
installer_1.SwiftToolInstaller.install(url, branch, version, useCache);
});
}
main().catch(error => { core.setFailed(error.message); });
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "install-swift-tool",
"version": "2.1.2",
"description": "Github action to install swift based tools",
"main": "lib/main.js",
"version": "2.1.3",
"description": "Install swift based tools inside a GitHub Action.",
"main": "lib/installer.js",
"repository": {
"type": "git",
"url": "github:Cyberbeni/install-swift-tool.git"
Expand Down
Loading

0 comments on commit 9d61b57

Please sign in to comment.