Skip to content

cargo-new: Custom Default Files #11250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
danii opened this issue Oct 17, 2022 · 2 comments
Closed

cargo-new: Custom Default Files #11250

danii opened this issue Oct 17, 2022 · 2 comments
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-new

Comments

@danii
Copy link

danii commented Oct 17, 2022

Problem

Creating a new Rust project via cargo new (or cargo init) always produces the same, hard coded files, main.rs, or lib.rs. There's no way to add predefined code (for example, #![forbid(unsafe_code)] is an attribute many people religiously use, and could benefit from having it off the bat), or even change indentation (for example, using spaces at 2, tabs, etc).

Proposed Solution

I propose that 2 new configuration options (both being configurable from .cargo/config.toml, environment variables and command line flags): bin preset and lib preset. These two options would allow users to craft their own boiler plate to be used upon running cargo new.

Both options are paths. If cargo new is ran without --lib, the file pointed to by the bin preset path is copied to src/main.rs within the package. Otherwise, the file pointed to by the lib preset path is copied to src/lib.rs.

This concept can be expanded further; we can also specify that if the bin preset or lib preset path point to a directory, that the whole directory by copied to src. This enables us to create multi-file presets. I do not have a use case for this, but it is easily possible.

Proposed Command Line Flag For the command line, rather than two flags, I propose one flag, `--preset` which takes a path. Two aren't necessary as only one can be used; `cargo new` only either creates a bin or a lib (`--lib`). The option overrides the environment variables and configuration file.
Proposed Environment Variables Two environment variables will be used for presets; `CARGO_CARGO_NEW_BIN_PRESET` and `CARGO_CARGO_NEW_LIB_PRESET`. These options override the configuration file.
Proposed Configuration Keys Two configuration keys will be used for presets; `cargo-new.bin-preset` and `cargo-new.lib-preset`.

Notes

I am personally willing to create a pull request to implement this.

I consider this a minor feature, but if needed I can start drafting an RFC.

@danii danii added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Oct 17, 2022
@epage
Copy link
Contributor

epage commented Oct 17, 2022

FYI we have #5151 for tracking package template support

@ehuss
Copy link
Contributor

ehuss commented Dec 11, 2022

I'm going to close in favor of #5151 as I believe templates would cover this use case.

@ehuss ehuss closed this as not planned Won't fix, can't repro, duplicate, stale Dec 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-new
Projects
None yet
Development

No branches or pull requests

4 participants