You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
malachite/src/main.rs

190 lines
6.4 KiB
Rust

3 years ago
use std::env;
use std::process::Command;
3 years ago
use clap::{App, AppSettings, Arg, ArgSettings, SubCommand};
use crate::internal::{crash, info};
3 years ago
3 years ago
use crate::workspace::read_cfg;
mod internal;
mod repository;
mod workspace;
3 years ago
fn main() {
fn build_app() -> App<'static, 'static> {
let app = App::new("Malachite")
.version(env!("CARGO_PKG_VERSION"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.arg(
Arg::with_name("verbose")
.short("v")
.long("verbose")
.multiple(true)
.set(ArgSettings::Global)
3 years ago
.help("Sets the level of verbosity"),
3 years ago
)
.arg(
Arg::with_name("exclude")
.short("e")
.long("exclude")
.multiple(true)
.set(ArgSettings::Global)
3 years ago
.help("Excludes packages from given operation"),
3 years ago
)
.arg(
Arg::with_name("all")
.long("all")
.set(ArgSettings::Global)
3 years ago
.help("Operates on every possible package"),
3 years ago
)
.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),
3 years ago
),
)
.subcommand(
3 years ago
SubCommand::with_name("repo-gen").about("Generates repository from build packages"),
3 years ago
)
.subcommand(
SubCommand::with_name("prune")
.about("Prunes duplicate packages older than X days from the repository")
.arg(
Arg::with_name("days")
.help("How old a duplicate package needs to be (in days) to be pruned")
.required(true)
3 years ago
.index(1),
),
)
.subcommand(SubCommand::with_name("init").about(
"Clones all git repositories from mlc.toml branching from current directory",
))
.subcommand(
SubCommand::with_name("pull").alias("update").about(
"Pulls all git repositories from mlc.toml branching from current directory",
),
3 years ago
)
.settings(&[
AppSettings::GlobalVersion,
AppSettings::VersionlessSubcommands,
AppSettings::ArgRequiredElseHelp,
3 years ago
AppSettings::InferSubcommands,
3 years ago
]);
app
}
let matches = build_app().get_matches();
3 years ago
if let true = matches.is_present("init") {
let config = workspace::read_cfg();
if config.mode == "workspace" {
for r in config.repo {
info(format!("Cloning (workspace mode): {}", r));
3 years ago
Command::new("git")
.args(&["clone", &r])
.spawn()
.unwrap()
.wait()
.unwrap();
}
} else if config.mode == "repository" {
for r in config.repo {
info(format!("Cloning (repository mode): {}", r));
3 years ago
Command::new("git")
.args(&["clone", "--no-checkout", &r])
.spawn()
.unwrap()
.wait()
.unwrap();
info(format!("Entering working directory: {}", r));
3 years ago
let dir = format!(
"{}/{}",
env::current_dir().unwrap().display(),
r.split('/').collect::<Vec<&str>>().last().unwrap()
);
env::set_current_dir(dir).unwrap();
info(format!("Resetting unstaged files: {}", r));
3 years ago
Command::new("git")
.arg("reset")
.spawn()
.unwrap()
.wait()
.unwrap();
info(format!("Checking out PKGBUILD: {}", r));
3 years ago
Command::new("git")
.args(&["checkout", "HEAD", "PKGBUILD"])
.spawn()
.unwrap()
.wait()
.unwrap();
}
} else {
crash("Invalid mode in mlc.toml".to_string(), 1);
3 years ago
}
}
if let true = matches.is_present("build") {
let config = workspace::read_cfg();
if config.mode != "repository" {
crash("Cannot build packages in workspace mode".to_string(), 2);
3 years ago
}
let packages: Vec<String> = matches
.subcommand()
.1
.unwrap()
.values_of("package(s)")
.unwrap()
.into_iter()
.map(|s| s.to_string())
.collect();
let mut repos: Vec<String> = vec![];
for r in config.repo {
let split = r.split('/').collect::<Vec<&str>>();
let a = split.last().unwrap();
repos.push(a.parse().unwrap());
}
for pkg in packages {
if !repos.contains(&pkg) {
crash(format!("Package {} not found in repos in mlc.toml", pkg), 3);
3 years ago
} else {
repository::build(pkg);
}
}
}
if let true = matches.is_present("pull") {
let config = workspace::read_cfg();
for r in config.repo {
info(format!("Entering working directory: {}", r));
3 years ago
let dir = format!(
"{}/{}",
env::current_dir().unwrap().display(),
r.split('/').collect::<Vec<&str>>().last().unwrap()
);
env::set_current_dir(dir).unwrap();
Command::new("git")
.args(&["pull", &r])
.spawn()
.unwrap()
.wait()
.unwrap();
}
}
if let true = matches.is_present("repo-gen") {
let config = read_cfg();
if config.mode != "repository" {
panic!("Cannot build packages in workspace mode")
}
repository::generate();
}
}