Skip to content

Commit

Permalink
Tweak output
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax committed Jan 17, 2025
1 parent ed34884 commit eccae7c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
11 changes: 8 additions & 3 deletions buildpacks/gradle/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use crate::gradle_command::GradleCommandError;
use crate::layers::gradle_home::handle_gradle_home_layer;
use crate::GradleBuildpackError::{GradleBuildIoError, GradleBuildUnexpectedStatusError};
use buildpacks_jvm_shared as shared;
use buildpacks_jvm_shared::output;
use buildpacks_jvm_shared::output::{
print_buildpack_name, print_section, print_subsection, track_timing,
print_buildpack_name, print_section, print_subsection, track_timing, BuildpackOutputText,
BuildpackOutputTextSection,
};
#[cfg(test)]
use buildpacks_jvm_shared_test as _;
Expand All @@ -20,7 +22,6 @@ use libcnb::{buildpack_main, Buildpack, Env};
#[cfg(test)]
use libcnb_test as _;
use libherokubuildpack::command::CommandExt;
use libherokubuildpack::log::log_header;
use serde::Deserialize;
use std::io::{stderr, stdout};
use std::process::{Command, ExitStatus};
Expand Down Expand Up @@ -126,7 +127,11 @@ impl Buildpack for GradleBuildpack {
})
.ok_or(GradleBuildpackError::BuildTaskUnknown)?;

log_header("Running build task");
print_section("Running Gradle build");
print_subsection(BuildpackOutputText::new(vec![
BuildpackOutputTextSection::regular("Running "),
BuildpackOutputTextSection::command(format!("./gradlew {} -x check", task_name)),
]));

let output = Command::new(&gradle_wrapper_executable_path)
.current_dir(&context.app_dir)
Expand Down
2 changes: 1 addition & 1 deletion buildpacks/jvm/src/layers/openjdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub(crate) fn handle_openjdk_layer(
)
.map_err(OpenJdkBuildpackError::CannotSymlinkUbuntuCertificates)?;

