Skip to content

Commit

Permalink
Merge branch 'main' into fix/rework-go-cts
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Jan 15, 2024
2 parents dcca9e7 + 2c6fc65 commit dd4fe5c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ jobs:
run: yarn cli snippets ${{ matrix.client.language }} ${{ matrix.client.toRun }}

- name: Zip artifact before storing
run: zip -r -y clients-${{ matrix.client.language }}.zip ${{ matrix.client.path }} ${{ matrix.client.testsToStore }} ${{ matrix.client.snippetsToStore }} -x "**/node_modules**" "**/__pycache__/**" "**/.yarn/cache/**" "**/build/**" "**/dist/**" "**/.gradle/**" "**/bin/**" "**/vendor/**" "**/target/**" "**/.dart_tool/**"
run: zip -r -y clients-${{ matrix.client.language }}.zip ${{ matrix.client.path }} ${{ matrix.client.testsToStore }} ${{ matrix.client.snippetsToStore }} -x "**/node_modules**" "**/__pycache__/**" "**/.yarn/cache/**" "**/build/**" "**/.build/**" "**/dist/**" "**/.gradle/**" "**/bin/**" "**/vendor/**" "**/target/**" "**/.dart_tool/**"

- name: Store ${{ matrix.client.language }} clients
uses: actions/upload-artifact@v4
Expand Down
56 changes: 25 additions & 31 deletions scripts/buildSpecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import fsp from 'fs/promises';

import yaml from 'js-yaml';

import { checkForCache, exists, run, toAbsolutePath } from './common.js';
import { Cache } from './cache.js';
import { exists, run, toAbsolutePath } from './common.js';
import { createSpinner } from './spinners.js';
import type { Spec } from './types.js';

