cadb
Version:
安卓/鸿蒙系统截图/录屏工具
196 lines (178 loc) • 5.33 kB
JavaScript
/**
* Created by Niki on 2025/5/7 20:35.
* Email: m13296644326@163.com
*/
const {
logRed,
shellConfigs,
logGreen,
ctripHarmony,
ztripHarmony,
} = require('../common/constants');
const path = require('path');
const {doProjectSyncNow, buildHar2} = require('../common/utils/harBuildUtil');
const fs = require('fs');
const {loadOrCollectProjectPathBatch, processFile} = require('../common/utils');
const JSON5 = require('json5');
const {execSync} = require('child_process');
const libraryName = 'ctmultinavigation';
const projectPath = '/Users/likai/fromGithub/ohos_multi_navigation';
const libraryHarDir = path.join(
projectPath,
libraryName,
'build/default/outputs/default',
);
const libraryHarPath = path.join(libraryHarDir, `${libraryName}.har`);
const libraryPackageJsonPath = path.join(
projectPath,
libraryName,
'oh-package.json5',
);
let ctripHarmonyPath = '';
let ztripHarmonyPath = '';
async function run() {
// console.log(incrementVersion('0.0.99'));
const newVersion = upgradeLbVersion();
const harInfo = await doBuildHar(newVersion);
if (!harInfo) {
return;
}
await copyHar2Project(harInfo);
await modifyPackageJsonAndInstall(harInfo);
}
async function doBuildHar(newVersion) {
if (fs.existsSync(libraryHarDir)) {
fs.rmdirSync(libraryHarDir, {recursive: true});
}
const success = await buildHar2(projectPath, libraryName);
if (!success) {
logRed('编译失败');
return null;
}
if (!fs.existsSync(libraryHarPath)) {
logRed(`${libraryHarPath} 不存在`);
return null;
}
logGreen('编译成功');
const harName = `${libraryName}-${newVersion}.har`;
const newPath = path.join(libraryHarDir, harName);
fs.renameSync(libraryHarPath, newPath);
return {
harName,
harPath: newPath,
};
}
async function copyHar2Project(harInfo) {
await loadLocalProjectPath();
const ctripDestDir = path.join(ctripHarmonyPath, 'libs');
const ztripDestDir = path.join(ztripHarmonyPath, 'libs');
deleteOldHar(ctripDestDir);
deleteOldHar(ztripDestDir);
moveHarToDest(harInfo, ctripDestDir);
moveHarToDest(harInfo, ztripDestDir);
}
function deleteOldHar(destDir) {
const oldHarList = fs.readdirSync(destDir).filter(fn => {
return fn.startsWith(libraryName) && fn.endsWith('.har');
});
oldHarList.forEach(fn => {
const filePath = path.join(destDir, fn);
fs.rmSync(filePath);
});
}
function moveHarToDest(harInfo, destDir) {
const {harName, harPath} = harInfo;
const destPath = path.join(destDir, harName);
if (fs.existsSync(destPath)) {
fs.rmSync(destPath);
}
fs.copyFileSync(harPath, destPath);
}
async function modifyPackageJsonAndInstall(harInfo) {
const ctripHarPath = path.join(ctripHarmonyPath, 'libs', harInfo.harName);
const ztripHarPath = path.join(ztripHarmonyPath, 'libs', harInfo.harName);
const ctripPackageJsonPath = path.join(ctripHarmonyPath, 'oh-package.json5');
const ztripPackageJsonPath = path.join(ztripHarmonyPath, 'oh-package.json5');
const ctripSuccess = await doModify(ctripHarPath, ctripPackageJsonPath);
const ztripSuccess = await doModify(ztripHarPath, ztripPackageJsonPath);
if (ctripSuccess) {
await doProjectSyncNow(ctripHarmonyPath);
}
if (ztripSuccess) {
await doProjectSyncNow(ztripHarmonyPath);
}
execSync('git add .', {
...shellConfigs,
cwd: ctripHarmonyPath,
});
execSync('git add .', {
...shellConfigs,
cwd: ztripHarmonyPath,
});
}
async function doModify(harPath, pkgJsonPath) {
let relativePath = path.relative(pkgJsonPath, harPath);
if (relativePath.startsWith('..')) {
relativePath = relativePath.replace('.', '');
}
let success = false;
await processFile(pkgJsonPath, (line = '') => {
const trimLine = line.trim();
if (
!trimLine.startsWith('//') &&
trimLine.includes(libraryName) &&
trimLine.includes('file:') &&
trimLine.includes('.har')
) {
const left = line.split(trimLine)[0];
console.log(`oh-package.json5匹配${libraryName}成功`);
success = true;
return `${left}"${libraryName}": "file:${relativePath}",`;
}
return line;
});
if (!success) {
logRed(`${pkgJsonPath} 中匹配${libraryName}失败`);
}
return success;
}
function upgradeLbVersion() {
const packageInfo = JSON5.parse(
fs.readFileSync(libraryPackageJsonPath, 'utf-8'),
);
const newVersion = incrementVersion(packageInfo.version);
packageInfo.version = newVersion;
fs.writeFileSync(
libraryPackageJsonPath,
JSON5.stringify(packageInfo, null, 2),
);
return newVersion;
}
function incrementVersion(version) {
// 将版本号字符串分割为数组
let parts = version.split('.').map(Number);
// 从最右边的部分开始递增
for (let i = parts.length - 1; i >= 0; i--) {
if (parts[i] < 99) {
parts[i] += 1;
break;
} else {
parts[i] = 0;
if (i === 0) {
throw new Error('Version number overflow');
}
}
}
// 将数组重新组合为字符串
return parts.join('.');
}
async function loadLocalProjectPath(args) {
const {noCache = false} = args || {};
const pathList = await loadOrCollectProjectPathBatch(
[ctripHarmony, ztripHarmony],
noCache,
);
ctripHarmonyPath = pathList[0];
ztripHarmonyPath = pathList[1];
}
run();