Skip to content

Commit

Permalink
Allow rendering a BUILD file for a single crate
Browse files Browse the repository at this point in the history
  • Loading branch information
illicitonion committed Dec 4, 2024
1 parent 69ddb09 commit 4329e41
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
8 changes: 7 additions & 1 deletion crate_universe/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod generate;
mod query;
mod render;
mod splice;
mod vendor;

Expand All @@ -15,12 +16,14 @@ use tracing_subscriber::FmtSubscriber;

pub use self::generate::GenerateOptions;
pub use self::query::QueryOptions;
pub use self::render::RenderOptions;
pub use self::splice::SpliceOptions;
pub use self::vendor::VendorOptions;

// Entrypoints
pub use generate::generate;
pub use query::query;
pub use render::render;
pub use splice::splice;
pub use vendor::vendor;

Expand All @@ -42,6 +45,9 @@ pub enum Options {

/// Vendor BUILD files to the workspace with either repository definitions or `cargo vendor` generated sources.
Vendor(VendorOptions),

/// Render a BUILD file for a single crate.
Render(RenderOptions),
}

// Convenience wrappers to avoid dependencies in the binary
Expand All @@ -51,7 +57,7 @@ pub fn parse_args() -> Options {
Options::parse()
}

const EXPECTED_LOGGER_NAMES: [&str; 4] = ["Generate", "Splice", "Query", "Vendor"];
const EXPECTED_LOGGER_NAMES: [&str; 5] = ["Generate", "Splice", "Query", "Vendor", "Render"];

/// A wrapper for the tracing-subscriber default [FormatEvent]
/// that prepends the name of the active CLI option.
Expand Down
57 changes: 57 additions & 0 deletions crate_universe/src/cli/render.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::config::RenderConfig;
use crate::context::CrateContext;
use crate::rendering::{Platforms, Renderer};
use crate::utils::target_triple::TargetTriple;

use anyhow::{Context, Result};
use clap::Parser;
use serde::{Deserialize, Serialize};

use std::collections::BTreeSet;
use std::path::PathBuf;

#[derive(Parser, Debug)]
#[clap(about = "Command line options for the `render` subcommand", version)]
pub struct RenderOptions {
#[clap(long)]
options_json: String,

#[clap(long)]
output_path: PathBuf,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct StructuredRenderOptions {
config: RenderConfig,

supported_platform_triples: BTreeSet<TargetTriple>,

platforms: Platforms,

crate_context: CrateContext,
}

pub fn render(opt: RenderOptions) -> Result<()> {
let RenderOptions {
options_json,
output_path,
} = opt;

let deserialized_options = serde_json::from_str(&options_json)
.with_context(|| format!("Failed to deserialize options_json from '{}'", options_json))?;

let StructuredRenderOptions {
config,
supported_platform_triples,
platforms,
crate_context,
} = deserialized_options;

let renderer = Renderer::new(config, supported_platform_triples);
let output = renderer.render_one_build_file(&platforms, &crate_context)
.with_context(|| format!("Failed to render BUILD.bazel file for crate {}", crate_context.name))?;
std::fs::write(&output_path, output.as_bytes())
.with_context(|| format!("Failed to write BUILD.bazel file to {}", output_path.display()))?;

Ok(())
}
4 changes: 4 additions & 0 deletions crate_universe/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ fn main() -> cli::Result<()> {
cli::init_logging("Vendor", verbose_logging);
cli::vendor(opt)
}
cli::Options::Render(opt) => {
cli::init_logging("Render", verbose_logging);
cli::render(opt)
}
}
}
2 changes: 1 addition & 1 deletion crate_universe/src/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl Renderer {
.collect()
}

fn render_one_build_file(&self, platforms: &Platforms, krate: &CrateContext) -> Result<String> {
pub(crate) fn render_one_build_file(&self, platforms: &Platforms, krate: &CrateContext) -> Result<String> {
let mut starlark = Vec::new();

// Banner comment for top of the file.
Expand Down

0 comments on commit 4329e41

Please sign in to comment.