Permalink
Cannot retrieve contributors at this time
98 lines (86 sloc)
2.67 KB
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
setup-dotnet/src/cache-utils.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as cache from '@actions/cache'; | |
import * as core from '@actions/core'; | |
import * as exec from '@actions/exec'; | |
import {cliCommand} from './constants'; | |
type NuGetFolderName = | |
| 'http-cache' | |
| 'global-packages' | |
| 'temp' | |
| 'plugins-cache'; | |
/** | |
* Get NuGet global packages, cache, and temp folders from .NET CLI. | |
* @returns (Folder Name)-(Path) mappings | |
* @see https://docs.microsoft.com/nuget/consume-packages/managing-the-global-packages-and-cache-folders | |
* @example | |
* Windows | |
* ```json | |
* { | |
* "http-cache": "C:\\Users\\user1\\AppData\\Local\\NuGet\\v3-cache", | |
* "global-packages": "C:\\Users\\user1\\.nuget\\packages\\", | |
* "temp": "C:\\Users\\user1\\AppData\\Local\\Temp\\NuGetScratch", | |
* "plugins-cache": "C:\\Users\\user1\\AppData\\Local\\NuGet\\plugins-cache" | |
* } | |
* ``` | |
* | |
* Mac/Linux | |
* ```json | |
* { | |
* "http-cache": "/home/user1/.local/share/NuGet/v3-cache", | |
* "global-packages": "/home/user1/.nuget/packages/", | |
* "temp": "/tmp/NuGetScratch", | |
* "plugins-cache": "/home/user1/.local/share/NuGet/plugins-cache" | |
* } | |
* ``` | |
*/ | |
export const getNuGetFolderPath = async () => { | |
const {stdout, stderr, exitCode} = await exec.getExecOutput( | |
cliCommand, | |
undefined, | |
{ignoreReturnCode: true, silent: true} | |
); | |
if (exitCode) { | |
throw new Error( | |
!stderr.trim() | |
? `The '${cliCommand}' command failed with exit code: ${exitCode}` | |
: stderr | |
); | |
} | |
const result: Record<NuGetFolderName, string> = { | |
'http-cache': '', | |
'global-packages': '', | |
temp: '', | |
'plugins-cache': '' | |
}; | |
const regex = /(?:^|\s)(?<key>[a-z-]+): (?<path>.+[/\\].+)$/gm; | |
let match: RegExpExecArray | null; | |
while ((match = regex.exec(stdout)) !== null) { | |
const key = match.groups!.key; | |
if ((key as NuGetFolderName) in result) { | |
result[key] = match.groups!.path; | |
} | |
} | |
return result; | |
}; | |
export function isCacheFeatureAvailable(): boolean { | |
if (cache.isFeatureAvailable()) { | |
return true; | |
} | |
if (isGhes()) { | |
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' | |
); | |
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 { | |
const url = process.env['GITHUB_SERVER_URL'] || 'https://github.com'; | |
return new URL(url).hostname.toUpperCase() !== 'GITHUB.COM'; | |
} |