Skip to content

Commit

Permalink
🚸 Improve output reporting of plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixr-codes committed Oct 19, 2024
1 parent c842e74 commit 40bd16f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
14 changes: 5 additions & 9 deletions src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::config;
use std::ffi::OsStr;
use std::process::Command;
use std::process::{Command, Output};

pub trait Plugin {
/// The optional name of the plugin.
fn name(&self) -> Option<String>;

fn run<I, K, V>(&self, env_vars: I) -> Result<Vec<u8>, Box<dyn std::error::Error>>
fn run<I, K, V>(&self, env_vars: I) -> Result<(String, Output), Box<dyn std::error::Error>>
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
Expand Down Expand Up @@ -59,7 +59,8 @@ impl Plugin for ExecutablePlugin {
self.name.clone()
}

fn run<I, K, V>(&self, env_vars: I) -> Result<Vec<u8>, Box<dyn std::error::Error>>
#[must_use]
fn run<I, K, V>(&self, env_vars: I) -> Result<(String, Output), Box<dyn std::error::Error>>
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
Expand All @@ -69,12 +70,7 @@ impl Plugin for ExecutablePlugin {
let cmd = cmd.args(&self.args).envs(env_vars);
let output = cmd.output()?;
let name = self.name().unwrap_or("<unnamed>".to_string());
if output.status.success() {
log::info!("Successfully run plugin {}", &name);
} else {
log::error!("Plugin {} did not run successful", &name);
}
Ok(output.stdout)
Ok((name, output))
}

fn panic(&self) -> bool {
Expand Down
16 changes: 15 additions & 1 deletion src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,21 @@ impl Project {
);
let result = plugin.run(envs);
match result {
Ok(res) => log::info!("[{}] {}", name, String::from_utf8_lossy(res.as_slice())),
Ok((name, output)) => {
let stdout = String::from_utf8_lossy(output.stdout.as_slice());
let stderr = String::from_utf8_lossy(output.stderr.as_slice());
for line in stdout.lines() {
log::info!("[stdout of {}] {}", name, line);
}
for line in stderr.lines() {
log::error!("[stderr of {}] {}", name, line);
}
if output.status.success() {
log::info!("Plugin {} ran successfully", name);
} else {
log::error!("Plugin {} ran unsuccessfully", name);
}
}
Err(e) => {
log::error!("Failed to run plugin {}: {}", name, e);
if plugin.panic() {
Expand Down

0 comments on commit 40bd16f

Please sign in to comment.