From 1796f12ef618fa63dba9c1d05900c5b935bb7779 Mon Sep 17 00:00:00 2001 From: Luca Fulchir Date: Sat, 14 Dec 2024 20:39:54 +0100 Subject: [PATCH] Extremely basic `bok` util that builds a lib Signed-off-by: Luca Fulchir --- Cargo.lock | 301 ++++++++++++++++++++++++++++++- bok-utils/Cargo.toml | 9 + bok-utils/examples/repos/lib.rs | 29 +++ bok-utils/examples/repos/main.rs | 82 --------- bok-utils/src/bin/bok.rs | 149 ++++++++++++++- 5 files changed, 477 insertions(+), 93 deletions(-) create mode 100644 bok-utils/examples/repos/lib.rs delete mode 100644 bok-utils/examples/repos/main.rs diff --git a/Cargo.lock b/Cargo.lock index bf553f5..7084876 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,7 +38,7 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" dependencies = [ - "windows-sys", + "windows-sys 0.59.0", ] [[package]] @@ -48,7 +48,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.59.0", ] [[package]] @@ -77,7 +77,7 @@ dependencies = [ "quote", "semver", "sha3", - "thiserror", + "thiserror 2.0.3", ] [[package]] @@ -103,6 +103,10 @@ dependencies = [ "proc-macro2", "quote", "sha3", + "slog", + "slog-async", + "slog-term", + "strum", "syn", ] @@ -198,6 +202,21 @@ dependencies = [ "libc", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + [[package]] name = "crunchy" version = "0.2.2" @@ -214,6 +233,15 @@ dependencies = [ "typenum", ] +[[package]] +name = "deranged" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" +dependencies = [ + "powerfmt", +] + [[package]] name = "derive-syn-parse" version = "0.2.0" @@ -235,6 +263,27 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if", + "dirs-sys-next", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -262,12 +311,35 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hermit-abi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" + +[[package]] +name = "is-terminal" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "itoa" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" + [[package]] name = "keccak" version = "0.1.5" @@ -283,6 +355,16 @@ version = "0.2.162" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags", + "libc", +] + [[package]] name = "macro_magic" version = "0.5.1" @@ -331,12 +413,24 @@ dependencies = [ "syn", ] +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + [[package]] name = "once_cell" version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "prettyplease" version = "0.2.25" @@ -365,12 +459,49 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom", + "libredox", + "thiserror 1.0.69", +] + +[[package]] +name = "rustversion" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248" + [[package]] name = "semver" version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" +[[package]] +name = "serde" +version = "1.0.216" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.216" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "sha3" version = "0.10.8" @@ -381,12 +512,65 @@ dependencies = [ "keccak", ] +[[package]] +name = "slog" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8347046d4ebd943127157b94d63abb990fcf729dc4e9978927fdf4ac3c998d06" + +[[package]] +name = "slog-async" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72c8038f898a2c79507940990f05386455b3a317d8f18d4caea7cbc3d5096b84" +dependencies = [ + "crossbeam-channel", + "slog", + "take_mut", + "thread_local", +] + +[[package]] +name = "slog-term" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6e022d0b998abfe5c3782c1f03551a596269450ccd677ea51c56f8b214610e8" +dependencies = [ + "is-terminal", + "slog", + "term", + "thread_local", + "time", +] + [[package]] name = "strsim" version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "strum" +version = "0.26.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + [[package]] name = "syn" version = "2.0.87" @@ -398,13 +582,50 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "take_mut" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60" + +[[package]] +name = "term" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f" +dependencies = [ + "dirs-next", + "rustversion", + "winapi", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + [[package]] name = "thiserror" version = "2.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa" dependencies = [ - "thiserror-impl", + "thiserror-impl 2.0.3", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -418,6 +639,47 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + +[[package]] +name = "time" +version = "0.3.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35e7868883861bd0e56d9ac6efcaaca0d6d5d82a2a7ec8209ff492c07cf37b21" +dependencies = [ + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2834e6017e3e5e4b9834939793b282bc03b37a3336245fa820e35e233e2a85de" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "tiny-keccak" version = "2.0.2" @@ -475,6 +737,37 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.59.0" diff --git a/bok-utils/Cargo.toml b/bok-utils/Cargo.toml index 59fae52..51a8392 100644 --- a/bok-utils/Cargo.toml +++ b/bok-utils/Cargo.toml @@ -9,6 +9,11 @@ license = "Apache-2.0 WITH LLVM-exception" keywords = [ "bok" ] categories = [ "config" ] +[[example]] +name = "repos" +crate-type = ["lib"] +path = "examples/repos/lib.rs" + [dependencies] clap = { version = "4.5", features = [ "derive", "unicode" ]} bok = { path = "../bok" } @@ -17,5 +22,9 @@ macro_magic = { version = "0.5" } prettyplease = "0.2" proc-macro2 = "1.0" quote = "1.0" +slog = { version = "2.7", features = ["max_level_trace", "release_max_level_debug"] } +slog-term = { version = "2.9" } +slog-async = { version = "2.8" } +strum = { version = "0.26", features = [ "derive" ] } syn = { version = "2", features = [ "full", "parsing", "extra-traits" ] } sha3 = { version = "0.10" } diff --git a/bok-utils/examples/repos/lib.rs b/bok-utils/examples/repos/lib.rs new file mode 100644 index 0000000..657bad6 --- /dev/null +++ b/bok-utils/examples/repos/lib.rs @@ -0,0 +1,29 @@ +/* + * Copyright 2024 Luca Fulchir + * + * Licensed under the Apache License, Version 2.0 with LLVM exception (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License and of the exception at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * https://spdx.org/licenses/LLVM-exception.html + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//! +//! Example of two package repositories, where one +//! extends ancd changes another + +// all packages we have +pub mod pkgs; + +pub mod r1; +pub mod r2; + +pub use r1::R1; +pub use r2::R2; diff --git a/bok-utils/examples/repos/main.rs b/bok-utils/examples/repos/main.rs deleted file mode 100644 index 14d4e64..0000000 --- a/bok-utils/examples/repos/main.rs +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2024 Luca Fulchir - * - * Licensed under the Apache License, Version 2.0 with LLVM exception (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License and of the exception at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * https://spdx.org/licenses/LLVM-exception.html - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -//! -//! Example of two package repositories, where one -//! extends ancd changes another - -// all packages we have -pub mod pkgs; - -pub mod r1; -pub mod r2; - -pub use r1::R1; -pub use r2::R2; - -fn main() { - //let one = repos::pkgs::one::One::default(); - - use ::std::sync::Arc; - let pkgs1 = Arc::new(R1::default()); - let pkgs2 = Arc::new(R2::default()); - use ::bok::Repository; - println!("pkgs1: {}", pkgs1.name()); - println!("pkgs2: {}", pkgs2.name()); - /* - let build_deps = ::bok::deps::Build(::bok::Collection::new()); - let run_deps = ::bok::deps::Runtime(::bok::Collection::new()); - - let one = pkgs1.one(); - use ::bok::Pkg; - //let res = one.dependencies_set(pkgs1, &build_deps, &run_deps); - //println!("{:?}", res); - - let two = pkgs2.two(); - //let res = two.dependencies_set(pkgs2, &build_deps, &run_deps); - //println!("{:?}", res); - */ - - /* - let mut pb: pkgs::one::OneBuilder = pkgs1.one(); - - let b = pb.as_builder(); - let a = pb.as_any_mut(); - let m_pb2: Option<&mut pkgs::one::OneBuilder> = a.downcast_mut(); - use ::bok::{Pkg, PkgBuilder}; - println!("m_pb2: {:?}", m_pb2); - let m_pkg = pb.default_unused().build(); - println!("m_pb2: {:?}", m_pkg); - if let Ok(pkg) = m_pkg { - println!("one: {}: {:?}", pkg.name(), pkg); - println!("hash one: {}", pkg.hash()); - println!("hash one code: {}", pkg.hash_code()); - } - */ - - /* - println!("pkgs1 - 1:{}", pkgs1.one().default_unused().build().my_attr); - println!("pkgs1 - 2:{}", pkgs1.two().my_attr); - println!("pkgs2 - 1:{}", pkgs2.one().my_attr); - println!("pkgs2 - 2:{}", pkgs2.two().my_attr); - println!("pkgs2 - 3:{}", pkgs2.three().my_attr); - - println!("pkgs2-1:\n{}", pkgs2.one()); - println!("pkgs2-2:\n{}", pkgs2.two()); - println!("pkgs2-3:\n{}", pkgs2.three()); - */ -} diff --git a/bok-utils/src/bin/bok.rs b/bok-utils/src/bin/bok.rs index 0eb1564..d89532f 100644 --- a/bok-utils/src/bin/bok.rs +++ b/bok-utils/src/bin/bok.rs @@ -17,25 +17,160 @@ use ::clap::Parser; -/// Simple program to greet a person +enum ExitCode { + Ok(), + Generic(), + CompileFailed(), +} + +impl Into<::std::process::ExitCode> for ExitCode { + fn into(self) -> ::std::process::ExitCode { + match self { + ExitCode::Ok() => ::std::process::ExitCode::SUCCESS, + ExitCode::Generic() => ::std::process::ExitCode::from(1), + ExitCode::CompileFailed() => ::std::process::ExitCode::from(1), + } + } +} + +/// Main arguments for bok #[derive(::clap::Parser, Debug)] -#[command(version, about, long_about = None)] -struct Args { +#[command(name="bok", about="tools to build and configure systems", + version, about, long_about = None)] +struct Cli { /// subcommand #[command(subcommand)] - command: Commands, + command: MainCmd, + /// log level of bok messages + #[arg(long, default_value = "info")] + log: LogLevel, } /// Available subcommands for `bok` #[derive(Debug, ::clap::Subcommand)] -enum Commands { +enum MainCmd { /// Build Build { #[arg(long, short)] path: Option<::std::ffi::OsString>, + #[arg(long, short, group = "target")] + example: Option, + #[arg(long, short, group = "target")] + lib: Option, }, + /// Repository stuff + Repo(RepoArgs), } -fn main() { - let args = Args::parse(); +#[derive( + ::clap::ValueEnum, ::strum::Display, Copy, Clone, Debug, PartialEq, Eq, +)] +enum LogLevel { + Error, + Warn, + Info, + Debug, + Trace, +} + +impl Into<::slog::Level> for LogLevel { + fn into(self) -> ::slog::Level { + match self { + LogLevel::Error => ::slog::Level::Error, + LogLevel::Warn => ::slog::Level::Warning, + LogLevel::Info => ::slog::Level::Info, + LogLevel::Debug => ::slog::Level::Debug, + LogLevel::Trace => ::slog::Level::Trace, + } + } +} + +/// Handle a repository +#[derive(Debug, ::clap::Args)] +struct RepoArgs { + #[arg(required = true)] + name: ::bok::RepoName, + #[command(subcommand)] + commands: RepoCommands, +} + +#[derive(Debug, ::clap::Subcommand)] +enum RepoCommands { + List {}, +} + +fn main() -> ::std::process::ExitCode { + let args = Cli::parse(); + + let log = { + use ::slog::Drain; + let decorator = ::slog_term::TermDecorator::new().build(); + let drain = ::slog_term::FullFormat::new(decorator).build().fuse(); + let drain = ::slog_async::Async::new(drain).build().fuse(); + let drain = ::slog::LevelFilter::new(drain, args.log.into()).fuse(); + + let log = ::slog::Logger::root(drain, ::slog::o!()); + log + }; + + match args.command { + MainCmd::Build { path, example, lib } => { + build(log, path, example, lib).into() + } + MainCmd::Repo(_) => { + ::slog::info!(log, "got repo cmd"); + ExitCode::Generic().into() + } + } +} + +fn build( + log: ::slog::Logger, + path: Option<::std::ffi::OsString>, + example: Option, + lib: Option, +) -> ExitCode { + if let Some(dir) = path { + if let Err(_) = ::std::env::set_current_dir(&dir) { + ::slog::error!(log, "can't change dir to {:?}", dir); + return ExitCode::Generic(); + }; + ::slog::debug!(log, "changed dir to {:?}", dir); + }; + + if !::std::path::Path::new("./Cargo.toml").exists() { + ::slog::error!( + log, + "path does not have the 'Cargo.toml' file. Can't build" + ); + return ExitCode::Generic(); + } + + let mut cargo = ::std::process::Command::new("cargo"); + cargo.arg("build"); + if let Some(example_name) = example { + cargo.arg("--example").arg(example_name); + } + if let Some(lib_name) = lib { + cargo.arg("--lib").arg(lib_name); + } + + cargo.stderr(::std::process::Stdio::null()); + cargo.stdout(::std::process::Stdio::null()); + let res = cargo.status(); + match res { + Ok(code) => { + if code.success() { + ::slog::info!(log, "compilation successful"); + ExitCode::Ok() + } else { + ::slog::error!(log, "cargo exited with error"); + ExitCode::CompileFailed() + } + } + Err(_) => { + ::slog::error!(log, "can't run cargo"); + ExitCode::CompileFailed() + } + } }