Skip to content

Commit 16c9983

Browse files
authored
Use bullet stream's built in stream_cmd and time_cmd for consistent output (#393)
* Update bullet_stream and dependencies * Fix compilation Error now returns W so both arms have to match return type of `()` * Use consistent interface * Use consistent interface * Use consistent interface * Use consistent interface * Use consistent interface * Use consistent interface
1 parent 0abf912 commit 16c9983

10 files changed

+265
-258
lines changed

Cargo.lock

+233-209
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ resolver = "2"
33
members = ["buildpacks/ruby", "commons"]
44

55
[workspace.dependencies]
6+
bullet_stream = { version = ">=0.7,<1.0", features = ["fun_run"] }
7+
fun_run = { version = ">=0.5,<1.0", features = ["which_problem"] }
68
cache_diff = { version = "1.1", features = ["bullet_stream"] }
79

810
[workspace.package]
@@ -20,3 +22,4 @@ panic_in_result_fn = "warn"
2022
pedantic = { level = "warn", priority = -1 }
2123
unwrap_used = "warn"
2224
module_name_repetitions = "allow"
25+

buildpacks/ruby/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ rust-version.workspace = true
77
workspace = true
88

99
[dependencies]
10-
bullet_stream = "0.3.0"
10+
bullet_stream.workspace = true
11+
fun_run.workspace = true
12+
cache_diff.workspace = true
1113
clap = { version = "4", default-features = false, features = ["derive", "error-context", "help", "std", "usage"] }
1214
commons = { path = "../../commons" }
1315
flate2 = { version = "1", default-features = false, features = ["zlib"] }
1416
fs-err = "3"
15-
fun_run = { version = "0.2", features = ["which_problem"] }
1617
glob = "0.3"
1718
indoc = "2"
1819
# libcnb has a much bigger impact on buildpack behaviour than any other dependencies,
@@ -30,7 +31,6 @@ ureq = { version = "2", default-features = false, features = ["tls"] }
3031
url = "2"
3132
magic_migrate = "1.0"
3233
toml = "0.8"
33-
cache_diff.workspace = true
3434

3535
[dev-dependencies]
3636
libcnb-test = "=0.26.1"

buildpacks/ruby/src/gem_list.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use bullet_stream::{state::SubBullet, style, Print};
1+
use bullet_stream::{state::SubBullet, Print};
22
use commons::gem_version::GemVersion;
33
use core::str::FromStr;
4-
use fun_run::{CmdError, CommandWithName};
4+
use fun_run::CmdError;
55
use regex::Regex;
66
use std::collections::HashMap;
77
use std::ffi::OsStr;
@@ -22,7 +22,7 @@ pub(crate) struct GemList {
2222
///
2323
/// Errors if the command `bundle list` is unsuccessful.
2424
pub(crate) fn bundle_list<T, K, V>(
25-
bullet: Print<SubBullet<Stdout>>,
25+
mut bullet: Print<SubBullet<Stdout>>,
2626
envs: T,
2727
) -> Result<(Print<SubBullet<Stdout>>, GemList), CmdError>
2828
where
@@ -33,12 +33,11 @@ where
3333
let mut cmd = Command::new("bundle");
3434
cmd.arg("list").env_clear().envs(envs);
3535

36-
let timer = bullet.start_timer(format!("Running {}", style::command(cmd.name())));
37-
let gem_list = cmd
38-
.named_output()
36+
let gem_list = bullet
37+
.time_cmd(&mut cmd)
3938
.map(|output| output.stdout_lossy())
4039
.and_then(|output| GemList::from_str(&output))?;
41-
Ok((timer.done(), gem_list))
40+
Ok((bullet, gem_list))
4241
}
4342

4443
/// Converts the output of `$ gem list` into a data structure that can be inspected and compared

