refactor: accept changes on cache-utils

This commit is contained in:
Nogic 2023-04-27 00:36:46 +00:00
parent cffdfc80c6
commit d958ca3660
4 changed files with 84 additions and 57 deletions

View File

@ -26,6 +26,35 @@ plugins-cache: /home/codespace/.local/share/NuGet/plugins-cache
], ],
[ [
` `
http-cache: /home/codespace/.local/share/NuGet/v3-cache
global-packages: /var/nuget
temp: /tmp/NuGetScratch
plugins-cache: /home/codespace/.local/share/NuGet/plugins-cache
`,
{
'http-cache': '/home/codespace/.local/share/NuGet/v3-cache',
'global-packages': '/var/nuget',
temp: '/tmp/NuGetScratch',
'plugins-cache': '/home/codespace/.local/share/NuGet/plugins-cache'
}
],
[
`
http-cache: C:\\Users\\user\\AppData\\Local\\NuGet\\v3-cache
global-packages: C:\\Users\\user\\.nuget\\packages\\
temp: C:\\Users\\user\\AppData\\Local\\Temp\\NuGetScratch
plugins-cache: C:\\Users\\user\\AppData\\Local\\NuGet\\plugins-cache
`,
{
'http-cache': 'C:\\Users\\user\\AppData\\Local\\NuGet\\v3-cache',
'global-packages': 'C:\\Users\\user\\.nuget\\packages\\',
temp: 'C:\\Users\\user\\AppData\\Local\\Temp\\NuGetScratch',
'plugins-cache':
'C:\\Users\\user\\AppData\\Local\\NuGet\\plugins-cache'
}
],
[
`
http-cache: C:\\Users\\user\\AppData\\Local\\NuGet\\v3-cache http-cache: C:\\Users\\user\\AppData\\Local\\NuGet\\v3-cache
global-packages: C:\\Users\\user\\.nuget\\packages\\ global-packages: C:\\Users\\user\\.nuget\\packages\\
temp: C:\\Users\\user\\AppData\\Local\\Temp\\NuGetScratch temp: C:\\Users\\user\\AppData\\Local\\Temp\\NuGetScratch

View File

@ -58608,16 +58608,9 @@ const cache = __importStar(__nccwpck_require__(7799));
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514)); const exec = __importStar(__nccwpck_require__(1514));
const constants_1 = __nccwpck_require__(9042); const constants_1 = __nccwpck_require__(9042);
const folders = new Set([
'http-cache',
'global-packages',
'temp',
'plugins-cache'
]);
/** /**
* Get NuGet global packages, cache, and temp folders from .NET CLI. * Get NuGet global packages, cache, and temp folders from .NET CLI.
* @returns (Folder Name)-(Path) mappings * @returns (Folder Name)-(Path) mappings
* @summary This function only works in .NET Core SDK 3.1 and above.
* @see https://docs.microsoft.com/nuget/consume-packages/managing-the-global-packages-and-cache-folders * @see https://docs.microsoft.com/nuget/consume-packages/managing-the-global-packages-and-cache-folders
* @example * @example
* Windows * Windows
@ -58653,12 +58646,12 @@ const getNuGetFolderPath = () => __awaiter(void 0, void 0, void 0, function* ()
temp: '', temp: '',
'plugins-cache': '' 'plugins-cache': ''
}; };
const regex = /^([a-z-]+): (.+[/\\].+)$/gm; const regex = /(?:^|\s)(?<key>[a-z-]+): (?<path>.+[/\\].+)$/gm;
let m; let match;
while ((m = regex.exec(stdout)) !== null) { while ((match = regex.exec(stdout)) !== null) {
const [, key, path] = m; const key = match.groups.key;
if (folders.has(key)) { if (key in result) {
result[key] = path; result[key] = match.groups.path;
} }
} }
return result; return result;
@ -58669,16 +58662,21 @@ function isCacheFeatureAvailable() {
return true; return true;
} }
if (isGhes()) { if (isGhes()) {
throw new Error('Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'); core.warning('Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.');
return false;
} }
core.warning('The runner was not able to contact the cache service. Caching will be skipped'); core.warning('The runner was not able to contact the cache service. Caching will be skipped');
return false; return false;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
/**
* Returns this action runs on GitHub Enterprise Server or not.
* (port from https://github.com/actions/toolkit/blob/457303960f03375db6f033e214b9f90d79c3fe5c/packages/cache/src/internal/cacheUtils.ts#L134)
*/
function isGhes() { function isGhes() {
const url = process.env['GITHUB_SERVER_URL'] || 'https://github.com'; const url = process.env['GITHUB_SERVER_URL'] || 'https://github.com';
return new URL(url).hostname.toUpperCase() !== 'GITHUB.COM'; return new URL(url).hostname.toUpperCase() !== 'GITHUB.COM';
} }
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
/***/ }), /***/ }),

30
dist/setup/index.js vendored
View File

