Support the download of CLI and Integrations
This commit is contained in:
parent
f187ae3a5d
commit
4106b39e1b
2 changed files with 69 additions and 18 deletions
|
@ -4,26 +4,66 @@ use regex::Regex;
|
|||
use semver::Version;
|
||||
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
|
||||
pub async fn worker() {
|
||||
// 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;
|
||||
}
|
||||
|
||||
// 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
|
||||
/// Returns option if update has been found with the right URL
|
||||
async fn search() -> Option<String> {
|
||||
let github_latest_version = get_latest_version().await;
|
||||
async fn search(repo_type: &RevancedRepo) -> Option<String> {
|
||||
let github_latest_version = get_latest_version(repo_type).await;
|
||||
|
||||
let latest_version = {
|
||||
let mut tag = github_latest_version.tag_name.chars();
|
||||
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
|
||||
if latest_version <= current_version {
|
||||
return None;
|
||||
|
@ -31,10 +71,15 @@ async fn search() -> Option<String> {
|
|||
}
|
||||
|
||||
// Either no current version, or version outdated, we need an update
|
||||
// Fetching the URL to the latest version
|
||||
github_latest_version
|
||||
.assets
|
||||
.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>>()
|
||||
.first()
|
||||
.map(|asset| {
|
||||
|
@ -44,26 +89,26 @@ async fn search() -> Option<String> {
|
|||
asset
|
||||
.browser_download_url
|
||||
.host()
|
||||
.expect("Can't get asset host"),
|
||||
.expect("Can't get asset host."),
|
||||
asset.browser_download_url.path()
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// 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();
|
||||
|
||||
octocrab
|
||||
.repos("revanced", "revanced-patches")
|
||||
.repos(repo.owner(), repo.repo())
|
||||
.releases()
|
||||
.get_latest()
|
||||
.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
|
||||
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()) {
|
||||
Err(why) => {
|
||||
eprintln!("Error reading directory: {:?}", why.kind());
|
||||
|
@ -71,19 +116,22 @@ fn get_current_version() -> Option<semver::Version> {
|
|||
}
|
||||
Ok(paths) => {
|
||||
for path in paths {
|
||||
let re = Regex::new(r"revanced-patches-(?P<version>\d+\.\d+\.\d+)\.jar")
|
||||
.expect("Can't build regex formula for revanced patches.");
|
||||
let re = Regex::new(&format!(
|
||||
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(
|
||||
&path
|
||||
.expect("Can't match patterns for revanced patches")
|
||||
.unwrap_or_else(|_| panic!("Can't match patterns for {:?}.", repo_type))
|
||||
.path()
|
||||
.display()
|
||||
.to_string(),
|
||||
) {
|
||||
return Some(
|
||||
Version::parse(&caps["version"])
|
||||
.expect("No version found in the asset of revanced patches"),
|
||||
);
|
||||
return Some(Version::parse(&caps["version"]).unwrap_or_else(|_| {
|
||||
panic!("No version found in the asset of {:?}.", repo_type)
|
||||
}));
|
||||
}
|
||||
}
|
||||
None
|
||||
|
|
|
@ -18,6 +18,7 @@ pub async fn download_file(url: String) {
|
|||
.and_then(|name| if name.is_empty() { None } else { Some(name) })
|
||||
.expect("Can't find the filename of URL.");
|
||||
|
||||
println!("Downloading {}.", fname);
|
||||
match reqwest::get(source.clone()).await {
|
||||
Ok(data) => match data.bytes().await {
|
||||
Ok(bytes) => {
|
||||
|
@ -30,6 +31,8 @@ pub async fn download_file(url: String) {
|
|||
// Write data to the file
|
||||
dest.write_all(&bytes)
|
||||
.unwrap_or_else(|_| panic!("Can't write to file at {}.", filepath.display()));
|
||||
|
||||
println!("File downloaded ({}).", fname);
|
||||
}
|
||||
Err(..) => eprintln!("Can't download {}.", fname),
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue