Skip to content

Commit

Permalink
Add automatic toolchain version detection (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
eifinger authored Jul 13, 2024
1 parent f406a0a commit baa2600
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 50 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/test-latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,34 @@ jobs:
version: latest
- run: rye sync
working-directory: __tests__/fixtures/rye-project

test-latest-automatic-toolchain:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, macos-14, oracle-aarch64]
steps:
- uses: actions/checkout@v4
- name: Determine expected python version
id: determine-expected-python-version
run: |
EXPECTED_PYTHON_VERSION=$(cat .python-version)
echo "EXPECTED_PYTHON_VERSION=$EXPECTED_PYTHON_VERSION" >> "$GITHUB_OUTPUT"
working-directory: __tests__/fixtures/rye-project
- name: Setup rye
uses: ./
with:
version: latest
working-directory: __tests__/fixtures/rye-project
- run: rye sync
working-directory: __tests__/fixtures/rye-project
- name: Check python version
run: |
ACTUAL_PYTHON_VERSION=$(ls $RYE_HOME/py)
if [ "$ACTUAL_PYTHON_VERSION" != "$EXPECTED_PYTHON_VERSION" ];
then
echo "$ACTUAL_PYTHON_VERSION is not the same as $EXPECTED_PYTHON_VERSION"
exit 1
fi
env:
EXPECTED_PYTHON_VERSION: ${{ steps.determine-expected-python-version.outputs.EXPECTED_PYTHON_VERSION }}
33 changes: 17 additions & 16 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.

44 changes: 30 additions & 14 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.

19 changes: 10 additions & 9 deletions src/restore-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {getArch} from './utils'