buildpacks/ruby/src/layers/bundle_download_layer.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use crate::RubyBuildpack;
88
use crate::RubyBuildpackError;
99
use bullet_stream::state::SubBullet;
10-
use bullet_stream::{style, Print};
10+
use bullet_stream::Print;
1111
use cache_diff::CacheDiff;
1212
use commons::gemfile_lock::ResolvedBundlerVersion;
1313
use commons::layer::diff_migrate::DiffMigrateLayer;
@@ -80,7 +80,7 @@ pub(crate) struct MetadataV1 {
8080
}
8181

8282
fn download_bundler(
83-
bullet: Print<SubBullet<Stdout>>,
83+
mut bullet: Print<SubBullet<Stdout>>,
8484
env: &Env,
8585
metadata: &Metadata,
8686
gem_path: &Path,
@@ -103,13 +103,12 @@ fn download_bundler(
103103
"--env-shebang", // Start the `bundle` executable with `#! /usr/bin/env ruby`
104104
]);
105105

106-
let timer = bullet.start_timer(format!("Running {}", style::command(short_name)));
107-
108-
cmd.named_output()
106+
bullet
107+
.time_cmd(&mut cmd.named(short_name))
109108
.map_err(|error| fun_run::map_which_problem(error, cmd.mut_cmd(), env.get("PATH").cloned()))
110109
.map_err(RubyBuildpackError::GemInstallBundlerCommandError)?;
111110

112-
Ok(timer.done())
111+
Ok(bullet)
113112
}
114113

115114
#[cfg(test)]

