Skip to content

fix(ci): add client utils to build process #179

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

Merged
merged 10 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ runs:
echo "::set-output name=SCRIPTS_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- scripts | wc -l)"

echo "::set-output name=JS_CLIENT_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- clients/algoliasearch-client-javascript | wc -l)"
echo "::set-output name=JS_ALGOLIASEARCH_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- clients/algoliasearch-client-javascript/packages/algoliasearch clients/algoliasearch-client-javascript/packages/client-search clients/algoliasearch-client-javascript/packages/client-analytics clients/algoliasearch-client-javascript/packages/client-personalization | wc -l)"
echo "::set-output name=JS_COMMON_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- clients/algoliasearch-client-javascript/packages/client-common clients/algoliasearch-client-javascript/packages/requester-browser-xhr clients/algoliasearch-client-javascript/packages/requester-node-http | wc -l)"
echo "::set-output name=JS_TEMPLATE_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- templates/javascript | wc -l)"

Expand Down Expand Up @@ -72,9 +73,20 @@ runs:
shell: bash
run: |
base_changed=${{ github.ref == 'refs/heads/main' || steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.COMMON_SPECS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 || steps.diff.outputs.JS_TEMPLATE_CHANGED > 0 }}
algoliasearch_changed=${{ steps.diff.outputs.JS_ALGOLIASEARCH_CHANGED > 0 }}

matrix=$(./scripts/ci/create-client-matrix.sh javascript $base_changed ${{ github.base_ref }})

if [[ $algoliasearch_changed == 'true' || $base_changed == 'true' ]]; then
algoliasearch_client='{"name": "algoliasearch","folder": "clients/algoliasearch-client-javascript/packages/algoliasearch"}'

if [[ $matrix == '{"client":["no-run"]}' ]]; then
matrix="{'client': [$algoliasearch_client]}"
else
matrix="{'client': $(echo $matrix | jq ".client + [$algoliasearch_client]" -c)}"
fi
fi

if [[ $matrix == '{"client":["no-run"]}' ]]; then
run="false"
else
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- main

env:
CACHE_VERSION: '5'
CACHE_VERSION: '6'

concurrency:
group: ${{ github.ref }}
Expand Down Expand Up @@ -78,7 +78,6 @@ jobs:
- client-common
- requester-browser-xhr
- requester-node-http
- algoliasearch
steps:
- uses: actions/checkout@v2

Expand All @@ -96,7 +95,7 @@ jobs:

- name: Build ${{ matrix.client }} client
if: steps.cache.outputs.cache-hit != 'true'
run: yarn workspace algoliasearch-client-javascript build:utils ${{ matrix.client }}
run: yarn workspace algoliasearch-client-javascript build ${{ matrix.client }}

client_javascript:
timeout-minutes: 10
Expand Down Expand Up @@ -124,7 +123,7 @@ jobs:
key: ${{ runner.os }}-${{ env.CACHE_VERSION }}-js-client-${{ matrix.client.name }}-${{ hashFiles(format('{0}/**', matrix.client.folder)) }}-${{ hashFiles(format('specs/bundled/{0}.yml', matrix.client.name)) }}

- name: Generate ${{ matrix.client.name }} client
if: steps.cache.outputs.cache-hit != 'true'
if: steps.cache.outputs.cache-hit != 'true' && matrix.client.name != 'algoliasearch'
run: yarn cli generate javascript ${{ matrix.client.name }}

- name: Check diff with pushed client
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/process-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- closed

env:
CACHE_VERSION: '5'
CACHE_VERSION: '6'

