2022-05-25 06:07:29 -04:00
|
|
|
import * as cache from '@actions/cache';
|
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as glob from '@actions/glob';
|
|
|
|
import path from 'path';
|
|
|
|
import fs from 'fs';
|
|
|
|
|
|
|
|
import {State, Outputs} from './constants';
|
|
|
|
import {PackageManagerInfo} from './package-managers';
|
|
|
|
import {getCacheDirectoryPath, getPackageManagerInfo} from './cache-utils';
|
|
|
|
|
|
|
|
export const restoreCache = async (
|
2022-10-17 12:33:22 -04:00
|
|
|
versionSpec: string,
|
2022-05-25 06:07:29 -04:00
|
|
|
packageManager: string,
|
|
|
|
cacheDependencyPath?: string
|
|
|
|
) => {
|
|
|
|
const packageManagerInfo = await getPackageManagerInfo(packageManager);
|
|
|
|
const platform = process.env.RUNNER_OS;
|
|
|
|
|
|
|
|
const cachePaths = await getCacheDirectoryPath(packageManagerInfo);
|
|
|
|
|
|
|
|
const dependencyFilePath = cacheDependencyPath
|
|
|
|
? cacheDependencyPath
|
|
|
|
: findDependencyFile(packageManagerInfo);
|
|
|
|
const fileHash = await glob.hashFiles(dependencyFilePath);
|
|
|
|
|
|
|
|
if (!fileHash) {
|
|
|
|
throw new Error(
|
|
|
|
'Some specified paths were not resolved, unable to cache dependencies.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-07 02:56:00 -04:00
|
|
|
const linuxVersion =
|
|
|
|
process.env.RUNNER_OS === 'Linux' ? `${process.env.ImageOS}-` : '';
|
|
|
|
const primaryKey = `setup-go-${platform}-${linuxVersion}go-${versionSpec}-${fileHash}`;
|
2022-05-25 06:07:29 -04:00
|
|
|
core.debug(`primary key is ${primaryKey}`);
|
|
|
|
|
|
|
|
core.saveState(State.CachePrimaryKey, primaryKey);
|
|
|
|
|
2022-06-28 08:17:12 -04:00
|
|
|
const cacheKey = await cache.restoreCache(cachePaths, primaryKey);
|
|
|
|
core.setOutput(Outputs.CacheHit, Boolean(cacheKey));
|
2022-05-25 06:07:29 -04:00
|
|
|
|
2022-06-28 08:17:12 -04:00
|
|
|
if (!cacheKey) {
|
|
|
|
core.info(`Cache is not found`);
|
|
|
|
core.setOutput(Outputs.CacheHit, false);
|
|
|
|
return;
|
2022-06-14 04:57:37 -04:00
|
|
|
}
|
2022-06-28 08:17:12 -04:00
|
|
|
|
|
|
|
core.saveState(State.CacheMatchedKey, cacheKey);
|
|
|
|
core.info(`Cache restored from key: ${cacheKey}`);
|
2022-05-25 06:07:29 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const findDependencyFile = (packageManager: PackageManagerInfo) => {
|
2023-03-08 03:45:16 -05:00
|
|
|
const dependencyFile = packageManager.dependencyFilePattern;
|
2022-05-25 06:07:29 -04:00
|
|
|
const workspace = process.env.GITHUB_WORKSPACE!;
|
|
|
|
const rootContent = fs.readdirSync(workspace);
|
|
|
|
|
|
|
|
const goSumFileExists = rootContent.includes(dependencyFile);
|
|
|
|
if (!goSumFileExists) {
|
|
|
|
throw new Error(
|
|
|
|
`Dependencies file is not found in ${workspace}. Supported file pattern: ${dependencyFile}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.join(workspace, dependencyFile);
|
|
|
|
};
|