Skip to content

Commit 14fd91a

Browse files
committed
feat: use API to check for updates
1 parent a2cc7fa commit 14fd91a

4 files changed

Lines changed: 68 additions & 47 deletions

File tree

src-tauri/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::sync::{Arc, Mutex};
99
use std::thread::available_parallelism;
1010
use std::time::Duration;
1111
use std::{fs, thread};
12+
use tauri::Manager;
1213

1314
mod result;
1415

@@ -26,6 +27,15 @@ fn main() {
2627
};
2728

2829
tauri::Builder::default()
30+
.setup(|app| {
31+
#[cfg(debug_assertions)] // only include this code on debug builds
32+
{
33+
let window = app.get_webview_window("main").unwrap();
34+
window.open_devtools();
35+
window.close_devtools();
36+
}
37+
Ok(())
38+
})
2939
.plugin(tauri_plugin_os::init())
3040
.plugin(tauri_plugin_dialog::init())
3141
.manage(shared_state)

src/components/Layout/index.jsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import React, { Suspense, useContext, useEffect } from 'react';
22
import Box from '@mui/material/Box';
33
import CssBaseline from '@mui/material/CssBaseline';
44
import { createTheme, ThemeProvider } from '@mui/material/styles';
5-
import { platform } from '@tauri-apps/plugin-os';
5+
import { getVersion } from '@tauri-apps/api/app';
6+
import { platform, arch } from '@tauri-apps/plugin-os';
67
import { Outlet } from 'react-router-dom';
7-
import packageJson from '../../../package.json';
88
import { MainContext } from '../../contexts/MainContextProvider';
99
import {
1010
getNumberOfThreads,
@@ -49,7 +49,7 @@ const Layout = () => {
4949
/**
5050
* Check for updates
5151
*/
52-
const checkForUpdates = () => {
52+
const checkForUpdates = async () => {
5353
if (loading) {
5454
return;
5555
}
@@ -59,7 +59,10 @@ const Layout = () => {
5959

6060
try {
6161
const res = platform();
62-
Updater(res.toLowerCase(), packageJson.version)
62+
const archRes = arch();
63+
const ver = 'v' + (await getVersion());
64+
65+
Updater(res.toLowerCase(), archRes, ver)
6366
.then((up) => {
6467
d1(setUpdate(up));
6568
})

src/routes/Settings/index.jsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ import RadioGroup from '@mui/material/RadioGroup';
3535
import Select from '@mui/material/Select';
3636
import TextField from '@mui/material/TextField';
3737
import Typography from '@mui/material/Typography';
38-
import { platform } from '@tauri-apps/plugin-os';
39-
import packageJson from '../../../package.json';
38+
import { getVersion } from '@tauri-apps/api/app';
39+
import { platform, arch } from '@tauri-apps/plugin-os';
4040
import AlertDialog from '../../components/AlertDialog';
4141
import GridList from '../../components/GridList';
4242
import Theme from '../../components/Theme';
@@ -146,7 +146,7 @@ const Settings = () => {
146146
/**
147147
* Check for updates
148148
*/
149-
const checkForUpdates = () => {
149+
const checkForUpdates = async () => {
150150
if (loading) {
151151
return;
152152
}
@@ -156,7 +156,10 @@ const Settings = () => {
156156

157157
try {
158158
const res = platform();
159-
Updater(res.toLowerCase(), packageJson.version)
159+
const archRes = arch();
160+
const ver = 'v' + (await getVersion());
161+
162+
Updater(res.toLowerCase(), archRes.toLowerCase(), ver)
160163
.then((up) => {
161164
d1(setUpdate(up));
162165
d1(setCheckedForUpdates(true));

src/utils/Updater/index.js

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,62 @@
1-
const Updater = (os, currentVersion) => {
1+
const Updater = (os, architectureName, currentVersion) => {
22
/**
3-
* Check whether version b is newer than version a
4-
* @param a Version a
5-
* @param b Version b
6-
* @returns {boolean} True if version b is newer than version a, otherwise false
3+
* Compare two semantic versions
4+
* @param ver1 Version 1
5+
* @param ver2 Version 2
6+
* @returns {number} 1 if ver1 > ver2, -1 if ver1 < ver2, 0 if equal
77
*/
8-
const isNewer = (a, b) => {
9-
const partsA = a.split('.');
10-
const partsB = b.split('.');
11-
const numParts =
12-
partsA.length > partsB.length ? partsA.length : partsB.length;
8+
const semverCompare = (ver1, ver2) => {
9+
const v1Parts = ver1.slice(1).split('.').map(Number);
10+
const v2Parts = ver2.slice(1).split('.').map(Number);
1311

14-
for (let i = 0; i < numParts; i += 1) {
15-
if ((parseInt(partsB[i], 10) || 0) !== (parseInt(partsA[i], 10) || 0)) {
16-
return (parseInt(partsB[i], 10) || 0) > (parseInt(partsA[i], 10) || 0);
17-
}
12+
for (let i = 0; i < 3; i++) {
13+
if (v1Parts[i] > v2Parts[i]) return 1;
14+
if (v1Parts[i] < v2Parts[i]) return -1;
1815
}
19-
20-
return false;
16+
return 0;
2117
};
2218

2319
/**
24-
* Parse the information inside an external update
25-
* @param update The update data
26-
* @returns {{infoUrl: null, updateUrl: boolean, downloadUrl: null, version: null}}
20+
* Parse the update data
21+
* @param data The update data
22+
* @returns {{updateUrl, infoUrl: *, version: SemVer, updateAvailable: boolean}} The parsed update data
2723
*/
28-
const parseUpdate = (update) => {
29-
const platform = update.platforms[os];
30-
const data = {
31-
updateUrl: false,
32-
downloadUrl: null,
33-
infoUrl: null,
34-
version: null,
35-
};
24+
const parseUpdate = (data) => {
25+
const platform = data.platforms.find(
26+
(p) => p.platformName.toLowerCase() === os.toLowerCase(),
27+
);
28+
if (!platform) {
29+
throw new Error(`Platform ${os} not found`);
30+
}
3631

37-
if (
38-
isNewer(
39-
currentVersion,
40-
`${platform.version.majorVersion}.${platform.version.minorVersion}.${platform.version.buildVersion}.${platform.version.revisionVersion}`,
41-
)
42-
) {
43-
data.updateAvailable = true;
32+
// Find the architecture
33+
const architecture = platform.architectures.find(
34+
(a) => a.name === architectureName,
35+
);
36+
if (!architecture) {
37+
throw new Error(
38+
`Architecture ${architectureName} not found for platform ${os}`,
39+
);
4440
}
4541

46-
data.updateUrl = platform.updateUrl;
47-
data.infoUrl = platform.infoUrl;
48-
data.version = `${platform.version.majorVersion}.${platform.version.minorVersion}.${platform.version.buildVersion}.${platform.version.revisionVersion}`;
42+
// Sort releases by semver in descending order
43+
const sortedReleases = architecture.releases.sort((a, b) => {
44+
return semverCompare(b.semver, a.semver);
45+
});
4946

50-
return data;
47+
return {
48+
updateUrl: sortedReleases[0].downloadUrl,
49+
infoUrl: sortedReleases[0].infoUrl,
50+
version: sortedReleases[0].semver,
51+
updateAvailable:
52+
semverCompare(currentVersion, sortedReleases[0].semver) < 0,
53+
};
5154
};
5255

5356
return new Promise((resolve, reject) => {
54-
fetch('https://codedead.com/Software/Advanced%20PortChecker/version.json')
57+
fetch(
58+
'https://api.codedead.com/api/v1/applications/47cd7e8f-2744-443c-850e-619df5d5c43f',
59+
)
5560
.then((res) => {
5661
if (!res.ok) {
5762
throw Error(res.statusText);

0 commit comments

Comments
 (0)