Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 1 | //! The cli entrypoint for the `generate` subcommand |
| 2 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 3 | use std::fs; |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 4 | use std::path::PathBuf; |
| 5 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 6 | use anyhow::{bail, Context as AnyhowContext, Result}; |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 7 | use clap::Parser; |
| 8 | |
| 9 | use crate::config::Config; |
| 10 | use crate::context::Context; |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 11 | use crate::lockfile::{lock_context, write_lockfile}; |
Adam Snaider | 1c095c9 | 2023-07-08 02:09:58 -0400 | [diff] [blame^] | 12 | use crate::metadata::{load_metadata, Annotations, Cargo}; |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 13 | use crate::rendering::{write_outputs, Renderer}; |
| 14 | use crate::splicing::SplicingManifest; |
| 15 | |
| 16 | /// Command line options for the `generate` subcommand |
| 17 | #[derive(Parser, Debug)] |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 18 | #[clap(about = "Command line options for the `generate` subcommand", version)] |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 19 | pub struct GenerateOptions { |
| 20 | /// The path to a Cargo binary to use for gathering metadata |
| 21 | #[clap(long, env = "CARGO")] |
| 22 | pub cargo: Option<PathBuf>, |
| 23 | |
| 24 | /// The path to a rustc binary for use with Cargo |
| 25 | #[clap(long, env = "RUSTC")] |
| 26 | pub rustc: Option<PathBuf>, |
| 27 | |
| 28 | /// The config file with information about the Bazel and Cargo workspace |
| 29 | #[clap(long)] |
| 30 | pub config: PathBuf, |
| 31 | |
| 32 | /// A generated manifest of splicing inputs |
| 33 | #[clap(long)] |
| 34 | pub splicing_manifest: PathBuf, |
| 35 | |
| 36 | /// The path to either a Cargo or Bazel lockfile |
| 37 | #[clap(long)] |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 38 | pub lockfile: Option<PathBuf>, |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 39 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 40 | /// The path to a [Cargo.lock](https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html) file. |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 41 | #[clap(long)] |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 42 | pub cargo_lockfile: PathBuf, |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 43 | |
| 44 | /// The directory of the current repository rule |
| 45 | #[clap(long)] |
| 46 | pub repository_dir: PathBuf, |
| 47 | |
| 48 | /// A [Cargo config](https://doc.rust-lang.org/cargo/reference/config.html#configuration) |
| 49 | /// file to use when gathering metadata |
| 50 | #[clap(long)] |
| 51 | pub cargo_config: Option<PathBuf>, |
| 52 | |
| 53 | /// Whether or not to ignore the provided lockfile and re-generate one |
| 54 | #[clap(long)] |
| 55 | pub repin: bool, |
| 56 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 57 | /// The path to a Cargo metadata `json` file. This file must be next to a `Cargo.toml` and `Cargo.lock` file. |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 58 | #[clap(long)] |
| 59 | pub metadata: Option<PathBuf>, |
| 60 | |
| 61 | /// If true, outputs will be printed instead of written to disk. |
| 62 | #[clap(long)] |
| 63 | pub dry_run: bool, |
| 64 | } |
| 65 | |
| 66 | pub fn generate(opt: GenerateOptions) -> Result<()> { |
| 67 | // Load the config |
| 68 | let config = Config::try_from_path(&opt.config)?; |
| 69 | |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 70 | // Go straight to rendering if there is no need to repin |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 71 | if !opt.repin { |
| 72 | if let Some(lockfile) = &opt.lockfile { |
| 73 | let context = Context::try_from_path(lockfile)?; |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 74 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 75 | // Render build files |
Adam Snaider | 1c095c9 | 2023-07-08 02:09:58 -0400 | [diff] [blame^] | 76 | let outputs = Renderer::new( |
| 77 | config.rendering, |
| 78 | config.supported_platform_triples, |
| 79 | config.generate_target_compatible_with, |
| 80 | ) |
| 81 | .render(&context)?; |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 82 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 83 | // Write the outputs to disk |
| 84 | write_outputs(outputs, &opt.repository_dir, opt.dry_run)?; |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 85 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 86 | return Ok(()); |
| 87 | } |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Ensure Cargo and Rustc are available for use during generation. |
Adam Snaider | 1c095c9 | 2023-07-08 02:09:58 -0400 | [diff] [blame^] | 91 | let cargo_bin = Cargo::new(match opt.cargo { |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 92 | Some(bin) => bin, |
| 93 | None => bail!("The `--cargo` argument is required when generating unpinned content"), |
Adam Snaider | 1c095c9 | 2023-07-08 02:09:58 -0400 | [diff] [blame^] | 94 | }); |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 95 | let rustc_bin = match &opt.rustc { |
| 96 | Some(bin) => bin, |
| 97 | None => bail!("The `--rustc` argument is required when generating unpinned content"), |
| 98 | }; |
| 99 | |
| 100 | // Ensure a path to a metadata file was provided |
| 101 | let metadata_path = match &opt.metadata { |
| 102 | Some(path) => path, |
| 103 | None => bail!("The `--metadata` argument is required when generating unpinned content"), |
| 104 | }; |
| 105 | |
| 106 | // Load Metadata and Lockfile |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 107 | let (cargo_metadata, cargo_lockfile) = load_metadata(metadata_path)?; |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 108 | |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 109 | // Annotate metadata |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 110 | let annotations = Annotations::new(cargo_metadata, cargo_lockfile.clone(), config.clone())?; |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 111 | |
Adam Snaider | 1c095c9 | 2023-07-08 02:09:58 -0400 | [diff] [blame^] | 112 | // Generate renderable contexts for each package |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 113 | let context = Context::new(annotations)?; |
| 114 | |
| 115 | // Render build files |
Adam Snaider | 1c095c9 | 2023-07-08 02:09:58 -0400 | [diff] [blame^] | 116 | let outputs = Renderer::new( |
| 117 | config.rendering.clone(), |
| 118 | config.supported_platform_triples.clone(), |
| 119 | config.generate_target_compatible_with, |
| 120 | ) |
| 121 | .render(&context)?; |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 122 | |
| 123 | // Write outputs |
| 124 | write_outputs(outputs, &opt.repository_dir, opt.dry_run)?; |
| 125 | |
Adam Snaider | 1c095c9 | 2023-07-08 02:09:58 -0400 | [diff] [blame^] | 126 | // Ensure Bazel lockfiles are written to disk so future generations can be short-circuited. |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 127 | if let Some(lockfile) = opt.lockfile { |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 128 | let splicing_manifest = SplicingManifest::try_from_path(&opt.splicing_manifest)?; |
| 129 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 130 | let lock_content = |
Adam Snaider | 1c095c9 | 2023-07-08 02:09:58 -0400 | [diff] [blame^] | 131 | lock_context(context, &config, &splicing_manifest, &cargo_bin, rustc_bin)?; |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 132 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 133 | write_lockfile(lock_content, &lockfile, opt.dry_run)?; |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 136 | // Write the updated Cargo.lock file |
| 137 | fs::write(&opt.cargo_lockfile, cargo_lockfile.to_string()) |
| 138 | .context("Failed to write Cargo.lock file back to the workspace.")?; |
| 139 | |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 140 | Ok(()) |
| 141 | } |