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

162 lines
5.3 KiB
Rust

use std::env;
use std::path::Path;
3 years ago
use std::process::Command;
use crate::internal::{crash, info};
use crate::repository::create_config;
use clap::{App, AppSettings, Arg, ArgSettings, SubCommand};
3 years ago
3 years ago
use crate::workspace::read_cfg;
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
3 years ago
mod internal;
3 years ago
mod operations;
3 years ago
mod repository;
mod workspace;
3 years ago
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);
}
3 years ago
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
)
.subcommand(
SubCommand::with_name("build")
.about("Builds the given packages")
.arg(
Arg::with_name("package(s)")
.help("The packages to operate on")
.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"),
)
.arg(
Arg::with_name("no-regen")
.short("n")
.long("no-regen")
.help("Does not regenerate repository after building given package(s)"),
),
3 years ago
)
.subcommand(
SubCommand::with_name("repo-gen").about("Generates repository from built packages"),
3 years ago
)
.subcommand(
SubCommand::with_name("prune")
.about("Prunes duplicate packages from the repository"),
3 years ago
)
.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",
)
.arg(
Arg::with_name("package(s)")
.help("The packages to operate on")
.multiple(true)
.index(1),
),
3 years ago
)
.subcommand(
SubCommand::with_name("config").about("Create and/or open local config file"),
)
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();
if Path::exists("mlc.toml".as_ref()) && Path::exists(".git".as_ref()) {
info(
"In a git repository, pulling latest mlc.toml. It is advised you run mlc pull/update"
.to_string(),
);
Command::new("git")
.arg("pull")
.spawn()
.unwrap()
.wait()
.unwrap();
}
if Path::exists("../.git".as_ref()) {
info("Parent directory is a git directory, pulling latest mlc.toml. It is advised you run mlc pull/update in all malachite directories".to_string());
let dir = env::current_dir().unwrap();
env::set_current_dir("../").unwrap();
Command::new("git")
.arg("pull")
.spawn()
.unwrap()
.wait()
.unwrap();
env::set_current_dir(dir).unwrap();
}
3 years ago
if let true = matches.is_present("init") {
operations::init();
3 years ago
}
if let true = matches.is_present("build") {
3 years ago
operations::build(&matches);
3 years ago
}
if let true = matches.is_present("pull") {
operations::pull(&matches);
3 years ago
}
if let true = matches.is_present("repo-gen") {
let config = read_cfg();
if config.mode != "repository" {
panic!("Cannot build packages in workspace mode")
}
info(format!("Generating repository: {}", config.name.unwrap()));
3 years ago
repository::generate();
}
if let true = matches.is_present("prune") {
operations::prune();
}
if let true = matches.is_present("config") {
operations::config();
}
3 years ago
}