buildpacks/ruby/src/layers/bundle_install_layer.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,8 @@ pub(crate) fn handle(
9090
cmd.args(["install"])
9191
.env_clear() // Current process env vars already merged into env
9292
.envs(&env);
93-
let mut cmd = cmd.named_fn(|cmd| display_name(cmd, &env));
9493
bullet
95-
.stream_with(
96-
format!("Running {}", style::command(cmd.name())),
97-
|stdout, stderr| cmd.stream_output(stdout, stderr),
98-
)
94+
.stream_cmd(&mut cmd.named_fn(|cmd| display_name(cmd, &env)))
9995
.map_err(|error| {
10096
fun_run::map_which_problem(error, cmd.mut_cmd(), env.get("PATH").cloned())
10197
})

buildpacks/ruby/src/rake_task_detect.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use bullet_stream::{
2-
state::SubBullet,
3-
{style, Print},
4-
};
1+
use bullet_stream::{state::SubBullet, Print};
52
use core::str::FromStr;
6-
use fun_run::{CmdError, CommandWithName};
3+
use fun_run::CmdError;
74
use std::io::Stdout;
85
use std::{ffi::OsStr, process::Command};
96

@@ -25,15 +22,14 @@ pub(crate) struct RakeDetect {
2522
///
2623
/// Will return `Err` if `bundle exec rake -p` command cannot be invoked by the operating system.
2724
pub(crate) fn call<T: IntoIterator<Item = (K, V)>, K: AsRef<OsStr>, V: AsRef<OsStr>>(
28-
bullet: Print<SubBullet<Stdout>>,
25+
mut bullet: Print<SubBullet<Stdout>>,
2926
envs: T,
3027
error_on_failure: bool,
3128
) -> Result<(Print<SubBullet<Stdout>>, RakeDetect), CmdError> {
3229
let mut cmd = Command::new("rake");
3330
cmd.args(["-P", "--trace"]).env_clear().envs(envs);
3431

35-
let timer = bullet.start_timer(format!("Running {}", style::command(cmd.name())));
36-
let output = cmd.named_output().or_else(|error| {
32+
let output = bullet.time_cmd(cmd).or_else(|error| {
3733
if error_on_failure {
3834
Err(error)
3935
} else {
@@ -45,7 +41,7 @@ pub(crate) fn call<T: IntoIterator<Item = (K, V)>, K: AsRef<OsStr>, V: AsRef<OsS
4541
}
4642
})?;
4743

48-
RakeDetect::from_str(&output.stdout_lossy()).map(|rake_detect| (timer.done(), rake_detect))
44+
RakeDetect::from_str(&output.stdout_lossy()).map(|rake_detect| (bullet, rake_detect))
4945
}
5046

5147
impl RakeDetect {

buildpacks/ruby/src/steps/rake_assets_install.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::RubyBuildpackError;
44
use bullet_stream::state::SubBullet;
55
use bullet_stream::{style, Print};
66
use commons::cache::{mib, AppCache, CacheConfig, CacheError, CacheState, KeepPath, PathState};
7-
use fun_run::{self, CommandWithName};
87
use libcnb::build::BuildContext;
98
use libcnb::Env;
109
use std::io::Stdout;
@@ -39,10 +38,7 @@ pub(crate) fn rake_assets_install(
3938
.envs(env);
4039

4140
bullet
42-
.stream_with(
43-
format!("Running {}", style::command(cmd.name())),
44-
|stdout, stderr| cmd.stream_output(stdout, stderr),
45-
)
41+
.stream_cmd(&mut cmd)
4642
.map_err(|error| {
4743
fun_run::map_which_problem(error, &mut cmd, env.get("PATH").cloned())
4844
})
@@ -85,10 +81,7 @@ pub(crate) fn rake_assets_install(
8581
.envs(env);
8682

8783
bullet
88-
.stream_with(
89-
format!("Running {}", style::command(cmd.name())),
90-
|stdout, stderr| cmd.stream_output(stdout, stderr),
91-
)
84+
.stream_cmd(&mut cmd)
9285
.map_err(|error| {
9386
fun_run::map_which_problem(error, &mut cmd, env.get("PATH").cloned())
9487
})

buildpacks/ruby/src/user_errors.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{DetectError, RubyBuildpackError};
22
use bullet_stream::{state::Bullet, state::SubBullet, style, Print};
3-
use fun_run::{CmdError, CommandWithName};
3+
use fun_run::CmdError;
44
use indoc::formatdoc;
55
use std::io::Stdout;
66
use std::process::Command;
@@ -11,7 +11,7 @@ pub(crate) fn on_error(err: libcnb::Error<RubyBuildpackError>) {
1111
let debug_info = style::important(DEBUG_INFO_STR);
1212
match cause(err) {
1313
Cause::OurError(error) => log_our_error(output, error),
14-
Cause::FrameworkError(error) =>
14+
Cause::FrameworkError(error) => {
1515
output
1616
.bullet(&debug_info)
1717
.sub_bullet(error.to_string())
@@ -28,7 +28,8 @@ pub(crate) fn on_error(err: libcnb::Error<RubyBuildpackError>) {
2828
If the issue persists, please try to reproduce the behavior locally using the `pack`
2929
CLI. If you can reproduce the behavior locally and believe you've found a bug in the
3030
buildpack or the framework please open an issue on the buildpack's GitHub repository.
31-
"}),
31+
"});
32+
}
3233
};
3334
}
3435

@@ -340,11 +341,7 @@ fn replace_app_path_with_relative(contents: impl AsRef<str>) -> String {
340341
}
341342

342343
fn debug_cmd(mut log: Print<SubBullet<Stdout>>, command: &mut Command) -> Print<Bullet<Stdout>> {
343-
let result = log.stream_with(
344-
format!("Running debug command {}", style::command(command.name())),
345-
|stdout, stderr| command.stream_output(stdout, stderr),
346-
);
347-
match result {
344+
match log.stream_cmd(command) {
348345
Ok(_) => log.done(),
349346
Err(e) => log.sub_bullet(e.to_string()).done(),
350347
}

commons/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ rust-version.workspace = true
77
workspace = true
88

99
[dependencies]
10+
cache_diff.workspace = true
1011
byte-unit = "5"
1112
# TODO: Consolidate on either the regex crate or the fancy-regex crate, since this repo currently uses both.
1213
fancy-regex = "0.14"
@@ -25,9 +26,8 @@ walkdir = "2"
2526
filetime = "0.2"
2627
magic_migrate = "1.0.1"
2728
toml = "0.8"
28-
cache_diff.workspace = true
2929

3030
[dev-dependencies]
31+
bullet_stream.workspace = true
3132
filetime = "0.2"
3233
toml = "0.8"
33-
bullet_stream = "0.3.0"

0 commit comments

Comments
 (0)