export const STATE_CACHE_KEY = 'cache-key'
export const STATE_CACHE_MATCHED_KEY = 'cache-matched-key'
export const workingDirInput = core.getInput('working-directory')
export const workingDir = workingDirInput ? `${path.sep}${workingDirInput}` : ''
export const venvPath = `${process.env['GITHUB_WORKSPACE']}${workingDir}${path.sep}.venv`
const workingDirInput = core.getInput('working-directory')
const workingDir = workingDirInput ? `${path.sep}${workingDirInput}` : ''
export const WORKING_DIR_PATH = `${process.env['GITHUB_WORKSPACE']}${workingDir}`
export const VENV_PATH = `${process.env['GITHUB_WORKSPACE']}${workingDir}${path.sep}.venv`
const CACHE_VERSION = '5'
const cacheLocalStoragePath =
`${core.getInput('cache-local-storage-path')}` || ''
Expand All @@ -34,7 +35,7 @@ export async function restoreCache(
try {
matchedKey = cacheLocalStoragePath
? await restoreCacheLocal(cacheKey)
: await cache.restoreCache([venvPath], cacheKey)
: await cache.restoreCache([VENV_PATH], cacheKey)
} catch (err) {
const message = (err as Error).message
core.warning(message)
Expand Down Expand Up @@ -73,7 +74,7 @@ function handleMatchResult(

const venvPathMatch = doesCachedVenvPathMatchCurrentVenvPath()
if (!venvPathMatch) {
fs.rmSync(venvPath, {recursive: true})
fs.rmSync(VENV_PATH, {recursive: true})
core.setOutput('cache-hit', false)
return
}
Expand All @@ -86,12 +87,12 @@ function handleMatchResult(
}

function doesCachedVenvPathMatchCurrentVenvPath(): boolean {
const ryeVenvPath = `${venvPath}${path.sep}rye-venv.json`
const ryeVenvPath = `${VENV_PATH}${path.sep}rye-venv.json`
const ryeVenv = JSON.parse(fs.readFileSync(ryeVenvPath, 'utf8'))
core.info(
`Checking if the cached .venv matches the current path: ${venvPath}`
`Checking if the cached .venv matches the current path: ${VENV_PATH}`
)
if (ryeVenv.venv_path !== venvPath) {
if (ryeVenv.venv_path !== VENV_PATH) {
core.warning(
`The .venv in the cache cannot be used because it is from another location: ${ryeVenv.venv_path}`
)
Expand All @@ -105,7 +106,7 @@ async function restoreCacheLocal(
): Promise<string | undefined> {
const storedCache = `${cacheLocalStoragePath}${path.sep}${primaryKey}`
if (await exists(storedCache)) {
await cp(`${storedCache}${path.sep}.venv`, venvPath, {
await cp(`${storedCache}${path.sep}.venv`, VENV_PATH, {
recursive: true
})
return primaryKey
Expand Down
8 changes: 4 additions & 4 deletions src/save-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from 'path'
import {
STATE_CACHE_MATCHED_KEY,
STATE_CACHE_KEY,
venvPath
VENV_PATH
} from './restore-cache'

const enableCache = core.getInput('enable-cache') === 'true'
Expand Down Expand Up @@ -36,18 +36,18 @@ async function saveCache(): Promise<void> {
core.info(`Cache hit occurred on key ${cacheKey}, not saving .venv.`)
return
}
core.info(`Saving .venv path: ${venvPath}`)
core.info(`Saving .venv path: ${VENV_PATH}`)
cacheLocalStoragePath
? await saveCacheLocal(cacheKey)
: await cache.saveCache([venvPath], cacheKey)
: await cache.saveCache([VENV_PATH], cacheKey)

core.info(`.venv saved with the key: ${cacheKey}`)
}

async function saveCacheLocal(cacheKey: string): Promise<void> {
const targetPath = `${cacheLocalStoragePath}${path.sep}${cacheKey}`
await io.mkdirP(targetPath)
await io.cp(venvPath, `${targetPath}${path.sep}.venv`, {
await io.cp(VENV_PATH, `${targetPath}${path.sep}.venv`, {
recursive: true
})
}
Expand Down
31 changes: 26 additions & 5 deletions src/setup-rye.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as io from '@actions/io'
import * as path from 'path'
import * as fs from 'fs'
import {downloadVersion, tryGetFromCache} from './download/download-version'
import {restoreCache} from './restore-cache'
import {restoreCache, WORKING_DIR_PATH} from './restore-cache'
import {
Architecture,
EARLIEST_VERSION_WITH_NO_MODIFY_PATHSUPPORT,
Expand Down Expand Up @@ -103,13 +103,18 @@ async function installRye(
core.debug(`Created temporary directory ${tempDir}`)
// Cache first to get the correct path
const cachedPath = await tc.cacheDir(tempDir, toolsCacheName, version, arch)
const toolchainVersion = await determineToolchainVersion()
const env = toolchainVersion
? {
...process.env,
RYE_HOME: cachedPath,
RYE_TOOLCHAIN_VERSION: toolchainVersion
}
: {...process.env, RYE_HOME: cachedPath}
const options: exec.ExecOptions = {
cwd: cachedPath,
silent: !core.isDebug(),
env: {
...process.env,
RYE_HOME: cachedPath
}
env: env
}
core.info(`Installing Rye into ${cachedPath}`)
const execArgs = ['self', 'install', '--yes']
Expand All @@ -122,6 +127,22 @@ async function installRye(
return cachedPath
}

async function determineToolchainVersion(): Promise<string | void> {
const pythonVersionFile = `${WORKING_DIR_PATH}${path.sep}.python-version`
if (fs.existsSync(pythonVersionFile)) {
const toolchainVersion = await fs.promises.readFile(
pythonVersionFile,
'utf8'
)
core.info(`Determined RYE_TOOLCHAIN_VERSION: ${toolchainVersion.trim()}`)
return toolchainVersion.trim()
}
core.warning(
`No .python-version file found, using default RYE_TOOLCHAIN_VERSION`
)
return
}

async function createConfigBackup(installedPath: string): Promise<void> {
if (fs.existsSync(`${installedPath}${path.sep}${RYE_CONFIG_TOML}`)) {
await io.cp(
Expand Down

0 comments on commit baa2600

Please sign in to comment.