Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{config::read_bin_config, CommandAndHandler};

/// The handler of the command.
fn handler(args: Vec<String>) -> 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(());
Expand All @@ -19,7 +19,7 @@ fn handler(args: Vec<String>) -> 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
Expand Down
78 changes: 74 additions & 4 deletions src/commands/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -109,7 +109,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()?;
Expand Down Expand Up @@ -193,7 +193,41 @@ pub fn handler(args: Vec<String>) -> 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::<Vec<_>>()
})?;
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();
Expand All @@ -214,7 +248,7 @@ pub fn handler(args: Vec<String>) -> 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"));
Expand All @@ -238,6 +272,42 @@ pub fn handler(args: Vec<String>) -> 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(())
Expand Down