111
This commit is contained in:
56
src-tauri/scripts/bump-version.cjs
Normal file
56
src-tauri/scripts/bump-version.cjs
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* 构建前将版本号 patch 段 +1,并同步写入 tauri.conf.json / Cargo.toml / package.json。
|
||||
*/
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
|
||||
const tauriDir = path.join(__dirname, "..");
|
||||
const rootDir = path.join(tauriDir, "..");
|
||||
|
||||
const tauriConfPath = path.join(tauriDir, "tauri.conf.json");
|
||||
const cargoTomlPath = path.join(tauriDir, "Cargo.toml");
|
||||
const packageJsonPath = path.join(rootDir, "package.json");
|
||||
|
||||
function bumpPatch(version) {
|
||||
const parts = String(version)
|
||||
.trim()
|
||||
.split(".")
|
||||
.map((p) => Number.parseInt(p, 10));
|
||||
if (parts.length === 0 || parts.some(Number.isNaN)) {
|
||||
throw new Error(`无法解析版本号: ${version}`);
|
||||
}
|
||||
parts[parts.length - 1] += 1;
|
||||
return parts.join(".");
|
||||
}
|
||||
|
||||
function updateJsonVersion(filePath, newVersion) {
|
||||
const data = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
||||
const oldVersion = data.version;
|
||||
data.version = newVersion;
|
||||
fs.writeFileSync(filePath, `${JSON.stringify(data, null, 2)}\n`, "utf8");
|
||||
return oldVersion;
|
||||
}
|
||||
|
||||
function updateCargoVersion(filePath, newVersion) {
|
||||
const content = fs.readFileSync(filePath, "utf8");
|
||||
const match = content.match(/^version\s*=\s*"([^"]+)"/m);
|
||||
if (!match) {
|
||||
throw new Error(`Cargo.toml 中未找到 version 字段: ${filePath}`);
|
||||
}
|
||||
const oldVersion = match[1];
|
||||
const next = content.replace(
|
||||
/^version\s*=\s*"[^"]+"/m,
|
||||
`version = "${newVersion}"`,
|
||||
);
|
||||
fs.writeFileSync(filePath, next, "utf8");
|
||||
return oldVersion;
|
||||
}
|
||||
|
||||
const conf = JSON.parse(fs.readFileSync(tauriConfPath, "utf8"));
|
||||
const newVersion = bumpPatch(conf.version);
|
||||
|
||||
updateJsonVersion(tauriConfPath, newVersion);
|
||||
updateJsonVersion(packageJsonPath, newVersion);
|
||||
updateCargoVersion(cargoTomlPath, newVersion);
|
||||
|
||||
console.log(`版本号已更新: ${conf.version} -> ${newVersion}`);
|
||||
@@ -6,6 +6,31 @@ const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const { execSync } = require("node:child_process");
|
||||
|
||||
const RELEASE_VERSION_URL =
|
||||
"http://81.71.163.140:8001/api/setreleaseversion";
|
||||
|
||||
async function notifyReleaseVersion(version) {
|
||||
const res = await fetch(RELEASE_VERSION_URL, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ version }),
|
||||
});
|
||||
const body = await res.text();
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}: ${body}`);
|
||||
}
|
||||
let parsed;
|
||||
try {
|
||||
parsed = JSON.parse(body);
|
||||
} catch {
|
||||
throw new Error(`响应不是 JSON: ${body}`);
|
||||
}
|
||||
if (parsed.ok === false) {
|
||||
throw new Error(parsed.message || "写入 releaseversion 失败");
|
||||
}
|
||||
console.log(`已同步 releaseversion 到服务器: ${version}`);
|
||||
}
|
||||
|
||||
const tauriDir = path.join(__dirname, "..");
|
||||
const conf = JSON.parse(
|
||||
fs.readFileSync(path.join(tauriDir, "tauri.conf.json"), "utf8"),
|
||||
@@ -45,6 +70,11 @@ const stagedExe =
|
||||
process.platform === "win32" ? `${productName}.exe` : productName;
|
||||
fs.copyFileSync(exePath, path.join(stageDir, stagedExe));
|
||||
|
||||
const updaterPath = path.join(releaseDir, "updater.exe");
|
||||
if (process.platform === "win32" && fs.existsSync(updaterPath)) {
|
||||
fs.copyFileSync(updaterPath, path.join(stageDir, "updater.exe"));
|
||||
}
|
||||
|
||||
for (const dir of ["resources", "binaries"]) {
|
||||
const src = path.join(releaseDir, dir);
|
||||
if (fs.existsSync(src)) {
|
||||
@@ -78,3 +108,8 @@ for (const dir of ["msi", "nsis"]) {
|
||||
}
|
||||
|
||||
console.log(`便携压缩包已生成: ${zipPath}`);
|
||||
|
||||
notifyReleaseVersion(version).catch((err) => {
|
||||
console.error(`同步 releaseversion 失败: ${err.message}`);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user