|
| 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; |
0 commit comments