Expand Down Expand Up @@ -96,29 +97,23 @@ async function transformBundle({
async function lintCommon(useCache: boolean): Promise<void> {
const spinner = createSpinner('linting common spec');

let hash = '';
const cacheFile = toAbsolutePath(`specs/dist/common.cache`);
if (useCache) {
const { cacheExists, hash: newCache } = await checkForCache({
folder: toAbsolutePath('specs/'),
generatedFiles: [],
filesToCache: ['common'],
cacheFile,
});

if (cacheExists) {
spinner.succeed("job skipped, cache found for 'common' spec");
return;
}
const cache = new Cache({
folder: toAbsolutePath('specs/'),
generatedFiles: [],
filesToCache: ['common'],
cacheFile: toAbsolutePath('specs/dist/common.cache'),
});

hash = newCache;
if (useCache && (await cache.isValid())) {
spinner.succeed("job skipped, cache found for 'common' spec");
return;
}

await run(`yarn specs:lint common`);

if (hash) {
if (useCache) {
spinner.text = 'storing common spec cache';
await fsp.writeFile(cacheFile, hash);
await cache.store();
}

spinner.succeed();
Expand Down Expand Up @@ -171,28 +166,27 @@ async function buildSpec(spec: string, outputFormat: string, useCache: boolean):
const isAlgoliasearch = spec === 'algoliasearch';
// In case of lite we use a the `search` spec as a base because only its bundled form exists.
const specBase = isAlgoliasearch ? 'search' : spec;
const cacheFile = toAbsolutePath(`specs/dist/${spec}.cache`);
let hash = '';
const cache = new Cache({
folder: toAbsolutePath('specs/'),
generatedFiles: [
`bundled/${spec}.yml`,
...(isAlgoliasearch ? [] : [`bundled/${spec}.doc.yml`]),
],
filesToCache: [specBase, 'common'],
cacheFile: toAbsolutePath(`specs/dist/${spec}.cache`),
});

const spinner = createSpinner(`starting '${spec}' spec`);

if (useCache) {
spinner.text = `checking cache for '${specBase}'`;

const { cacheExists, hash: newCache } = await checkForCache({
folder: toAbsolutePath('specs/'),
generatedFiles: [`bundled/${spec}.yml`],
filesToCache: [specBase, 'common'],
cacheFile,
});

if (cacheExists) {
if (await cache.isValid()) {
spinner.succeed(`job skipped, cache found for '${specBase}'`);
return;
}

spinner.text = `cache not found for '${specBase}'`;
hash = newCache;
}

// First linting the base
Expand Down Expand Up @@ -232,9 +226,9 @@ async function buildSpec(spec: string, outputFormat: string, useCache: boolean):
await run(`yarn specs:fix bundled/${spec}.doc.yml`);
}

if (hash) {
if (useCache) {
spinner.text = `storing '${spec}' spec cache`;
await fsp.writeFile(cacheFile, hash);
await cache.store();
}

spinner.succeed(`building complete for '${spec}' spec`);
Expand Down
56 changes: 56 additions & 0 deletions scripts/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import fsp from 'fs/promises';

import { hashElement } from 'folder-hash';

import { exists } from './common';

export class Cache {
folder: string;
generatedFiles: string[];
filesToCache: string[];
cacheFile: string;

constructor({
folder,
generatedFiles,
filesToCache,
cacheFile,
}: {
folder: string;
generatedFiles: string[];
filesToCache: string[];
cacheFile: string;
}) {
this.folder = folder;
this.generatedFiles = generatedFiles;
this.filesToCache = filesToCache;
this.cacheFile = cacheFile;
}

async computeHash(): Promise<string> {
let hash = '';

for (const generatedFile of this.generatedFiles) {
hash += (await hashElement(`${this.folder}/${generatedFile}`)).hash;
}

for (const fileToCache of this.filesToCache) {
hash += (await hashElement(`${this.folder}/${fileToCache}`)).hash;
}

return hash;
}

async isValid(): Promise<boolean> {
if (!(await exists(this.cacheFile))) {
return false;
}

const storedHash = (await fsp.readFile(this.cacheFile)).toString();
return storedHash === (await this.computeHash());
}

async store(): Promise<void> {
await fsp.writeFile(this.cacheFile, await this.computeHash());
}
}
60 changes: 9 additions & 51 deletions scripts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@ import path from 'path';
import { Octokit } from '@octokit/rest';
import { execaCommand, execa } from 'execa';
import type { ExecaError } from 'execa';
import { hashElement } from 'folder-hash';
import { remove } from 'fs-extra';

import clientsConfig from '../config/clients.config.json' assert { type: 'json' };
import releaseConfig from '../config/release.config.json' assert { type: 'json' };

import { buildSpecs } from './buildSpecs';
import { Cache } from './cache';
import { getDockerImage } from './config';
import { generateOpenapitools } from './pre-gen';
import { getGitAuthor } from './release/common.js';
import { createSpinner } from './spinners.js';
import type {
CheckForCache,
CheckForCacheOptions,
Generator,
Language,
RunOptions,
} from './types.js';
import type { Generator, Language, RunOptions } from './types.js';

export const MAIN_BRANCH = releaseConfig.mainBranch;
export const OWNER = releaseConfig.owner;
Expand Down Expand Up @@ -156,62 +150,26 @@ export async function gitCommit({
await execa('git', ['commit', '-m', messageWithCoAuthors], { cwd });
}

export async function checkForCache({
folder,
generatedFiles,
filesToCache,
cacheFile,
}: CheckForCacheOptions): Promise<CheckForCache> {
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) {
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) {
return {
cacheExists: true,
hash: cache.hash,
};
}
}

return cache;
}

async function buildCustomGenerators(): Promise<void> {
const spinner = createSpinner('building custom generators');

const cacheFile = toAbsolutePath('generators/.cache');
const { cacheExists, hash } = await checkForCache({
const cache = new Cache({
folder: toAbsolutePath('generators/'),
generatedFiles: ['build'],
generatedFiles: ['build/classes'],
filesToCache: ['src', 'build.gradle', 'settings.gradle'],
cacheFile,
cacheFile: toAbsolutePath('generators/.cache'),
});

const cacheExists = await cache.isValid();

if (cacheExists) {
spinner.succeed('job skipped, cache found for custom generators');
return;
}

await run('./gradle/gradlew --no-daemon -p generators assemble', { language: 'java' });

if (hash) {
spinner.text = 'storing custom generators cache';
await fsp.writeFile(cacheFile, hash);
}
spinner.text = 'storing custom generators cache';
await cache.store();

spinner.succeed();
}
Expand Down
12 changes: 0 additions & 12 deletions scripts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,6 @@ export type Generator = Record<string, any> & {
additionalProperties: AdditionalProperties;
};

export type CheckForCacheOptions = {
folder: string;
generatedFiles: string[];
filesToCache: string[];
cacheFile: string;
};

export type CheckForCache = {
cacheExists: boolean;
hash: string;
};

export type RunOptions = {
errorMessage?: string;
cwd?: string;
Expand Down

0 comments on commit dd4fe5c

Please sign in to comment.