forked from NomicFoundation/slang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Clean up build/main separation of the
infra
CLI
- Loading branch information
Showing
3 changed files
with
26 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)?, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters