Change code to make it more extandable in future

This commit is contained in:
Ivan Zosimov (Akvelon INC) 2022-02-22 14:55:30 +03:00
parent d450effe85
commit f2efd31d13
7 changed files with 75 additions and 44 deletions

View file

@ -15,6 +15,10 @@ inputs:
default: ${{ github.token }} default: ${{ github.token }}
cache: cache:
description: Used to specify whether go-modules caching is needed. Set to true, if you'd like to enable caching. description: Used to specify whether go-modules caching is needed. Set to true, if you'd like to enable caching.
default: false
modules-manager:
description: 'Used to specify non-default package manager'
default: 'default'
cache-dependency-path: cache-dependency-path:
description: 'Used to specify the path to a dependency file: go.sum' description: 'Used to specify the path to a dependency file: go.sum'
runs: runs:

View file

@ -3927,11 +3927,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}); });
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = exports.defaultPackageManager = void 0; exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
const exec = __importStar(__webpack_require__(986)); const exec = __importStar(__webpack_require__(986));
exports.defaultPackageManager = { exports.supportedPackageManagers = {
goSumFilePattern: 'go.sum', default: {
getCacheFolderCommand: 'go env GOMODCACHE' dependencyFilePattern: 'go.sum',
getCacheFolderCommand: 'go env GOMODCACHE'
}
}; };
exports.getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () { exports.getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true }); let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true });
@ -3943,8 +3945,11 @@ exports.getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, fu
} }
return stdout.trim(); return stdout.trim();
}); });
exports.getPackageManagerInfo = () => __awaiter(void 0, void 0, void 0, function* () { exports.getPackageManagerInfo = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
return exports.defaultPackageManager; if (!exports.supportedPackageManagers.packageManager) {
throw new Error(`It's not possible to use ${packageManager}, please, check correctness of the package manager name spelling.`);
}
return exports.supportedPackageManagers.packageManager;
}); });
exports.getCacheDirectoryPath = (packageManagerInfo) => __awaiter(void 0, void 0, void 0, function* () { exports.getCacheDirectoryPath = (packageManagerInfo) => __awaiter(void 0, void 0, void 0, function* () {
const stdout = yield exports.getCommandOutput(packageManagerInfo.getCacheFolderCommand); const stdout = yield exports.getCommandOutput(packageManagerInfo.getCacheFolderCommand);
@ -46368,12 +46373,13 @@ function run() {
} }
exports.run = run; exports.run = run;
const cachePackages = () => __awaiter(void 0, void 0, void 0, function* () { const cachePackages = () => __awaiter(void 0, void 0, void 0, function* () {
const cachingFlag = core.getInput('cache'); const cachingFlag = core.getBooleanInput('cache');
if (!cachingFlag) if (!cachingFlag)
return; return;
const packageManager = core.getInput('package-manager');
const state = core.getState(constants_1.State.CacheMatchedKey); const state = core.getState(constants_1.State.CacheMatchedKey);
const primaryKey = core.getState(constants_1.State.CachePrimaryKey); const primaryKey = core.getState(constants_1.State.CachePrimaryKey);
const packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(); const packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(packageManager);
const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo); const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo);
if (!fs_1.default.existsSync(cachePath)) { if (!fs_1.default.existsSync(cachePath)) {
throw new Error(`Cache folder path is retrieved but doesn't exist on disk: ${cachePath}`); throw new Error(`Cache folder path is retrieved but doesn't exist on disk: ${cachePath}`);

40
dist/setup/index.js vendored
View file

@ -3705,7 +3705,7 @@ function run() {
// stable will be true unless false is the exact input // stable will be true unless false is the exact input
// since getting unstable versions should be explicit // since getting unstable versions should be explicit
let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE'; let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE';
const cache = core.getInput('cache'); const cache = core.getBooleanInput('cache');
core.info(`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`); core.info(`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`);
if (versionSpec) { if (versionSpec) {
let token = core.getInput('token'); let token = core.getInput('token');
@ -3723,8 +3723,9 @@ function run() {
if (isGhes()) { if (isGhes()) {
throw new Error('Caching is not supported on GHES'); throw new Error('Caching is not supported on GHES');
} }
const packageManager = core.getInput('package-manager');
const cacheDependencyPath = core.getInput('cache-dependency-path'); const cacheDependencyPath = core.getInput('cache-dependency-path');
yield cache_restore_1.restoreCache(cache, cacheDependencyPath); yield cache_restore_1.restoreCache(packageManager, cacheDependencyPath);
} }
// add problem matchers // add problem matchers
const matchersPath = path_1.default.join(__dirname, '../..', 'matchers.json'); const matchersPath = path_1.default.join(__dirname, '../..', 'matchers.json');
@ -4170,11 +4171,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}); });
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = exports.defaultPackageManager = void 0; exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
const exec = __importStar(__webpack_require__(986)); const exec = __importStar(__webpack_require__(986));
exports.defaultPackageManager = { exports.supportedPackageManagers = {
goSumFilePattern: 'go.sum', default: {
getCacheFolderCommand: 'go env GOMODCACHE' dependencyFilePattern: 'go.sum',
getCacheFolderCommand: 'go env GOMODCACHE'
}
}; };
exports.getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () { exports.getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true }); let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true });
@ -4186,8 +4189,11 @@ exports.getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, fu
} }
return stdout.trim(); return stdout.trim();
}); });
exports.getPackageManagerInfo = () => __awaiter(void 0, void 0, void 0, function* () { exports.getPackageManagerInfo = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
return exports.defaultPackageManager; if (!exports.supportedPackageManagers.packageManager) {
throw new Error(`It's not possible to use ${packageManager}, please, check correctness of the package manager name spelling.`);
}
return exports.supportedPackageManagers.packageManager;
}); });
exports.getCacheDirectoryPath = (packageManagerInfo) => __awaiter(void 0, void 0, void 0, function* () { exports.getCacheDirectoryPath = (packageManagerInfo) => __awaiter(void 0, void 0, void 0, function* () {
const stdout = yield exports.getCommandOutput(packageManagerInfo.getCacheFolderCommand); const stdout = yield exports.getCommandOutput(packageManagerInfo.getCacheFolderCommand);
@ -34239,13 +34245,13 @@ const fs_1 = __importDefault(__webpack_require__(747));
const constants_1 = __webpack_require__(196); const constants_1 = __webpack_require__(196);
const cache_utils_1 = __webpack_require__(143); const cache_utils_1 = __webpack_require__(143);
exports.restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () { exports.restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () {
const packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(); const packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(packageManager);
const platform = process.env.RUNNER_OS; const platform = process.env.RUNNER_OS;
const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo); const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo);
const goSumFilePath = cacheDependencyPath const dependencyFilePath = cacheDependencyPath
? cacheDependencyPath ? cacheDependencyPath
: findGoSumFile(packageManagerInfo); : findDependencyFile(packageManagerInfo);
const fileHash = yield glob.hashFiles(goSumFilePath); const fileHash = yield glob.hashFiles(dependencyFilePath);
if (!fileHash) { if (!fileHash) {
throw new Error('Some specified paths were not resolved, unable to cache dependencies.'); throw new Error('Some specified paths were not resolved, unable to cache dependencies.');
} }
@ -34261,15 +34267,15 @@ exports.restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0
core.saveState(constants_1.State.CacheMatchedKey, cacheKey); core.saveState(constants_1.State.CacheMatchedKey, cacheKey);
core.info(`Cache restored from key: ${cacheKey}`); core.info(`Cache restored from key: ${cacheKey}`);
}); });
const findGoSumFile = (packageManager) => { const findDependencyFile = (packageManager) => {
let goSumFile = packageManager.goSumFilePattern; let dependencyFile = packageManager.dependencyFilePattern;
const workspace = process.env.GITHUB_WORKSPACE; const workspace = process.env.GITHUB_WORKSPACE;
const rootContent = fs_1.default.readdirSync(workspace); const rootContent = fs_1.default.readdirSync(workspace);
const goSumFileExists = rootContent.includes(goSumFile); const goSumFileExists = rootContent.includes(dependencyFile);
if (!goSumFileExists) { if (!goSumFileExists) {
throw new Error(`Dependencies file go.sum is not found in ${workspace}. Supported file pattern: ${goSumFile}`); throw new Error(`Dependencies file is not found in ${workspace}. Supported file pattern: ${dependencyFile}`);
} }
return path_1.default.join(workspace, goSumFile); return path_1.default.join(workspace, dependencyFile);
}; };

