diff --git a/Cargo.lock b/Cargo.lock index adb7283..70e42e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,6 +68,7 @@ dependencies = [ "anyhow", "assert_cmd", "clap", + "clap_complete", "colored", "dirs", "home", @@ -153,6 +154,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.5.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c757a3b7e39161a4e56f9365141ada2a6c915a8622c408ab6bb4b5d047371031" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.5.55" diff --git a/Cargo.toml b/Cargo.toml index 97ef13b..f64bcec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ path = "src/main.rs" [dependencies] # CLI framework clap = { version = "4.4", features = ["derive", "cargo"] } +clap_complete = "4.4" # Filesystem dirs = "5.0" diff --git a/README.md b/README.md index 8dc1dbd..3d66dd7 100644 --- a/README.md +++ b/README.md @@ -290,6 +290,31 @@ constructor injection -- the simplest form of dependency inversion. | `anvil board --remove NAME` | Remove a board profile | | `anvil board --default NAME` | Set the default board | | `anvil refresh [--force] [--file P] [--ignore P] [--unignore P]` | Update project infrastructure | +| `anvil completions SHELL` | Generate tab-completion script (bash, zsh, fish, powershell) | + +--- + +## Tab Completion + +Anvil can generate shell completion scripts so that pressing Tab completes +commands, flags, and arguments: + +```bash +# Bash (add to ~/.bashrc) +eval "$(anvil completions bash)" + +# Zsh (add to ~/.zshrc) +eval "$(anvil completions zsh)" + +# Fish +anvil completions fish > ~/.config/fish/completions/anvil.fish + +# PowerShell (add to $PROFILE) +anvil completions powershell | Out-String | Invoke-Expression +``` + +After setup, `anvil ref` completes to `anvil refresh`, and +`anvil pin --` shows all available flags. --- diff --git a/build-release.sh b/build-release.sh new file mode 100644 index 0000000..efc49c2 --- /dev/null +++ b/build-release.sh @@ -0,0 +1,79 @@ +#!/bin/bash +# Build release binaries for distribution +# This script builds both Linux and Windows binaries (cross-compile) +# +# For Windows-only builds, use build-release.ps1 on Windows +# For Linux-only builds, comment out the Windows section below +set -e + +VERSION=${1:-$(git describe --tags --always)} +RELEASE_DIR="release-artifacts" + +echo "Building Anvil $VERSION release binaries..." +echo "" + +# Clean previous artifacts +rm -rf "$RELEASE_DIR" +mkdir -p "$RELEASE_DIR" + +# Build Linux binary (optimized) +echo "Building Linux x86_64 binary..." +cargo build --release +strip target/release/anvil + +# Package Linux binary +echo "Packaging Linux binaries..." +cd target/release +tar -czf "../../$RELEASE_DIR/anvil-${VERSION}-linux-x86_64.tar.gz" anvil +zip -q "../../$RELEASE_DIR/anvil-${VERSION}-linux-x86_64.zip" anvil +cd ../.. + +# Build Windows binary (cross-compile) +echo "" +echo "Building Windows x86_64 binary..." + +# Check if Windows target is installed +if ! rustup target list | grep -q "x86_64-pc-windows-gnu (installed)"; then + echo "Installing Windows target..." + rustup target add x86_64-pc-windows-gnu +fi + +# Check if MinGW is installed +if ! command -v x86_64-w64-mingw32-gcc &> /dev/null; then + echo "Warning: MinGW not found. Install with: sudo apt install mingw-w64" + echo "Skipping Windows build." +else + cargo build --release --target x86_64-pc-windows-gnu + x86_64-w64-mingw32-strip target/x86_64-pc-windows-gnu/release/anvil.exe + + # Package Windows binary + echo "Packaging Windows binary..." + cd target/x86_64-pc-windows-gnu/release + zip -q "../../../$RELEASE_DIR/anvil-${VERSION}-windows-x86_64.zip" anvil.exe + cd ../../.. +fi + +# Generate checksums +echo "" +echo "Generating checksums..." +cd "$RELEASE_DIR" +sha256sum * > SHA256SUMS +cd .. + +# Display results +echo "" +echo "===========================================================" +echo " Release artifacts built successfully!" +echo "===========================================================" +echo "" +echo "Artifacts in $RELEASE_DIR/:" +ls -lh "$RELEASE_DIR" +echo "" +echo "Checksums:" +cat "$RELEASE_DIR/SHA256SUMS" +echo "" +echo "Upload these files to your Gitea release:" +echo " 1. Go to: Releases -> $VERSION -> Edit Release" +echo " 2. Drag and drop files from $RELEASE_DIR/" +echo " 3. Save" +echo "" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f529559..889caad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ -use clap::{Parser, Subcommand}; +use clap::{CommandFactory, Parser, Subcommand}; +use clap_complete::{generate, Shell}; use colored::*; use anyhow::Result; use anvil::version::ANVIL_VERSION; @@ -167,6 +168,12 @@ enum Commands { dir: Option, }, + /// Generate shell completion scripts + Completions { + /// Shell to generate completions for (bash, zsh, fish, powershell) + shell: Shell, + }, + /// View pin maps, assign pins, and audit wiring Pin { /// Capability filter (pwm, analog, spi, i2c, uart, interrupt) @@ -228,7 +235,11 @@ fn main() -> Result<()> { let cli = Cli::parse(); - print_banner(); + // Completions output must be clean -- no banner + let is_completions = matches!(cli.command, Commands::Completions { .. }); + if !is_completions { + print_banner(); + } match cli.command { Commands::New { name, template, board, list_templates, list_boards } => { @@ -352,6 +363,11 @@ fn main() -> Result<()> { commands::lib::list_libraries(dir.as_deref()) } } + Commands::Completions { shell } => { + let mut cmd = Cli::command(); + generate(shell, &mut cmd, "anvil", &mut std::io::stdout()); + Ok(()) + } Commands::Pin { name, pin, assign, remove, audit, brief, generate, capabilities, init_from, mode, cs, board, dir,