Skip to content

Commit

Permalink
feat: add cargo build command
Browse files Browse the repository at this point in the history
  • Loading branch information
lowitea committed Dec 24, 2024
1 parent 73c3ee9 commit 992bbfe
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ cargo pike plugin pack
- `--debug` - Сборка и упаковка debug-версии плагина
- `--target-dir <TARGET_DIR>` - Директория собранных бинарных файлов. Значение по умолчанию: `target`

### `plugin build`

Альяс для команды `cargo build`.

```bash
cargo pike plugin build
```

#### Доступные опции

- `--release` - Сборка release-версии плагина

### `config apply`

Применение конфигурации сервисов плагина к запущенному командой `run` кластеру пикодаты.
Expand Down
36 changes: 23 additions & 13 deletions src/commands/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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(())
Expand Down
13 changes: 13 additions & 0 deletions src/commands/plugin/build.rs
Original file line number Diff line number Diff line change
@@ -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")
}
1 change: 1 addition & 0 deletions src/commands/plugin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub(crate) mod new;
pub(crate) mod pack;
pub(crate) mod build;
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 992bbfe

Please sign in to comment.