From 6f142d9ee531b114a2d1c6ed3b55983dd72e83a3 Mon Sep 17 00:00:00 2001 From: Gabriele Musco Date: Wed, 21 Aug 2024 19:48:40 +0200 Subject: [PATCH] feat: function to check for updates --- Cargo.toml | 3 +++ dist/appimage/build_appimage.sh | 2 +- meson_options.txt | 6 +++++ src/downloader.rs | 2 +- src/main.rs | 3 +++ src/meson.build | 10 ++++++++ src/updater.rs | 44 +++++++++++++++++++++++++++++++++ 7 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/updater.rs diff --git a/Cargo.toml b/Cargo.toml index 9a73d6b..37a4483 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,3 +30,6 @@ openxr = { git = "https://github.com/galister/openxrs", rev = "af4a55d", feature ash = "0.38.0" sha2 = "0.10.8" tokio = { version = "1.39.3", features = ["process"] } + +[features] +appimage = [] diff --git a/dist/appimage/build_appimage.sh b/dist/appimage/build_appimage.sh index 71bf558..3594814 100755 --- a/dist/appimage/build_appimage.sh +++ b/dist/appimage/build_appimage.sh @@ -7,7 +7,7 @@ if [[ ! -f Cargo.toml ]]; then exit 1 fi -meson setup appimage_build -Dprefix=/usr -Dprofile=default +meson setup appimage_build -Dprefix=/usr -Dprofile=default -Dappimage=true DESTDIR="$PWD/AppDir" ninja -C appimage_build install curl -SsLO https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage chmod +x linuxdeploy-x86_64.AppImage diff --git a/meson_options.txt b/meson_options.txt index 649ad5d..51b4f13 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -8,3 +8,9 @@ option( value: 'default', description: 'The build profile. One of "default" or "development".' ) +option( + 'appimage', + type: 'boolean', + value: false, + description: 'Enable AppImage specific features.' +) diff --git a/src/downloader.rs b/src/downloader.rs index bb9a4b7..8fe3906 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -25,7 +25,7 @@ fn sync_client() -> reqwest::blocking::Client { .expect("Failed to build reqwest::blocking::Client") } -fn async_client() -> reqwest::Client { +pub fn async_client() -> reqwest::Client { reqwest::Client::builder() .timeout(TIMEOUT) .default_headers(headers()) diff --git a/src/main.rs b/src/main.rs index da75234..b3a52c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,9 @@ pub mod vulkaninfo; pub mod xdg; pub mod xr_devices; +#[cfg(feature = "appimage")] +pub mod updater; + fn restore_steam_xr_files() { let active_runtime = get_current_active_runtime(); let openvrpaths = get_current_openvrpaths(); diff --git a/src/meson.build b/src/meson.build index 8805504..e414164 100644 --- a/src/meson.build +++ b/src/meson.build @@ -23,6 +23,16 @@ else message('Building in debug mode') endif +features = [] + +if get_option('appimage') == true + features += 'appimage' +endif + +if features.length() > 0 + cargo_options += ['--features', ','.join(features)] +endif + cargo_env = [ 'CARGO_HOME=' + meson.project_build_root() / 'cargo-home' ] cargo_build = custom_target( diff --git a/src/updater.rs b/src/updater.rs new file mode 100644 index 0000000..6706171 --- /dev/null +++ b/src/updater.rs @@ -0,0 +1,44 @@ +use anyhow::bail; +use reqwest::Method; +use serde::Deserialize; + +use crate::{constants::VERSION, downloader::async_client}; + +const GITLAB_PROJ_ID: &str = "46446166"; + +#[derive(Deserialize, Clone)] +struct GitLabCommit { + id: String, +} + +type GitLabCommitList = Vec; + +pub async fn check_updates_available() -> anyhow::Result { + let client = async_client(); + if VERSION.contains("-") { + // dev releases, check commits + let shorthash = VERSION.split('-').last().unwrap_or_default(); + let url = format!( + "https://gitlab.com/api/v4/projects/{GITLAB_PROJ_ID}/repository/commits?ref_name=main" + ); + let res = client.request(Method::GET, url).send().await?; + match res.error_for_status() { + Err(e) => Err(e.into()), + Ok(res) => { + let text = res.text().await?; + let commit_list: GitLabCommitList = serde_json::from_str(&text)?; + if commit_list + .get(0) + .is_some_and(|c| c.id.starts_with(shorthash)) + { + Ok(false) + } else { + Ok(true) + } + } + } + } else { + // stable releases, check tags + bail!("Not implemented yet!") + } +}