cargo_bazel/cli/
render.rs1use crate::context::SingleBuildFileRenderContext;
2use crate::rendering::Renderer;
3
4use anyhow::{Context, Result};
5use clap::Parser;
6
7use std::path::PathBuf;
8use std::sync::Arc;
9
10#[derive(Parser, Debug)]
11#[clap(about = "Command line options for the `render` subcommand", version)]
12pub struct RenderOptions {
13 #[clap(long)]
14 options_json: String,
15
16 #[clap(long)]
17 output_path: PathBuf,
18}
19
20pub fn render(opt: RenderOptions) -> Result<()> {
21 let RenderOptions {
22 options_json,
23 output_path,
24 } = opt;
25
26 let deserialized_options = serde_json::from_str(&options_json)
27 .with_context(|| format!("Failed to deserialize options_json from '{}'", options_json))?;
28
29 let SingleBuildFileRenderContext {
30 config,
31 supported_platform_triples,
32 platform_conditions,
33 crate_context,
34 } = deserialized_options;
35
36 let renderer = Renderer::new(config, supported_platform_triples);
37 let platforms = renderer.render_platform_labels(Arc::clone(&platform_conditions));
38 let engine = renderer.create_engine(platform_conditions);
39 let output = renderer
40 .render_one_build_file(&engine, &platforms, &crate_context)
41 .with_context(|| {
42 format!(
43 "Failed to render BUILD.bazel file for crate {}",
44 crate_context.name
45 )
46 })?;
47 std::fs::write(&output_path, output.as_bytes()).with_context(|| {
48 format!(
49 "Failed to write BUILD.bazel file to {}",
50 output_path.display()
51 )
52 })?;
53
54 Ok(())
55}