Skip to content

Commit

Permalink
Export isExplicitVersion and evaluateVersions (#796)
Browse files Browse the repository at this point in the history
* Export isExplicitVersion and evaluateVersions

* Lint

* Add docs
  • Loading branch information
luketomlinson committed May 7, 2021
1 parent 09e59b9 commit 1c367e0
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions packages/tool-cache/src/tool-cache.ts
Expand Up @@ -472,9 +472,9 @@ export function find(
arch = arch || os.arch()

// attempt to resolve an explicit version
if (!_isExplicitVersion(versionSpec)) {
if (!isExplicitVersion(versionSpec)) {
const localVersions: string[] = findAllVersions(toolName, arch)
const match = _evaluateVersions(localVersions, versionSpec)
const match = evaluateVersions(localVersions, versionSpec)
versionSpec = match
}

Expand Down Expand Up @@ -514,7 +514,7 @@ export function findAllVersions(toolName: string, arch?: string): string[] {
if (fs.existsSync(toolPath)) {
const children: string[] = fs.readdirSync(toolPath)
for (const child of children) {
if (_isExplicitVersion(child)) {
if (isExplicitVersion(child)) {
const fullPath = path.join(toolPath, child, arch || '')
if (fs.existsSync(fullPath) && fs.existsSync(`${fullPath}.complete`)) {
versions.push(child)
Expand Down Expand Up @@ -652,7 +652,12 @@ function _completeToolPath(tool: string, version: string, arch?: string): void {
core.debug('finished caching tool')
}

function _isExplicitVersion(versionSpec: string): boolean {
/**
* Check if version string is explicit
*
* @param versionSpec version string to check
*/
export function isExplicitVersion(versionSpec: string): boolean {
const c = semver.clean(versionSpec) || ''
core.debug(`isExplicit: ${c}`)

Expand All @@ -662,7 +667,17 @@ function _isExplicitVersion(versionSpec: string): boolean {
return valid
}

function _evaluateVersions(versions: string[], versionSpec: string): string {
/**
* Get the highest satisfiying semantic version in `versions` which satisfies `versionSpec`
*
* @param versions array of versions to evaluate
* @param versionSpec semantic version spec to satisfy
*/

export function evaluateVersions(
versions: string[],
versionSpec: string
): string {
let version = ''
core.debug(`evaluating ${versions.length} versions`)
versions = versions.sort((a, b) => {
Expand Down

0 comments on commit 1c367e0

Please sign in to comment.