Support the download of CLI and Integrations

This commit is contained in:
Mylloon 2023-09-05 17:16:39 +02:00
parent f187ae3a5d
commit 4106b39e1b
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 69 additions and 18 deletions

View file

@ -4,26 +4,66 @@ use regex::Regex;
use semver::Version; use semver::Version;
use std::fs; use std::fs;
#[derive(Clone, Debug)]
enum RevancedRepo {
Patches,
Cli,
Integrations,
}
impl RevancedRepo {
fn owner(&self) -> &str {
"ReVanced"
}
fn repo(&self) -> &str {
match self {
Self::Patches => "revanced-patches",
Self::Cli => "revanced-cli",
Self::Integrations => "revanced-integrations",
}
}
fn targeted_ext(&self) -> &str {
match self {
RevancedRepo::Patches => ".jar",
RevancedRepo::Cli => "-all.jar",
RevancedRepo::Integrations => ".apk",
}
}
}
/// Revanced worker /// Revanced worker
pub async fn worker() { pub async fn worker() {
// Download patches if needed // Download patches if needed
if let Some(patches_url) = search().await { if let Some(patches_url) = search(&RevancedRepo::Patches).await {
utils::download_file(patches_url).await; utils::download_file(patches_url).await;
} }
// Download CLI if needed
if let Some(cli_url) = search(&RevancedRepo::Cli).await {
utils::download_file(cli_url).await;
}
// Download Integrations if needed
if let Some(integrations_url) = search(&RevancedRepo::Integrations).await {
utils::download_file(integrations_url).await;
}
} }
/// Search for the latest version /// Search for the latest version
/// Returns option if update has been found with the right URL /// Returns option if update has been found with the right URL
async fn search() -> Option<String> { async fn search(repo_type: &RevancedRepo) -> Option<String> {
let github_latest_version = get_latest_version().await; let github_latest_version = get_latest_version(repo_type).await;
let latest_version = { let latest_version = {
let mut tag = github_latest_version.tag_name.chars(); let mut tag = github_latest_version.tag_name.chars();
tag.next(); tag.next();
Version::parse(tag.as_str()).expect("Can't parse the latest version") Version::parse(tag.as_str())
.unwrap_or_else(|_| panic!("Can't parse the latest version of {:?}.", repo_type))
}; };
if let Some(current_version) = get_current_version() { if let Some(current_version) = get_current_version(repo_type) {
// No need to update // No need to update
if latest_version <= current_version { if latest_version <= current_version {
return None; return None;
@ -31,10 +71,15 @@ async fn search() -> Option<String> {
} }
// Either no current version, or version outdated, we need an update // Either no current version, or version outdated, we need an update
// Fetching the URL to the latest version
github_latest_version github_latest_version
.assets .assets
.iter() .iter()
.filter(|ele| ele.browser_download_url.path().ends_with(".jar")) .filter(|ele| {
ele.browser_download_url
.path()
.ends_with(repo_type.targeted_ext())
})
.collect::<Vec<&Asset>>() .collect::<Vec<&Asset>>()
.first() .first()
.map(|asset| { .map(|asset| {
@ -44,26 +89,26 @@ async fn search() -> Option<String> {
asset asset
.browser_download_url .browser_download_url
.host() .host()
.expect("Can't get asset host"), .expect("Can't get asset host."),
asset.browser_download_url.path() asset.browser_download_url.path()
) )
}) })
} }
/// Grab the latest patches version published by ReVanced /// Grab the latest patches version published by ReVanced
async fn get_latest_version() -> Release { async fn get_latest_version(repo: &RevancedRepo) -> Release {
let octocrab = octocrab::instance(); let octocrab = octocrab::instance();
octocrab octocrab
.repos("revanced", "revanced-patches") .repos(repo.owner(), repo.repo())
.releases() .releases()
.get_latest() .get_latest()
.await .await
.expect("Can't find the latest version of Revanced") .unwrap_or_else(|_| panic!("Can't find the latest version of {}.", repo.repo()))
} }
/// Find the current version already downloaded /// Find the current version already downloaded
fn get_current_version() -> Option<semver::Version> { fn get_current_version(repo_type: &RevancedRepo) -> Option<semver::Version> {
match fs::read_dir(utils::get_data_directory()) { match fs::read_dir(utils::get_data_directory()) {
Err(why) => { Err(why) => {
eprintln!("Error reading directory: {:?}", why.kind()); eprintln!("Error reading directory: {:?}", why.kind());
@ -71,19 +116,22 @@ fn get_current_version() -> Option<semver::Version> {
} }
Ok(paths) => { Ok(paths) => {
for path in paths { for path in paths {
let re = Regex::new(r"revanced-patches-(?P<version>\d+\.\d+\.\d+)\.jar") let re = Regex::new(&format!(
.expect("Can't build regex formula for revanced patches."); r"{}-(?P<version>\d+\.\d+\.\d+)\{}",
repo_type.repo(),
repo_type.targeted_ext()
))
.unwrap_or_else(|_| panic!("Can't build regex formula for {:?}.", repo_type));
if let Some(caps) = re.captures( if let Some(caps) = re.captures(
&path &path
.expect("Can't match patterns for revanced patches") .unwrap_or_else(|_| panic!("Can't match patterns for {:?}.", repo_type))
.path() .path()
.display() .display()
.to_string(), .to_string(),
) { ) {
return Some( return Some(Version::parse(&caps["version"]).unwrap_or_else(|_| {
Version::parse(&caps["version"]) panic!("No version found in the asset of {:?}.", repo_type)
.expect("No version found in the asset of revanced patches"), }));
);
} }
} }
None None

View file

@ -18,6 +18,7 @@ pub async fn download_file(url: String) {
.and_then(|name| if name.is_empty() { None } else { Some(name) }) .and_then(|name| if name.is_empty() { None } else { Some(name) })
.expect("Can't find the filename of URL."); .expect("Can't find the filename of URL.");
println!("Downloading {}.", fname);
match reqwest::get(source.clone()).await { match reqwest::get(source.clone()).await {
Ok(data) => match data.bytes().await { Ok(data) => match data.bytes().await {
Ok(bytes) => { Ok(bytes) => {
@ -30,6 +31,8 @@ pub async fn download_file(url: String) {
// Write data to the file // Write data to the file
dest.write_all(&bytes) dest.write_all(&bytes)
.unwrap_or_else(|_| panic!("Can't write to file at {}.", filepath.display())); .unwrap_or_else(|_| panic!("Can't write to file at {}.", filepath.display()));
println!("File downloaded ({}).", fname);
} }
Err(..) => eprintln!("Can't download {}.", fname), Err(..) => eprintln!("Can't download {}.", fname),
}, },