Skip to content

Commit

Permalink
refactor: Clean up build/main separation of the infra CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Jan 12, 2024
1 parent ca979db commit d29b404
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
25 changes: 12 additions & 13 deletions crates/infra/cli/build.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
use anyhow::Result;
use clap::CommandFactory;
use clap_complete::generate;
use clap_complete::shells::Zsh;
use clap_complete::{Generator, Shell};
use infra_utils::cargo::CargoWorkspace;
use infra_utils::codegen::Codegen;

// Need this hack to import the source crate as a module:
#[path = "./src/main.rs"]
mod crate_main;
pub use crate_main::*;
// We use the `clap_complete` pattern of sharing the same crate entry point between
// the main lib/bin target and the build script to generate the completions at compile-time.
// See <https://docs.rs/clap_complete/latest/clap_complete/generator/fn.generate_to.html>.
include!("src/lib.rs");

fn main() -> Result<()> {
generate_zsh_completions()?;

Ok(())
generate_completions(Shell::Zsh)
}

/// Generate auto-completions for the shell.
fn generate_zsh_completions() -> Result<()> {
fn generate_completions(shell: Shell) -> Result<()> {
let mut buffer = vec![];
let mut command = <crate_main::commands::CLI as CommandFactory>::command();

generate(Zsh, &mut command, "infra", &mut buffer);
let mut command = CLI::command();
command.build();

shell.generate(&command, &mut buffer);

let crate_dir = CargoWorkspace::locate_source_crate("infra_cli")?;

Codegen::write_only()?.write_file(
crate_dir.join("generated/infra.zsh-completions"),
crate_dir.join(format!("generated/infra.{shell}-completions")),
String::from_utf8(buffer)?,
)
}
12 changes: 12 additions & 0 deletions crates/infra/cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub mod commands;
pub mod toolchains;
pub mod utils;

pub use crate::commands::CLI;
pub use crate::utils::Terminal;

#[test]
fn verify_clap_cli() {
// Catches problems earlier in the development cycle:
<CLI as clap::CommandFactory>::command().debug_assert();
}
15 changes: 2 additions & 13 deletions crates/infra/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
pub mod commands;
pub mod toolchains;
pub mod utils;
#![allow(unused_crate_dependencies)] // dependencies are almost exclusively used by the lib target

use clap::Parser;
use infra_cli::{Terminal, CLI};

use crate::commands::CLI;
use crate::utils::Terminal;

#[allow(dead_code)] // as it is referenced from 'build.rs' of the same crate.
fn main() {
let std_hook = std::panic::take_hook();

Expand All @@ -23,9 +18,3 @@ fn main() {
CLI::parse().execute().unwrap();
Terminal::success();
}

#[test]
fn verify_clap_cli() {
// Catches problems earlier in the development cycle:
<CLI as clap::CommandFactory>::command().debug_assert();
}

0 comments on commit d29b404

Please sign in to comment.