cargo-forge 0.2.0

An interactive Rust project generator with templates and common features
<p align="center"><img src="assets/banner.svg" alt="cargo-forge โ€” animated banner" width="100%"></p>

<div align="center">

# โš’๏ธ Cargo-Forge

**A Rust project generator with templates and common features**

[![Crates.io](https://img.shields.io/crates/v/cargo-forge.svg)](https://crates.io/crates/cargo-forge)
[![Documentation](https://docs.rs/cargo-forge/badge.svg)](https://docs.rs/cargo-forge)
[![License](https://img.shields.io/crates/l/cargo-forge.svg)](https://github.com/marcuspat/cargo-forge#license)
[![Test Coverage](https://img.shields.io/badge/coverage-45.84%25-yellow.svg)](https://github.com/marcuspat/cargo-forge)

*Generate Rust projects with templates and optional features*

</div>

## ๐ŸŽฌ Demo

![cargo-forge generating an API server project](demo.gif)

*Recorded from the actual binary with [asciinema](https://asciinema.org) + [agg](https://github.com/asciinema/agg).*

## ๐Ÿš€ Quick Start

```bash
# Install cargo-forge
cargo install cargo-forge

# Create a new project interactively
cargo-forge new

# Or specify project type directly
cargo-forge new my-api --project-type api-server

# Initialize in current directory
cargo-forge init --project-type library
```

๐Ÿ“– See [Quick Reference](docs/QUICK_REFERENCE.md) for shell completions and common commands, or [Troubleshooting](docs/TROUBLESHOOTING.md) for platform-specific install issues.

## โœจ Features

### ๐ŸŽฏ Project Types

Cargo-Forge supports 7 project types with templates:

| Type | Description | Key Features |
|------|-------------|--------------|
| **cli-tool** | Command-line applications | โ€ข Clap dependency setup<br>โ€ข Basic project structure<br>โ€ข Ready for CLI development |
| **library** | Rust library crates | โ€ข Library template<br>โ€ข Examples directory<br>โ€ข Documentation ready<br>โ€ข Tests structure |
| **api-server** | REST API servers | โ€ข Axum web framework<br>โ€ข Basic HTTP server setup<br>โ€ข Route handlers structure<br>โ€ข Ready for API development |
| **wasm-app** | WebAssembly applications | โ€ข wasm-bindgen setup<br>โ€ข Web-sys integration<br>โ€ข Build scripts<br>โ€ข HTML template |
| **game-engine** | Game development | โ€ข Bevy engine<br>โ€ข Asset pipeline structure<br>โ€ข Basic game setup<br>โ€ข Development ready |
| **embedded** | Embedded systems | โ€ข no_std setup<br>โ€ข Memory configuration<br>โ€ข HAL integration<br>โ€ข Debug configs |
| **workspace** | Multi-crate projects | โ€ข Organized structure<br>โ€ข Shared dependencies<br>โ€ข Cross-crate testing<br>โ€ข Unified configuration |

_Note: the templates are deliberately minimal starting points, not frameworks. `api-server` gives you a running server with one health-check route; the rest of the handlers/routes/models are yours to write. `game-engine` compiles a windowed Bevy app; real assets and systems are up to you._

### ๐Ÿ› ๏ธ Core Features

Current features available in v0.2.0:

#### **Project Structure**
- **Working starters**: every template generates real starter code (an Axum
  health-check server, a Clap CLI with a `hello` command, a library with a
  unit-tested `greet` API, a wasm-bindgen export, a minimal Bevy app).
  api-server, cli-tool, library and workspace starters are compile-verified
  by the test suite. The game-engine starter (bevy 0.20) was verified
  manually with `cargo check --target wasm32-unknown-unknown` (the automated
  suite skips game-engine cargo checks); native desktop builds
  additionally need Bevy's Linux windowing libraries (noted in the
  generated README). The wasm-app starter follows the standard wasm-pack
  flow
- **Dependency Management**: Appropriate dependencies for each project type
- **Documentation**: README files with project-specific instructions
- **Testing**: the library and cli-tool starters ship tests you can run with
  `cargo test` right after generation

#### **Development Tools**
- **Dry Run Mode**: Preview project structure before creation (add
  `--non-interactive` when running without a TTY, e.g. in CI)
- **Non-interactive Mode**: CI-friendly project generation
- **Name Validation**: Ensures valid Cargo package names
- **License Field**: `--license` writes the SPDX `license` field into
  `Cargo.toml` (same convention as `cargo new --license`; no LICENSE file
  is generated)
- **Shell Completions**: Bash, zsh, fish, and PowerShell support
- **Config File**: `--from-config` reads TOML (JSON also accepted for
  backward compatibility); `default_author`, `default_license`,
  `preferred_project_types` and per-type `default_features` are applied

#### **Optional Features** (select interactively or via config)
- **Docker**: Dockerfile (multi-stage) + .dockerignore
- **CI**: GitHub Actions workflow
- **Database**: PostgreSQL, MySQL, or SQLite scaffolding via sqlx

#### **Planned**
- Authentication scaffolding (JWT/OAuth)
- GitLab CI support

## ๐Ÿ“‹ Comparison with cargo-generate

| Feature | cargo-forge | cargo-generate |
|---------|------------|----------------|
| **Interactive Mode** | โœ… Prompt-based (inquire) | โš ๏ธ Requires manual input |
| **Project Types** | โœ… 7 built-in types | โœ… Arbitrary template repos |
| **Defaults** | โœ… Pre-configured options | โŒ Manual configuration |
| **Name Validation** | โœ… Built-in validation | โš ๏ธ Basic validation |
| **Dry Run Mode** | โœ… Preview before creation | โŒ Not available |
| **Non-interactive Mode** | โœ… CI-friendly with defaults | โœ… Available |
| **Custom Templates** | โŒ Built-in types only | โœ… Various engines |
| **Shell Completions** | โœ… All major shells | โš ๏ธ Manual setup |
| **Error Recovery** | โœ… Graceful handling | โš ๏ธ Basic errors |
| **Performance** | โœ… ~0.3s generation | โš ๏ธ Varies by template |

## ๐ŸŽฎ Usage Examples

### Command-Line Mode (Current)

```bash
# Create an API server (Axum starter with a GET / health check)
cargo-forge new my-api \
  --project-type api-server \
  --author "Jane Doe" \
  --description "My awesome API" \
  --license MIT

# Create a CLI tool in non-interactive mode (great for CI)
cargo-forge new my-cli \
  --project-type cli-tool \
  --non-interactive

# Initialize a library in current directory
cargo-forge init --project-type library

# Dry run to preview what will be created
cargo-forge new my-project --dry-run

# Use saved configuration (TOML; JSON is also accepted)
cargo-forge new my-project --from-config ~/.forge/defaults.toml
```

Example `defaults.toml`:

```toml
default_author = "Jane Doe"
default_license = "MIT"
preferred_project_types = ["api-server"]
default_features = { api-server = ["docker", "ci"] }
edition = "2021"
```

### Advanced Usage

```bash
# Create a workspace with multiple crates
cargo-forge new my-workspace --project-type workspace

# Generate a game with Bevy engine
cargo-forge new my-game --project-type game-engine

# Create an embedded project for STM32
cargo-forge new my-firmware --project-type embedded
```

## ๐Ÿ“ Generated Project Structure

### Example: API Server

```
my-api/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ main.rs           # Tokio main: binds 0.0.0.0:3000 (PORT env overrides)
โ”‚   โ”œโ”€โ”€ handlers.rs       # GET / health-check handler
โ”‚   โ”œโ”€โ”€ routes.rs         # Router definition
โ”‚   โ””โ”€โ”€ models.rs         # Health model
โ”œโ”€โ”€ config/
โ”‚   โ””โ”€โ”€ default.toml      # Configuration template
โ”œโ”€โ”€ .env.example          # Documented env vars
โ”œโ”€โ”€ .gitignore            # Git ignore file
โ”œโ”€โ”€ Cargo.toml            # Project manifest with Axum dependencies
โ””โ”€โ”€ README.md             # Project documentation
```

## ๐Ÿ”ง Configuration

### Command-Line Options

Cargo-Forge supports various command-line options:

```bash
# Non-interactive mode (great for CI/CD)
cargo-forge new my-project --project-type api-server --non-interactive

# Dry run to preview what will be created
# (add --non-interactive when there is no TTY, e.g. in CI)
cargo-forge new my-project --project-type library --dry-run --non-interactive

# Initialize in current directory
cargo-forge init --project-type cli-tool
```

### Project Customization

After project creation, you can customize:
- Add dependencies to `Cargo.toml`
- Modify source files to fit your needs
- Update configuration files as needed
- Add additional features and integrations

## ๐Ÿงช Testing

The library and cli-tool starters include tests that run immediately:

```bash
# In a freshly generated library or cli-tool project
cargo test
```

## ๐Ÿณ Docker Support (optional feature)

Select the `docker` feature (interactively, or via `default_features` in your config) to generate a multi-stage Dockerfile plus .dockerignore. For `api-server` projects the container exposes port 3000. The generated Dockerfile uses multi-stage builds for optimal image size:

```dockerfile
# Build stage
FROM rust:1.70 as builder
WORKDIR /app
COPY . .
RUN cargo build --release

# Runtime stage
FROM debian:bookworm-slim
COPY --from=builder /app/target/release/app /usr/local/bin/
CMD ["app"]
```

## ๐Ÿ”’ Security

Properties of the generated templates today:

- No hardcoded secrets in templates
- Secure default configurations
- Environment variable usage for sensitive data

**Planned:** security audit integration in CI and OWASP-aligned checks for the `api-server` template โ€” both depend on the CI/CD integration above, which isn't implemented yet.

## ๐Ÿค Contributing

We love contributions! See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.

### Development Setup

```bash
# Clone the repository
git clone https://github.com/marcuspat/cargo-forge
cd cargo-forge

# Run tests
cargo test

# Run with coverage
cargo tarpaulin

# Build for release
cargo build --release
```

## ๐Ÿ“ˆ Performance

Cargo-Forge is optimized for speed:

- Project generation: ~0.3 seconds per project (bounded by
  `generation_is_fast` in the test suite; 2s/project CI ceiling)
- Name validation: <1ms
- Cross-platform: Works on Windows, Mac, and Linux

## ๐Ÿ› Troubleshooting

### Common Issues

**Q: Command not found after installation**
```bash
# Ensure cargo bin directory is in PATH
export PATH="$HOME/.cargo/bin:$PATH"
```

**Q: Permission denied errors**
```bash
# Check directory permissions
ls -la .
# Use sudo if needed (not recommended)
```

**Q: Template rendering fails**
```bash
# Validate your input
cargo-forge new --dry-run
# Check for special characters in project name
```

### Getting Help

- ๐Ÿ“– [Documentation]https://docs.rs/cargo-forge
- ๐Ÿ› [Issue Tracker]https://github.com/marcuspat/cargo-forge/issues

## ๐Ÿ“š Documentation

- **[Quick Reference]docs/QUICK_REFERENCE.md** - Command cheat sheet and quick examples
- **[Project Types Guide]docs/PROJECT_TYPES.md** - Detailed guide for each project type
- **[FAQ]docs/FAQ.md** - Frequently asked questions
- **[Troubleshooting]docs/TROUBLESHOOTING.md** - Common issues and solutions
- **[Contributing]CONTRIBUTING.md** - Development guidelines
- **[Changelog]CHANGELOG.md** - Version history and roadmap

## ๐Ÿ“œ License

Licensed under either of:

- Apache License, Version 2.0 ([LICENSE-APACHE]LICENSE-APACHE)
- MIT license ([LICENSE-MIT]LICENSE-MIT)

at your option.

## ๐Ÿ™ Acknowledgments

- The Rust community for feedback
- Contributors who help make Cargo-Forge better
- Similar projects: cargo-generate, create-react-app

---

<div align="center">

**Built with โค๏ธ by the Rust community**

[Documentation](https://docs.rs/cargo-forge) โ€ข [Crates.io](https://crates.io/crates/cargo-forge)

</div>