|
| 1 | +//! Library for writing Bazel target build data and workspace status stamping info. |
| 2 | +//! |
| 3 | +//! Configuration is read from the environment variables `OUTPUT`, `TARGET`, `STAMPED`, |
| 4 | +//! `VERSION_FILE`, and `INFO_FILE`, matching the `build_data_writer.sh` script it replaces. |
| 5 | +//! The command line accepts no arguments other than `-h`/`--help`. |
| 6 | +
|
| 7 | +// Errors must be returned, not panicked; see the `rust-lint` config in .bazelrc. |
| 8 | +#![deny(clippy::expect_used, clippy::unwrap_used)] |
| 9 | + |
| 10 | +use std::env; |
| 11 | +use std::ffi::OsString; |
| 12 | +use std::fs::{self, File}; |
| 13 | +use std::io::{BufWriter, Write}; |
| 14 | +use std::path::{Path, PathBuf}; |
| 15 | +use std::process::ExitCode; |
| 16 | + |
| 17 | +use anyhow::{Context, Result, bail}; |
| 18 | +use lexopt::prelude::*; |
| 19 | + |
| 20 | +mod flags { |
| 21 | + // keep-sorted start |
| 22 | + pub const HELP: &str = "help"; |
| 23 | + pub const HELP_SHORT: char = 'h'; |
| 24 | + // keep-sorted end |
| 25 | +} |
| 26 | + |
| 27 | +/// Environment variables consumed by the tool. |
| 28 | +mod env_vars { |
| 29 | + // keep-sorted start |
| 30 | + pub const INFO_FILE: &str = "INFO_FILE"; |
| 31 | + pub const OUTPUT: &str = "OUTPUT"; |
| 32 | + pub const STAMPED: &str = "STAMPED"; |
| 33 | + pub const TARGET: &str = "TARGET"; |
| 34 | + pub const VERSION_FILE: &str = "VERSION_FILE"; |
| 35 | + // keep-sorted end |
| 36 | +} |
| 37 | + |
| 38 | +/// Usage text for `--help`. `lexopt` does not generate help, so this is maintained by hand. |
| 39 | +const USAGE: &str = "\ |
| 40 | +Usage: build_data_writer |
| 41 | +
|
| 42 | +Write a build_data.txt file describing a target and its workspace status. |
| 43 | +All configuration comes from environment variables; no arguments are accepted. |
| 44 | +
|
| 45 | +Environment variables: |
| 46 | + OUTPUT Path of the build data file to write (required) |
| 47 | + TARGET Label of the target being built (required) |
| 48 | + STAMPED TRUE or FALSE; whether workspace status stamping is enabled (required) |
| 49 | + VERSION_FILE Path to volatile-status.txt to append; ignored when empty |
| 50 | + INFO_FILE Path to stable-status.txt to append; ignored when empty |
| 51 | +
|
| 52 | +Options: |
| 53 | + -h, --help Print this help"; |
| 54 | + |
| 55 | +/// Configuration options for writing a `build_data.txt` file. |
| 56 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 57 | +pub struct Options { |
| 58 | + /// Path to the output `build_data.txt` file. |
| 59 | + pub output: PathBuf, |
| 60 | + /// Target label string (e.g. `//pkg:target`). |
| 61 | + pub target: String, |
| 62 | + /// Whether stamping is enabled. |
| 63 | + pub stamped: bool, |
| 64 | + /// Optional path to Bazel's `volatile-status.txt` (`ctx.version_file`). |
| 65 | + pub version_file: Option<PathBuf>, |
| 66 | + /// Optional path to Bazel's `stable-status.txt` (`ctx.info_file`). |
| 67 | + pub info_file: Option<PathBuf>, |
| 68 | +} |
| 69 | + |
| 70 | +/// Result of parsing the command line and environment. |
| 71 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 72 | +pub enum Cli { |
| 73 | + /// `--help` was requested. |
| 74 | + Help, |
| 75 | + /// Write the build data file with the given options. |
| 76 | + Run(Options), |
| 77 | +} |
| 78 | + |
| 79 | +fn parse_stamped(val: &str) -> Result<bool> { |
| 80 | + match val { |
| 81 | + "TRUE" => Ok(true), |
| 82 | + "FALSE" => Ok(false), |
| 83 | + other => bail!("{} must be TRUE or FALSE, got '{other}'", env_vars::STAMPED), |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +fn required_env<F>(get_env: &F, key: &str) -> Result<String> |
| 88 | +where |
| 89 | + F: Fn(&str) -> Option<OsString>, |
| 90 | +{ |
| 91 | + get_env(key) |
| 92 | + .with_context(|| format!("missing required environment variable: {key}"))? |
| 93 | + .into_string() |
| 94 | + .map_err(|raw| anyhow::anyhow!("{key} environment variable is not valid UTF-8: {raw:?}")) |
| 95 | +} |
| 96 | + |
| 97 | +/// Parses command-line arguments (excluding `argv[0]` and subcommand name) and the environment |
| 98 | +/// provided by `get_env` into [`Cli`]. |
| 99 | +/// |
| 100 | +/// # Errors |
| 101 | +/// |
| 102 | +/// Returns an error when an argument other than `-h`/`--help` is given, when `OUTPUT`, `TARGET`, |
| 103 | +/// or `STAMPED` is unset, or when `STAMPED` is neither `TRUE` nor `FALSE`. |
| 104 | +pub fn parse_env_and_args<I, T, F>(args: I, get_env: F) -> Result<Cli> |
| 105 | +where |
| 106 | + I: IntoIterator<Item = T>, |
| 107 | + T: Into<OsString>, |
| 108 | + F: Fn(&str) -> Option<OsString>, |
| 109 | +{ |
| 110 | + let mut parser = lexopt::Parser::from_args(args.into_iter().map(Into::into)); |
| 111 | + if let Some(arg) = parser.next()? { |
| 112 | + match arg { |
| 113 | + Short(flags::HELP_SHORT) | Long(flags::HELP) => return Ok(Cli::Help), |
| 114 | + _ => bail!("{}", arg.unexpected()), |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + let output = get_env(env_vars::OUTPUT) |
| 119 | + .map(PathBuf::from) |
| 120 | + .with_context(|| { |
| 121 | + format!( |
| 122 | + "missing required environment variable: {}", |
| 123 | + env_vars::OUTPUT |
| 124 | + ) |
| 125 | + })?; |
| 126 | + let target = required_env(&get_env, env_vars::TARGET)?; |
| 127 | + let stamped = parse_stamped(&required_env(&get_env, env_vars::STAMPED)?)?; |
| 128 | + let version_file = get_env(env_vars::VERSION_FILE) |
| 129 | + .filter(|s| !s.is_empty()) |
| 130 | + .map(PathBuf::from); |
| 131 | + let info_file = get_env(env_vars::INFO_FILE) |
| 132 | + .filter(|s| !s.is_empty()) |
| 133 | + .map(PathBuf::from); |
| 134 | + |
| 135 | + Ok(Cli::Run(Options { |
| 136 | + output, |
| 137 | + target, |
| 138 | + stamped, |
| 139 | + version_file, |
| 140 | + info_file, |
| 141 | + })) |
| 142 | +} |
| 143 | + |
| 144 | +/// Parses command-line arguments (excluding `argv[0]` and subcommand name) and the process |
| 145 | +/// environment into [`Cli`]. |
| 146 | +/// |
| 147 | +/// # Errors |
| 148 | +/// |
| 149 | +/// See [`parse_env_and_args`]. |
| 150 | +pub fn parse_args<I, T>(args: I) -> Result<Cli> |
| 151 | +where |
| 152 | + I: IntoIterator<Item = T>, |
| 153 | + T: Into<OsString>, |
| 154 | +{ |
| 155 | + parse_env_and_args(args, |key| env::var_os(key)) |
| 156 | +} |
| 157 | + |
| 158 | +/// Parses CLI arguments (excluding `argv[0]` and subcommand name) and the process environment, |
| 159 | +/// writes the build data file, and returns the process exit code. |
| 160 | +pub fn run_cli<I, T>(args: I) -> ExitCode |
| 161 | +where |
| 162 | + I: IntoIterator<Item = T>, |
| 163 | + T: Into<OsString>, |
| 164 | +{ |
| 165 | + let options = match parse_args(args) { |
| 166 | + Ok(Cli::Help) => { |
| 167 | + println!("{USAGE}"); |
| 168 | + return ExitCode::SUCCESS; |
| 169 | + } |
| 170 | + Ok(Cli::Run(opts)) => opts, |
| 171 | + Err(err) => { |
| 172 | + eprintln!("build_data_writer: error: {err:#}"); |
| 173 | + return ExitCode::FAILURE; |
| 174 | + } |
| 175 | + }; |
| 176 | + if let Err(err) = write_build_data(&options) { |
| 177 | + eprintln!("build_data_writer: error: {err:#}"); |
| 178 | + return ExitCode::FAILURE; |
| 179 | + } |
| 180 | + ExitCode::SUCCESS |
| 181 | +} |
| 182 | + |
| 183 | +/// Writes the build data file according to `options`. |
| 184 | +/// |
| 185 | +/// # Errors |
| 186 | +/// |
| 187 | +/// Returns an error when the output file cannot be created or written, or when a configured |
| 188 | +/// status file cannot be read. |
| 189 | +pub fn write_build_data(options: &Options) -> Result<()> { |
| 190 | + let file = File::create(&options.output) |
| 191 | + .with_context(|| format!("creating {}", options.output.display()))?; |
| 192 | + let mut writer = BufWriter::new(file); |
| 193 | + |
| 194 | + writeln!(writer, "TARGET {}", options.target)?; |
| 195 | + writeln!( |
| 196 | + writer, |
| 197 | + "STAMPED {}", |
| 198 | + if options.stamped { "TRUE" } else { "FALSE" } |
| 199 | + )?; |
| 200 | + |
| 201 | + if let Some(version_file) = &options.version_file { |
| 202 | + append_status_file(&mut writer, version_file)?; |
| 203 | + } |
| 204 | + if let Some(info_file) = &options.info_file { |
| 205 | + append_status_file(&mut writer, info_file)?; |
| 206 | + } |
| 207 | + |
| 208 | + writer |
| 209 | + .flush() |
| 210 | + .with_context(|| format!("writing {}", options.output.display())) |
| 211 | +} |
| 212 | + |
| 213 | +fn append_status_file<W: Write>(writer: &mut W, path: &Path) -> Result<()> { |
| 214 | + let contents = fs::read(path).with_context(|| format!("reading {}", path.display()))?; |
| 215 | + writer.write_all(&contents)?; |
| 216 | + if !contents.is_empty() && !contents.ends_with(b"\n") { |
| 217 | + writer.write_all(b"\n")?; |
| 218 | + } |
| 219 | + Ok(()) |
| 220 | +} |
0 commit comments