#single-file #cargo-subcommand #merge #cargo #subcommand #combine #file-input

app rustmerge

A Cargo subcommand that merges all Rust source files in a package into a single file

8 releases

0.1.7 Nov 19, 2024
0.1.6 Oct 15, 2024

#83 in Cargo plugins

Download history 582/week @ 2024-10-11 85/week @ 2024-10-18 1/week @ 2024-10-25 132/week @ 2024-11-15 21/week @ 2024-11-22 2/week @ 2024-11-29

155 downloads per month

MIT license

38KB
750 lines

rustmerge

rustmerge is a Cargo subcommand that merges all Rust source files in a package or workspace into single files. It works with both workspace projects and single-package projects.

Its primary use case is to simplify the process of sharing Rust projects with AI tools, e.g., for training machine learning models or code analysis. By merging all source files into a single file per package, you can easily share the project with tools that require a single file as input.

Features

  • Merges all .rs files in a package into a single file
  • Works with both workspace and single-package projects
  • Can process all packages in a workspace at once
  • Excludes test modules from the merged output
  • Maintains the module structure of the original project
  • Preserves cfg attributes on modules
  • Custom output path for merged files

Installation

You can install rustmerge using Cargo:

cargo install rustmerge

Usage

In a single-package project:

cargo rustmerge

In a workspace (specific package):

cargo rustmerge <package_name>

Process all packages in a workspace:

cargo rustmerge --all

Custom output path:

cargo rustmerge [<package_name>] --output <path>

If there's only one package in the workspace and you're not using --all, you can omit the package name.

By default, the merged Rust file(s) will be created in the target directory of your current working directory, named rustmerge/<package_name>.rs.

Examples

  1. Merge a single-package project:

    cd my-rust-project
    cargo rustmerge
    
  2. Merge a specific package in a workspace:

    cd my-rust-workspace
    cargo rustmerge my-package
    
  3. Merge all packages in a workspace:

    cd my-rust-workspace
    cargo rustmerge --all
    
  4. Merge with a custom output path:

    cargo rustmerge --output /path/to/output/merged_project.rs
    
  5. Merge all packages with a custom output directory:

    cargo rustmerge --all --output /path/to/output/dir
    

Output

The tool will print information about the merged files, including their locations and sizes. For example:

Merged and formatted Rust program for package 'my-package' created in "/path/to/project/target/rustmerge/my-package.rs"
File size: 12345 bytes

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Dependencies

~2.7–4.5MB
~80K SLoC