setup-dotnet/src/cache-restore.ts

51 lines
1.6 KiB
TypeScript
Raw Normal View History

feat: Cache NuGet global-packages folder (#303) * feat: cache NuGet global-packages folder * fix: remove unused files * docs: fix incorrect action * ci: add e2e test for cache * docs: accept suggested changes on README * docs: add simple cache example * build: change main script path * fix: change relative path to install scripts * fix: change relative path to problem matcher * refactor: accept changes on cache-utils * fix: revert main script path changes * test: fix cache-utils unit test * test: fix cache-utils unit test * feat: add `cache-dependency-path` variables * build: change main script dist path * ci: add `cache-dependency-path` e2e test & missing lock file * fix: accept change suggestions * ci: copy NuGet lock file to root to pass "test-setup-with-cache" e2e test * docs: change README guide * fix: apply suggestions from code review Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> * test: fix some failed unit tests - fix `restoreCache()` test for 9703c8 - update installer script * build: rebuild dist * Update unit-tests - Additional unit test were added to setup-dotnet.test.ts * Update unit tests for unix systems * Format and lint unit tests * fix: avoid use '/' on `path.join` * fix: rebuild dist * fix: apply suggestions from code review Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> * build: add `DisableImplicitNuGetFallbackFolder` option also add guide on README * docs: highlight warnings and notes * docs: update note about handling NU1403 --------- Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> Co-authored-by: IvanZosimov <ivanzosimov@github.com>
2023-05-29 10:43:18 +00:00
import {readdir} from 'node:fs/promises';
import {join} from 'node:path';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as glob from '@actions/glob';
import {getNuGetFolderPath} from './cache-utils';
import {lockFilePatterns, State, Outputs} from './constants';
export const restoreCache = async (cacheDependencyPath?: string) => {
const lockFilePath = cacheDependencyPath || (await findLockFile());
const fileHash = await glob.hashFiles(lockFilePath);
if (!fileHash) {
throw new Error(
'Some specified paths were not resolved, unable to cache dependencies.'
);
}
const platform = process.env.RUNNER_OS;
const primaryKey = `dotnet-cache-${platform}-${fileHash}`;
core.debug(`primary key is ${primaryKey}`);
core.saveState(State.CachePrimaryKey, primaryKey);
const {'global-packages': cachePath} = await getNuGetFolderPath();
const cacheKey = await cache.restoreCache([cachePath], primaryKey);
core.setOutput(Outputs.CacheHit, Boolean(cacheKey));
if (!cacheKey) {
core.info('Dotnet cache is not found');
return;
}
core.saveState(State.CacheMatchedKey, cacheKey);
core.info(`Cache restored from key: ${cacheKey}`);
};
const findLockFile = async () => {
const workspace = process.env.GITHUB_WORKSPACE!;
const rootContent = await readdir(workspace);
const lockFile = lockFilePatterns.find(item => rootContent.includes(item));
if (!lockFile) {
throw new Error(
`Dependencies lock file is not found in ${workspace}. Supported file patterns: ${lockFilePatterns.toString()}`
);
}
return join(workspace, lockFile);
};