version checker logic

This commit is contained in:
Mylloon 2023-09-05 10:47:07 +02:00
parent a3eef3bc11
commit e090a7adf1
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
6 changed files with 99 additions and 6 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
/target /target
/data

2
Cargo.lock generated
View file

@ -1153,6 +1153,8 @@ version = "0.1.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"octocrab", "octocrab",
"regex",
"semver",
"tokio", "tokio",
] ]

View file

@ -12,4 +12,6 @@ license = "AGPL-3.0-or-later"
[dependencies] [dependencies]
actix-web = "4.4" actix-web = "4.4"
octocrab = "0.30" octocrab = "0.30"
regex = "1.9.5"
semver = "1.0.18"
tokio = "1.32.0" tokio = "1.32.0"

View file

@ -1,9 +1,21 @@
use std::{ use std::{
thread, fs, thread,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
mod revanced; 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] #[actix_web::main]
async fn main() { async fn main() {
@ -22,10 +34,15 @@ async fn main() {
println!("Scheduler starting"); println!("Scheduler starting");
// Prepare threads // Prepare threads
let thread_a = thread::spawn(revanced::search); let thread_revanced = thread::spawn(revanced::worker);
// Run threads // 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(); let runtime = start.elapsed();
@ -40,6 +57,9 @@ async fn main() {
} }
}); });
// Initializations
init();
// Run scheduler // Run scheduler
scheduler.join().expect("Scheduler panicked"); scheduler.join().expect("Scheduler panicked");
} }

View file

@ -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() { /// Revanced worker
println!("{:#?}", get_latest_version().await) 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<String> {
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::<Vec<&Asset>>()
.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 { async fn get_latest_version() -> Release {
let octocrab = octocrab::instance(); let octocrab = octocrab::instance();
@ -14,3 +58,22 @@ async fn get_latest_version() -> Release {
.await .await
.expect("Can't find the latest version of Revanced") .expect("Can't find the latest version of Revanced")
} }
/// Find the current version already downloaded
fn get_current_version() -> Option<semver::Version> {
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<version>\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
}
}
}

4
src/utils.rs Normal file
View file

@ -0,0 +1,4 @@
/// Data directory
pub fn get_data_directory() -> String {
"./data".to_owned()
}