output::print_subsection("Done.");
output::print_subsection("Done");
} else {
output::print_subsection(BuildpackOutputText::new(vec![
BuildpackOutputTextSection::regular("Skipping ("),
Expand Down
2 changes: 1 addition & 1 deletion buildpacks/maven/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl Buildpack for MavenBuildpack {
output::print_section("Running Maven build");
output::print_subsection(BuildpackOutputText::new(vec![
BuildpackOutputTextSection::regular("Running "),
BuildpackOutputTextSection::value(format!(
BuildpackOutputTextSection::command(format!(
"{} {} {}",
mvn_executable.to_string_lossy(),
shell_words::join(&maven_options),
Expand Down
2 changes: 1 addition & 1 deletion buildpacks/sbt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Buildpack for SbtBuildpack {
output::print_section("Running sbt build");
output::print_subsection(BuildpackOutputText::new(vec![
BuildpackOutputTextSection::regular("Running "),
BuildpackOutputTextSection::value(format!("sbt {}", shell_words::join(&tasks))),
BuildpackOutputTextSection::command(format!("sbt {}", shell_words::join(&tasks))),
]));

let mut command = Command::new("sbt");
Expand Down
31 changes: 22 additions & 9 deletions shared/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time::{Duration, Instant};

pub fn print_buildpack_name(buildpack_name: impl AsRef<str>) {
let buildpack_name = buildpack_name.as_ref();
println!("{ANSI_BUILDPACK_NAME_CODE}# {buildpack_name}{ANSI_RESET_CODE}\n");
print!("\n{ANSI_BUILDPACK_NAME_CODE}# {buildpack_name}{ANSI_RESET_CODE}\n\n");
}

pub fn print_section(text: impl Into<BuildpackOutputText>) {
Expand Down Expand Up @@ -71,10 +71,19 @@ pub fn run_command<E, F: FnOnce(std::io::Error) -> E, F2: FnOnce(Output) -> E>(
let child = if quiet {
command.output_and_write_streams(std::io::sink(), std::io::sink())
} else {
command.output_and_write_streams(
line_mapped(std::io::stdout(), add_prefix_to_non_empty(" ")),
line_mapped(std::io::stderr(), add_prefix_to_non_empty(" ")),
)
const SPACE_ASCII: u8 = 0x20;
let prefix = vec![SPACE_ASCII; 6];

print!("\n");

let output = command.output_and_write_streams(
line_mapped(std::io::stdout(), add_prefix_to_non_empty(prefix.clone())),
line_mapped(std::io::stderr(), add_prefix_to_non_empty(prefix)),
);

print!("\n");

output
};

child.map_err(io_error_fn).and_then(|output| {
Expand Down Expand Up @@ -222,6 +231,10 @@ impl BuildpackOutputTextSection {
pub fn value(value: impl Into<String>) -> Self {
BuildpackOutputTextSection::Value(value.into())
}

pub fn command(value: impl Into<String>) -> Self {
BuildpackOutputTextSection::Command(value.into())
}
}

impl From<String> for BuildpackOutputText {
Expand Down Expand Up @@ -283,12 +296,12 @@ fn format_duration(duration: &Duration) -> String {

const VALUE_DELIMITER_CHAR: char = '`';
const ANSI_RESET_CODE: &str = "\u{1b}[0m";
const ANSI_VALUE_CODE: &str = "\u{1b}[0;34m";
const ANSI_VALUE_CODE: &str = "\u{1b}[0;33m";
const ANSI_YELLOW_CODE: &str = "\u{1b}[0;33m";
const ANSI_RED_CODE: &str = "\u{1b}[0;31m";
const ANSI_BUILDPACK_NAME_CODE: &str = "\u{1b}[1;35m";
const ANSI_URL_CODE: &str = "\u{1b}[0;34m";
const ANSI_COMMAND_CODE: &str = "\u{1b}[0;34m";
const ANSI_COMMAND_CODE: &str = "\u{1b}[1;36m";
const ERROR_WARNING_LINE_PREFIX: &str = "! ";

#[cfg(test)]
Expand All @@ -311,7 +324,7 @@ mod test {
..Default::default()
};

assert_eq!(text.to_ansi_string(), "\u{1b}[0m\u{1b}[0;33m! Hello\u{1b}[0m\n\u{1b}[0m\u{1b}[0;33m! `\u{1b}[0;34mWorld\u{1b}[0m`\u{1b}[0;33m\u{1b}[0m\n\u{1b}[0m\u{1b}[0;33m! How\u{1b}[0m\n\u{1b}[0m\u{1b}[0;33m! are you?");
assert_eq!(text.to_ansi_string(), "\u{1b}[0m\u{1b}[0;33m! Hello\u{1b}[0m\n\u{1b}[0m\u{1b}[0;33m! `\u{1b}[0;33mWorld\u{1b}[0m`\u{1b}[0;33m\u{1b}[0m\n\u{1b}[0m\u{1b}[0;33m! How\u{1b}[0m\n\u{1b}[0m\u{1b}[0;33m! are you?");
}

#[test]
Expand All @@ -329,7 +342,7 @@ mod test {

assert_eq!(
text.to_ansi_string(),
"\u{1b}[0m\u{1b}[0;33m! Intro\u{1b}[0m\n\u{1b}[0m\u{1b}[0;33m! `\u{1b}[0;34mWith\u{1b}[0m\n\u{1b}[0m\u{1b}[0;33m! \u{1b}[0;34mNewline\u{1b}[0m`\u{1b}[0;33m\u{1b}[0m\n\u{1b}[0m\u{1b}[0;33m! Outro"
"\u{1b}[0m\u{1b}[0;33m! Intro\u{1b}[0m\n\u{1b}[0m\u{1b}[0;33m! `\u{1b}[0;33mWith\u{1b}[0m\n\u{1b}[0m\u{1b}[0;33m! \u{1b}[0;33mNewline\u{1b}[0m`\u{1b}[0;33m\u{1b}[0m\n\u{1b}[0m\u{1b}[0;33m! Outro"
);
}

Expand Down

0 comments on commit eccae7c

Please sign in to comment.