Skip to content

Commit 2d46e6f

Browse files
authored
fix(ci): add client utils to build process (#179)
1 parent d693e5f commit 2d46e6f

File tree

11 files changed

+132
-59
lines changed

11 files changed

+132
-59
lines changed

.github/actions/setup/action.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ runs:
3838
echo "::set-output name=SCRIPTS_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- scripts | wc -l)"
3939
4040
echo "::set-output name=JS_CLIENT_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- clients/algoliasearch-client-javascript | wc -l)"
41+
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)"
4142
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)"
4243
echo "::set-output name=JS_TEMPLATE_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- templates/javascript | wc -l)"
4344
@@ -72,9 +73,20 @@ runs:
7273
shell: bash
7374
run: |
7475
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 }}
76+
algoliasearch_changed=${{ steps.diff.outputs.JS_ALGOLIASEARCH_CHANGED > 0 }}
7577
7678
matrix=$(./scripts/ci/create-client-matrix.sh javascript $base_changed ${{ github.base_ref }})
7779
80+
if [[ $algoliasearch_changed == 'true' || $base_changed == 'true' ]]; then
81+
algoliasearch_client='{"name": "algoliasearch","folder": "clients/algoliasearch-client-javascript/packages/algoliasearch"}'
82+
83+
if [[ $matrix == '{"client":["no-run"]}' ]]; then
84+
matrix="{'client': [$algoliasearch_client]}"
85+
else
86+
matrix="{'client': $(echo $matrix | jq ".client + [$algoliasearch_client]" -c)}"
87+
fi
88+
fi
89+
7890
if [[ $matrix == '{"client":["no-run"]}' ]]; then
7991
run="false"
8092
else

.github/workflows/check.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
- main
99

1010
env:
11-
CACHE_VERSION: '5'
11+
CACHE_VERSION: '6'
1212

1313
concurrency:
1414
group: ${{ github.ref }}
@@ -78,7 +78,6 @@ jobs:
7878
- client-common
7979
- requester-browser-xhr
8080
- requester-node-http
81-
- algoliasearch
8281
steps:
8382
- uses: actions/checkout@v2
8483

@@ -96,7 +95,7 @@ jobs:
9695

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

101100
client_javascript:
102101
timeout-minutes: 10
@@ -124,7 +123,7 @@ jobs:
124123
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)) }}
125124

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

130129
- name: Check diff with pushed client

.github/workflows/process-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- closed
77

88
env:
9-
CACHE_VERSION: '5'
9+
CACHE_VERSION: '6'
1010

1111
jobs:
1212
build:

clients.config.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
2-
"java": {
3-
"folder": "clients/algoliasearch-client-java-2",
4-
"tests": {
5-
"extension": ".test.java",
6-
"outputFolder": "src/test/java/com/algolia"
7-
}
8-
},
9-
"javascript": {
10-
"folder": "clients/algoliasearch-client-javascript",
11-
"tests": {
12-
"extension": ".test.ts",
13-
"outputFolder": "src"
14-
}
15-
},
16-
"php": {
17-
"folder": "clients/algoliasearch-client-php"
2+
"java": {
3+
"folder": "clients/algoliasearch-client-java-2",
4+
"tests": {
5+
"extension": ".test.java",
6+
"outputFolder": "src/test/java/com/algolia"
187
}
8+
},
9+
"javascript": {
10+
"folder": "clients/algoliasearch-client-javascript",
11+
"tests": {
12+
"extension": ".test.ts",
13+
"outputFolder": "src"
14+
}
15+
},
16+
"php": {
17+
"folder": "clients/algoliasearch-client-php"
18+
}
1919
}

