From 35d375f12a4329602419ef24c3622854443afdb8 Mon Sep 17 00:00:00 2001 From: dbydd <1992003927@qq.com> Date: Fri, 8 Nov 2024 18:56:39 +0800 Subject: [PATCH 1/3] make it clean --- src/commands/patch.rs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/commands/patch.rs b/src/commands/patch.rs index e716312..e2627cb 100644 --- a/src/commands/patch.rs +++ b/src/commands/patch.rs @@ -6,6 +6,7 @@ use std::{ use anyhow::Result; use color_print::cprintln; +use inventory::iter; use serde::{Deserialize, Serialize}; use toml::Table; @@ -109,7 +110,7 @@ pub fn do_patch(name: &str, git: &str, commit: &str, https: bool) -> Result<()> .arg("clone") .arg(match https { true => String::from(git), - false => git_https_to_ssh(git)? + false => git_https_to_ssh(git)?, }) .arg(format!("crates/{}", name)) .spawn()?; @@ -193,7 +194,41 @@ pub fn handler(args: Vec) -> Result<()> { // TODO: Check if the rev exists. use rev instead of the hash commit. // Do the patch - do_patch(&patch_name, git_url, commit, args.contains(&String::from("--https")))?; + do_patch( + &patch_name, + git_url, + commit, + args.contains(&String::from("--https")), + )?; + } + "patch_all" => { + let do_not_patch = get_patched_table().map(|table| { + table + .iter() + .map(|pack| pack.name.clone()) + .collect::>() + })?; + for patch in get_patch_table()? + .iter() + .filter(|a| do_not_patch.binary_search(&a.name).is_err()) + { + let urls: Vec<&str> = patch.source.as_ref().unwrap().split("#").collect(); + if urls.len() < 2 { + return Err(anyhow!("This is not a valid patch source")); + } + let commit = urls[1]; + let git_url_end = urls[0].find('?').unwrap_or(urls[0].len()); + let git_url = &urls[0][4..git_url_end]; + + // TODO: Check if the rev exists. use rev instead of the hash commit. + // Do the patch + do_patch( + &patch.name, + git_url, + commit, + args.contains(&String::from("--https")), + )?; + } } "remove" => { let patch_name = args[3].clone(); From 50762896aefc3da6e491132f648d48194d5760b4 Mon Sep 17 00:00:00 2001 From: dbydd <1992003927@qq.com> Date: Fri, 8 Nov 2024 18:56:53 +0800 Subject: [PATCH 2/3] make it clean --- src/commands/config.rs | 4 ++-- src/commands/patch.rs | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/commands/config.rs b/src/commands/config.rs index 3ca8264..5309bc3 100644 --- a/src/commands/config.rs +++ b/src/commands/config.rs @@ -6,7 +6,7 @@ use crate::{config::read_bin_config, CommandAndHandler}; /// The handler of the command. fn handler(args: Vec) -> Result<()> { - let commands = vec!["get_env", "get_cfg", "get_triple", "get_meta"]; + let commands = ["get_env", "get_cfg", "get_triple", "get_meta"]; if args.len() < 5 || (!commands.contains(&args[4].as_str())) { println!("kbuild [config_file] [bin] [{}] [name]", commands.join("|")); return Ok(()); @@ -19,7 +19,7 @@ fn handler(args: Vec) -> Result<()> { // Convert kernel configuration to rustflags. // This rustflags will be passed to the rust build command. - let binary_config = read_bin_config(&file_name, bin)?; + let binary_config = read_bin_config(file_name, bin)?; let value = match ops { "get_env" => binary_config diff --git a/src/commands/patch.rs b/src/commands/patch.rs index e2627cb..67bf916 100644 --- a/src/commands/patch.rs +++ b/src/commands/patch.rs @@ -6,7 +6,6 @@ use std::{ use anyhow::Result; use color_print::cprintln; -use inventory::iter; use serde::{Deserialize, Serialize}; use toml::Table; @@ -249,7 +248,7 @@ pub fn handler(args: Vec) -> Result<()> { .stdout(Stdio::piped()) .spawn()?; let outputs = process.wait_with_output()?; - if outputs.stdout.len() != 0 { + if !outputs.stdout.is_empty() { println!(); println!("{}", String::from_utf8(outputs.stdout.clone())?); return Err(anyhow!("You have some files unhandle, please ensure this package don't have any item to handle")); From 0c63f039ec22b06f80bfc4e4054c0558d829c902 Mon Sep 17 00:00:00 2001 From: dbydd <1992003927@qq.com> Date: Fri, 8 Nov 2024 20:12:57 +0800 Subject: [PATCH 3/3] furture more, added remove_all --- src/commands/patch.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/commands/patch.rs b/src/commands/patch.rs index 67bf916..838066c 100644 --- a/src/commands/patch.rs +++ b/src/commands/patch.rs @@ -4,7 +4,7 @@ use std::{ process::{Command, Stdio}, }; -use anyhow::Result; +use anyhow::{Ok, Result}; use color_print::cprintln; use serde::{Deserialize, Serialize}; use toml::Table; @@ -272,6 +272,42 @@ pub fn handler(args: Vec) -> Result<()> { } fs::write("Cargo.toml", toml::to_string(&cargo_toml)?)?; } + "remove_all" => { + for patched in get_patched_table()?.iter() { + let process = Command::new("git") + .arg("status") + .arg("-s") + .current_dir(current_dir()?.join(format!("crates/{}", patched.name))) + .stdout(Stdio::piped()) + .spawn()?; + let outputs = process.wait_with_output()?; + if !outputs.stdout.is_empty() { + println!(); + println!("{}", String::from_utf8(outputs.stdout.clone())?); + return Err(anyhow!("Your crate {} have some files unhandle, please ensure this package don't have any item to handle",patched.name)); + } + } + + for patched in get_patched_table()?.iter() { + // remove patch from disk + fs::remove_dir_all(format!("crates/{}", patched.name))?; + + // remove patch from Cargo.toml + let mut cargo_toml: Table = toml::from_str(&fs::read_to_string("Cargo.toml")?)?; + let patch_table = cargo_toml.get_mut("patch").unwrap().as_table_mut().unwrap(); + let git_table = patch_table + .get_mut(&patched.git) + .unwrap() + .as_table_mut() + .unwrap(); + if git_table.len() == 1 { + patch_table.remove(&patched.git); + } else { + git_table.remove(&patched.name); + } + fs::write("Cargo.toml", toml::to_string(&cargo_toml)?)?; + } + } _ => {} } Ok(())