From c8afda6809db70bc3a45e75d5a80e25ba5155312 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Thu, 3 Oct 2024 21:11:46 +0200 Subject: [PATCH] Add version and commit to UI during production build This sets the version in the Framework7 parameters to the build version during production build, and adds the commit hash to the Vuex store. Signed-off-by: Florian Hotze --- bundles/org.openhab.ui/pom.xml | 2 +- bundles/org.openhab.ui/web/build/build.js | 23 +++- bundles/org.openhab.ui/web/package-lock.json | 107 ++++++++++++++++++ bundles/org.openhab.ui/web/package.json | 1 + .../org.openhab.ui/web/src/components/app.vue | 2 +- .../org.openhab.ui/web/src/js/store/index.js | 3 + .../org.openhab.ui/web/src/pages/about.vue | 6 +- 7 files changed, 140 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.ui/pom.xml b/bundles/org.openhab.ui/pom.xml index cc4dec4329..6d1e33bac5 100644 --- a/bundles/org.openhab.ui/pom.xml +++ b/bundles/org.openhab.ui/pom.xml @@ -67,7 +67,7 @@ npm - run build-prod + run build-prod ${project.version} diff --git a/bundles/org.openhab.ui/web/build/build.js b/bundles/org.openhab.ui/web/build/build.js index c2ce8d4580..8b6f436354 100644 --- a/bundles/org.openhab.ui/web/build/build.js +++ b/bundles/org.openhab.ui/web/build/build.js @@ -1,16 +1,37 @@ +const util = require('node:util'); +const exec = util.promisify(require('node:child_process').exec); + 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'; +let version = process.argv[2]; +if (version.endsWith('SNAPSHOT')) version += '-' + new Date().toISOString().slice(0, 16).replaceAll(/[T:-]/g, ""); const spinner = ora(env === 'production' ? chalk.cyan('Building for production...') : chalk.cyan('Building development version...')); spinner.start(); -rm('./www/').then(() => { +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 || 'unknown' + } + return Promise.all([rm('./www/'), replaceInFile(versionReplace), replaceInFile(commitReplace)]); +}).then(() => { webpack(config, (err, stats) => { if (err) throw err; spinner.stop(); diff --git a/bundles/org.openhab.ui/web/package-lock.json b/bundles/org.openhab.ui/web/package-lock.json index 9bc863cc82..a3e276b04e 100644 --- a/bundles/org.openhab.ui/web/package-lock.json +++ b/bundles/org.openhab.ui/web/package-lock.json @@ -111,6 +111,7 @@ "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", @@ -18740,6 +18741,68 @@ "node": ">=0.10.0" } }, + "node_modules/replace-in-file": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/replace-in-file/-/replace-in-file-7.2.0.tgz", + "integrity": "sha512-CiLXVop3o8/h2Kd1PwKPPimmS9wUV0Ki6Fl8+1ITD35nB3Gl/PrW5IONpTE0AXk0z4v8WYcpEpdeZqMXvSnWpg==", + "dev": true, + "license": "MIT", + "dependencies": { + "chalk": "^4.1.2", + "glob": "^8.1.0", + "yargs": "^17.7.2" + }, + "bin": { + "replace-in-file": "bin/cli.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/replace-in-file/node_modules/brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, + "license": "MIT", + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/replace-in-file/node_modules/glob": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-8.1.0.tgz", + "integrity": "sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ==", + "deprecated": "Glob versions prior to v9 are no longer supported", + "dev": true, + "license": "ISC", + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^5.0.1", + "once": "^1.3.0" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/replace-in-file/node_modules/minimatch": { + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", + "integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==", + "dev": true, + "license": "ISC", + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/request": { "version": "2.88.0", "resolved": "https://registry.npmjs.org/request/-/request-2.88.0.tgz", @@ -35906,6 +35969,50 @@ "is-finite": "^1.0.0" } }, + "replace-in-file": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/replace-in-file/-/replace-in-file-7.2.0.tgz", + "integrity": "sha512-CiLXVop3o8/h2Kd1PwKPPimmS9wUV0Ki6Fl8+1ITD35nB3Gl/PrW5IONpTE0AXk0z4v8WYcpEpdeZqMXvSnWpg==", + "dev": true, + "requires": { + "chalk": "^4.1.2", + "glob": "^8.1.0", + "yargs": "^17.7.2" + }, + "dependencies": { + "brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0" + } + }, + "glob": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-8.1.0.tgz", + "integrity": "sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ==", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^5.0.1", + "once": "^1.3.0" + } + }, + "minimatch": { + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", + "integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==", + "dev": true, + "requires": { + "brace-expansion": "^2.0.1" + } + } + } + }, "request": { "version": "2.88.0", "resolved": "https://registry.npmjs.org/request/-/request-2.88.0.tgz", diff --git a/bundles/org.openhab.ui/web/package.json b/bundles/org.openhab.ui/web/package.json index ef08aa7a02..ae633a75ed 100644 --- a/bundles/org.openhab.ui/web/package.json +++ b/bundles/org.openhab.ui/web/package.json @@ -149,6 +149,7 @@ "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", diff --git a/bundles/org.openhab.ui/web/src/components/app.vue b/bundles/org.openhab.ui/web/src/components/app.vue index 5e258fea52..7233042bb6 100644 --- a/bundles/org.openhab.ui/web/src/components/app.vue +++ b/bundles/org.openhab.ui/web/src/components/app.vue @@ -297,7 +297,7 @@ export default { f7params: { id: 'org.openhab.ui', // App bundle ID name: 'openHAB', // App name - version: '3.0.0', // App version, TODO retrieve from the server (with the build information) + version: '%VERSION%', // App version, replaced during production build theme: theme || 'auto', // theme: (document.documentURI && document.documentURI.indexOf('?theme=ios') > 0) ? 'ios' // : (document.documentURI && document.documentURI.indexOf('?theme=md') > 0) ? 'md' diff --git a/bundles/org.openhab.ui/web/src/js/store/index.js b/bundles/org.openhab.ui/web/src/js/store/index.js index 0631b320a3..7f27998789 100644 --- a/bundles/org.openhab.ui/web/src/js/store/index.js +++ b/bundles/org.openhab.ui/web/src/js/store/index.js @@ -26,6 +26,9 @@ const store = new Vuex.Store({ apiEndpoints: null, locale: null, runtimeInfo: null, + uiInfo: { + commit: '%GIT_COMMIT_HASH%' // replaced during production build + }, websiteUrl: null, developerDock: false, pagePath: null diff --git a/bundles/org.openhab.ui/web/src/pages/about.vue b/bundles/org.openhab.ui/web/src/pages/about.vue index 95a86525ea..1810a5e217 100644 --- a/bundles/org.openhab.ui/web/src/pages/about.vue +++ b/bundles/org.openhab.ui/web/src/pages/about.vue @@ -12,8 +12,12 @@

- openHAB {{ $store.state.runtimeInfo.version }}
{{ $store.state.runtimeInfo.buildString }} + openHAB {{ $store.state.runtimeInfo.version }}
+ {{ $store.state.runtimeInfo.buildString }}

+

+ Main UI Commit {{ $store.state.uiInfo.commit }} +