From c86a2c23e6371af2bf53985ef0f8dd32ebd1fa14 Mon Sep 17 00:00:00 2001 From: michal Date: Sun, 23 Jan 2022 23:00:44 +0000 Subject: [PATCH] added ability to shorten repo urls --- Cargo.toml | 13 ++++-- src/internal/structs.rs | 14 +++++++ src/main.rs | 87 +++++++++++++++++++++++++++------------- src/repository/config.rs | 3 +- src/workspace/read.rs | 35 ++++++++++++++-- 5 files changed, 117 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 55b2bd2..eb3dade 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,8 +10,15 @@ license-file = "LICENSE.md" name = "mlc" path = "src/main.rs" +[profile.release] +incremental = true +debug = false +lto = "fat" +codegen-units = 1 + [dependencies] +mimalloc = { version = "0.1.27", default-features = false } clap = { version = "2.34.0", default-features = false } -toml = "0.5.8" -serde = "1.0.134" -serde_derive = "1.0.134" \ No newline at end of file +toml = { version = "0.5.8", default-features = false } +serde = { version = "1.0.134", default-features = false } +serde_derive = { version = "1.0.134", default-features = false } \ No newline at end of file diff --git a/src/internal/structs.rs b/src/internal/structs.rs index ff46046..973dd82 100755 --- a/src/internal/structs.rs +++ b/src/internal/structs.rs @@ -6,3 +6,17 @@ pub struct Config { pub name: Option, pub repo: Vec, } + +#[derive(Debug, Deserialize)] +pub struct UnexpandedConfig { + pub mode: String, + pub name: Option, + pub repo: Vec, + pub urls: Vec, +} + +#[derive(Debug)] +pub struct SplitRepo { + pub indx: usize, + pub name: String +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 036770f..7cad5ec 100755 --- a/src/main.rs +++ b/src/main.rs @@ -8,11 +8,22 @@ use crate::repository::create_config; use crate::workspace::read_cfg; +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; + mod internal; mod repository; mod workspace; fn main() { + extern "C" { + fn geteuid() -> u32; + } + + if unsafe { geteuid() } == 0 { + crash("Running malachite as root is disallowed as it can lead to system breakage. Instead, malachite will prompt you when it needs superuser permissions".to_string(), 1); + } + fn build_app() -> App<'static, 'static> { let app = App::new("Malachite") .version(env!("CARGO_PKG_VERSION")) @@ -25,32 +36,29 @@ fn main() { .set(ArgSettings::Global) .help("Sets the level of verbosity"), ) - .arg( - // TODO implement --exclude - Arg::with_name("exclude") - .short("e") - .long("exclude") - .multiple(true) - .set(ArgSettings::Global) - .help("Excludes packages from given operation"), - ) - .arg( - // TODO implement --all - Arg::with_name("all") - .long("all") - .set(ArgSettings::Global) - .help("Operates on every possible package"), - ) .subcommand( SubCommand::with_name("build") .about("Builds the given packages") .arg( Arg::with_name("package(s)") .help("The packages to operate on") - .required(true) .multiple(true) .index(1), - ), + ) + .arg( + Arg::with_name("all") + .long("all") + .help("Builds all packages in mlc.toml (except if -x is specified)") + .conflicts_with("package(s)") + ) + .arg( + Arg::with_name("exclude") + .short("x") + .long("exclude") + .multiple(true) + .takes_value(true) + .help("Excludes packages from given operation") + ) ) .subcommand( SubCommand::with_name("repo-gen").about("Generates repository from built packages"), @@ -87,6 +95,7 @@ fn main() { let matches = build_app().get_matches(); + if let true = matches.is_present("init") { let config = workspace::read_cfg(); if config.mode == "workspace" { @@ -110,6 +119,7 @@ fn main() { .unwrap(); info(format!("Entering working directory: {}", r)); + let cdir = env::current_dir().unwrap(); let dir = format!( "{}/{}", env::current_dir().unwrap().display(), @@ -132,6 +142,9 @@ fn main() { .unwrap() .wait() .unwrap(); + + info(format!("Exiting work directory: {}", r)); + env::set_current_dir(cdir).unwrap(); } } else { crash("Invalid mode in mlc.toml".to_string(), 1); @@ -140,18 +153,26 @@ fn main() { if let true = matches.is_present("build") { let config = workspace::read_cfg(); + let mut packages: Vec = matches + .subcommand_matches("build") + .unwrap() + .values_of_lossy("package(s)") + .unwrap_or(vec![]); + + let exclude: Vec = matches + .subcommand_matches("build") + .unwrap() + .values_of_lossy("exclude") + .unwrap_or(vec![]); + + for pkg in &exclude { + packages.retain(|x| &*x != pkg); + } + if config.mode != "repository" { crash("Cannot build packages in workspace mode".to_string(), 2); } - let packages: Vec = matches - .subcommand() - .1 - .unwrap() - .values_of("package(s)") - .unwrap() - .into_iter() - .map(|s| s.to_string()) - .collect(); + let mut repos: Vec = vec![]; for r in config.repo { let split = r.split('/').collect::>(); @@ -159,6 +180,12 @@ fn main() { repos.push(a.parse().unwrap()); } + if matches.subcommand_matches("build").unwrap().is_present("exclude") { + for ex in exclude { + repos.retain(|x| *x != ex); + } + } + for pkg in packages { if !repos.contains(&pkg) { crash(format!("Package {} not found in repos in mlc.toml", pkg), 3); @@ -166,6 +193,12 @@ fn main() { repository::build(pkg); } } + + if matches.subcommand_matches("build").unwrap().is_present("all") { + for pkg in repos { + repository::build(pkg); + } + } } if let true = matches.is_present("pull") { diff --git a/src/repository/config.rs b/src/repository/config.rs index 0932c22..c62fc76 100644 --- a/src/repository/config.rs +++ b/src/repository/config.rs @@ -6,7 +6,8 @@ use crate::crash; const DEFAULT_CONFIG: &str = r#"mode = "" # either "repository" or "workspace" name = "" # only required when in repository mode, decides what to call the repository and relevant files -repo = [""] # an array of git repos to clone from"#; +repo = [""] # an array of git repos to clone from, formatted url_index::repo_name, e.g. if you had urls = [ "https://example.com/%repo%" ], 1::package would expand to https://example.com/package +urls = [""] # an array of urls to clone from, in the format https://example.com/%repo% (the %repo% is NOT optional)"#; pub fn create_config() { if env::current_dir().unwrap().read_dir().unwrap().next().is_some() { diff --git a/src/workspace/read.rs b/src/workspace/read.rs index 25e75e1..165f8d0 100755 --- a/src/workspace/read.rs +++ b/src/workspace/read.rs @@ -2,7 +2,7 @@ use crate::crash; use std::fs; use std::path::Path; -use crate::internal::structs::Config; +use crate::internal::structs::{Config, SplitRepo, UnexpandedConfig}; pub fn read_cfg() -> Config { if !Path::exists("mlc.toml".as_ref()) { @@ -10,7 +10,34 @@ pub fn read_cfg() -> Config { } let file = fs::read_to_string("mlc.toml").unwrap(); - let config: Config = toml::from_str(&file).unwrap(); + let config: UnexpandedConfig = toml::from_str(&file).unwrap(); - config -} + let mut trimmed_urls: Vec = vec![]; + let mut expanded_repos: Vec = vec![]; + + for url in config.urls { + let a = url.split("%repo%").collect::>()[0]; + let mut b = vec![a.to_string()]; + trimmed_urls.append(&mut b); + } + + let config_repos = config.repo; + for x in config_repos { + let split: Vec<&str> = x.split("::").collect(); + let sr_struct = SplitRepo { + indx: (&split[0]).parse().unwrap(), + name: (&split[1]).parse().unwrap() + }; + let index = sr_struct.indx; + let expanded = format!("{}{}", trimmed_urls[index-1], sr_struct.name); + println!("{}", expanded); + expanded_repos.push(expanded); + } + + Config { + mode: config.mode, + name: config.name, + repo: expanded_repos + } + +} \ No newline at end of file