Skip to content

Commit c887d5c

Browse files
fix: generate version info during build (#6277)
1 parent 7ff995f commit c887d5c

File tree

15 files changed

+93
-26
lines changed

15 files changed

+93
-26
lines changed

.github/workflows/release-custom-tag.yml

+12-3
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,25 @@ jobs:
4343
run: |
4444
npm install -g npm
4545
yarn install
46-
yarn build
4746
48-
- name: publish
47+
- name: version
4948
run: |
5049
git config user.email ${{ secrets.BOT_GIT_EMAIL }}
5150
git config user.name ${{ secrets.BOT_GIT_USERNAME }}
5251
53-
${GITHUB_WORKSPACE}/node_modules/.bin/lerna publish ${{ github.event.inputs.release_type }} \
52+
${GITHUB_WORKSPACE}/node_modules/.bin/lerna version ${{ github.event.inputs.release_type }} \
5453
--dist-tag ${{ github.event.inputs.npm_tag }}
5554
env:
5655
GH_TOKEN: ${{ secrets.ACCESS_TOKEN }}
5756
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
5857
NPM_CONFIG_PROVENANCE: true
58+
59+
- name: build
60+
run: yarn build
61+
62+
- name: publish to npm
63+
run: ${GITHUB_WORKSPACE}/node_modules/.bin/lerna publish from-git
64+
env:
65+
GH_TOKEN: ${{ secrets.ACCESS_TOKEN }}
66+
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
67+
NPM_CONFIG_PROVENANCE: true

.github/workflows/release-snapshot.yml

+16-5
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,34 @@ jobs:
3434
run: |
3535
npm install -g npm
3636
yarn install
37-
yarn build
3837
39-
- name: publish
38+
- name: version
4039
run: |
4140
git config user.email ${{ secrets.BOT_GIT_EMAIL }}
4241
git config user.name ${{ secrets.BOT_GIT_USERNAME }}
4342
4443
git_hash=$(git rev-parse --short "$GITHUB_SHA")
4544
current_branch=$(node -p -e "'${{ github.ref }}'.replace('refs/heads/', '')")
45+
echo "current_branch=${current_branch}" >> "$GITHUB_ENV"
4646
47-
${GITHUB_WORKSPACE}/node_modules/.bin/lerna publish "0.0.0-${git_hash}" \
47+
${GITHUB_WORKSPACE}/node_modules/.bin/lerna version "0.0.0-${git_hash}" \
4848
--exact \
4949
--no-push \
5050
--no-git-tag-version \
51-
--pre-dist-tag dev \
52-
--allow-branch ${current_branch}
51+
--allow-branch ${{ env.current_branch }}
52+
env:
53+
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
54+
NPM_CONFIG_PROVENANCE: true
5355

56+
- name: build
57+
run: yarn build
58+
59+
- name: publish to npm
60+
run: |
61+
${GITHUB_WORKSPACE}/node_modules/.bin/lerna publish from-git \
62+
--pre-dist-tag dev \
63+
--allow-branch ${{ env.current_branch }}
5464
env:
65+
GH_TOKEN: ${{ secrets.ACCESS_TOKEN }}
5566
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
5667
NPM_CONFIG_PROVENANCE: true

.github/workflows/release.yml

+12-3
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,32 @@ jobs:
7272
run: |
7373
npm install -g npm
7474
yarn install --immutable
75-
yarn build
7675
77-
- name: publish
76+
- name: version
7877
run: |
7978
npm config set //registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}
8079
8180
git config user.email ${{ secrets.BOT_GIT_EMAIL }}
8281
git config user.name ${{ secrets.BOT_GIT_USERNAME }}
8382
84-
${GITHUB_WORKSPACE}/node_modules/.bin/lerna publish ${{ github.event.inputs.release_type }} \
83+
${GITHUB_WORKSPACE}/node_modules/.bin/lerna version ${{ github.event.inputs.release_type }} \
8584
${{ (github.event.inputs.prerelease == 'true' && '--conventional-prerelease') || '--conventional-graduate' }} \
8685
--create-release github
8786
env:
8887
GH_TOKEN: ${{ secrets.ACCESS_TOKEN }}
8988
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
9089
NPM_CONFIG_PROVENANCE: true
9190

91+
- name: build
92+
run: yarn build
93+
94+
- name: publish to npm
95+
run: ${GITHUB_WORKSPACE}/node_modules/.bin/lerna publish from-git
96+
env:
97+
GH_TOKEN: ${{ secrets.ACCESS_TOKEN }}
98+
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
99+
NPM_CONFIG_PROVENANCE: true
100+
92101
### Semantic Release Bot comments for issues and PRs ###
93102
- name: Add release comments to issues and PRs
94103
uses: actions/github-script@v7

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ packages/cypress-commands/api.json
2424

2525
packages/main/scripts/wrapperGeneration/json
2626
packages/main/tmp
27+
packages/*/src/generated/
2728

2829
.nx/cache

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"start": "yarn setup && yarn create-cypress-commands-docs && npm-run-all -p start:watcher start:storybook",
1010
"start:watcher": "lerna run watch:css",
1111
"start:storybook": "storybook dev -p 6006",
12-
"setup": "lerna run build:i18n && lerna run build:css && lerna run build:css-bundle && rimraf node_modules/@types/mocha",
12+
"setup": "lerna run build:i18n && lerna run build:css && lerna run build:css-bundle && lerna run build:version-info && rimraf node_modules/@types/mocha",
1313
"build": "yarn setup && tsc --build tsconfig.build.json && lerna run build:client && lerna run build:wrapper",
1414
"build:storybook": "yarn build && yarn create-cypress-commands-docs && storybook build -o .out",
1515
"build:storybook-sitemap": "node ./scripts/create-storybook-sitemap.js --directory .out",

packages/base/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"license": "Apache-2.0",
2727
"sideEffects": false,
2828
"scripts": {
29-
"clean": "rimraf dist"
29+
"clean": "rimraf dist",
30+
"build:version-info": "node ../../scripts/generate-version-info.js"
3031
},
3132
"peerDependencies": {
3233
"@types/react": "*",

packages/base/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Device from './Device/index.js';
2+
import VersionInfo from './generated/VersionInfo.js';
23
import * as hooks from './hooks/index.js';
34
import { I18nStore } from './stores/I18nStore.js';
45
import { StyleStore } from './stores/StyleStore.js';
@@ -9,3 +10,4 @@ export * from './utils/index.js';
910
export * from './hooks/index.js';
1011

1112
export { I18nStore, StyleStore, ThemingParameters, Device, hooks };
13+
export const version = VersionInfo.version;

packages/charts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"clean": "rimraf dist",
3131
"build:css": "postcss --base src --dir dist/css src/**/*.css",
3232
"build:css-bundle": "node ../../config/merge-css-modules.js",
33+
"build:version-info": "node ../../scripts/generate-version-info.js",
3334
"watch:css": "yarn build:css --watch"
3435
},
3536
"dependencies": {

packages/charts/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { ScatterChart } from './components/ScatterChart/ScatterChart.js';
2020
import { TimelineChartPlaceholder } from './components/TimelineChart/Placeholder.js';
2121
import { TimelineChart } from './components/TimelineChart/TimelineChart.js';
2222
import { TimelineChartAnnotation } from './components/TimelineChart/TimelineChartAnnotation.js';
23+
import VersionInfo from './generated/VersionInfo.js';
2324

2425
export {
2526
BarChart,
@@ -45,3 +46,4 @@ export {
4546
ColumnChartWithTrendPlaceholder,
4647
TimelineChartPlaceholder
4748
};
49+
export const version = VersionInfo.version;

packages/compat/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"clean": "rimraf dist tmp",
3131
"build:css": "postcss --base src --dir dist/css src/**/*.css",
3232
"build:css-bundle": "node ../../config/merge-css-modules.js",
33+
"build:version-info": "node ../../scripts/generate-version-info.js",
3334
"watch:css": "yarn build:css --watch"
3435
},
3536
"dependencies": {

packages/compat/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import VersionInfo from './generated/VersionInfo.js';
2+
13
export * from './components/Loader/index.js';
24
export * from './components/OverflowToolbarButton/index.js';
35
export * from './components/OverflowToolbarToggleButton/index.js';
@@ -13,3 +15,4 @@ export * from './components/ToolbarSpacer/index.js';
1315
export { LoaderType } from './enums/LoaderType.js';
1416
export { ToolbarDesign } from './enums/ToolbarDesign.js';
1517
export { ToolbarStyle } from './enums/ToolbarStyle.js';
18+
export const version = VersionInfo.version;

packages/main/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"build:wrapper": "babel src --out-dir wrappers --extensions .ts,.tsx --env-name wrapper && tsc --project tsconfig.build.json --declarationDir wrappers",
4545
"build:css": "postcss --base src --dir dist/css src/**/*.css",
4646
"build:css-bundle": "node ../../config/merge-css-modules.js",
47+
"build:version-info": "node ../../scripts/generate-version-info.js",
4748
"watch:css": "yarn build:css --watch"
4849
},
4950
"dependencies": {

packages/main/src/components/ThemeProvider/index.tsx

+3-13
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,11 @@ import { attachThemeLoaded, detachThemeLoaded } from '@ui5/webcomponents-base/di
88
import { I18nStore, StyleStore, useIsomorphicLayoutEffect, useStylesheet } from '@ui5/webcomponents-react-base';
99
import type { FC, ReactNode } from 'react';
1010
import { useEffect, useId } from 'react';
11-
import pkgJson from '../../../package.json';
12-
import { parseSemVer } from '../../internal/utils.js';
11+
import VersionInfo from '../../generated/VersionInfo.js';
1312
import { styleData } from './ThemeProvider.css.js';
1413

15-
let _versionInfo = null;
1614
let _versionInfoInjected = false;
1715

18-
function getVersionInfo() {
19-
if (!_versionInfo) {
20-
_versionInfo = parseSemVer(pkgJson.version);
21-
}
22-
return _versionInfo;
23-
}
24-
2516
function ThemeProviderStyles() {
2617
const uniqueId = useId();
2718
useStylesheet(styleData, `${ThemeProvider.displayName}-${uniqueId}`);
@@ -95,15 +86,14 @@ const ThemeProvider: FC<ThemeProviderPropTypes> = (props: ThemeProviderPropTypes
9586
if (_versionInfoInjected) {
9687
return;
9788
}
98-
const versionInfo = getVersionInfo();
9989
globalThis['@ui5/webcomponents-react'] ??= {};
10090
globalThis['@ui5/webcomponents-react'].Runtimes ??= [];
10191

102-
globalThis['@ui5/webcomponents-react'].Runtimes.push(versionInfo);
92+
globalThis['@ui5/webcomponents-react'].Runtimes.push(VersionInfo);
10393
_versionInfoInjected = true;
10494
return () => {
10595
globalThis['@ui5/webcomponents-react'].Runtimes = globalThis['@ui5/webcomponents-react'].Runtimes.filter(
106-
(info) => info !== versionInfo
96+
(info) => info !== VersionInfo
10797
);
10898
_versionInfoInjected = false;
10999
};

packages/main/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as AnalyticalTableHooks from './components/AnalyticalTable/pluginHooks/AnalyticalTableHooks.js';
2+
import VersionInfo from './generated/VersionInfo.js';
23

34
export * from './components/ActionSheet/index.js';
45
export * from './components/AnalyticalCardHeader/index.js';
@@ -37,3 +38,4 @@ export type { CommonProps, Ui5CustomEvent, Ui5DomRef, UI5WCSlotsNode, Nullable }
3738
export * from './webComponents/index.js';
3839

3940
export { AnalyticalTableHooks };
41+
export const version = VersionInfo.version;

scripts/generate-version-info.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import fs from 'fs/promises';
2+
3+
const generate = async () => {
4+
const version = JSON.parse(await fs.readFile('package.json')).version;
5+
6+
// Parse version
7+
const matches = version.match(/^([0-9]+)\.([0-9]+)\.([0-9]+)(.*)$/);
8+
if (!matches) {
9+
throw new Error('Unsupported version format');
10+
}
11+
12+
const isNext = version.match(/[a-f0-9]{9}$/);
13+
const buildTime = Math.floor(new Date().getTime() / 1000);
14+
15+
const fileContent = `const VersionInfo = {
16+
version: '${version}',
17+
major: ${matches[1]},
18+
minor: ${matches[2]},
19+
patch: ${matches[3]},
20+
suffix: '${matches[4]}',
21+
isNext: ${isNext ? 'true' : 'false'},
22+
buildTime: ${buildTime}
23+
};
24+
25+
export default VersionInfo;
26+
`;
27+
28+
await fs.mkdir('src/generated/', { recursive: true });
29+
await fs.writeFile('src/generated/VersionInfo.ts', fileContent);
30+
};
31+
32+
generate().then(() => {
33+
console.log('Version info file generated.');
34+
});

0 commit comments

Comments
 (0)