Skip to content

Commit 78de468

Browse files
optimize: 版本比对功能,能够解析临时版本号了。
1 parent 1328c79 commit 78de468

File tree

2 files changed

+196
-45
lines changed

2 files changed

+196
-45
lines changed

packages/gui/src/bridge/update/backend.js

+63-45
Original file line numberDiff line numberDiff line change
@@ -40,63 +40,81 @@ function downloadFile (uri, filePath, onProgress, onSuccess, onError) {
4040
}
4141

4242
function parseVersion (version) {
43-
const matched = version.match(/^v?(\d+\.\d+\.\d+)(.*)$/)
43+
const matched = version.match(/^v?(\d{1,2}\.\d{1,2}\.\d{1,3}(?:\.\d{1,2})?)(.*)$/)
4444
const versionArr = matched[1].split('.')
4545
return {
46-
major: Number.parseInt(versionArr[0]),
47-
minor: Number.parseInt(versionArr[1]),
48-
patch: Number.parseInt(versionArr[2]),
49-
suffix: matched[2],
46+
major: Number.parseInt(versionArr[0]), // 大版本
47+
minor: Number.parseInt(versionArr[1]), // 中版本
48+
patch: Number.parseInt(versionArr[2]), // 小版本
49+
temp: Number.parseInt(versionArr[3]) || 0, // 临时版本
50+
pre: matched[2], // 预发布版本号
5051
}
5152
}
5253

5354
/**
5455
* 比较版本号
5556
*
56-
* @param version 线上版本号
57-
* @param curVersion 当前版本号
57+
* @param onlineVersion 线上版本号
58+
* @param currentVersion 当前版本号
5859
* @returns {number} 比较线上版本号是否为更新版本,1=是|0=相等|-1=否|-99=出现异常,比较结果未知
5960
*/
60-
function isNewVersion (version, curVersion) {
61-
if (version === curVersion) {
61+
function isNewVersion (onlineVersion, currentVersion) {
62+
if (onlineVersion === currentVersion) {
6263
return 0
6364
}
6465

6566
try {
66-
const versionObj = parseVersion(version)
67-
const curVersionObj = parseVersion(curVersion)
67+
const versionObj = parseVersion(onlineVersion)
68+
const curVersionObj = parseVersion(currentVersion)
69+
70+
// 大版本
6871
if (versionObj.major > curVersionObj.major) {
69-
return 1 // 大版本号更大,为更新版本
72+
return 1 // 大版本号更大,为新版本,需要更新
73+
} else if (versionObj.major < curVersionObj.major) {
74+
return -1 // 大版本号更小,为旧版本,无需更新
7075
}
7176

72-
if (curVersionObj.major === versionObj.major) {
73-
if (versionObj.minor > curVersionObj.minor) {
74-
return 2 // 中版本号更大,为更新版本
75-
}
77+
// 中版本
78+
if (versionObj.minor > curVersionObj.minor) {
79+
return 2 // 中版本号更大,为新版本,需要更新
80+
} else if (versionObj.minor < curVersionObj.minor) {
81+
return -2 // 中版本号更小,为旧版本,无需更新
82+
}
7683

77-
if (curVersionObj.minor === versionObj.minor) {
78-
if (versionObj.patch > curVersionObj.patch) {
79-
return 3 // 小版本号更大,为更新版本
80-
}
84+
// 小版本
85+
if (versionObj.patch > curVersionObj.patch) {
86+
return 3 // 小版本号更大,为新版本,需要更新
87+
} else if (versionObj.patch < curVersionObj.patch) {
88+
return -3 // 小版本号更小,为旧版本,无需更新
89+
}
8190

82-
if (versionObj.patch === curVersionObj.patch) {
83-
if (versionObj.suffix && curVersionObj.suffix) {
84-
// 当两个后缀版本号都存在时,直接比较后缀版本号字符串的大小
85-
if (versionObj.suffix > curVersionObj.suffix) {
86-
return 41
87-
}
88-
} else if (!versionObj.suffix && curVersionObj.suffix) {
89-
// 线上版本号没有后缀版本号,说明为正式版本,为更新版本
90-
return 42
91-
}
92-
}
91+
// 临时版本号
92+
if (versionObj.temp > curVersionObj.temp) {
93+
return 4 // 临时版本号更大,为新版本,需要更新
94+
} else if (versionObj.temp < curVersionObj.temp) {
95+
return -4 // 临时版本号更小,为旧版本,无需更新
96+
}
97+
98+
// 预发布版本号
99+
if (versionObj.pre && curVersionObj.pre) {
100+
// 当两个后缀版本号都存在时,直接比较后缀版本号字符串的大小
101+
if (versionObj.pre > curVersionObj.pre) {
102+
return 51
103+
} else if (versionObj.pre < curVersionObj.pre) {
104+
return -51
93105
}
106+
} else if (!versionObj.pre && curVersionObj.pre) {
107+
// 线上版本号没有后缀版本号,说明为正式版本,为新版本,需要更新
108+
return 52
109+
} else if (versionObj.pre && !curVersionObj.pre) {
110+
return -52
111+
} else {
112+
return -53 // 相同版本,无需更新(一般不会出现,除非例如 `2.0.0` 和 `2.0.0.0` 进行比较)
94113
}
95114
} catch (e) {
96-
log.error(`比对版本失败,当前版本号:${curVersion},比对版本号${version}, error:`, e)
97-
return -99
115+
log.error(`比对版本失败,当前版本号:${currentVersion},线上版本号${onlineVersion}, error:`, e)
116+
return -99 // 比对异常
98117
}
99-
return -1
100118
}
101119

102120
/**
@@ -159,7 +177,7 @@ function updateHandle (app, api, win, beforeQuit, quit, log) {
159177
let data
160178
try {
161179
data = JSON.parse(body)
162-
} catch (e) {
180+
} catch {
163181
log.error('检查更新失败,github API返回数据格式不正确:', body)
164182
win.webContents.send('update', { key: 'error', action: 'checkForUpdate', error: '检查更新失败,github API返回数据格式不正确' })
165183
return
@@ -195,27 +213,27 @@ function updateHandle (app, api, win, beforeQuit, quit, log) {
195213
log.info('最近正式版本:', versionData.name)
196214

197215
// 获取版本号
198-
let version = versionData.name
199-
if (version.indexOf('v') === 0) {
200-
version = version.substring(1)
216+
let onlineVersion = versionData.name
217+
if (onlineVersion.indexOf('v') === 0) {
218+
onlineVersion = onlineVersion.substring(1)
201219
}
202220

203221
// 比对版本号,是否为新版本
204-
const isNew = isNewVersion(version, curVersion)
205-
log.info(`版本比对结果:isNewVersion('${version}', '${curVersion}') = ${isNew}`)
222+
const isNew = isNewVersion(onlineVersion, curVersion)
223+
log.info(`版本比对结果:isNewVersion('${onlineVersion}', '${curVersion}') = ${isNew}`)
206224
if (isNew > 0) {
207-
log.info(`检查更新:发现新版本 '${version}',当前版本号为 '${curVersion}'`)
225+
log.info(`检查更新:发现新版本 '${onlineVersion}',当前版本号为 '${curVersion}'`)
208226
win.webContents.send('update', {
209227
key: 'available',
210228
value: {
211-
version,
229+
version: onlineVersion,
212230
releaseNotes: versionData.body
213231
? (versionData.body.replace(/\r\n/g, '\n').replace(/https:\/\/github.com\/docmirror\/dev-sidecar/g, '').replace(/(?<=(^|\n))[ \t]*(?:#[ #]*)?#\s*/g, '') || '无')
214232
: '无',
215233
},
216234
})
217235
} else {
218-
log.info(`检查更新:没有新版本,最近发布的版本号为 '${version}',而当前版本号为 '${curVersion}'`)
236+
log.info(`检查更新:没有新版本,最近发布的版本号为 '${onlineVersion}',而当前版本号为 '${curVersion}'`)
219237
win.webContents.send('update', { key: 'notAvailable' })
220238
}
221239

