Skip to content

Commit

Permalink
Improve injection of build info into the UI (#2828)
Browse files Browse the repository at this point in the history
Fixes #2814.
Follow-up for #2796.

Signed-off-by: Florian Hotze <[email protected]>
  • Loading branch information
florian-h05 authored Oct 24, 2024
1 parent 96f3810 commit 331f794
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 128 deletions.
20 changes: 2 additions & 18 deletions bundles/org.openhab.ui/web/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,17 @@ const webpack = require('webpack');
const ora = require('ora');
const rm = require('rimraf').rimraf;
const chalk = require('chalk');
const replaceInFile = require('replace-in-file')

const config = require('./webpack.config.js');

const env = process.env.NODE_ENV || 'development';
const target = process.env.TARGET || 'web';
const timestamp = new Date().toISOString().slice(0, 16).replaceAll(/[T:-]/g, "");
let version = process.argv[2] || timestamp;
if (version.endsWith('SNAPSHOT')) version += '-' + timestamp;

const spinner = ora(env === 'production' ? chalk.cyan('Building for production...') : chalk.cyan('Building development version...'));
spinner.start();

exec('git rev-parse --short HEAD').then((result) => {
return Promise.resolve(result.stdout.trim());
}).then((commit) => {
const versionReplace = {
files: './src/components/app.vue',
from: /%VERSION%/g,
to: version
}
const commitReplace = {
files: './src/js/store/index.js',
from: /%GIT_COMMIT_HASH%/g,
to: commit || ''
}
return Promise.all([rm('./www/'), replaceInFile(versionReplace), replaceInFile(commitReplace)]);
exec(`npm run generate-build-info ${process.argv[2]}`).then(() => {
return rm('./www/')
}).then(() => {
webpack(config, (err, stats) => {
if (err) throw err;
Expand Down
27 changes: 27 additions & 0 deletions bundles/org.openhab.ui/web/build/generate-build-info.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import util from 'node:util'
import { exec } from 'node:child_process'
import fs from 'node:fs'

const promisifiedExec = util.promisify(exec);

const env = process.env.NODE_ENV || 'development';

const timestamp = new Date().toISOString().slice(0, 16).replaceAll(/[T:-]/g, "");
let version = process.argv[2] || (env === 'production' ? timestamp : 'development');
if (version.endsWith('SNAPSHOT')) version += '-' + timestamp;

promisifiedExec('git rev-parse --short HEAD').then((result) => {
return Promise.resolve(result.stdout.trim());
}).then((commit) => {
if (env === 'development') commit = 'development'
const content = `export default {
version: '${version} ', // App version
commit: '${commit || ''}' // UI commit hash
}
`

fs.writeFileSync('./src/assets/build-info.js', content, {
encoding: 'utf-8',
flag: 'w'
})
})
107 changes: 0 additions & 107 deletions bundles/org.openhab.ui/web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion bundles/org.openhab.ui/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
"bundler": "webpack"
},
"scripts": {
"generate-build-info": "node build/generate-build-info.mjs",
"build-prod": "cross-env NODE_ENV=production node ./build/build.js",
"webpack-analyzer": "cross-env NODE_ENV=production cross-env WEBPACK_ANALYZER=1 node ./build/build.js",
"webpack-analyzer-report": "cross-env NODE_ENV=production cross-env WEBPACK_ANALYZER=1 WEBPACK_ANALYZER_REPORT=1 node ./build/build.js",
"webpack-analyzer-report-stats": "cross-env NODE_ENV=production cross-env WEBPACK_ANALYZER=1 WEBPACK_ANALYZER_REPORT=1 WEBPACK_ANALYZER_REPORT_STATS=1 node ./build/build.js",
"predev:blockly": "cross-env NODE_ENV=development npm run generate-build-info",
"dev:blockly": "cross-env SOURCE_MAPS=1 NODE_ENV=development webpack-dev-server --config ./build/webpack.config.js",
"predev": "cross-env NODE_ENV=development npm run generate-build-info",
"dev": "cross-env NODE_ENV=development webpack-dev-server --config ./build/webpack.config.js",
"start": "npm run dev",
"lint": "npx eslint --ext js --ext vue src",
Expand Down Expand Up @@ -149,7 +152,6 @@
"postcss-loader": "^8.1.1",
"postcss-preset-env": "^10.0.0",
"process": "^0.11.10",
"replace-in-file": "^7.2.0",
"rimraf": "^6.0.1",
"standard": "^17.1.0",
"style-loader": "^4.0.0",
Expand Down
1 change: 1 addition & 0 deletions bundles/org.openhab.ui/web/src/assets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build-info.js
4 changes: 3 additions & 1 deletion bundles/org.openhab.ui/web/src/components/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@
<script>
import Framework7 from 'framework7/framework7-lite.esm.bundle.js'
import buildInfo from '@/assets/build-info'
import routes from '@/js/routes.js'
import PanelRight from '@/pages/panel-right.vue'
import EmptyStatePlaceholder from '@/components/empty-state-placeholder.vue'
Expand Down Expand Up @@ -297,7 +299,7 @@ export default {
f7params: {
id: 'org.openhab.ui', // App bundle ID
name: 'openHAB', // App name
version: '%VERSION%', // App version, replaced during production build
version: buildInfo.version, // App version
theme: theme || 'auto',
// theme: (document.documentURI && document.documentURI.indexOf('?theme=ios') > 0) ? 'ios'
// : (document.documentURI && document.documentURI.indexOf('?theme=md') > 0) ? 'md'
Expand Down
4 changes: 3 additions & 1 deletion bundles/org.openhab.ui/web/src/js/store/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Vue from 'vue'
import Vuex from 'vuex'

import buildInfo from '@/assets/build-info'

import components from './modules/components'
import model from './modules/model'
import states from './modules/states'
Expand All @@ -27,7 +29,7 @@ const store = new Vuex.Store({
locale: null,
runtimeInfo: null,
uiInfo: {
commit: '%GIT_COMMIT_HASH%' // replaced during production build
commit: buildInfo.commit
},
websiteUrl: null,
developerDock: false,
Expand Down

0 comments on commit 331f794

Please sign in to comment.