diff options
| author | Fuwn <[email protected]> | 2022-02-07 04:26:07 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-06-09 00:46:03 -0700 |
| commit | bfa483c6aa5db5c9825faded62176904d516faf7 (patch) | |
| tree | 043b73bdd939f955f2b4d1e6309c8b697d6e0544 /crates/divina/src | |
| download | archived-divina-bfa483c6aa5db5c9825faded62176904d516faf7.tar.xz archived-divina-bfa483c6aa5db5c9825faded62176904d516faf7.zip | |
feat(divina): pre-release :star:
Diffstat (limited to 'crates/divina/src')
| -rw-r--r-- | crates/divina/src/cli.rs | 79 | ||||
| -rw-r--r-- | crates/divina/src/lib.rs | 75 | ||||
| -rw-r--r-- | crates/divina/src/main.rs | 22 |
3 files changed, 176 insertions, 0 deletions
diff --git a/crates/divina/src/cli.rs b/crates/divina/src/cli.rs new file mode 100644 index 0000000..6034b38 --- /dev/null +++ b/crates/divina/src/cli.rs @@ -0,0 +1,79 @@ +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +use structopt::clap::{App, AppSettings, Arg, SubCommand}; + +/// Create CLI +fn cli() -> App<'static, 'static> { + App::new(env!("CARGO_PKG_NAME")) + .about(env!("CARGO_PKG_DESCRIPTION")) + .version(env!("CARGO_PKG_VERSION")) + .author(env!("CARGO_PKG_AUTHORS")) + .setting(AppSettings::SubcommandRequiredElseHelp) + .subcommands(vec![ + SubCommand::with_name("init").about("").args(&[ + Arg::with_name("type") + .long("type") + .takes_value(true) + .possible_values(&["bin", "lib"]), + Arg::with_name("git").long("git").takes_value(true), + Arg::with_name("path") + .required(true) + .index(1) + .takes_value(true), + ]), + SubCommand::with_name("build").about("Build your project"), + SubCommand::with_name("clean") + .about("Cleanup Divina's non-essential temporary files and directories"), + SubCommand::with_name("config") + .about("") + .setting(AppSettings::SubcommandRequiredElseHelp) + .subcommands(vec![ + SubCommand::with_name("show").about("Print your configuration"), + SubCommand::with_name("validate").about("Check if your configuration will compile"), + ]), + ]) + .args(&[ + Arg::with_name("debug").short("d").long("debug"), + Arg::with_name("trace").short("t").long("trace"), + ]) +} + +/// Execute CLI +pub fn execute(divina: &mut crate::Divina) { + let matches = cli().get_matches(); + + match matches.subcommand() { + ("init", Some(s_matches)) => { + let repository = s_matches + .value_of("git") + .unwrap_or("https://github.com/divinaland/init.git"); + let path = s_matches.value_of("path").unwrap_or_else(|| unreachable!()); + + divina_git::clone(repository, &format!("./{}", path)) + .expect("!! could to clone init repository, perhaps the repository is invalid ?"); + } + ("build", Some(_s_matches)) => { + divina + .compiler + .find_sources(&divina.expose_config().clone()) + .compile() + .link(); + } + ("clean", Some(_s_matches)) => + if std::path::Path::new("out/").exists() { + println!(":: removing directory 'out/'"); + std::fs::remove_dir_all("out/") + .expect("!! could not remove directory 'out/', check permissions"); + } else { + println!(":: directory 'out/' does not exist"); + }, + ("config", Some(s_matches)) => + match s_matches.subcommand() { + ("show", _) => divina.print_config(), + ("validate", _) => println!(":: no issues found"), + _ => unreachable!(), + }, + _ => unreachable!(), + } +} diff --git a/crates/divina/src/lib.rs b/crates/divina/src/lib.rs new file mode 100644 index 0000000..675efd2 --- /dev/null +++ b/crates/divina/src/lib.rs @@ -0,0 +1,75 @@ +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +#![deny( + warnings, + nonstandard_style, + unused, + future_incompatible, + rust_2018_idioms, + unsafe_code +)] +#![deny(clippy::all, clippy::nursery, clippy::pedantic)] +#![recursion_limit = "128"] +#![doc( + html_logo_url = "https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/160/twitter/282/ribbon_1f380.png", + html_favicon_url = "https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/160/twitter/282/ribbon_1f380.png" +)] + +// #[macro_use] +// extern crate log; + +#[cfg(windows)] +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; + +#[cfg(unix)] +#[global_allocator] +static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc; + +mod cli; + +use divina_compile::Compiler; +use divina_config::Config; + +#[derive(Default)] +pub struct Divina { + config: Config, + compiler: Compiler, +} +impl Divina { + #[must_use] + pub fn new() -> Self { Self::default() } + + pub fn perform(&mut self) { crate::cli::execute(self); } + + /// Prepare `self.config` for configuration + pub fn new_config(&mut self) -> &mut Self { + self.config = Config::new(); + self + } + + /// Configure `self.config` + pub fn configure_config(&mut self) { self.config.configure("Divina.lua"); } + + /// Print `self.config` + pub fn print_config(&self) { + println!("{:?}", self.config); + } + + #[must_use] + pub const fn expose_config(&self) -> &Config { &self.config } + + pub fn configure_compiler(&mut self, compiler: Compiler) { self.compiler = compiler; } +} + +/// Preliminary setup +pub fn setup() { + dotenv::dotenv().ok(); + human_panic::setup_panic!(Metadata { + version: env!("CARGO_PKG_VERSION"), + name: env!("CARGO_PKG_NAME"), + authors: env!("CARGO_PKG_AUTHORS"), + homepage: env!("CARGO_PKG_HOMEPAGE"), + }); +} diff --git a/crates/divina/src/main.rs b/crates/divina/src/main.rs new file mode 100644 index 0000000..dc7e17e --- /dev/null +++ b/crates/divina/src/main.rs @@ -0,0 +1,22 @@ +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +use divina::Divina; + +#[tokio::main] +async fn main() { + // Preliminary pokes + divina::setup(); + + let mut divina = Divina::new(); + // Store 'Divina.lua' configuration + divina.new_config().configure_config(); + // Create a new compiler + divina.configure_compiler(divina_compile::Compiler::new()); + // Handle CLI + divina.perform(); + + // Process doesn't exit on Unix properly, this solves it... + #[cfg(unix)] + std::process::exit(0); +} |