View file

@ -15,15 +15,15 @@ export const restoreCache = async (
packageManager: string, packageManager: string,
cacheDependencyPath?: string cacheDependencyPath?: string
) => { ) => {
const packageManagerInfo = await getPackageManagerInfo(); const packageManagerInfo = await getPackageManagerInfo(packageManager);
const platform = process.env.RUNNER_OS; const platform = process.env.RUNNER_OS;
const cachePath = await getCacheDirectoryPath(packageManagerInfo); const cachePath = await getCacheDirectoryPath(packageManagerInfo);
const goSumFilePath = cacheDependencyPath const dependencyFilePath = cacheDependencyPath
? cacheDependencyPath ? cacheDependencyPath
: findGoSumFile(packageManagerInfo); : findDependencyFile(packageManagerInfo);
const fileHash = await glob.hashFiles(goSumFilePath); const fileHash = await glob.hashFiles(dependencyFilePath);
if (!fileHash) { if (!fileHash) {
throw new Error( throw new Error(
@ -48,17 +48,17 @@ export const restoreCache = async (
core.info(`Cache restored from key: ${cacheKey}`); core.info(`Cache restored from key: ${cacheKey}`);
}; };
const findGoSumFile = (packageManager: PackageManagerInfo) => { const findDependencyFile = (packageManager: PackageManagerInfo) => {
let goSumFile = packageManager.goSumFilePattern; let dependencyFile = packageManager.dependencyFilePattern;
const workspace = process.env.GITHUB_WORKSPACE!; const workspace = process.env.GITHUB_WORKSPACE!;
const rootContent = fs.readdirSync(workspace); const rootContent = fs.readdirSync(workspace);
const goSumFileExists = rootContent.includes(goSumFile); const goSumFileExists = rootContent.includes(dependencyFile);
if (!goSumFileExists) { if (!goSumFileExists) {
throw new Error( throw new Error(
`Dependencies file go.sum is not found in ${workspace}. Supported file pattern: ${goSumFile}` `Dependencies file is not found in ${workspace}. Supported file pattern: ${dependencyFile}`
); );
} }
return path.join(workspace, goSumFile); return path.join(workspace, dependencyFile);
}; };

View file

@ -21,13 +21,15 @@ export async function run() {
} }
const cachePackages = async () => { const cachePackages = async () => {
const cachingFlag = core.getInput('cache'); const cachingFlag = core.getBooleanInput('cache');
if (!cachingFlag) return; if (!cachingFlag) return;
const packageManager = core.getInput('package-manager');
const state = core.getState(State.CacheMatchedKey); const state = core.getState(State.CacheMatchedKey);
const primaryKey = core.getState(State.CachePrimaryKey); const primaryKey = core.getState(State.CachePrimaryKey);
const packageManagerInfo = await getPackageManagerInfo(); const packageManagerInfo = await getPackageManagerInfo(packageManager);
const cachePath = await getCacheDirectoryPath(packageManagerInfo); const cachePath = await getCacheDirectoryPath(packageManagerInfo);

View file

@ -1,13 +1,19 @@
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
type SupportedPackageManagers = {
[prop: string]: PackageManagerInfo;
};
export interface PackageManagerInfo { export interface PackageManagerInfo {
goSumFilePattern: string; dependencyFilePattern: string;
getCacheFolderCommand: string; getCacheFolderCommand: string;
} }
export const defaultPackageManager: PackageManagerInfo = { export const supportedPackageManagers: SupportedPackageManagers = {
goSumFilePattern: 'go.sum', default: {
getCacheFolderCommand: 'go env GOMODCACHE' dependencyFilePattern: 'go.sum',
getCacheFolderCommand: 'go env GOMODCACHE'
}
}; };
export const getCommandOutput = async (toolCommand: string) => { export const getCommandOutput = async (toolCommand: string) => {
@ -27,8 +33,14 @@ export const getCommandOutput = async (toolCommand: string) => {
return stdout.trim(); return stdout.trim();
}; };
export const getPackageManagerInfo = async () => { export const getPackageManagerInfo = async (packageManager: string) => {
return defaultPackageManager; if (!supportedPackageManagers.packageManager) {
throw new Error(
`It's not possible to use ${packageManager}, please, check correctness of the package manager name spelling.`
);
}
return supportedPackageManagers.packageManager;
}; };
export const getCacheDirectoryPath = async ( export const getCacheDirectoryPath = async (

View file

@ -18,7 +18,7 @@ export async function run() {
// stable will be true unless false is the exact input // stable will be true unless false is the exact input
// since getting unstable versions should be explicit // since getting unstable versions should be explicit
let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE'; let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE';
const cache = core.getInput('cache'); const cache = core.getBooleanInput('cache');
core.info(`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`); core.info(`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`);
@ -47,8 +47,9 @@ export async function run() {
if (isGhes()) { if (isGhes()) {
throw new Error('Caching is not supported on GHES'); throw new Error('Caching is not supported on GHES');
} }
const packageManager = core.getInput('package-manager');
const cacheDependencyPath = core.getInput('cache-dependency-path'); const cacheDependencyPath = core.getInput('cache-dependency-path');
await restoreCache(cache, cacheDependencyPath); await restoreCache(packageManager, cacheDependencyPath);
} }
// add problem matchers // add problem matchers