Skip to content

Commit 2c441ba

Browse files
authored
Refactor/add check build status cli (#1220)
* feat(cli): add check build status cli * feat(common): fix banner timezone issue * feat(cli): update version * feat(cli): fix iquery issue & add gen-version in cli * feat(cli): add log * feat(cli): add changeline replacement * feat(cli): upgrade version * fix(cli): fix no version.json issue * feat(cli): upgrade version
1 parent 8a44442 commit 2c441ba

9 files changed

+153
-113
lines changed

cli/bin/erda.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import init from '../lib/init';
2424
import i18n from '../lib/i18n';
2525
import generateService from '../lib/service-generator';
2626
import checkCliVersion from '../lib/check-cli-version';
27-
import comparisonBuild from '../lib/comparison-build';
27+
import checkBuildStatus from '../lib/check-build-status';
2828

2929
const program = new Command();
3030

@@ -119,15 +119,14 @@ program
119119
});
120120

121121
program
122-
.command('comparison-build')
122+
.command('check-build-status')
123123
.description(
124-
'bundle all erda ui modules to public directory, compare git commit sha with previous build, if match it will skip build and reuse last built files. Only used in pipeline build',
124+
'compare git commit sha with previous build, if match it will skip build and reuse last built files. Only used in pipeline build',
125125
)
126126
.option('-s, --skip', 'skip the cli version check')
127-
.option('-m, --module', 'module name to build')
128127
.action(async (options) => {
129128
await checkCliVersion(options);
130-
comparisonBuild(options);
129+
checkBuildStatus();
131130
});
132131

133132
program.parse(process.argv);

cli/lib/check-build-status.ts

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright (c) 2021 Terminus, Inc.
3+
*
4+
* This program is free software: you can use, redistribute, and/or modify
5+
* it under the terms of the GNU Affero General Public License, version 3
6+
* or later ("AGPL"), as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful, but WITHOUT
9+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10+
* FITNESS FOR A PARTICULAR PURPOSE.
11+
*
12+
* You should have received a copy of the GNU Affero General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
16+
/* eslint-disable no-await-in-loop */
17+
import { promisify } from 'util';
18+
import child_process from 'child_process';
19+
import fs from 'fs';
20+
import path from 'path';
21+
import { EOL } from 'os';
22+
import { logInfo, logSuccess, logWarn, logError } from './util/log';
23+
24+
const asyncExec = promisify(child_process.exec);
25+
26+
const externalModules = ['fdp', 'admin'];
27+
const subModules = ['market', 'uc'];
28+
const allModules = ['shell', 'core', 'admin', 'fdp', 'market', 'uc'];
29+
30+
const checkBuildStatus = async () => {
31+
const cwd = process.cwd();
32+
const enterprisePath = path.resolve(cwd, 'erda-ui-enterprise');
33+
const requireBuildMap = allModules.reduce<{ [k: string]: string }>((acc, module) => {
34+
acc[`skip-${module}-build`] = 'true';
35+
return acc;
36+
}, {});
37+
38+
for (const moduleName of allModules) {
39+
const staticPath = path.join(cwd, `public/static/${moduleName}`);
40+
logInfo('-----------------------------------------');
41+
logInfo('Looking for build cache at:', staticPath);
42+
logInfo(`Start compare diff for ${moduleName}`);
43+
const gitCwd = externalModules.includes(moduleName) ? enterprisePath : cwd;
44+
let { stdout: headSha } = await asyncExec('git rev-parse --short HEAD', { cwd: gitCwd });
45+
headSha = headSha.replace(/\n/, '');
46+
let { stdout: externalHeadSha } = await asyncExec('git rev-parse --short HEAD', {
47+
cwd: enterprisePath,
48+
});
49+
externalHeadSha = externalHeadSha.replace(/\n/, '');
50+
const skipKey = `skip-${moduleName}-build`;
51+
52+
try {
53+
const gitVersionPath = path.resolve(staticPath, '.git-version');
54+
let prevGitSha = null;
55+
let skipBuild = true;
56+
let nextSha = headSha;
57+
if (fs.existsSync(gitVersionPath)) {
58+
prevGitSha = fs.readFileSync(gitVersionPath, { encoding: 'utf8' }).replace('\n', '');
59+
logInfo('Found previous git sha:', prevGitSha);
60+
const [prevSha, prevExternalSha] = prevGitSha.split('/');
61+
const { stdout: diff } = await asyncExec(`git diff --name-only ${prevSha} ${headSha}`, { cwd: gitCwd });
62+
63+
const fileRegex = new RegExp(
64+
`(^${subModules.includes(moduleName) ? `modules/${moduleName}` : moduleName}/.*)`,
65+
'gm',
66+
);
67+
let match = fileRegex.exec(diff);
68+
if (match) {
69+
logInfo('File diff:');
70+
skipBuild = false;
71+
}
72+
while (match) {
73+
logInfo(match[1]);
74+
match = fileRegex.exec(diff);
75+
}
76+
77+
if (moduleName === 'shell') {
78+
logWarn('In case shell has part of code maintained under enterprise, need to check enterprise code base');
79+
nextSha = `${headSha}/${externalHeadSha}`;
80+
const { stdout: externalDiff } = await asyncExec(
81+
`git diff --name-only ${prevExternalSha} ${externalHeadSha}`,
82+
{ cwd: enterprisePath },
83+
);
84+
if (new RegExp('^cmp/', 'gm').test(externalDiff) || new RegExp('^msp/', 'gm').test(externalDiff)) {
85+
logWarn(
86+
`Diff detected in enterprise for shell between ${prevExternalSha} and ${externalHeadSha}`,
87+
externalDiff,
88+
);
89+
skipBuild = false;
90+
} else {
91+
logInfo('No diff found for enterprise part of shell');
92+
}
93+
}
94+
}
95+
96+
if (skipBuild) {
97+
logInfo(`no change detected between ${prevGitSha} and ${headSha}. will skip build ${moduleName}`);
98+
} else {
99+
logWarn(`Diff detected between ${prevGitSha} and ${headSha}, will start new built for ${moduleName}`);
100+
requireBuildMap[skipKey] = nextSha;
101+
}
102+
} catch (error) {
103+
logError(`Compare diff failed for ${moduleName}`, error.toString());
104+
requireBuildMap[skipKey] = moduleName === 'shell' ? `${headSha}/${externalHeadSha}` : headSha;
105+
}
106+
}
107+
108+
const metaPath = process.env.METAFILE;
109+
Object.keys(requireBuildMap).forEach((moduleKey) => {
110+
fs.appendFile(metaPath!, `${moduleKey}=${requireBuildMap[moduleKey]}${EOL}`, (err) => {
111+
if (err) {
112+
logError('err', err);
113+
}
114+
logSuccess(`emit ${moduleKey} value ${requireBuildMap[moduleKey]} to METAFILE`);
115+
});
116+
});
117+
118+
const data = { version: Date.parse(new Date().toString()) };
119+
120+
// generate version.json
121+
const publicPath = path.resolve(process.cwd(), 'public');
122+
fs.mkdir(publicPath, '0777', () => {
123+
fs.writeFile(`${publicPath}/version.json`, JSON.stringify(data), (err) => {
124+
if (err) {
125+
logError('version.json generated fail', err);
126+
} else {
127+
logSuccess('version.json generated ok.');
128+
}
129+
});
130+
});
131+
};
132+
133+
export default checkBuildStatus;

