2024-09-05 08:06:45 -04:00
import * as cache from "@actions/cache" ;
import * as core from "@actions/core" ;
import {
cacheDependencyGlob ,
cacheLocalPath ,
cacheSuffix ,
} from "../utils/inputs" ;
import { getArch , getPlatform } from "../utils/platforms" ;
2024-11-23 09:21:51 +01:00
import { hashFiles } from "../hash/hash-files" ;
2024-08-23 23:58:26 +02:00
2024-09-05 08:06:45 -04:00
export const STATE_CACHE_KEY = "cache-key" ;
export const STATE_CACHE_MATCHED_KEY = "cache-matched-key" ;
const CACHE_VERSION = "1" ;
2024-08-23 23:58:26 +02:00
export async function restoreCache ( version : string ) : Promise < void > {
2024-09-05 08:06:45 -04:00
const cacheKey = await computeKeys ( version ) ;
2024-08-23 23:58:26 +02:00
2024-09-05 08:06:45 -04:00
let matchedKey : string | undefined ;
2024-08-23 23:58:26 +02:00
core . info (
2024-09-05 08:06:45 -04:00
` Trying to restore uv cache from GitHub Actions cache with key: ${ cacheKey } ` ,
) ;
2024-08-23 23:58:26 +02:00
try {
2024-09-05 08:06:45 -04:00
matchedKey = await cache . restoreCache ( [ cacheLocalPath ] , cacheKey ) ;
2024-08-23 23:58:26 +02:00
} catch ( err ) {
2024-09-05 08:06:45 -04:00
const message = ( err as Error ) . message ;
core . warning ( message ) ;
core . setOutput ( "cache-hit" , false ) ;
return ;
2024-08-23 23:58:26 +02:00
}
2024-09-05 08:06:45 -04:00
core . saveState ( STATE_CACHE_KEY , cacheKey ) ;
2024-08-23 23:58:26 +02:00
2024-09-05 08:06:45 -04:00
handleMatchResult ( matchedKey , cacheKey ) ;
2024-08-23 23:58:26 +02:00
}
async function computeKeys ( version : string ) : Promise < string > {
2024-09-05 08:06:45 -04:00
let cacheDependencyPathHash = "-" ;
if ( cacheDependencyGlob !== "" ) {
2024-09-06 14:44:31 +02:00
core . info (
` Searching files using cache dependency glob: ${ cacheDependencyGlob . split ( "\n" ) . join ( "," ) } ` ,
) ;
2024-11-23 09:21:51 +01:00
cacheDependencyPathHash += await hashFiles ( cacheDependencyGlob , true ) ;
2024-09-05 08:06:45 -04:00
if ( cacheDependencyPathHash === "-" ) {
2024-08-23 23:58:26 +02:00
throw new Error (
2024-12-08 11:09:18 +01:00
` No file matched to [ ${ cacheDependencyGlob . split ( "\n" ) . join ( "," ) } ], make sure you have checked out the target repository and configured the cache-dependency-glob input correctly. ` ,
2024-09-05 08:06:45 -04:00
) ;
2024-08-23 23:58:26 +02:00
}
2024-08-24 09:23:57 +02:00
} else {
2024-09-05 08:06:45 -04:00
cacheDependencyPathHash += "no-dependency-glob" ;
2024-08-23 23:58:26 +02:00
}
2024-09-05 08:06:45 -04:00
const suffix = cacheSuffix ? ` - ${ cacheSuffix } ` : "" ;
return ` setup-uv- ${ CACHE_VERSION } - ${ getArch ( ) } - ${ getPlatform ( ) } - ${ version } ${ cacheDependencyPathHash } ${ suffix } ` ;
2024-08-23 23:58:26 +02:00
}
function handleMatchResult (
matchedKey : string | undefined ,
2024-09-05 08:06:45 -04:00
primaryKey : string ,
2024-08-23 23:58:26 +02:00
) : void {
if ( ! matchedKey ) {
2024-09-05 08:06:45 -04:00
core . info ( ` No GitHub Actions cache found for key: ${ primaryKey } ` ) ;
core . setOutput ( "cache-hit" , false ) ;
return ;
2024-08-23 23:58:26 +02:00
}
2024-09-05 08:06:45 -04:00
core . saveState ( STATE_CACHE_MATCHED_KEY , matchedKey ) ;
2024-08-23 23:58:26 +02:00
core . info (
2024-09-05 08:06:45 -04:00
` uv cache restored from GitHub Actions cache with key: ${ matchedKey } ` ,
) ;
core . setOutput ( "cache-hit" , true ) ;
2024-08-23 23:58:26 +02:00
}