Skip to content

Commit

Permalink
POC
Browse files Browse the repository at this point in the history
  • Loading branch information
dsame committed Apr 24, 2023
1 parent 5b32c90 commit f2c2c87
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 61 deletions.
57 changes: 33 additions & 24 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71138,12 +71138,15 @@ const path_1 = __importDefault(__nccwpck_require__(1017));
const fs_1 = __importDefault(__nccwpck_require__(7147));
const constants_1 = __nccwpck_require__(9042);
const cache_utils_1 = __nccwpck_require__(1678);
const util_1 = __nccwpck_require__(2629);
const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () {
const packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(packageManager);
if (!packageManagerInfo) {
throw new Error(`Caching for '${packageManager}' is not supported`);
}
const platform = process.env.RUNNER_OS;
const stepId = process.env.GITHUB_ACTION;
const nodeVersion = util_1.resolveVersionInput();
const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo, packageManager);
const lockFilePath = cacheDependencyPath
? cacheDependencyPath
Expand All @@ -71152,10 +71155,11 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0,
if (!fileHash) {
throw new Error('Some specified paths were not resolved, unable to cache dependencies.');
}
const primaryKey = `node-cache-${platform}-${packageManager}-${fileHash}`;
const keyPrefix = `node-cache-${platform}-${packageManager}-${stepId}-${nodeVersion}`;
const primaryKey = `${keyPrefix}-${fileHash}`;
core.debug(`primary key is ${primaryKey}`);
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
const cacheKey = yield cache.restoreCache([cachePath], primaryKey);
const cacheKey = yield cache.restoreCache([cachePath], primaryKey, [keyPrefix]);
core.setOutput('cache-hit', Boolean(cacheKey));
if (!cacheKey) {
core.info(`${packageManager} cache is not found`);
Expand Down Expand Up @@ -72040,7 +72044,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.run = void 0;
const core = __importStar(__nccwpck_require__(2186));
const fs_1 = __importDefault(__nccwpck_require__(7147));
const os_1 = __importDefault(__nccwpck_require__(2037));
const auth = __importStar(__nccwpck_require__(7573));
const path = __importStar(__nccwpck_require__(1017));
Expand All @@ -72055,7 +72058,7 @@ function run() {
// Version is optional. If supplied, install / use from the tool cache
// If not supplied then task is still used to setup proxy, auth, etc...
//
const version = resolveVersionInput();
const version = util_1.resolveVersionInput();
let arch = core.getInput('architecture');
const cache = core.getInput('cache');
// if architecture supplied but node-version is not
Expand Down Expand Up @@ -72102,25 +72105,6 @@ function run() {
});
}
exports.run = run;
function resolveVersionInput() {
let version = core.getInput('node-version');
const versionFileInput = core.getInput('node-version-file');
if (version && versionFileInput) {
core.warning('Both node-version and node-version-file inputs are specified, only node-version will be used');
}
if (version) {
return version;
}
if (versionFileInput) {
const versionFilePath = path.join(process.env.GITHUB_WORKSPACE, versionFileInput);
if (!fs_1.default.existsSync(versionFilePath)) {
throw new Error(`The specified node version file at: ${versionFilePath} does not exist`);
}
version = util_1.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8'));
core.info(`Resolved ${versionFileInput} as ${version}`);
}
return version;
}


/***/ }),
Expand Down Expand Up @@ -72158,10 +72142,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
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.printEnvDetailsAndSetOutput = exports.parseNodeVersionFile = void 0;
exports.resolveVersionInput = exports.printEnvDetailsAndSetOutput = exports.parseNodeVersionFile = void 0;
const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514));
const path_1 = __importDefault(__nccwpck_require__(1017));
const fs_1 = __importDefault(__nccwpck_require__(7147));
function parseNodeVersionFile(contents) {
var _a, _b, _c;
let nodeVersion;
Expand Down Expand Up @@ -72221,6 +72210,26 @@ function getToolVersion(tool, options) {
}
});
}
function resolveVersionInput() {
let version = core.getInput('node-version');
const versionFileInput = core.getInput('node-version-file');
if (version && versionFileInput) {
core.warning('Both node-version and node-version-file inputs are specified, only node-version will be used');
}
if (version) {
return version;
}
if (versionFileInput) {
const versionFilePath = path_1.default.join(process.env.GITHUB_WORKSPACE, versionFileInput);
if (!fs_1.default.existsSync(versionFilePath)) {
throw new Error(`The specified node version file at: ${versionFilePath} does not exist`);
}
version = parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8'));
core.info(`Resolved ${versionFileInput} as ${version}`);
}
return version;
}
exports.resolveVersionInput = resolveVersionInput;


