Skip to content

Commit

Permalink
new: Include proto/moon version in cache key. (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Dec 16, 2023
1 parent 58d4b7f commit b6e638b
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 226 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,19 @@ jobs:
- run: npm install -g pnpm
- run: pnpm install
- run: pnpm run check
action-default:
name: 'Action'
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
fail-fast: false
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
- run: npm install -g pnpm
- run: pnpm install
- run: pnpm run build
- uses: ./ # self
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.3.0

- Now includes the moon and proto versions in the cache key.
- Updated dependencies.

# 0.2.1

- Support proto v0.24 changes.
Expand Down
50 changes: 44 additions & 6 deletions helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import crypto from 'node:crypto';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
Expand Down Expand Up @@ -37,24 +38,35 @@ export function getToolsDir() {
return path.join(getProtoHome(), 'tools');
}

export function getUidFile() {
return path.join(getProtoHome(), 'id');
}

export function getWorkingDir() {
return process.env.GITHUB_WORKSPACE ?? process.cwd();
}

export function isCacheEnabled(): boolean {
export function isCacheEnabled() {
return core.getBooleanInput('cache') && cache.isFeatureAvailable();
}

export function isUsingMoon() {
return fs.existsSync(path.join(getWorkingDir(), core.getInput('workspace-root'), '.moon'));
}

export function extractMajorMinor(version: string) {
const [major, minor] = version.split('.');

return `${major}.${minor}`;
}

export function getCacheKeyPrefix() {
// v1 - Before proto v0.24 changes
return 'moonrepo-toolchain-v2';
}

export async function getToolchainCacheKey() {
const hasher = crypto.createHash('sha1');
const files = ['.prototools'];

if (isUsingMoon()) {
Expand All @@ -67,9 +79,27 @@ export async function getToolchainCacheKey() {
}
}

const toolchainHash = await glob.hashFiles(files.join('\n'));
core.debug(`Hashing files: ${files.join(', ')}`);

return `${getCacheKeyPrefix()}-${process.platform}-${toolchainHash}`;
hasher.update(await glob.hashFiles(files.join('\n')));

const protoVersion = process.env.PROTO_CLI_VERSION;

if (protoVersion) {
core.debug(`Hashing proto version: ${protoVersion}`);

hasher.update(extractMajorMinor(protoVersion));
}

const moonVersion = process.env.MOON_CLI_VERSION;

if (moonVersion) {
core.debug(`Hashing moon version: ${moonVersion}`);

hasher.update(extractMajorMinor(moonVersion));
}

return `${getCacheKeyPrefix()}-${process.platform}-${hasher.digest('hex')}`;
}

export async function installBin(bin: string) {
Expand Down Expand Up @@ -98,20 +128,28 @@ export async function installBin(bin: string) {

const binDir = getBinDir();
const binPath = path.join(binDir, WINDOWS ? `${bin}.exe` : bin);
const envPrefix = bin.toUpperCase();

await execa(script, version === 'latest' ? [] : [version], {
env: {
[`${bin.toUpperCase()}_INSTALL_DIR`]: binDir,
[`${envPrefix}_INSTALL_DIR`]: binDir,
},
stdio: core.isDebug() || !!process.env[`${bin.toUpperCase()}_DEBUG`] ? 'inherit' : 'pipe',
stdio: core.isDebug() || !!process.env[`${envPrefix}_DEBUG`] ? 'inherit' : 'pipe',
});

core.info(`Installed binary to ${binPath}`);

core.info('Checking version');

try {
await execa(binPath, ['--version'], { stdio: 'inherit' });
const result = await execa(binPath, ['--version'], { stdio: 'pipe' });

if (result.stdout) {
// eslint-disable-next-line require-atomic-updates
process.env[`${envPrefix}_CLI_VERSION`] = result.stdout.replace(bin, '').trim();

core.info(result.stdout);
}
} catch (error) {
core.error(String(error));
}
Expand Down
10 changes: 5 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getShimsDir,
getToolchainCacheKey,
getToolsDir,
getUidFile,
installBin,
isCacheEnabled,
isUsingMoon,
Expand All @@ -22,12 +23,11 @@ async function restoreCache() {

const primaryKey = await getToolchainCacheKey();
const cachePrefix = getCacheKeyPrefix();

const cacheKey = await cache.restoreCache(
[getPluginsDir(), getToolsDir()],
[getPluginsDir(), getToolsDir(), getUidFile()],
primaryKey,
[`${cachePrefix}-${process.platform}`, cachePrefix],
{},
false,
);

if (cacheKey) {
Expand All @@ -50,14 +50,14 @@ async function run() {
core.addPath(binDir);
core.addPath(shimsDir);

await restoreCache();

await installBin('proto');

if (isUsingMoon()) {
await installBin('moon');
}

await restoreCache();

if (core.getBooleanInput('auto-install')) {
core.info('Auto-installing tools');

Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@moonrepo/setup-toolchain",
"version": "0.2.1",
"version": "0.3.0",
"description": "A GitHub action to setup and cache the proto and moon toolchains.",
"main": "dist/index.js",
"scripts": {
Expand All @@ -26,15 +26,15 @@
"execa": "^5.1.1"
},
"devDependencies": {
"@types/node": "^20.9.4",
"@types/node": "^20.10.4",
"@vercel/ncc": "^0.38.1",
"eslint": "^8.54.0",
"eslint-config-moon": "^2.0.12",
"prettier": "^3.1.0",
"eslint": "^8.56.0",
"eslint-config-moon": "^2.0.13",
"prettier": "^3.1.1",
"prettier-config-moon": "^1.1.2",
"ts-node": "^10.9.1",
"ts-node": "^10.9.2",
"tsconfig-moon": "^1.3.0",
"typescript": "^5.3.2"
"typescript": "^5.3.3"
},
"engines": {
"node": ">=16.0.0"
Expand Down
Loading

0 comments on commit b6e638b

Please sign in to comment.