Skip to content

Commit

Permalink
Set RYE_HOME before installing rye
Browse files Browse the repository at this point in the history
  • Loading branch information
eifinger committed Mar 4, 2024
1 parent f8aec3f commit 0a881e9
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 67 deletions.
13 changes: 3 additions & 10 deletions dist/save-cache/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/save-cache/index.js.map

Large diffs are not rendered by default.

51 changes: 27 additions & 24 deletions dist/setup/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/setup/index.js.map

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion src/download/download-version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as core from '@actions/core'
import * as tc from '@actions/tool-cache'
import * as io from '@actions/io'
import {Architecture, OWNER, REPO, extract, validateChecksum} from '../utils'
import {ryeHomePath} from '../restore-cache'

export function tryGetFromCache(
arch: Architecture,
Expand All @@ -9,7 +11,15 @@ export function tryGetFromCache(
core.debug(`Trying to get Rye from cache for ${version}...`)
const cachedVersions = tc.findAllVersions('rye', arch)
core.debug(`Cached versions: ${cachedVersions}`)
return tc.find('rye', version, arch)
const foundPath = tc.find('rye', version, arch)
if (foundPath) {
core.info(`Found Rye in cache for ${version}`)
io.cp(foundPath, ryeHomePath, {
copySourceDirectory: true,
recursive: true
})
return ryeHomePath
}
}

export async function downloadVersion(
Expand Down
7 changes: 2 additions & 5 deletions src/restore-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const workingDirInput = core.getInput('working-directory')
export const workingDir = workingDirInput ? `/${workingDirInput}` : ''
export const venvPath = `${process.env['GITHUB_WORKSPACE']}${workingDir}/.venv`
export const ryeHomePath = resolve(`${process.env['GITHUB_WORKSPACE']}/../.rye`)
const CACHE_VERSION = '3'
const CACHE_VERSION = '4'
const cacheLocalStoragePath =
`${core.getInput('cache-local-storage-path')}` || ''
const cacheDependencyPath = `${process.env['GITHUB_WORKSPACE']}${workingDir}/requirements**.lock`
Expand All @@ -33,7 +33,7 @@ export async function restoreCache(
try {
matchedKey = cacheLocalStoragePath
? await restoreCacheLocal(cacheKey)
: await cache.restoreCache([venvPath, ryeHomePath], cacheKey)
: await cache.restoreCache([venvPath], cacheKey)
} catch (err) {
const message = (err as Error).message
core.warning(message)
Expand Down Expand Up @@ -82,8 +82,5 @@ async function restoreCacheLocal(
await cp(`${storedCache}/.venv`, venvPath, {
recursive: true
})
await cp(`${storedCache}/.rye`, ryeHomePath, {
recursive: true
})
return primaryKey
}
6 changes: 1 addition & 5 deletions src/save-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ async function saveCache(): Promise<void> {
return
}
core.info(`Saving .venv path: ${venvPath}`)
core.info(`Saving .rye path: ${ryeHomePath}`)
cacheLocalStoragePath
? await saveCacheLocal(cacheKey)
: await cache.saveCache([venvPath, ryeHomePath], cacheKey)
: await cache.saveCache([venvPath], cacheKey)

core.info(`Cache saved with the key: ${cacheKey}`)
}
Expand All @@ -52,9 +51,6 @@ async function saveCacheLocal(cacheKey: string): Promise<void> {
await cp(venvPath, `${targetPath}/.venv`, {
recursive: true
})
await cp(ryeHomePath, `${targetPath}/.rye`, {
recursive: true
})
}

run()
35 changes: 15 additions & 20 deletions src/setup-rye.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as core from '@actions/core'
import * as tc from '@actions/tool-cache'
import * as exec from '@actions/exec'
import * as io from '@actions/io'
import * as path from 'path'
import {downloadVersion, tryGetFromCache} from './download/download-version'
import {restoreCache, ryeHomePath} from './restore-cache'
Expand All @@ -28,7 +27,6 @@ async function run(): Promise<void> {
if (arch === undefined) {
throw new Error(`Unsupported architecture: ${process.arch}`)
}

const setupResult = await setupRye(
platform,
arch,
Expand All @@ -44,14 +42,12 @@ async function run(): Promise<void> {
}
core.setOutput('rye-version', setupResult.version)

addRyeToPath(setupResult.cachedPath)
addRyeToPath(setupResult.installedPath)
addMatchers()

if (enableCache) {
await restoreCache(cachePrefix, setupResult.version)
}
core.exportVariable('RYE_HOME', ryeHomePath)
core.info(`Set RYE_HOME to ${ryeHomePath}`)
} catch (err) {
core.setFailed((err as Error).message)
}
Expand All @@ -63,8 +59,8 @@ async function setupRye(
versionInput: string,
checkSum: string | undefined,
githubToken: string | undefined
): Promise<{version: string; cachedPath: string}> {
let cachedPath: string | undefined
): Promise<{version: string; installedPath: string}> {
let installedPath: string | undefined
let downloadPath: string
let version: string
if (versionInput === 'latest') {
Expand All @@ -73,10 +69,10 @@ async function setupRye(
downloadPath = result.downloadPath
} else {
version = versionInput
cachedPath = tryGetFromCache(arch, versionInput)
if (cachedPath) {
installedPath = tryGetFromCache(arch, versionInput)
if (installedPath) {
core.info(`Found Rye in cache for ${versionInput}`)
return {version, cachedPath}
return {version, installedPath}
}
downloadPath = await downloadVersion(
platform,
Expand All @@ -87,27 +83,24 @@ async function setupRye(
)
}

cachedPath = await installRye(downloadPath, arch, version)
return {version, cachedPath}
installedPath = await installRye(downloadPath, arch, version)
return {version, installedPath}
}

async function installRye(
downloadPath: string,
arch: string,
version: string
): Promise<string> {
const tempDir = path.join(process.env['RUNNER_TEMP'] || '', 'rye_temp_home')
await io.mkdirP(tempDir)
core.debug(`Created temporary directory ${tempDir}`)
const options: exec.ExecOptions = {
cwd: tempDir,
cwd: ryeHomePath,
silent: !core.isDebug(),
env: {
...process.env,
RYE_HOME: tempDir
RYE_HOME: ryeHomePath
}
}
core.info(`Installing Rye into ${tempDir}`)
core.info(`Installing Rye into ${ryeHomePath}`)
const execArgs = ['self', 'install', '--yes']
if (
compareVersions(version, EARLIEST_VERSION_WITH_NO_MODIFY_PATHSUPPORT) >= 0
Expand All @@ -116,14 +109,16 @@ async function installRye(
}
await exec.exec(downloadPath, execArgs, options)

const cachedPath = await tc.cacheDir(tempDir, 'rye', version, arch)
core.info(`Moved Rye into ${cachedPath}`)
const cachedPath = await tc.cacheDir(ryeHomePath, 'rye', version, arch)
core.info(`Cached Rye into ${cachedPath}`)
return cachedPath
}

function addRyeToPath(cachedPath: string): void {
core.addPath(`${cachedPath}/shims`)
core.info(`Added ${cachedPath}/shims to the path`)
core.exportVariable('RYE_HOME', ryeHomePath)
core.info(`Set RYE_HOME to ${ryeHomePath}`)
}

function addMatchers(): void {
Expand Down

0 comments on commit 0a881e9

Please sign in to comment.