jobs:
build:
Expand Down
32 changes: 16 additions & 16 deletions clients.config.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"java": {
"folder": "clients/algoliasearch-client-java-2",
"tests": {
"extension": ".test.java",
"outputFolder": "src/test/java/com/algolia"
}
},
"javascript": {
"folder": "clients/algoliasearch-client-javascript",
"tests": {
"extension": ".test.ts",
"outputFolder": "src"
}
},
"php": {
"folder": "clients/algoliasearch-client-php"
"java": {
"folder": "clients/algoliasearch-client-java-2",
"tests": {
"extension": ".test.java",
"outputFolder": "src/test/java/com/algolia"
}
},
"javascript": {
"folder": "clients/algoliasearch-client-javascript",
"tests": {
"extension": ".test.ts",
"outputFolder": "src"
}
},
"php": {
"folder": "clients/algoliasearch-client-php"
}
}
2 changes: 1 addition & 1 deletion clients/algoliasearch-client-javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"private": true,
"scripts": {
"build:utils": "UTILS=${0:-all} yarn rollup -c rollup.config.js",
"build:utils": "yarn build client-common && yarn build requester-browser-xhr && yarn build requester-node-http",
"build": "CLIENT=${0:-all} yarn rollup -c rollup.config.js",
"clean:utils": "yarn workspace @algolia/client-common clean && yarn workspace @algolia/requester-node-http clean && yarn workspace @algolia/requester-browser-xhr clean",
"clean": "rm -rf packages/*/dist",
Expand Down
8 changes: 4 additions & 4 deletions clients/algoliasearch-client-javascript/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { version } from './version';

// Retrieve package to build
const client = process.env.CLIENT?.replace('@algolia/', '');
const utils = process.env.UTILS;
const UTILS = ['client-common', 'requester-browser-xhr', 'requester-node-http'];

function createLicence(name) {
return `/*! ${name}.umd.js | ${version} | © Algolia, inc. | https://github.com/algolia/algoliasearch-client-javascript */`;
Expand Down Expand Up @@ -64,7 +64,7 @@ function getAvailableClients() {
}

function initPackagesConfig() {
if (utils) {
if (UTILS.includes(client)) {
const commonOptions = {
input: 'index.ts',
formats: ['cjs-node', 'esm-node'],
Expand Down Expand Up @@ -100,10 +100,10 @@ function initPackagesConfig() {
},
];

return utils === 'all'
return client === 'all'
? availableUtils
: availableUtils.filter(
(availableUtil) => availableUtil.package === utils
(availableUtil) => availableUtil.package === client
);
}

Expand Down
33 changes: 33 additions & 0 deletions scripts/buildClients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,39 @@ import type { Generator } from './types';

const multiBuildLanguage = new Set(['javascript']);

/**
* Build JavaScript utils packages used in generated clients.
*/
export async function buildJSClientUtils(
verbose: boolean,
client?: string
): Promise<void> {
if (!client || client === 'all') {
const spinner = createSpinner('building JavaScript utils', verbose).start();
await run('yarn workspace algoliasearch-client-javascript clean:utils', {
verbose,
});
await run('yarn workspace algoliasearch-client-javascript build:utils', {
verbose,
});

spinner.succeed();
return;
}

const spinner = createSpinner(
`building JavaScript ${client} utils`,
verbose
).start();

await run(`yarn workspace @algolia/${client} clean`, { verbose });
await run(`yarn workspace algoliasearch-client-javascript build ${client}`, {
verbose,
});

spinner.succeed();
}

/**
* Build only a specific client for one language, used by javascript for example.
*/
Expand Down
46 changes: 33 additions & 13 deletions scripts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,48 @@ export const DOCKER = Boolean(process.env.DOCKER);
// This script is run by `yarn workspace ...`, which means the current working directory is `./script`
export const ROOT_DIR = path.resolve(process.cwd(), '..');

export const GENERATORS = Object.fromEntries(
Object.entries(openapitools['generator-cli'].generators).map(([key, gen]) => {
return [
key,
{
...gen,
output: gen.output.replace('#{cwd}/', ''),
...splitGeneratorKey(key),
},
];
})
export const GENERATORS: Record<string, Generator> = {
// Default `algoliasearch` package as it's built similarly to generated clients
'javascript-algoliasearch': {
language: 'javascript',
client: 'algoliasearch',
key: 'javascript-algoliasearch',
additionalProperties: {
packageName: 'algoliasearch',
packageVersion: '0',
},
},
};

// Build `GENERATORS` from the openapitools file
Object.entries(openapitools['generator-cli'].generators).forEach(
([key, gen]) => {
GENERATORS[key] = {
...gen,
output: gen.output.replace('#{cwd}/', ''),
...splitGeneratorKey(key),
};
}
);

export const LANGUAGES = [
...new Set(Object.values(GENERATORS).map((gen) => gen.language)),
];

export const CLIENTS = [
export const CLIENTS_JS_UTILS = [
'client-common',
'requester-browser-xhr',
'requester-node-http',
];

export const CLIENTS_JS = [
'algoliasearch',
...new Set(Object.values(GENERATORS).map((gen) => gen.client)),
];

export const CLIENTS_JS = CLIENTS.concat([]);
export const CLIENTS = CLIENTS_JS.filter(
(client) => client !== 'algoliasearch'
);

/**
* Takes a generator key in the form 'language-client' and returns the Generator object.
Expand Down
20 changes: 5 additions & 15 deletions scripts/generate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { buildJSClientUtils } from './buildClients';
import { buildSpecs } from './buildSpecs';
import { CI, run, runIfExists } from './common';
import { getLanguageFolder } from './config';
Expand Down Expand Up @@ -68,21 +69,10 @@ export async function generate(
await formatter(lang, getLanguageFolder(lang), verbose);
}

// build common packages
if (lang === 'javascript') {
const spinner = createSpinner(
'cleaning JavaScript client utils',
verbose
).start();
await run('yarn workspace algoliasearch-client-javascript clean:utils', {
verbose,
});
spinner.text = 'building JavaScript client utils';
await run('yarn workspace algoliasearch-client-javascript build:utils', {
verbose,
});

spinner.succeed();
// JavaScript utils are tested independently, we only build them
// during dev to ease the process
if (!CI && lang === 'javascript') {
await buildJSClientUtils(verbose, 'all');
}
}

Expand Down
27 changes: 23 additions & 4 deletions scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
import { Argument, program } from 'commander';
import inquirer from 'inquirer';

import { buildClients } from './buildClients';
import { buildClients, buildJSClientUtils } from './buildClients';
import { buildSpecs } from './buildSpecs';
import {
CI,
CLIENTS,
CLIENTS_JS,
CLIENTS_JS_UTILS,
createGeneratorKey,
DOCKER,
GENERATORS,
Expand Down Expand Up @@ -135,7 +136,9 @@ buildCommand
)
)
.addArgument(
new Argument('[client]', 'The client').choices(['all'].concat(CLIENTS_JS))
new Argument('[client]', 'The client').choices(
['all'].concat([...CLIENTS_JS_UTILS, ...CLIENTS_JS])
)
)
.option('-v, --verbose', 'make the compilation verbose')
.option('-i, --interactive', 'open prompt to query parameters')
Expand All @@ -146,10 +149,26 @@ buildCommand
{ verbose, interactive }
) => {
language = await promptLanguage(language, interactive);
client = await promptClient(client, interactive, CLIENTS_JS);
client = await promptClient(client, interactive, [
...CLIENTS_JS_UTILS,
...CLIENTS_JS,
]);

// We build the JavaScript utils before generated clients as they
// rely on them
if (
(language === 'javascript' || language === 'all') &&
(!client || client === 'all' || CLIENTS_JS_UTILS.includes(client))
) {
await buildJSClientUtils(Boolean(verbose), client);
}

await buildClients(
generatorList({ language, client, clientList: CLIENTS_JS }),
generatorList({
language,
client,
clientList: CLIENTS_JS,
}),
Boolean(verbose)
);
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/release/create-release-issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function readVersions(): Versions {
Object.values(GENERATORS).forEach((gen) => {
if (!versions[gen.language]) {
versions[gen.language] = {
current: gen.additionalProperties.packageVersion,
current: gen.additionalProperties?.packageVersion,
langName: gen.language,
next: undefined,
};
Expand Down