Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dwenegar committed Mar 21, 2021
1 parent 4d2c74b commit 2069dff
Show file tree
Hide file tree
Showing 15 changed files with 616 additions and 494 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"bracketSpacing": false,
"arrowParens": "avoid",
"parser": "typescript"
}
}
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ branding:
icon: moon
color: blue
inputs:
repo-token:
description: 'The GITHUB_TOKEN secret'
required: false
lua-version:
description: 'Lua version to use.'
required: false
Expand Down
263 changes: 96 additions & 167 deletions dist/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ var InputNames;
(function (InputNames) {
InputNames["LuaVersion"] = "lua-version";
InputNames["LuarocksVersion"] = "luarocks-version";
InputNames["RepoToken"] = "repo-token";
})(InputNames = exports.InputNames || (exports.InputNames = {}));
4 changes: 3 additions & 1 deletion lib/input-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const LUAROCKS_VERSION_ALIASES = {
function getInputs() {
const luaVersion = core_1.getInput(constants_1.InputNames.LuaVersion, { required: true });
const luarocksVersion = core_1.getInput(constants_1.InputNames.LuarocksVersion, { required: false });
const repoToken = core_1.getInput(constants_1.InputNames.RepoToken, { required: true });
let resolvedLuaVersion = luaVersion;
if (LUA_VERSION_ALIASES[luaVersion]) {
resolvedLuaVersion = LUA_VERSION_ALIASES[luaVersion];
Expand All @@ -32,7 +33,8 @@ function getInputs() {
}
return {
luaVersion: resolvedLuaVersion,
luarocksVersion: resolvedLuarocksVersion
luarocksVersion: resolvedLuarocksVersion,
repoToken: repoToken
};
}
exports.getInputs = getInputs;
169 changes: 50 additions & 119 deletions lib/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,143 +14,74 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
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);
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.installLuarocks = exports.installLua = void 0;
const tc = __importStar(require("@actions/tool-cache"));
const core = __importStar(require("@actions/core"));
const exec = __importStar(require("@actions/exec"));
const httpm = __importStar(require("@actions/http-client"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const sys = __importStar(require("./system"));
function installLua(version, installPath) {
return __awaiter(this, void 0, void 0, function* () {
const arch = sys.getArch();
const platform = sys.getPlatform();
core.info(`Attempting to download Lua ${version}`);
const info = yield getLuaDownloadInfo(version, platform, arch);
if (!info) {
throw new Error(`Unable to find Lua version '${version}' for platform ${platform} and architecture ${arch}.`);
}
try {
core.info('Installing from archive');
return yield installLuaVersion(info, installPath);
}
catch (err) {
throw new Error(`Failed to download version ${version}: ${err}`);
}
});
async function extractArchive(archivePath, destPath) {
const archiveSuffix = sys.getArchiveSuffix();
if (archiveSuffix === 'zip') {
return await tc.extractZip(archivePath, destPath);
}
return await tc.extractTar(archivePath, destPath);
}
exports.installLua = installLua;
function installLuarocks(version, installPath, luaPath) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Attempting to download Luarocks ${version}`);
const info = yield getLuarocksDownloadInfo(version);
if (!info) {
throw new Error(`Unable to find Luarocks version '${version}'.`);
}
try {
core.info('Installing from archive');
return yield installLuarocksVersion(info, installPath, luaPath);
}
catch (err) {
throw new Error(`Failed to download version ${version}: ${err}`);
}
});
function getLuaDownloadUrl(version) {
const platform = sys.getPlatform();
const arch = sys.getArch();
const archiveSuffix = sys.getArchiveSuffix();
const targetAssetName = `lua-${version}-${platform}-${arch}.${archiveSuffix}`;
return `https://github.com/luadevkit/versions/releases/download/v${version}/${targetAssetName}`;
}
exports.installLuarocks = installLuarocks;
function installLuaVersion(info, installPath) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Downloading Lua ${info.version} from ${info.url}`);
const downloadPath = yield tc.downloadTool(info.url);
const extractionPath = yield extractArchive(downloadPath, path.join(installPath, 'lua'));
core.info(`Extracted Lua to ${extractionPath}`);
return extractionPath;
});
function getLuarocksDownloadUrl(version) {
const archiveSuffix = sys.getArchiveSuffix();
const targetAssetName = `v${version}.${archiveSuffix}`;
return `https://github.com/luarocks/luarocks/archive/${targetAssetName}`;
}
async function installLua(authToken, version, installPath) {
try {
core.info(`Attempting to install Lua ${version}`);
const url = getLuaDownloadUrl(version);
core.info(`Downloading Lua ${version} from ${url}`);
const downloadPath = await tc.downloadTool(url, undefined, authToken);
return await extractArchive(downloadPath, path.join(installPath, 'lua'));
}
catch (err) {
throw new Error(`Failed to install Lua version ${version}: ${err}`);
}
}
function installLuarocksVersion(info, installPath, luaPath) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Downloading Luarocks ${info.version} from ${info.url}`);
const downloadPath = yield tc.downloadTool(info.url);
const extractionPath = yield extractArchive(downloadPath, os.tmpdir());
core.info(`Extracted Luarocks to ${extractionPath}`);
const sourcePath = path.join(extractionPath, `luarocks-${info.version}`);
exports.installLua = installLua;
async function execIn(cmd, cwd) {
await exec.exec(cmd, undefined, { cwd: cwd });
}
async function installLuarocks(authToken, version, installPath, luaPath) {
try {
core.info(`Attempting to install Luarocks ${version}`);
const url = getLuarocksDownloadUrl(version);
core.info(`Downloading Luarocks ${version} from ${url}`);
const downloadPath = await tc.downloadTool(url, undefined, authToken);
const extractionPath = await extractArchive(downloadPath, os.tmpdir());
const sourcePath = path.join(extractionPath, `luarocks-${version}`);
const platform = sys.getPlatform();
if (platform === 'windows') {
yield execIn(`.\\install.bat /P ${installPath} /SELFCONTAINED /LUA ${luaPath} /NOREG /NOADMIN /Q`, sourcePath);
await execIn(`.\\install.bat /P ${installPath} /SELFCONTAINED /LUA ${luaPath} /NOREG /NOADMIN /Q`, sourcePath);
}
else {
yield execIn(`./configure --prefix=${installPath} --with-lua=${luaPath}`, sourcePath);
yield execIn('make -s', sourcePath);
yield execIn('make -s install', sourcePath);
await execIn(`./configure --prefix=${installPath} --with-lua=${luaPath}`, sourcePath);
await execIn('make -s', sourcePath);
await execIn('make -s install', sourcePath);
}
return installPath;
});
}
function extractArchive(archivePath, destPath) {
return __awaiter(this, void 0, void 0, function* () {
const archiveSuffix = sys.getArchiveSuffix();
if (archiveSuffix === 'zip') {
return yield tc.extractZip(archivePath, destPath);
}
return yield tc.extractTar(archivePath, destPath);
});
}
function getGitHubRelease(releaseUrl) {
return __awaiter(this, void 0, void 0, function* () {
const http = new httpm.HttpClient('setup-lua', [], {
allowRedirects: true,
maxRedirects: 3
});
const response = yield http.getJson(releaseUrl);
return response.result;
});
}
function getLuaDownloadInfo(version, platform, arch) {
return __awaiter(this, void 0, void 0, function* () {
const releaseUrl = `https://api.github.com/repos/luadevkit/build/releases/tags/v${version}`;
const githubRelease = yield getGitHubRelease(releaseUrl);
if (!githubRelease) {
return;
}
const archiveSuffix = sys.getArchiveSuffix();
const targetAssetName = `lua-${version}-${platform}-${arch}.${archiveSuffix}`;
for (const asset of githubRelease.assets) {
if (asset.name == targetAssetName)
return {
version: version,
fileName: asset.name,
url: asset.browser_download_url
};
}
});
}
function getLuarocksDownloadInfo(version) {
return __awaiter(this, void 0, void 0, function* () {
const archiveSuffix = sys.getArchiveSuffix();
const targetAssetName = `v${version}.${archiveSuffix}`;
return {
version: version,
fileName: targetAssetName,
url: `https://github.com/luarocks/luarocks/archive/${targetAssetName}`
};
});
}
function execIn(cmd, cwd) {
return __awaiter(this, void 0, void 0, function* () {
yield exec.exec(cmd, undefined, { cwd: cwd });
});
}
catch (err) {
throw new Error(`Failed to install version ${version}: ${err}`);
}
}
exports.installLuarocks = installLuarocks;
85 changes: 40 additions & 45 deletions lib/setup-lua.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,10 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
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);
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.setupLua = void 0;
const core = __importStar(require("@actions/core"));
Expand All @@ -35,42 +26,46 @@ const installer_1 = require("./installer");
const path_1 = require("path");
const child_process_1 = require("child_process");
const sys = __importStar(require("./system"));
function setupLua() {
return __awaiter(this, void 0, void 0, function* () {
try {
const inputs = input_helper_1.getInputs();
const installPrefix = path_1.join(process.cwd(), '.install');
const luaInstallPath = path_1.join(installPrefix);
const luarocksInstallPath = path_1.join(installPrefix, 'luarocks');
const luaVersion = inputs.luaVersion;
core.info(`Setup Lua version ${luaVersion}`);
const luaPath = yield installer_1.installLua(luaVersion, luaInstallPath);
const luaBinPath = path_1.join(luaPath, 'bin');
core.addPath(luaBinPath);
core.info(`Added ${luaBinPath} to the path`);
const luarocksVersion = inputs.luarocksVersion;
if (luarocksVersion) {
core.info(`Setup Luarocks version ${luarocksVersion}`);
const luarocksPath = yield installer_1.installLuarocks(luarocksVersion, luarocksInstallPath, luaPath);
const platform = sys.getPlatform();
const luarocksBinPath = platform === 'windows' ? luarocksPath : path_1.join(luarocksPath, 'bin');
core.addPath(luarocksBinPath);
core.info(`Added ${luarocksBinPath} to the path`);
let luarocksSystemPath = child_process_1.execSync('luarocks path --lr-bin', { encoding: 'utf-8' }).trim();
core.addPath(luarocksSystemPath);
core.info(`Added ${luarocksSystemPath} to the path`);
let luarocksLuaPath = child_process_1.execSync('luarocks path --lr-path', { encoding: 'utf-8' }).trim();
core.exportVariable('LUA_PATH', ';;' + luarocksLuaPath);
core.info(`Set LUA_PATH to ${luarocksLuaPath}`);
let luarocksLuaCPath = child_process_1.execSync('luarocks path --lr-cpath', { encoding: 'utf-8' }).trim();
core.exportVariable('LUA_CPATH', ';;' + luarocksLuaCPath);
core.info(`Set LUA_CPATH to ${luarocksLuaCPath}`);
core.info(`Successfully setup Luarocks ${luarocksVersion}`);
}
function exportVariable(name, value) {
core.exportVariable(name, value);
core.info(`Set ${name} to ${process.env[name]}`);
}
function addPath(value) {
core.addPath(value);
core.info(`Added ${value} to PATH`);
}
async function setupLua() {
try {
const inputs = input_helper_1.getInputs();
const platform = sys.getPlatform();
const installPrefix = path_1.join(process.cwd(), '.install');
const luaInstallPath = path_1.join(installPrefix);
const luarocksInstallPath = path_1.join(installPrefix, 'luarocks');
const luaVersion = inputs.luaVersion;
core.info(`Setup Lua version ${luaVersion}`);
const luaPath = await installer_1.installLua(inputs.repoToken, luaVersion, luaInstallPath);
const luaBinPath = path_1.join(luaPath, 'bin');
addPath(luaBinPath);
if (platform == 'linux' || platform == 'darwin') {
exportVariable('LD_LIBRARY_PATH', luaBinPath);
}
catch (error) {
core.setFailed(error.message);
const luarocksVersion = inputs.luarocksVersion;
if (luarocksVersion) {
core.info(`Setup Luarocks version ${luarocksVersion}`);
const luarocksPath = await installer_1.installLuarocks(inputs.repoToken, luarocksVersion, luarocksInstallPath, luaPath);
const luarocksBinPath = platform === 'windows' ? luarocksPath : path_1.join(luarocksPath, 'bin');
addPath(luarocksBinPath);
let luarocksSystemPath = child_process_1.execSync('luarocks path --lr-bin', { encoding: 'utf-8' }).trim();
addPath(luarocksSystemPath);
let luarocksLuaPath = child_process_1.execSync('luarocks path --lr-path', { encoding: 'utf-8' }).trim();
exportVariable('LUA_PATH', luarocksLuaPath);
let luarocksLuaCPath = child_process_1.execSync('luarocks path --lr-cpath', { encoding: 'utf-8' }).trim();
exportVariable('LUA_CPATH', luarocksLuaCPath);
core.info(`Successfully setup Luarocks ${luarocksVersion}`);
}
});
}
catch (error) {
core.setFailed(error.message);
}
}
exports.setupLua = setupLua;
Loading

0 comments on commit 2069dff

Please sign in to comment.