Skip to content
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

Add template option to cargo-new command? #396

Closed
gchp opened this issue Aug 18, 2014 · 10 comments
Closed

Add template option to cargo-new command? #396

gchp opened this issue Aug 18, 2014 · 10 comments
Labels
Command-new E-hard Experience: Hard

Comments

@gchp
Copy link
Contributor

gchp commented Aug 18, 2014

I'm wondering what you think of extending the cargo new command to allow users to specify a template which will be used to scaffold the project.

For example:

cargo new --template=http://github.com/gchp/nickel-rs-template

Where nickel-rs-template is a repository containing a sample nickel.rs app with some placeholder values which would get replaced when being generated.

An advantage of this is that 3rd party developers could create and maintain templates for working for their project. For instance the example I've listed above could allow developers to quickly generate a "hello world" nickel.rs web application.

The idea behind this is similar to that seen in Django: django-admin.py startproject --template=xxx.

I'd be happy to work on this, but wanted to see if it's something that may be useful and get any feedback / thoughts on it before beginning.

@steveklabnik
Copy link
Member

I think that use cases like "generate a new nickel project" belong in a nickel binary, not as a Cargo extension. Note how your Django example is part of Django, not part of virtualenv.

@gchp
Copy link
Contributor Author

gchp commented Aug 18, 2014

@steveklabnik I agree, I don't think cargo should care about nickel or any other project for that matter. What I'm suggesting is more the functionality to take a project template and scaffold a project based on that. It would allow for the creation of more than the simple lib or bin setup available currently.

The templates would be external, and maintained by a 3rd party. So the reponsibility of maintaining the templates would be on whoever developed them in the first place.

Fair point on the Django example.

@tomjakubowski
Copy link
Contributor

Leiningen, the Clojure equivalent of Cargo, does something much like this: https://github.com/technomancy/leiningen/blob/stable/doc/TEMPLATES.md

@alexcrichton
Copy link
Member

The cargo-new command could definitely be souped up, and this seems like a great idea! A similar improvement would be implementing cargo new for a pre-existing directory.

@gchp
Copy link
Contributor Author

gchp commented Jan 2, 2015

Its been a while since I looked at this originally. Getting back into it today and wondering, what do you think of using rust-mustache for these templates?

It would mean that the command would download the template files, compile each one as a mustache template then write them out to the target directory. If not rust-mustache, is there a different preferred template system that would be ok to use here?

gchp added a commit to gchp/cargo that referenced this issue Jun 25, 2015
Project templates allow users to define custom templates which can be
used to scaffold projects. These templates consists of a directory of
files, each of which will be treated as a mustache template.

Example:

    cargo new myproject --template=http://github.com/someone/nickel-template

This would download the template called 'nickel-template' to
~/.cargo/templates/nickel-template. Each file in that directory would
then be compiled and rendered to the new project directory created by
cargo. Once the template is downloaded, future uses can just reference
it by name:

    cargo new webapp --template nickel-template

Certain files will be ignored in template directories. These are namely
files which mustache will not be able to compile. Image files, for
example are all ignored.

Closes issue rust-lang#396
@madeso
Copy link

madeso commented Jul 11, 2015

I think it looks great so far, however from a interface viewpoint I would like to see 2 additions:

  1. a centralized list with published templates, like crates.io for libraries and only using git/http sources as a fallback
  2. a manifest.toml for each template with useful data such as template description, used mustache variables etc

@pwoolcoc
Copy link
Contributor

So it isn't super extensive right now, but I have a cargo subcommand here: https://github.com/pwoolcoc/cargo-template that does something like this. Currently "templates" are really just existing cargo projects, and I fill in the name and authors section of the Cargo.toml after copying the rest of the files in the project over (minus obvious unnecessary stuff like .git/ and target/. I also have an index that points to existing templates: https://github.com/rusttemplates/templates . I only have a couple so far, but would love to have more added!

@alexcrichton
Copy link
Member

@pwoolcoc oh awesome! You may also be interested in #3004 which is adding a similar-ish feature to cargo new

@pwoolcoc
Copy link
Contributor

@alexcrichton yea thanks, I was told about that PR after I posted my project to /r/rust. I guess my github-search-fu wasn't good enough. I'm excited for #3004 to land!

@sfackler
Copy link
Member

I think this can be closed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Command-new E-hard Experience: Hard
Projects
None yet
Development

No branches or pull requests

7 participants