From 992bbfef5a0c5b3f6ea6dfd2838fb8f2c4766af3 Mon Sep 17 00:00:00 2001 From: lowit Date: Tue, 24 Dec 2024 18:33:18 +0300 Subject: [PATCH] feat: add cargo build command --- README.md | 12 ++++++++++++ src/commands/lib.rs | 36 +++++++++++++++++++++++------------- src/commands/plugin/build.rs | 13 +++++++++++++ src/commands/plugin/mod.rs | 1 + src/main.rs | 9 +++++++++ 5 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 src/commands/plugin/build.rs diff --git a/README.md b/README.md index 0dbc28f..31d74b1 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,18 @@ cargo pike plugin pack - `--debug` - Сборка и упаковка debug-версии плагина - `--target-dir ` - Директория собранных бинарных файлов. Значение по умолчанию: `target` +### `plugin build` + +Альяс для команды `cargo build`. + +```bash +cargo pike plugin build +``` + +#### Доступные опции + +- `--release` - Сборка release-версии плагина + ### `config apply` Применение конфигурации сервисов плагина к запущенному командой `run` кластеру пикодаты. diff --git a/src/commands/lib.rs b/src/commands/lib.rs index 98cfeda..31a33e0 100644 --- a/src/commands/lib.rs +++ b/src/commands/lib.rs @@ -1,5 +1,6 @@ use anyhow::{bail, Context, Result}; -use std::process::Command; +use std::io::{BufRead, BufReader, Read}; +use std::process::{Command, Stdio}; pub enum BuildType { Release, @@ -8,19 +9,28 @@ pub enum BuildType { #[allow(clippy::needless_pass_by_value)] pub fn cargo_build(build_type: BuildType) -> Result<()> { - let output = match build_type { - BuildType::Release => Command::new("cargo") - .args(["build", "--release"]) - .output() - .context("running cargo build")?, - BuildType::Debug => Command::new("cargo") - .arg("build") - .output() - .context("running cargo build")?, - }; + let mut args = vec!["build"]; + if let BuildType::Release = build_type { + args.push("--release"); + } + + let mut child = Command::new("cargo") + .args(args) + .stdout(Stdio::piped()) + .spawn() + .context("running cargo build")?; + + let stdout = child.stdout.take().expect("Failed to capture stdout"); + let reader = BufReader::new(stdout); + for line in reader.lines() { + let line = line.unwrap_or_else(|e| format!("{e}")); + print!("{}", line); + } - if !output.status.success() { - bail!("build error: {}", String::from_utf8_lossy(&output.stderr)); + if !child.wait().unwrap().success() { + let mut stderr = String::new(); + child.stderr.unwrap().read_to_string(&mut stderr).unwrap(); + bail!("build error: {stderr}"); } Ok(()) diff --git a/src/commands/plugin/build.rs b/src/commands/plugin/build.rs new file mode 100644 index 0000000..51a83e4 --- /dev/null +++ b/src/commands/plugin/build.rs @@ -0,0 +1,13 @@ +use anyhow::{Context, Result}; +use lib::{cargo_build, BuildType}; + +use crate::commands::lib; + +pub fn cmd(release: bool) -> Result<()> { + let build_type = if release { + BuildType::Release + } else { + BuildType::Debug + }; + cargo_build(build_type).context("building of plugin") +} diff --git a/src/commands/plugin/mod.rs b/src/commands/plugin/mod.rs index 035f129..5607acf 100644 --- a/src/commands/plugin/mod.rs +++ b/src/commands/plugin/mod.rs @@ -1,2 +1,3 @@ pub(crate) mod new; pub(crate) mod pack; +pub(crate) mod build; diff --git a/src/main.rs b/src/main.rs index 2ff1c9c..ec40f1c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,6 +78,11 @@ enum Plugin { #[arg(long, value_name = "TARGET_DIR", default_value = "target")] target_dir: PathBuf, }, + /// Alias for cargo build command + Build { + #[arg(long, short)] + release: bool, + }, /// Create a new Picodata plugin New { #[arg(value_name = "path")] @@ -154,6 +159,10 @@ fn main() -> Result<()> { commands::plugin::pack::cmd(debug, &target_dir) .context("failed to execute \"pack\" command")?; } + Plugin::Build { release } => { + commands::plugin::build::cmd(release) + .context("failed to execute \"build\" command")?; + } Plugin::New { path, without_git,