cli/lib/comparison-build.ts

-90
This file was deleted.

cli/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@erda-ui/cli",
3-
"version": "1.0.17",
3+
"version": "1.0.23",
44
"description": "Command line interface for rapid Erda UI development",
55
"bin": {
66
"erda-ui": "dist/bin/erda.js"
@@ -10,7 +10,7 @@
1010
"build": "rm -rf ./dist && tsc --version && tsc --project .",
1111
"local": "npm run build && local-package-publisher -p",
1212
"remove-local": "local-package-publisher -u",
13-
"pub": "npm version patch && npm run build && npm publish",
13+
"pub": "npm run build && npm version patch && npm publish",
1414
"precommit": "lint-staged"
1515
},
1616
"repository": {
@@ -34,7 +34,7 @@
3434
"dayjs": "^1.10.4",
3535
"dotenv": "^8.2.0",
3636
"i18next-scanner": "^3.0.0",
37-
"inquirer": "^8.0.0",
37+
"inquirer": "8.0.0",
3838
"inquirer-select-directory": "^1.2.0",
3939
"js-base64": "^3.6.1",
4040
"lodash": "^4.17.21",

core/webpack.production.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ const MiniCssExtractPlugin = require('mini-css-extract-plugin');
1717
const os = require('os');
1818
const webpack = require('webpack');
1919
const GitRevisionPlugin = require('git-revision-webpack-plugin');
20-
const moment = require('moment');
2120

2221
const gitRevisionPlugin = new GitRevisionPlugin();
2322
const banner = `commit: ${gitRevisionPlugin.commithash().slice(0, 6)}
2423
branch: ${gitRevisionPlugin.branch()}
25-
buildTime: ${moment(Date.now()).format('YYYY-MM-DD HH:mm:ss')}
24+
buildTime: ${new Date().toLocaleString('zh-CH', { timeZone: 'Asia/Shanghai' })}
2625
buildBy: ${os.userInfo().username}`;
2726

2827
module.exports = {

cspell.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@
125125
"JDBC",
126126
"jsjl",
127127
"Jupyter",
128-
"JVMs",
129128
"jvms",
129+
"JVMs",
130130
"jxcx",
131131
"jzts",
132132
"keyframes",
@@ -150,6 +150,7 @@
150150
"mesos",
151151
"metadatas",
152152
"metadatum",
153+
"METAFILE",
153154
"methd",
154155
"middlewares",
155156
"mindmap",

erda.yml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ services:
2222
COLLECTOR_ADDR: "collector:7076"
2323
COLLECTOR_PUBLIC_ADDR: "collector:7076"
2424
SCHEDULER_ADDR: "scheduler:9091"
25+
FDP_UI_ADDR: "fdp-ui:80"
2526
resources:
2627
cpu: ${request_cpu:0.2}
2728
max_cpu: 0.2

0 commit comments

Comments
 (0)