-
Notifications
You must be signed in to change notification settings - Fork 24
chore: cache custom generators #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
587c13f
01a4497
63917b6
4baa527
7162df1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ build | |
pom.xml | ||
|
||
dist | ||
.cache | ||
|
||
.openapi-generator | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,17 @@ import fsp from 'fs/promises'; | |
import path from 'path'; | ||
|
||
import execa from 'execa'; // https://github.com/sindresorhus/execa/tree/v5.1.1 | ||
import { hashElement } from 'folder-hash'; | ||
|
||
import openapitools from '../openapitools.json'; | ||
|
||
import { createSpinner } from './oraLog'; | ||
import type { Generator, RunOptions } from './types'; | ||
import type { | ||
CheckForCache, | ||
CheckForCacheOptions, | ||
Generator, | ||
RunOptions, | ||
} from './types'; | ||
|
||
export const CI = Boolean(process.env.CI); | ||
export const DOCKER = Boolean(process.env.DOCKER); | ||
|
@@ -194,11 +200,80 @@ export async function gitCommit({ | |
); | ||
} | ||
|
||
export async function checkForCache( | ||
{ | ||
job, | ||
folder, | ||
generatedFiles, | ||
filesToCache, | ||
cacheFile, | ||
}: CheckForCacheOptions, | ||
verbose: boolean | ||
): Promise<CheckForCache> { | ||
const spinner = createSpinner(`checking cache for ${job}`, verbose).start(); | ||
const cache: CheckForCache = { | ||
cacheExists: false, | ||
hash: '', | ||
}; | ||
const generatedFilesExists = ( | ||
await Promise.all( | ||
generatedFiles.map((generatedFile) => | ||
exists(`${folder}/${generatedFile}`) | ||
) | ||
) | ||
).every((exist) => exist); | ||
|
||
for (const fileToCache of filesToCache) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would be more elegant with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But less readable :( I'd rather keep this one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by elegant I meant readable, as you prefer |
||
const fileHash = (await hashElement(`${folder}/${fileToCache}`)).hash; | ||
|
||
cache.hash = `${cache.hash}-${fileHash}`; | ||
} | ||
|
||
// We only skip if both the cache and the generated file exists | ||
if (generatedFilesExists && (await exists(cacheFile))) { | ||
const storedHash = (await fsp.readFile(cacheFile)).toString(); | ||
if (storedHash === cache.hash) { | ||
spinner.succeed(`job skipped, cache found for ${job}`); | ||
return { | ||
cacheExists: true, | ||
hash: cache.hash, | ||
}; | ||
} | ||
} | ||
|
||
spinner.info(`cache not found for ${job}`); | ||
|
||
return cache; | ||
} | ||
|
||
export async function buildCustomGenerators(verbose: boolean): Promise<void> { | ||
const cacheFile = toAbsolutePath('generators/.cache'); | ||
const { cacheExists, hash } = await checkForCache( | ||
{ | ||
job: 'custom generators', | ||
folder: toAbsolutePath('generators/'), | ||
generatedFiles: ['build'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue with only checking if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll let you choose what to do here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've went with the naive solution because I wanted to have it merged before the demo, but storing hash as we do on the CI might be the more precise solution. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fun fact, I had the local changes during the demo but built #313 before so it invalidated the cache and added like 5s to the demo 🤷🏼 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah on the CI they store a zip of the generated files it's even more hardcore |
||
filesToCache: ['src', 'build.gradle', 'settings.gradle'], | ||
cacheFile, | ||
}, | ||
verbose | ||
); | ||
|
||
if (cacheExists) { | ||
return; | ||
} | ||
|
||
const spinner = createSpinner('building custom generators', verbose).start(); | ||
|
||
await run('./gradle/gradlew --no-daemon -p generators assemble', { | ||
verbose, | ||
}); | ||
|
||
if (hash) { | ||
spinner.text = 'storing custom generators cache'; | ||
await fsp.writeFile(cacheFile, hash); | ||
} | ||
|
||
spinner.succeed(); | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.