From 7319514b5da6a47c516f0e7ad90d95f865927c72 Mon Sep 17 00:00:00 2001 From: CloudBridge UY Date: Wed, 15 Mar 2023 10:42:55 -0300 Subject: [PATCH] chore: updated project structure for monorepo development --- .cargo/config.toml | 5 ++++ Cargo.lock | 50 +++++++++++++++++++++++++++++++-- Cargo.toml | 48 +++++++++---------------------- crates/a/Cargo.toml | 32 +++++++++++++++++++++ {src => crates/a/src}/gpt.rs | 0 {src => crates/a/src}/lib.rs | 0 {src => crates/a/src}/main.rs | 0 {src => crates/a/src}/record.rs | 0 {src => crates/a/src}/util.rs | 0 crates/b/Cargo.toml | 21 ++++++++++++++ crates/b/src/lib.rs | 1 + {v2 => crates/b/src}/main.rs | 26 ++++++++++++----- lib/README.md | 5 ++++ xtask/Cargo.toml | 10 +++++++ xtask/src/build.rs | 3 ++ xtask/src/cli.rs | 26 +++++++++++++++++ xtask/src/main.rs | 31 ++++++++++++++++++++ xtask/src/run.rs | 8 ++++++ 18 files changed, 222 insertions(+), 44 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 crates/a/Cargo.toml rename {src => crates/a/src}/gpt.rs (100%) rename {src => crates/a/src}/lib.rs (100%) rename {src => crates/a/src}/main.rs (100%) rename {src => crates/a/src}/record.rs (100%) rename {src => crates/a/src}/util.rs (100%) create mode 100644 crates/b/Cargo.toml create mode 100644 crates/b/src/lib.rs rename {v2 => crates/b/src}/main.rs (74%) create mode 100644 lib/README.md create mode 100644 xtask/Cargo.toml create mode 100644 xtask/src/build.rs create mode 100644 xtask/src/cli.rs create mode 100644 xtask/src/main.rs create mode 100644 xtask/src/run.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..a9c3714 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,5 @@ +[alias] +xtask = "run --package xtask --bin xtask --" + +[env] +CARGO_WORKSPACE_DIR = { value = "", relative = true } diff --git a/Cargo.lock b/Cargo.lock index 3fedfbb..dbdd947 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -61,6 +61,13 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "b" +version = "0.0.0" +dependencies = [ + "clap 4.1.8", +] + [[package]] name = "base64" version = "0.21.0" @@ -449,6 +456,18 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" +[[package]] +name = "duct" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37ae3fc31835f74c2a7ceda3aeede378b0ae2e74c8f1c36559fcc9ae2a4e7d3e" +dependencies = [ + "libc", + "once_cell", + "os_pipe", + "shared_child", +] + [[package]] name = "either" version = "1.8.1" @@ -742,11 +761,10 @@ dependencies = [ [[package]] name = "gpt" -version = "0.1.0" +version = "0.0.0" dependencies = [ "bat", "chrono", - "clap 4.1.8", "copypasta-ext", "env_logger", "log", @@ -1337,6 +1355,16 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "os_pipe" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a53dbb20faf34b16087a931834cba2d7a73cc74af2b7ef345a4c8324e2409a12" +dependencies = [ + "libc", + "windows-sys 0.45.0", +] + [[package]] name = "os_str_bytes" version = "6.4.1" @@ -1676,6 +1704,16 @@ dependencies = [ "yaml-rust", ] +[[package]] +name = "shared_child" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0d94659ad3c2137fef23ae75b03d5241d633f8acded53d672decfa0e6e0caef" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "shell-escape" version = "0.1.5" @@ -2412,6 +2450,14 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3" +[[package]] +name = "xtask" +version = "0.1.0" +dependencies = [ + "clap 4.1.8", + "duct", +] + [[package]] name = "yaml-rust" version = "0.4.5" diff --git a/Cargo.toml b/Cargo.toml index 48a8753..1508f06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,38 +1,16 @@ -[package] -name = "gpt" -description = "A CLI tool to generate and pretty-print code snippets from GPT3" -repository = "https://github.com/guzmonne/gpt" -license = "MIT" -version = "0.1.0" -edition = "2021" -create-type = "bin" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -bat = "0.22.1" -copypasta-ext = "0.4.4" -reqwest = { version = "0.11.14", features = ["blocking"] } -serde = { version = "1.0.152", features = ["derive"] } -serde_json = "1.0.93" -env_logger = "0.10.0" -log = "0.4.17" -chrono = "0.4.23" -clap = { version = "4.1.8", features = ["derive"] } +[workspace] +members = ["xtask/", "lib/*", "crates/*"] -[[bin]] -name = "a" -path = "src/main.rs" - -[[bin]] -name = "v2" -path = "v2/main.rs" - -[lib] -name = "a" -path = "src/lib.rs" +[workspace.package] +edition = "2021" +license = "MIT" +authors = ["Guzmán Monné"] -[features] -clipboard = [] +[profile.dev] +# Disabling debug info speeds up builds. +debug = 0 -default = ["clipboard"] +[profile.release] +incremental = true +# Set this to 1 or 2 to get more useful backtraces in debugger. +debug = 0 diff --git a/crates/a/Cargo.toml b/crates/a/Cargo.toml new file mode 100644 index 0000000..887bdaa --- /dev/null +++ b/crates/a/Cargo.toml @@ -0,0 +1,32 @@ +[package] +name = "gpt" +version = "0.0.0" +homepage = "https://github.com/cloudbridgeuy/a" +description = "A CLI tool to interact with ChatGPT as a Software Engineer" +autobins = false + +authors.workspace = true +edition.workspace = true +license.workspace = true + +[[bin]] +name = "a" +path = "src/main.rs" + +[lib] +name = "a" +path = "src/lib.rs" + +[dependencies] +bat = "0.22.1" +copypasta-ext = "0.4.4" +reqwest = { version = "0.11.14", features = ["blocking"] } +serde = { version = "1.0.152", features = ["derive"] } +serde_json = "1.0.93" +env_logger = "0.10.0" +log = "0.4.17" +chrono = "0.4.23" + +[features] +clipboard = [] +default = ["clipboard"] diff --git a/src/gpt.rs b/crates/a/src/gpt.rs similarity index 100% rename from src/gpt.rs rename to crates/a/src/gpt.rs diff --git a/src/lib.rs b/crates/a/src/lib.rs similarity index 100% rename from src/lib.rs rename to crates/a/src/lib.rs diff --git a/src/main.rs b/crates/a/src/main.rs similarity index 100% rename from src/main.rs rename to crates/a/src/main.rs diff --git a/src/record.rs b/crates/a/src/record.rs similarity index 100% rename from src/record.rs rename to crates/a/src/record.rs diff --git a/src/util.rs b/crates/a/src/util.rs similarity index 100% rename from src/util.rs rename to crates/a/src/util.rs diff --git a/crates/b/Cargo.toml b/crates/b/Cargo.toml new file mode 100644 index 0000000..15e193d --- /dev/null +++ b/crates/b/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "b" +version = "0.0.0" +homepage = "https://github.com/cloudbridgeuy/a" +description = "A CLI tool to interact with ChatGPT as a Software Engineer" +autobins = false + +authors.workspace = true +edition.workspace = true +license.workspace = true + +[[bin]] +name = "b" +path = "src/main.rs" + +[lib] +name = "b" +path = "src/lib.rs" + +[dependencies] +clap = { version = "4.1.8", features = ["derive"] } diff --git a/crates/b/src/lib.rs b/crates/b/src/lib.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/crates/b/src/lib.rs @@ -0,0 +1 @@ + diff --git a/v2/main.rs b/crates/b/src/main.rs similarity index 74% rename from v2/main.rs rename to crates/b/src/main.rs index 0195d7e..606a358 100644 --- a/v2/main.rs +++ b/crates/b/src/main.rs @@ -8,27 +8,35 @@ struct Cli { #[command(subcommand)] command: Option, + /// Name of the chat session. #[arg(short, long)] session: Option, + /// URL endpoint of the OpenAI ChatGPT API. #[arg(short, long, default_value_t=String::from("https://api.openai.com/v1/chat/completions"))] url: String, + /// ChatGPT model to use. #[arg(short, long, default_value_t=String::from("gpt-3.5-turbo"))] model: String, + /// Temperature value of ChatGPT response. #[arg(long, default_value_t=0.0, value_parser = in_range)] temperature: f32, + /// Top-p value of ChatGPT response. #[arg(long, default_value_t=0.8, value_parser = in_range)] top_p: f32, + /// Presence penalty value of ChatGPT response. #[arg(long, default_value_t=0.0, value_parser = in_range)] presence_penalty: f32, + /// Frequencey penalty value of ChatGPT response. #[arg(long, default_value_t=0.0, value_parser = in_range)] frequency_penalty: f32, + /// Prompt that should be send to ChatGPT. prompt: Vec, } @@ -54,7 +62,7 @@ enum Commands { } } -#[derive(Debug, Subcommand)] +#[derive(Debug, Subcommand, Clone)] enum NewCommand { /// Create a new chat session Chat, @@ -65,18 +73,22 @@ fn main() { match cli.command { Some(Commands::Whisper) => { - println!("Whisper to OpenAI"); + handle_whisper(&cli); } - Some(Commands::New { command }) => { - handle_new_command(&command); + Some(Commands::New { ref command }) => { + handle_new(&cli, &command); } None => { - handle_chat_session(&cli); + handle_chat(&cli); } } } -fn handle_new_command(command: &NewCommand) { +fn handle_whisper(_cli: &Cli) { + println!("Whisper to OpenAI"); +} + +fn handle_new(_cli: &Cli, command: &NewCommand) { match command { NewCommand::Chat => { println!("Create a new chat session"); @@ -84,7 +96,7 @@ fn handle_new_command(command: &NewCommand) { } } -fn handle_chat_session(cli: &Cli) { +fn handle_chat(cli: &Cli) { let _url = &cli.url; let _model = &cli.model; let _temperature = &cli.temperature; diff --git a/lib/README.md b/lib/README.md new file mode 100644 index 0000000..5ef8562 --- /dev/null +++ b/lib/README.md @@ -0,0 +1,5 @@ +# /Lib + +> Crates in this directory are published to crates.io and **must obey** semver. + +_We currently don't have anything published. Yet._ diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml new file mode 100644 index 0000000..b9b31f8 --- /dev/null +++ b/xtask/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "xtask" +version = "0.1.0" +publish = false +license = "MIT" +edition = "2021" + +[dependencies] +clap = { version = "4.1.8", features = ["derive"] } # a simple to use, efficient, and full-featured Command Line Argument Parser +duct = "0.13.6" # a library for running child processes diff --git a/xtask/src/build.rs b/xtask/src/build.rs new file mode 100644 index 0000000..48b1673 --- /dev/null +++ b/xtask/src/build.rs @@ -0,0 +1,3 @@ +pub fn script(name: &str) { + println!("Hello, {}!", name); +} diff --git a/xtask/src/cli.rs b/xtask/src/cli.rs new file mode 100644 index 0000000..bf86b5e --- /dev/null +++ b/xtask/src/cli.rs @@ -0,0 +1,26 @@ +use clap::{Parser, Subcommand}; + +#[derive(Debug, Parser)] +#[command(name="xtasks")] +#[command(about="Run project tasks using rust instead of scripts")] +pub struct App { + #[command(subcommand)] + pub command: Option, +} + +#[derive(Debug, Subcommand)] +pub enum Commands { + /// Runs one of the project binaries + Run { + /// Name of the binary to run. + #[arg(short, long)] + name: String, + }, + /// Builds one of the project binaries + Build { + /// Name of the binary to run. + #[arg(short, long)] + name: String, + } +} + diff --git a/xtask/src/main.rs b/xtask/src/main.rs new file mode 100644 index 0000000..0472fc4 --- /dev/null +++ b/xtask/src/main.rs @@ -0,0 +1,31 @@ +//! See +//! +//! This binary defines various auxiliary build commands, which are not +//! expressible with just `cargo`. +//! +//! The binary is integrated into the `cargo` command line by using an +//! alias in `.cargo/config`. + +mod cli; +mod run; +mod build; + +use clap::Parser; + +fn main() -> Result<(), Box> { + let cli = cli::App::parse(); + + match &cli.command { + Some(cli::Commands::Run { name }) => { + run::script(name)?; + return Ok(()); + } + Some(cli::Commands::Build { name }) => { + build::script(name); + return Ok(()); + } + None => { + panic!("No command specified"); + } + } +} diff --git a/xtask/src/run.rs b/xtask/src/run.rs new file mode 100644 index 0000000..48cb42e --- /dev/null +++ b/xtask/src/run.rs @@ -0,0 +1,8 @@ +use std::error::Error; +use duct::cmd; + +pub fn script(name: &str) -> Result<(), Box>{ + cmd!("cargo", "run", "--bin", name).run()?; + + Ok(()) +}