clients/algoliasearch-client-javascript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
],
77
"private": true,
88
"scripts": {
9-
"build:utils": "UTILS=${0:-all} yarn rollup -c rollup.config.js",
9+
"build:utils": "yarn build client-common && yarn build requester-browser-xhr && yarn build requester-node-http",
1010
"build": "CLIENT=${0:-all} yarn rollup -c rollup.config.js",
1111
"clean:utils": "yarn workspace @algolia/client-common clean && yarn workspace @algolia/requester-node-http clean && yarn workspace @algolia/requester-browser-xhr clean",
1212
"clean": "rm -rf packages/*/dist",

clients/algoliasearch-client-javascript/rollup.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { version } from './version';
1313

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

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

6666
function initPackagesConfig() {
67-
if (utils) {
67+
if (UTILS.includes(client)) {
6868
const commonOptions = {
6969
input: 'index.ts',
7070
formats: ['cjs-node', 'esm-node'],
@@ -100,10 +100,10 @@ function initPackagesConfig() {
100100
},
101101
];
102102

103-
return utils === 'all'
103+
return client === 'all'
104104
? availableUtils
105105
: availableUtils.filter(
106-
(availableUtil) => availableUtil.package === utils
106+
(availableUtil) => availableUtil.package === client
107107
);
108108
}
109109

scripts/buildClients.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,39 @@ import type { Generator } from './types';
55

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

8+
/**
9+
* Build JavaScript utils packages used in generated clients.
10+
*/
11+
export async function buildJSClientUtils(
12+
verbose: boolean,
13+
client?: string
14+
): Promise<void> {
15+
if (!client || client === 'all') {
16+
const spinner = createSpinner('building JavaScript utils', verbose).start();
17+
await run('yarn workspace algoliasearch-client-javascript clean:utils', {
18+
verbose,
19+
});
20+
await run('yarn workspace algoliasearch-client-javascript build:utils', {
21+
verbose,
22+
});
23+
24+
spinner.succeed();
25+
return;
26+
}
27+
28+
const spinner = createSpinner(
29+
`building JavaScript ${client} utils`,
30+
verbose
31+
).start();
32+
33+
await run(`yarn workspace @algolia/${client} clean`, { verbose });
34+
await run(`yarn workspace algoliasearch-client-javascript build ${client}`, {
35+
verbose,
36+
});
37+
38+
spinner.succeed();
39+
}
40+
841
/**
942
* Build only a specific client for one language, used by javascript for example.
1043
*/

scripts/common.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,48 @@ export const DOCKER = Boolean(process.env.DOCKER);
1313
// This script is run by `yarn workspace ...`, which means the current working directory is `./script`
1414
export const ROOT_DIR = path.resolve(process.cwd(), '..');
1515

16-
export const GENERATORS = Object.fromEntries(
17-
Object.entries(openapitools['generator-cli'].generators).map(([key, gen]) => {
18-
return [
19-
key,
20-
{
21-
...gen,
22-
output: gen.output.replace('#{cwd}/', ''),
23-
...splitGeneratorKey(key),
24-
},
25-
];
26-
})
16+
export const GENERATORS: Record<string, Generator> = {
17+
// Default `algoliasearch` package as it's built similarly to generated clients
18+
'javascript-algoliasearch': {
19+
language: 'javascript',
20+
client: 'algoliasearch',
21+
key: 'javascript-algoliasearch',
22+
additionalProperties: {
23+
packageName: 'algoliasearch',
24+
packageVersion: '0',
25+
},
26+
},
27+
};
28+
29+
// Build `GENERATORS` from the openapitools file
30+
Object.entries(openapitools['generator-cli'].generators).forEach(
31+
([key, gen]) => {
32+
GENERATORS[key] = {
33+
...gen,
34+
output: gen.output.replace('#{cwd}/', ''),
35+
...splitGeneratorKey(key),
36+
};
37+
}
2738
);
2839

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

33-
export const CLIENTS = [
44+
export const CLIENTS_JS_UTILS = [
45+
'client-common',
46+
'requester-browser-xhr',
47+
'requester-node-http',
48+
];
49+
50+
export const CLIENTS_JS = [
51+
'algoliasearch',
3452
...new Set(Object.values(GENERATORS).map((gen) => gen.client)),
3553
];
3654

37-
export const CLIENTS_JS = CLIENTS.concat([]);
55+
export const CLIENTS = CLIENTS_JS.filter(
56+
(client) => client !== 'algoliasearch'
57+
);
3858

3959
/**
4060
* Takes a generator key in the form 'language-client' and returns the Generator object.

scripts/generate.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { buildJSClientUtils } from './buildClients';
12
import { buildSpecs } from './buildSpecs';
23
import { CI, run, runIfExists } from './common';
34
import { getLanguageFolder } from './config';
@@ -68,21 +69,10 @@ export async function generate(
6869
await formatter(lang, getLanguageFolder(lang), verbose);
6970
}
7071

71-
// build common packages
72-
if (lang === 'javascript') {
73-
const spinner = createSpinner(
74-
'cleaning JavaScript client utils',
75-
verbose
76-
).start();
77-
await run('yarn workspace algoliasearch-client-javascript clean:utils', {
78-
verbose,
79-
});
80-
spinner.text = 'building JavaScript client utils';
81-
await run('yarn workspace algoliasearch-client-javascript build:utils', {
82-
verbose,
83-
});
84-
85-
spinner.succeed();
72+
// JavaScript utils are tested independently, we only build them
73+
// during dev to ease the process
74+
if (!CI && lang === 'javascript') {
75+
await buildJSClientUtils(verbose, 'all');
8676
}
8777
}
8878

scripts/index.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
import { Argument, program } from 'commander';
33
import inquirer from 'inquirer';
44

5-
import { buildClients } from './buildClients';
5+
import { buildClients, buildJSClientUtils } from './buildClients';
66
import { buildSpecs } from './buildSpecs';
77
import {
88
CI,
99
CLIENTS,
1010
CLIENTS_JS,
11+
CLIENTS_JS_UTILS,
1112
createGeneratorKey,
1213
DOCKER,
1314
GENERATORS,
@@ -135,7 +136,9 @@ buildCommand
135136
)
136137
)
137138
.addArgument(
138-
new Argument('[client]', 'The client').choices(['all'].concat(CLIENTS_JS))
139+
new Argument('[client]', 'The client').choices(
140+
['all'].concat([...CLIENTS_JS_UTILS, ...CLIENTS_JS])
141+
)
139142
)
140143
.option('-v, --verbose', 'make the compilation verbose')
141144
.option('-i, --interactive', 'open prompt to query parameters')
@@ -146,10 +149,26 @@ buildCommand
146149
{ verbose, interactive }
147150
) => {
148151
language = await promptLanguage(language, interactive);
149-
client = await promptClient(client, interactive, CLIENTS_JS);
152+
client = await promptClient(client, interactive, [
153+
...CLIENTS_JS_UTILS,
154+
...CLIENTS_JS,
155+
]);
156+
157+
// We build the JavaScript utils before generated clients as they
158+
// rely on them
159+
if (
160+
(language === 'javascript' || language === 'all') &&
161+
(!client || client === 'all' || CLIENTS_JS_UTILS.includes(client))
162+
) {
163+
await buildJSClientUtils(Boolean(verbose), client);
164+
}
150165

151166
await buildClients(
152-
generatorList({ language, client, clientList: CLIENTS_JS }),
167+
generatorList({
168+
language,
169+
client,
170+
clientList: CLIENTS_JS,
171+
}),
153172
Boolean(verbose)
154173
);
155174
}

scripts/release/create-release-issue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function readVersions(): Versions {
2828
Object.values(GENERATORS).forEach((gen) => {
2929
if (!versions[gen.language]) {
3030
versions[gen.language] = {
31-
current: gen.additionalProperties.packageVersion,
31+
current: gen.additionalProperties?.packageVersion,
3232
langName: gen.language,
3333
next: undefined,
3434
};

0 commit comments

Comments
 (0)