/***/ }),
Expand Down
8 changes: 6 additions & 2 deletions src/cache-restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getPackageManagerInfo,
PackageManagerInfo
} from './cache-utils';
import {resolveVersionInput} from "./util";

export const restoreCache = async (
packageManager: string,
Expand All @@ -20,6 +21,8 @@ export const restoreCache = async (
throw new Error(`Caching for '${packageManager}' is not supported`);
}
const platform = process.env.RUNNER_OS;
const stepId = process.env.GITHUB_ACTION
const nodeVersion = resolveVersionInput()

const cachePath = await getCacheDirectoryPath(
packageManagerInfo,
Expand All @@ -36,12 +39,13 @@ export const restoreCache = async (
);
}

const primaryKey = `node-cache-${platform}-${packageManager}-${fileHash}`;
const keyPrefix = `node-cache-${platform}-${packageManager}-${stepId}-${nodeVersion}`;
const primaryKey = `${keyPrefix}-${fileHash}`;
core.debug(`primary key is ${primaryKey}`);

core.saveState(State.CachePrimaryKey, primaryKey);

const cacheKey = await cache.restoreCache([cachePath], primaryKey);
const cacheKey = await cache.restoreCache([cachePath], primaryKey, [keyPrefix]);
core.setOutput('cache-hit', Boolean(cacheKey));

if (!cacheKey) {
Expand Down
36 changes: 1 addition & 35 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as path from 'path';
import {restoreCache} from './cache-restore';
import {isCacheFeatureAvailable} from './cache-utils';
import {getNodejsDistribution} from './distributions/installer-factory';
import {parseNodeVersionFile, printEnvDetailsAndSetOutput} from './util';
import {parseNodeVersionFile, printEnvDetailsAndSetOutput, resolveVersionInput} from './util';

export async function run() {
try {
Expand Down Expand Up @@ -76,37 +76,3 @@ export async function run() {
core.setFailed(err.message);
}
}

function resolveVersionInput(): string {
let version = core.getInput('node-version');
const versionFileInput = core.getInput('node-version-file');

if (version && versionFileInput) {
core.warning(
'Both node-version and node-version-file inputs are specified, only node-version will be used'
);
}

if (version) {
return version;
}

if (versionFileInput) {
const versionFilePath = path.join(
process.env.GITHUB_WORKSPACE!,
versionFileInput
);

if (!fs.existsSync(versionFilePath)) {
throw new Error(
`The specified node version file at: ${versionFilePath} does not exist`
);
}

version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8'));

core.info(`Resolved ${versionFileInput} as ${version}`);
}

return version;
}
35 changes: 35 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import path from "path";
import fs from "fs";

export function parseNodeVersionFile(contents: string): string {
let nodeVersion: string | undefined;
Expand Down Expand Up @@ -61,3 +63,36 @@ async function getToolVersion(tool: string, options: string[]) {
return '';
}
}
export function resolveVersionInput(): string {
let version = core.getInput('node-version');
const versionFileInput = core.getInput('node-version-file');

if (version && versionFileInput) {
core.warning(
'Both node-version and node-version-file inputs are specified, only node-version will be used'
);
}

if (version) {
return version;
}

if (versionFileInput) {
const versionFilePath = path.join(
process.env.GITHUB_WORKSPACE!,
versionFileInput
);

if (!fs.existsSync(versionFilePath)) {
throw new Error(
`The specified node version file at: ${versionFilePath} does not exist`
);
}

version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8'));

core.info(`Resolved ${versionFileInput} as ${version}`);
}

return version;
}

0 comments on commit f2c2c87

Please sign in to comment.