1、优化打包脚本,方便版本和日期识别
This commit is contained in:
shenzuqiang 2026-05-07 10:40:49 +08:00
parent 247e14703d
commit 3322ada07e
7 changed files with 40 additions and 17 deletions

View File

@ -44,12 +44,12 @@
"arkOptions": { "arkOptions": {
"buildProfileFields": { "buildProfileFields": {
"CHANNEL": "harmony", "CHANNEL": "harmony",
"BUILD_TIME": "2605070954" "BUILD_TIME": "2605071040"
} }
} }
}, },
"output": { "output": {
"artifactName": "rabbit_harmony_release_2605070954" "artifactName": "rabbit_harmony_release_v1.0.1_2605071040"
} }
} }
], ],

View File

@ -2,11 +2,11 @@
* Use these variables when you tailor your ArkTS code. They must be of the const type. * Use these variables when you tailor your ArkTS code. They must be of the const type.
*/ */
export const HAR_VERSION = '0.0.1'; export const HAR_VERSION = '0.0.1';
export const BUILD_MODE_NAME = 'release'; export const BUILD_MODE_NAME = 'debug';
export const DEBUG = false; export const DEBUG = true;
export const TARGET_NAME = 'default'; export const TARGET_NAME = 'default';
export const CHANNEL = 'harmony'; export const CHANNEL = 'harmony';
export const BUILD_TIME = '2605061515'; export const BUILD_TIME = '2605071039';
/** /**
* BuildProfile Class is used only for compatibility purposes. * BuildProfile Class is used only for compatibility purposes.

View File

@ -2,11 +2,11 @@
* Use these variables when you tailor your ArkTS code. They must be of the const type. * Use these variables when you tailor your ArkTS code. They must be of the const type.
*/ */
export const HAR_VERSION = '1.0.0'; export const HAR_VERSION = '1.0.0';
export const BUILD_MODE_NAME = 'release'; export const BUILD_MODE_NAME = 'debug';
export const DEBUG = false; export const DEBUG = true;
export const TARGET_NAME = 'default'; export const TARGET_NAME = 'default';
export const CHANNEL = 'harmony'; export const CHANNEL = 'harmony';
export const BUILD_TIME = '2605061515'; export const BUILD_TIME = '2605071039';
/** /**
* BuildProfile Class is used only for compatibility purposes. * BuildProfile Class is used only for compatibility purposes.

View File

@ -2,11 +2,11 @@
* Use these variables when you tailor your ArkTS code. They must be of the const type. * Use these variables when you tailor your ArkTS code. They must be of the const type.
*/ */
export const HAR_VERSION = '0.0.1'; export const HAR_VERSION = '0.0.1';
export const BUILD_MODE_NAME = 'release'; export const BUILD_MODE_NAME = 'debug';
export const DEBUG = false; export const DEBUG = true;
export const TARGET_NAME = 'default'; export const TARGET_NAME = 'default';
export const CHANNEL = 'harmony'; export const CHANNEL = 'harmony';
export const BUILD_TIME = '2605061515'; export const BUILD_TIME = '2605071039';
/** /**
* BuildProfile Class is used only for compatibility purposes. * BuildProfile Class is used only for compatibility purposes.

View File

@ -25,10 +25,20 @@ updateJsonTime();
function syncBuildFileNameByJson() { function syncBuildFileNameByJson() {
const filePath = path.resolve(__dirname, 'build-profile.json5'); const filePath = path.resolve(__dirname, 'build-profile.json5');
const appConfigPath = path.resolve(__dirname, 'AppScope/app.json5');
if (!fs.existsSync(filePath)) return; if (!fs.existsSync(filePath)) return;
try { try {
// 1. 读取并解析 JSON (JSON5 格式通常可以当作普通字符串解析) // 1. 获取版本号 (从 AppScope/app.json5 读取)
let versionName = '1.0.0';
if (fs.existsSync(appConfigPath)) {
const appContent = fs.readFileSync(appConfigPath, 'utf8');
const vMatch = appContent.match(/"versionName"\s*:\s*"([^"]+)"/);
if (vMatch) versionName = vMatch[1];
}
// 2. 读取并解析 build-profile.json5
const rawContent = fs.readFileSync(filePath, 'utf8'); const rawContent = fs.readFileSync(filePath, 'utf8');
// 使用正则提取关键信息,避免因 JSON5 语法导致的 JSON.parse 失败 // 使用正则提取关键信息,避免因 JSON5 语法导致的 JSON.parse 失败
@ -49,12 +59,14 @@ function syncBuildFileNameByJson() {
buildMode = isDebuggable ? 'debug' : 'release'; buildMode = isDebuggable ? 'debug' : 'release';
} }
// 2. 生成动态名称 // 3. 生成动态名称
const now = new Date(); const now = new Date();
const timeStr = `${now.getFullYear().toString().slice(-2)}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}`; const timeStr = `${now.getFullYear().toString().slice(-2)}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}`;
const newName = `rabbit_harmony_${buildMode}_${timeStr}`; // const newName = `rabbit_harmony_${buildMode}_${timeStr}`;
// 格式示例: rabbit_harmony_release_v1.0.0_2605062215
const newName = `rabbit_harmony_${buildMode}_v${versionName}_${timeStr}`;
// 3. 物理替换并写入 // 4. 物理替换并写入
const updatedArtifactName = rawContent.replace(/("artifactName"\s*:\s*")[^"]*(")/g, `$1${newName}$2`); const updatedArtifactName = rawContent.replace(/("artifactName"\s*:\s*")[^"]*(")/g, `$1${newName}$2`);
const updateBuildTimeContent = updatedArtifactName.replace(/("BUILD_TIME"\s*:\s*")[^"]*(")/g, `$1${timeStr}$2`); const updateBuildTimeContent = updatedArtifactName.replace(/("BUILD_TIME"\s*:\s*")[^"]*(")/g, `$1${timeStr}$2`);

View File

@ -26,7 +26,7 @@
{ {
"name": "default", "name": "default",
"output": { "output": {
"artifactName": "rabbit_harmony_2657954" "artifactName": "rabbit_harmony_v1.0.1_26571040"
} }
}, },
{ {

View File

@ -4,10 +4,21 @@ import path from 'path';
function updateArtifactName() { function updateArtifactName() {
const filePath = path.resolve(__dirname, 'build-profile.json5'); const filePath = path.resolve(__dirname, 'build-profile.json5');
const appConfigPath = path.resolve(__dirname, '..', '..', 'AppScope', 'app.json5');
if (fs.existsSync(filePath)) { if (fs.existsSync(filePath)) {
let versionName = '1.0.0';
if (fs.existsSync(appConfigPath)) {
const appContent = fs.readFileSync(appConfigPath, 'utf8');
const vMatch = appContent.match(/"versionName"\s*:\s*"([^"]+)"/);
if (vMatch) versionName = vMatch[1];
}
const now = new Date(); const now = new Date();
const timeStr = `${now.getFullYear().toString().slice(-2)}${now.getMonth() + 1}${now.getDate()}${now.getHours()}${now.getMinutes()}`; const timeStr = `${now.getFullYear().toString().slice(-2)}${now.getMonth() + 1}${now.getDate()}${now.getHours()}${now.getMinutes()}`;
const newArtifactName = `rabbit_harmony_${timeStr}`; // const newArtifactName = `rabbit_harmony_${timeStr}`;
// 格式示例: rabbit_harmony_v1.0.0_2605062215
const newArtifactName = `rabbit_harmony_v${versionName}_${timeStr}`;
let content = fs.readFileSync(filePath, 'utf8'); let content = fs.readFileSync(filePath, 'utf8');
// 使用正则表达式精准匹配 BUILD_TIME 后的值并替换,匹配模式: "BUILD_TIME": "任意内容" // 使用正则表达式精准匹配 BUILD_TIME 后的值并替换,匹配模式: "BUILD_TIME": "任意内容"