1- import { dirname } from "node:path" ;
2- import { chdir , exit } from "node:process" ;
1+ import { dirname , join } from "node:path" ;
2+ import { chdir , env , exit } from "node:process" ;
33import { isDeepStrictEqual } from "node:util" ;
44import { access } from "node:fs/promises" ;
5+ import { homedir } from "node:os" ;
56
67import { DownloadProvider } from "@/index.js" ;
78import logging from "@/logging/index.js" ;
@@ -11,7 +12,7 @@ import { cacheKey, PESDE_PACKAGE_DIRS } from "./cache.js";
1112import { cacheDir , find } from "@actions/tool-cache" ;
1213import * as core from "@actions/core" ;
1314import * as cache from "@actions/cache" ;
14- import { ensureExists } from "@/util.js " ;
15+ import { mv , rmRF } from "@actions/io " ;
1516
1617export type Tool = "pesde" | "lune" ;
1718export type Repo = { owner : string ; repo : string } ;
@@ -23,23 +24,21 @@ const tools: Record<Tool, Repo> = {
2324const parentLogger = logging . child ( { scope : "actions" } ) ;
2425parentLogger . exitOnError = true ;
2526
26- const PESDE_HOME = expandRelativeToWorkspace ( core . getInput ( "home" ) || process . env . PESDE_HOME || "~/.pesde" ) ;
27-
28- await ensureExists ( PESDE_HOME ) ;
27+ const PESDE_HOME = expandRelativeToWorkspace ( core . getInput ( "home" ) || env . PESDE_HOME || "~/.pesde" ) ;
2928core . exportVariable ( "PESDE_HOME" , PESDE_HOME ) ;
3029
3130parentLogger . info ( `Discovered pesde home directory: ${ PESDE_HOME } ` ) ;
3231
3332function expandRelativeToWorkspace ( path : string ) {
34- const workspaceRoot = dirname ( process . env . GITHUB_WORKSPACE ! ) ;
33+ const homeDir = homedir ( ) ;
3534
3635 // Expand tilde
3736 if ( path === "~" || path . startsWith ( "~/" ) ) {
38- path = path . replace ( / ^ ~ (? = $ | \/ | \\ ) / , workspaceRoot ) ;
37+ path = path . replace ( / ^ ~ (? = $ | \/ | \\ ) / , homeDir ) ;
3938 }
4039
4140 // Expand $HOME and ${HOME}
42- path = path . replace ( / \$ H O M E / g, path ) . replace ( / \$ \{ H O M E \} / g, path ) ;
41+ path = path . replace ( / \$ H O M E / g, homeDir ) . replace ( / \$ \{ H O M E \} / g, homeDir ) ;
4342
4443 return path ;
4544}
@@ -79,14 +78,22 @@ async function setupTool(repo: Repo, version: string) {
7978
8079const cacheLogger = parentLogger . child ( { scope : "actions.cache" } ) ;
8180
81+ // on windows, it is impossible for `saveCache` to cache unless the pesde home dir can
82+ // be represented relatively to the github workspace. so, we create a tempdir which can
83+ // be cached, and transform it during restore. on all other platforms, we just use the
84+ // regular pesde home
85+ const pesdeHome = process . platform === "win32" ? join ( dirname ( env . GITHUB_WORKSPACE ! ) , ".pesde-tmp" ) : PESDE_HOME ;
86+ const cacheDirs = [ ...PESDE_PACKAGE_DIRS , pesdeHome ] ;
87+
8288if ( core . getState ( "post" ) === "true" ) {
8389 // post-run invocation, just cache or exit
8490
8591 if ( core . getState ( "needsCache" ) === "true" ) {
86- const toCache = [ ...PESDE_PACKAGE_DIRS , PESDE_HOME ] ;
92+ await mv ( PESDE_HOME , pesdeHome ) ; // both paths are same everywhere except on windows
93+
8794 const cacheableDirs = await Promise . all (
8895 // filter out dirs which do not exist and cannot be cached
89- toCache . map ( async ( p ) => {
96+ cacheDirs . map ( async ( p ) => {
9097 return await access ( p )
9198 . then ( ( ) => p )
9299 . catch ( ( ) => null ) ;
@@ -97,6 +104,9 @@ if (core.getState("post") === "true") {
97104 const cacheId = await cache . saveCache ( cacheableDirs , await cacheKey ( ) ) ;
98105 core . saveState ( "needsCache" , false ) ; // notify future runs caching isn't required
99106
107+ // remove only for windows, where it is a temp dir
108+ if ( process . platform === "win32" ) await rmRF ( pesdeHome ) ;
109+
100110 cacheLogger . info ( `Successfully cached to ${ cacheId } , exiting` ) ;
101111 }
102112 } else {
@@ -117,13 +127,16 @@ await setupTool(tools.pesde, core.getInput("version") || "latest");
117127core . saveState ( "post" , true ) ;
118128
119129if ( core . getBooleanInput ( "cache" ) ) {
120- await cache . restoreCache ( PESDE_PACKAGE_DIRS , await cacheKey ( ) ) . then ( ( hit ) => {
130+ await cache . restoreCache ( cacheDirs , await cacheKey ( ) ) . then ( async ( hit ) => {
121131 if ( ! hit ) {
122132 cacheLogger . warn ( "Cache miss, dispatching future post-run to save cache" ) ;
123133 core . saveState ( "needsCache" , true ) ; // notify future runs that they need to cache
124134 return ;
125135 }
126136
137+ // move the temporary pesde home onto the expected path (windows only)
138+ await mv ( pesdeHome , PESDE_HOME ) ;
139+
127140 cacheLogger . info ( `Restored cache key ${ hit } successfully` ) ;
128141 } ) ;
129142}
0 commit comments