From e090a7adf173ef843473612cfaaf748b9470eec8 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 5 Sep 2023 10:47:07 +0200 Subject: [PATCH] version checker logic --- .gitignore | 2 ++ Cargo.lock | 2 ++ Cargo.toml | 2 ++ src/main.rs | 26 ++++++++++++++++--- src/revanced.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++--- src/utils.rs | 4 +++ 6 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 src/utils.rs diff --git a/.gitignore b/.gitignore index ea8c4bf..5ce53e1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target + +/data diff --git a/Cargo.lock b/Cargo.lock index 689a535..784af9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1153,6 +1153,8 @@ version = "0.1.0" dependencies = [ "actix-web", "octocrab", + "regex", + "semver", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index 463ba0d..852e8c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,6 @@ license = "AGPL-3.0-or-later" [dependencies] actix-web = "4.4" octocrab = "0.30" +regex = "1.9.5" +semver = "1.0.18" tokio = "1.32.0" diff --git a/src/main.rs b/src/main.rs index 2b12940..7679390 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,21 @@ use std::{ - thread, + fs, thread, time::{Duration, Instant}, }; mod revanced; +mod utils; + +/// Initialize +pub fn init() { + // Make data directory + if let Err(why) = fs::create_dir(utils::get_data_directory()) { + match why.kind() { + std::io::ErrorKind::AlreadyExists => (), + _ => eprintln!("{:?}", why.kind()), + } + } +} #[actix_web::main] async fn main() { @@ -22,10 +34,15 @@ async fn main() { println!("Scheduler starting"); // Prepare threads - let thread_a = thread::spawn(revanced::search); + let thread_revanced = thread::spawn(revanced::worker); // Run threads - rt.block_on(async { thread_a.join().expect("Thread A panicked").await }); + rt.block_on(async { + thread_revanced + .join() + .expect("Thread Revanced panicked") + .await + }); let runtime = start.elapsed(); @@ -40,6 +57,9 @@ async fn main() { } }); + // Initializations + init(); + // Run scheduler scheduler.join().expect("Scheduler panicked"); } diff --git a/src/revanced.rs b/src/revanced.rs index 055e038..d4d9db9 100644 --- a/src/revanced.rs +++ b/src/revanced.rs @@ -1,9 +1,53 @@ -use octocrab::models::repos::Release; +use crate::utils; +use octocrab::models::repos::{Asset, Release}; +use regex::Regex; +use semver::Version; +use std::fs; -pub async fn search() { - println!("{:#?}", get_latest_version().await) +/// Revanced worker +pub async fn worker() { + if let Some(patches_url) = search().await { + // Download + println!("TODO // Download {}", patches_url); + } } +/// Search for the latest version +/// Returns option if update has been found with the right URL +async fn search() -> Option { + let github_latest_version = get_latest_version().await; + + let latest_version = { + let mut tag = github_latest_version.tag_name.chars(); + tag.next(); + Version::parse(tag.as_str()).unwrap() + }; + + if let Some(current_version) = get_current_version() { + // No need to update + if latest_version <= current_version { + return None; + } + } + + // Either no current version, or version outdated, we need an update + github_latest_version + .assets + .iter() + .filter(|ele| ele.browser_download_url.path().ends_with(".jar")) + .collect::>() + .first() + .map(|asset| { + format!( + "{}://{}{}", + asset.browser_download_url.scheme(), + asset.browser_download_url.host().unwrap(), + asset.browser_download_url.path() + ) + }) +} + +/// Grab the latest patches version published by ReVanced async fn get_latest_version() -> Release { let octocrab = octocrab::instance(); @@ -14,3 +58,22 @@ async fn get_latest_version() -> Release { .await .expect("Can't find the latest version of Revanced") } + +/// Find the current version already downloaded +fn get_current_version() -> Option { + match fs::read_dir(utils::get_data_directory()) { + Err(why) => { + eprintln!("Error reading directory: {:?}", why.kind()); + None + } + Ok(paths) => { + for path in paths { + let re = Regex::new(r"revanced-patches-(?P\d+\.\d+\.\d+)\.jar").unwrap(); + if let Some(caps) = re.captures(&path.unwrap().path().display().to_string()) { + return Some(Version::parse(&caps["version"]).unwrap()); + } + } + None + } + } +} diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..1dcfc2c --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,4 @@ +/// Data directory +pub fn get_data_directory() -> String { + "./data".to_owned() +}