Skip to content

Commit

Permalink
Update logging to bullet_stream
Browse files Browse the repository at this point in the history
  • Loading branch information
schneems committed Oct 2, 2024
1 parent 6080b50 commit f23beb9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
37 changes: 19 additions & 18 deletions buildpacks/ruby/src/layers/bundle_download_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
use crate::layers::shared::{cached_layer_write_metadata, MetadataDiff};
use crate::RubyBuildpack;
use crate::RubyBuildpackError;
use bullet_stream::style;
use bullet_stream::state::SubBullet;
use bullet_stream::{style, Print};
use commons::gemfile_lock::ResolvedBundlerVersion;
use commons::output::{
fmt,
section_log::{log_step, log_step_timed},
};
use fun_run::{self, CommandWithName};
use libcnb::data::layer_name;
use libcnb::layer::{EmptyLayerCause, LayerState};
Expand All @@ -21,32 +18,36 @@ use libcnb::Env;
use magic_migrate::{try_migrate_deserializer_chain, TryMigrate};
use serde::{Deserialize, Deserializer, Serialize};
use std::convert::Infallible;
use std::io::Stdout;
use std::path::Path;
use std::process::Command;

pub(crate) fn handle(
context: &libcnb::build::BuildContext<RubyBuildpack>,
env: &Env,
mut bullet: Print<SubBullet<Stdout>>,
metadata: &Metadata,
) -> libcnb::Result<LayerEnv, RubyBuildpackError> {
) -> libcnb::Result<(Print<SubBullet<Stdout>>, LayerEnv), RubyBuildpackError> {
let layer_ref = cached_layer_write_metadata(layer_name!("bundler"), context, metadata)?;
match &layer_ref.state {
LayerState::Restored { cause } => {
log_step(cause);
bullet = bullet.sub_bullet(cause);
Ok((bullet, layer_ref.read_env()?))
}
LayerState::Empty { cause } => {
match cause {
EmptyLayerCause::NewlyCreated => {}
EmptyLayerCause::InvalidMetadataAction { cause }
| EmptyLayerCause::RestoredLayerAction { cause } => {
log_step(cause);
bullet = bullet.sub_bullet(cause);
}
}
let layer_env = download_bundler(env, metadata, &layer_ref.path())?;
let (bullet, layer_env) = download_bundler(bullet, env, metadata, &layer_ref.path())?;
layer_ref.write_env(&layer_env)?;

Ok((bullet, layer_ref.read_env()?))
}
}
Ok(layer_ref.read_env()?)
}

pub(crate) type Metadata = MetadataV1;
Expand Down Expand Up @@ -81,10 +82,11 @@ pub(crate) enum MetadataError {
}

fn download_bundler(
bullet: Print<SubBullet<Stdout>>,
env: &Env,
metadata: &Metadata,
path: &Path,
) -> Result<LayerEnv, RubyBuildpackError> {
) -> Result<(Print<SubBullet<Stdout>>, LayerEnv), RubyBuildpackError> {
let bin_dir = path.join("bin");
let gem_path = path;

Expand All @@ -104,12 +106,11 @@ fn download_bundler(
"--env-shebang", // Start the `bundle` executable with `#! /usr/bin/env ruby`
]);

log_step_timed(format!("Running {}", fmt::command(short_name)), || {
cmd.named_output().map_err(|error| {
fun_run::map_which_problem(error, cmd.mut_cmd(), env.get("PATH").cloned())
})
})
.map_err(RubyBuildpackError::GemInstallBundlerCommandError)?;
let timer = bullet.start_timer(format!("Running {}", style::command(short_name)));

cmd.named_output()
.map_err(|error| fun_run::map_which_problem(error, cmd.mut_cmd(), env.get("PATH").cloned()))
.map_err(RubyBuildpackError::GemInstallBundlerCommandError)?;

let layer_env = LayerEnv::new()
.chainable_insert(Scope::All, ModificationBehavior::Delimiter, "PATH", ":")
Expand All @@ -127,7 +128,7 @@ fn download_bundler(
gem_path,
);

Ok(layer_env)
Ok((timer.done(), layer_env))
}

#[cfg(test)]
Expand Down
19 changes: 10 additions & 9 deletions buildpacks/ruby/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use bullet_stream::{style, Print};
use commons::cache::CacheError;
use commons::gemfile_lock::GemfileLock;
use commons::metadata_digest::MetadataDigest;
use commons::output::warn_later::WarnGuard;
#[allow(clippy::wildcard_imports)]
use commons::output::{build_log::*, fmt};
use commons::output::build_log::*;
use commons::output::warn_later::WarnGuard;
use core::str::FromStr;
use fs_err::PathExt;
use fun_run::CmdError;
Expand Down Expand Up @@ -148,7 +148,7 @@ impl Buildpack for RubyBuildpack {
};

// ## Install executable ruby version
(_, env) = {
(build_output, env) = {
let bullet = build_output.bullet(format!(
"Ruby version {} from {}",
style::value(ruby_version.to_string()),
Expand All @@ -169,21 +169,22 @@ impl Buildpack for RubyBuildpack {
};

// ## Setup bundler
(logger, env) = {
let section = logger.section(&format!(
(_, env) = {
let bullet = build_output.bullet(format!(
"Bundler version {} from {}",
fmt::value(bundler_version.to_string()),
fmt::value(gemfile_lock.bundler_source())
style::value(bundler_version.to_string()),
style::value(gemfile_lock.bundler_source())
));
let layer_env = layers::bundle_download_layer::handle(
let (bullet, layer_env) = layers::bundle_download_layer::handle(
&context,
&env,
bullet,
&layers::bundle_download_layer::Metadata {
version: bundler_version,
},
)?;

(section.end_section(), layer_env.apply(Scope::Build, &env))
(bullet.done(), layer_env.apply(Scope::Build, &env))
};

// ## Bundle install
Expand Down

0 comments on commit f23beb9

Please sign in to comment.