@ -71019,16 +71019,9 @@ const cache = __importStar(__nccwpck_require__(7799));
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514)); const exec = __importStar(__nccwpck_require__(1514));
const constants_1 = __nccwpck_require__(9042); const constants_1 = __nccwpck_require__(9042);
const folders = new Set([
'http-cache',
'global-packages',
'temp',
'plugins-cache'
]);
/** /**
* Get NuGet global packages, cache, and temp folders from .NET CLI. * Get NuGet global packages, cache, and temp folders from .NET CLI.
* @returns (Folder Name)-(Path) mappings * @returns (Folder Name)-(Path) mappings
* @summary This function only works in .NET Core SDK 3.1 and above.
* @see https://docs.microsoft.com/nuget/consume-packages/managing-the-global-packages-and-cache-folders * @see https://docs.microsoft.com/nuget/consume-packages/managing-the-global-packages-and-cache-folders
* @example * @example
* Windows * Windows
@ -71064,12 +71057,12 @@ const getNuGetFolderPath = () => __awaiter(void 0, void 0, void 0, function* ()
temp: '', temp: '',
'plugins-cache': '' 'plugins-cache': ''
}; };
const regex = /^([a-z-]+): (.+[/\\].+)$/gm; const regex = /(?:^|\s)(?<key>[a-z-]+): (?<path>.+[/\\].+)$/gm;
let m; let match;
while ((m = regex.exec(stdout)) !== null) { while ((match = regex.exec(stdout)) !== null) {
const [, key, path] = m; const key = match.groups.key;
if (folders.has(key)) { if (key in result) {
result[key] = path; result[key] = match.groups.path;
} }
} }
return result; return result;
@ -71080,16 +71073,21 @@ function isCacheFeatureAvailable() {
return true; return true;
} }
if (isGhes()) { if (isGhes()) {
throw new Error('Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'); core.warning('Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.');
return false;
} }
core.warning('The runner was not able to contact the cache service. Caching will be skipped'); core.warning('The runner was not able to contact the cache service. Caching will be skipped');
return false; return false;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
/**
* Returns this action runs on GitHub Enterprise Server or not.
* (port from https://github.com/actions/toolkit/blob/457303960f03375db6f033e214b9f90d79c3fe5c/packages/cache/src/internal/cacheUtils.ts#L134)
*/
function isGhes() { function isGhes() {
const url = process.env['GITHUB_SERVER_URL'] || 'https://github.com'; const url = process.env['GITHUB_SERVER_URL'] || 'https://github.com';
return new URL(url).hostname.toUpperCase() !== 'GITHUB.COM'; return new URL(url).hostname.toUpperCase() !== 'GITHUB.COM';
} }
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
/***/ }), /***/ }),

View File

@ -4,18 +4,15 @@ import * as exec from '@actions/exec';
import {cliCommand} from './constants'; import {cliCommand} from './constants';
const folders = new Set([ type NuGetFolderName =
'http-cache', | 'http-cache'
'global-packages', | 'global-packages'
'temp', | 'temp'
'plugins-cache' | 'plugins-cache';
] as const);
type NuGetFolder = Parameters<typeof folders.has>[0];
/** /**
* Get NuGet global packages, cache, and temp folders from .NET CLI. * Get NuGet global packages, cache, and temp folders from .NET CLI.
* @returns (Folder Name)-(Path) mappings * @returns (Folder Name)-(Path) mappings
* @summary This function only works in .NET Core SDK 3.1 and above.
* @see https://docs.microsoft.com/nuget/consume-packages/managing-the-global-packages-and-cache-folders * @see https://docs.microsoft.com/nuget/consume-packages/managing-the-global-packages-and-cache-folders
* @example * @example
* Windows * Windows
@ -53,20 +50,20 @@ export const getNuGetFolderPath = async () => {
); );
} }
const result: Record<NuGetFolder, string> = { const result: Record<NuGetFolderName, string> = {
'http-cache': '', 'http-cache': '',
'global-packages': '', 'global-packages': '',
temp: '', temp: '',
'plugins-cache': '' 'plugins-cache': ''
}; };
const regex = /^([a-z-]+): (.+[/\\].+)$/gm; const regex = /(?:^|\s)(?<key>[a-z-]+): (?<path>.+[/\\].+)$/gm;
let m: RegExpExecArray | null; let match: RegExpExecArray | null;
while ((m = regex.exec(stdout)) !== null) { while ((match = regex.exec(stdout)) !== null) {
const [, key, path] = m; const key = match.groups!.key;
if (folders.has(key as NuGetFolder)) { if ((key as NuGetFolderName) in result) {
result[key] = path; result[key] = match.groups!.path;
} }
} }
@ -79,18 +76,23 @@ export function isCacheFeatureAvailable(): boolean {
} }
if (isGhes()) { if (isGhes()) {
throw new Error( core.warning(
'Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.' 'Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'
); );
return false;
} }
core.warning( core.warning(
'The runner was not able to contact the cache service. Caching will be skipped' 'The runner was not able to contact the cache service. Caching will be skipped'
); );
return false; return false;
}
/**
* Returns this action runs on GitHub Enterprise Server or not.
* (port from https://github.com/actions/toolkit/blob/457303960f03375db6f033e214b9f90d79c3fe5c/packages/cache/src/internal/cacheUtils.ts#L134)
*/
function isGhes(): boolean { function isGhes(): boolean {
const url = process.env['GITHUB_SERVER_URL'] || 'https://github.com'; const url = process.env['GITHUB_SERVER_URL'] || 'https://github.com';
return new URL(url).hostname.toUpperCase() !== 'GITHUB.COM'; return new URL(url).hostname.toUpperCase() !== 'GITHUB.COM';
} }
}