@@ -230,7 +248,7 @@ function updateHandle (app, api, win, beforeQuit, quit, log) {
230248
let bodyObj
231249
try {
232250
bodyObj = JSON.parse(body)
233-
} catch (e) {
251+
} catch {
234252
bodyObj = null
235253
}
236254

@@ -256,7 +274,7 @@ function updateHandle (app, api, win, beforeQuit, quit, log) {
256274
log.info('download dir:', fileDir)
257275
try {
258276
fs.accessSync(fileDir, fs.constants.F_OK)
259-
} catch (e) {
277+
} catch {
260278
fs.mkdirSync(fileDir)
261279
}
262280
const filePath = path.join(fileDir, `${value.version}.zip`)

packages/gui/test/versionTest.mjs

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import assert from 'node:assert'
2+
3+
const log = console
4+
5+
function parseVersion (version) {
6+
const matched = version.match(/^v?(\d{1,2}\.\d{1,2}\.\d{1,3}(?:\.\d{1,2})?)(.*)$/)
7+
const versionArr = matched[1].split('.')
8+
return {
9+
major: Number.parseInt(versionArr[0]), // 大版本
10+
minor: Number.parseInt(versionArr[1]), // 中版本
11+
patch: Number.parseInt(versionArr[2]), // 小版本
12+
temp: Number.parseInt(versionArr[3]) || 0, // 临时版本
13+
pre: matched[2], // 预发布版本号
14+
}
15+
}
16+
17+
/**
18+
* 比较版本号
19+
*
20+
* @param onlineVersion 线上版本号
21+
* @param currentVersion 当前版本号
22+
* @returns {number} 比较线上版本号是否为更新版本,1=是|0=相等|-1=否|-99=出现异常,比较结果未知
23+
*/
24+
function isNewVersion (onlineVersion, currentVersion) {
25+
if (onlineVersion === currentVersion) {
26+
return 0
27+
}
28+
29+
try {
30+
const versionObj = parseVersion(onlineVersion)
31+
const curVersionObj = parseVersion(currentVersion)
32+
33+
// 大版本
34+
if (versionObj.major > curVersionObj.major) {
35+
return 1 // 大版本号更大,为新版本,需要更新
36+
} else if (versionObj.major < curVersionObj.major) {
37+
return -1 // 大版本号更小,为旧版本,无需更新
38+
}
39+
40+
// 中版本
41+
if (versionObj.minor > curVersionObj.minor) {
42+
return 2 // 中版本号更大,为新版本,需要更新
43+
} else if (versionObj.minor < curVersionObj.minor) {
44+
return -2 // 中版本号更小,为旧版本,无需更新
45+
}
46+
47+
// 小版本
48+
if (versionObj.patch > curVersionObj.patch) {
49+
return 3 // 小版本号更大,为新版本,需要更新
50+
} else if (versionObj.patch < curVersionObj.patch) {
51+
return -3 // 小版本号更小,为旧版本,无需更新
52+
}
53+
54+
// 临时版本号
55+
if (versionObj.temp > curVersionObj.temp) {
56+
return 4 // 临时版本号更大,为新版本,需要更新
57+
} else if (versionObj.temp < curVersionObj.temp) {
58+
return -4 // 临时版本号更小,为旧版本,无需更新
59+
}
60+
61+
// 预发布版本号
62+
if (versionObj.pre && curVersionObj.pre) {
63+
// 当两个后缀版本号都存在时,直接比较后缀版本号字符串的大小
64+
if (versionObj.pre > curVersionObj.pre) {
65+
return 51
66+
} else if (versionObj.pre < curVersionObj.pre) {
67+
return -51
68+
}
69+
} else if (!versionObj.pre && curVersionObj.pre) {
70+
// 线上版本号没有后缀版本号,说明为正式版本,为新版本,需要更新
71+
return 52
72+
} else if (versionObj.pre && !curVersionObj.pre) {
73+
return -52
74+
} else {
75+
return -53 // 相同版本,无需更新(一般不会出现,除非例如 `2.0.0` 和 `2.0.0.0` 进行比较)
76+
}
77+
} catch (e) {
78+
log.error(`比对版本失败,当前版本号:${currentVersion},线上版本号:${onlineVersion}, error:`, e)
79+
return -99 // 比对异常
80+
}
81+
}
82+
83+
let ret;
84+
85+
ret = isNewVersion('2.0.0', '1.0.0')
86+
console.log(ret)
87+
assert.strictEqual(ret, 1)
88+
ret = isNewVersion('1.0.0', '2.0.0')
89+
console.log(ret)
90+
assert.strictEqual(ret, -1)
91+
92+
ret = isNewVersion('2.1.0', '2.0.0')
93+
console.log(ret)
94+
assert.strictEqual(ret, 2)
95+
ret = isNewVersion('2.0.0', '2.1.0')
96+
console.log(ret)
97+
assert.strictEqual(ret, -2)
98+
99+
ret = isNewVersion('2.0.1', '2.0.0')
100+
console.log(ret)
101+
assert.strictEqual(ret, 3)
102+
ret = isNewVersion('2.0.0', '2.0.1')
103+
console.log(ret)
104+
assert.strictEqual(ret, -3)
105+
106+
ret = isNewVersion('2.0.0.1', '2.0.0')
107+
console.log(ret)
108+
assert.strictEqual(ret, 4)
109+
ret = isNewVersion('2.0.0', '2.0.0.1')
110+
console.log(ret)
111+
assert.strictEqual(ret, -4)
112+
113+
ret = isNewVersion('2.0.0-RC2', '2.0.0-RC1')
114+
console.log(ret)
115+
assert.strictEqual(ret, 51)
116+
ret = isNewVersion('2.0.0-RC1', '2.0.0-RC2')
117+
console.log(ret)
118+
assert.strictEqual(ret, -51)
119+
120+
ret = isNewVersion('2.0.0', '2.0.0-RC1')
121+
console.log(ret)
122+
assert.strictEqual(ret, 52)
123+
ret = isNewVersion('2.0.0-RC1', '2.0.0')
124+
console.log(ret)
125+
assert.strictEqual(ret, -52)
126+
127+
ret = isNewVersion('2.0.0.0', '2.0.0')
128+
console.log(ret)
129+
assert.strictEqual(ret, -53)
130+
131+
ret = isNewVersion('x', 'v')
132+
console.log(ret)
133+
assert.strictEqual(ret, -99)

0 commit comments

Comments
 (0)