Skip to content

Commit

Permalink
mmrl.json
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Oct 20, 2023
1 parent c596188 commit 0b400b9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 53 deletions.
56 changes: 19 additions & 37 deletions src/cmd/install.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
extern crate serde;
extern crate serde_ini;

use crate::utils::{confirm, download_from_url, read_module_prop_file, is_url};
use crate::utils::{confirm, download_from_url, get_mmrl_json, is_url};

use crate::android_root::{get_downloads_dir, get_install_cli};
use crate::cmd::info::info;
use crate::repo::{find_module, find_version, get_id_details, Repo};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::io::{BufRead, BufReader, Error, ErrorKind};
use std::path::Path;
use std::process::{exit, Command, Stdio};

#[derive(Deserialize, Serialize, Clone, PartialEq, Debug)]
struct Dependencies {
name: Vec<String>,
}

#[derive(Deserialize, Serialize, Clone, PartialEq, Default, Debug)]
struct MMRLINI {
// key1: String,
// key2: u32,
// key3: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
dependencies: Option<Box<Dependencies>>,
fn check_requires(path: String) {
let mini = get_mmrl_json(&path);

for req in mini.unwrap().require {
let dep_path = Path::new("/data/adb/modules")
.join(req.clone())
.join("module.prop");
let dep_path_update = Path::new("/data/adb/modules_update")
.join(req.clone())
.join("module.prop");
if !dep_path_update.exists() || !dep_path.exists(){
println!("This module requires {} to be installed", req.clone());
exit(1)
}
}
}

pub async fn install(client: Client, yes: bool, json: &Repo, id: String) {
Expand All @@ -46,29 +48,8 @@ pub async fn install(client: Client, yes: bool, json: &Repo, id: String) {
println!("Downloading {}", module.name);
println!("Version: {}\n", &version.version);

let path = download_from_url(client.clone(), version.zip_url, module.name, path).await;

let mini = read_module_prop_file(&path);

if mini.is_ok() {
let props: Result<MMRLINI, serde_ini::de::Error> =
serde_ini::from_str::<MMRLINI>(&mini.unwrap());
if props.is_ok() {
let deps = props.unwrap().dependencies.unwrap();
for dep in deps.name {
let dep_path = Path::new("/data/adb/modules")
.join(dep.clone())
.join("module.prop");
let dep_path_update = Path::new("/data/adb/modules_update")
.join(dep.clone())
.join("module.prop");
if !dep_path.exists() || !dep_path_update.exists() {
println!("This module requires {} to be installed", dep.clone());
exit(1)
}
}
}
}
download_from_url(client.clone(), version.zip_url, module.name, path).await;
check_requires(path.clone());

let success = yes || confirm("\nDo you want to continue [y/N]? ");

Expand Down Expand Up @@ -100,6 +81,7 @@ pub async fn install(client: Client, yes: bool, json: &Repo, id: String) {
]
.join("/");
download_from_url(client, id.clone(), id, path).await;
check_requires(path.clone());

let success = yes || confirm("\nDo you want to continue [y/N]? ");

Expand Down
47 changes: 31 additions & 16 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
use std::fs::File;
use std::io;
use std::io::prelude::*;
use zip::ZipArchive;
use futures_util::StreamExt;
use indicatif::{ProgressBar, ProgressStyle};
use regex::Regex;
use reqwest::Client;
use std::cmp::min;
use std::io::Write;
use regex::Regex;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::path::Path;
use zip::ZipArchive;
use serde::{Deserialize, Serialize};

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MMRLJSON {
pub require: Vec<String>,
}

pub fn get_mmrl_json(path: &str) -> Result<MMRLJSON, serde_json::Error> {
let fname = Path::new(path);
let zipfile = File::open(fname).unwrap();

let mut archive = ZipArchive::new(zipfile).unwrap();

let mut file = match archive.by_name("mmrl.json") {
Ok(file) => file,
Err(..) => {
println!("mmrl.json not found");
return serde_json::from_str("{\"require\":[]}")
}
};

pub fn read_module_prop_file(zip_file_path: &str) -> std::io::Result<String> {
let zip_file = File::open(zip_file_path)?;
let mut archive = ZipArchive::new(zip_file)?;
let mut module_prop_file = archive.by_name("mmrl.ini")?;
let mut contents = String::new();
module_prop_file.read_to_string(&mut contents)?;
Ok(contents)
file.read_to_string(&mut contents).unwrap();
return serde_json::from_str(contents.as_str());
}

pub fn confirm(msg: &str) -> bool {
Expand All @@ -41,7 +58,6 @@ pub fn confirm(msg: &str) -> bool {
}
}


pub async fn download_from_url(client: Client, url: String, name: String, path: &String) -> String {
let res = client
.get(url.clone())
Expand Down Expand Up @@ -85,7 +101,6 @@ pub async fn download_from_url(client: Client, url: String, name: String, path:
}

pub fn is_url(url: &str) -> bool {
let url_regex: &str =
r"https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}(\.[a-z]{2,4})?\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)";
let url_regex: &str = r"https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}(\.[a-z]{2,4})?\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)";
return Regex::new(url_regex).unwrap().is_match(url);
}
}

0 comments on commit 0b400b9

Please sign in to comment.