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

Project examples as templates #78

Open
dcarosone opened this issue Jul 31, 2018 · 5 comments
Open

Project examples as templates #78

dcarosone opened this issue Jul 31, 2018 · 5 comments

Comments

@dcarosone
Copy link

In #47, one of the possibilities noted is

sub dir in a repo

I wanted to make a suggestion that's a little more extensive than this, and I think it's enough of an expansion to list as a separate issue.

A number of projects have examples/ directories with cargo support for running them directly, and there's also a proposal to let cargo support larger per-directory examples in a sub-structure under there. These are often good starting points for customising your own code; I know I've done this a number of times, especially with some embedded projects. That sounds a lot like a template!

I think is worth combining these proposals and having some specific conventions to make it easier, such that project example directories can always/automatically be templates. I'd like it to be sufficiently low-friction that neither general crate authors nor template consumers need to do anything much extra or unusual to use examples as templates, and offering templates becomes the norm.

This probably involves some changes on both sides, perhaps something like this:

  • example directories look like templates, with placeholders, and have an example.toml with placeholder values. Cargo knows how to use this when running in-place as an example.
  • this in turn suggests that making a new example (directory) should itself make a bare template (cargo new --example?) so that there's a cohesive story from start to, well, new start :)
  • when given a repo, cargo generate can look in the examples directories to find templates, and (in interactive mode) offer a list to pick from, so that repos can be discoverable as either single templates at the root, or more structured collections.

One of the strongest reasons for this is to keep examples and templates in sync with the libraries they represent; cargo should test each of the templates-as-examples just like today. If they're in a separate repo, there's always a risk they'll languish. If they're in the one repo, examples can also be updated to match library code changes on branches.

@dcarosone
Copy link
Author

a proposal to let cargo support larger per-directory examples in a sub-structure

I couldn't recall where I'd seen this to link at the time, but it just came up again:
https://internals.rust-lang.org/t/pre-rfc-project-based-examples-for-cargo-projects/8088

taurr referenced this issue in taurr/cargo-generate Jul 25, 2021
Using cargo generate --git <repo>#<sub-folder, this one
1) Allows to use a subfolder as the actual template (see #47)
2) Allows to e.g. generate a project from an example in a repo (see #78)
3) Avoids the conflicting requirements for cargo-generate#295 and cargo-generate#291, allowing for easier implementation of `--pick` for cargo-generate#291.
taurr referenced this issue in taurr/cargo-generate Jul 25, 2021
Using cargo generate --git <repo>#<sub-folder, this one
1) Allows to use a subfolder as the actual template (see #47)
2) Allows to e.g. generate a project from an example in a repo (see #78)
3) Avoids the conflicting requirements for cargo-generate#295 and cargo-generate#291, allowing for easier implementation of `--pick` for cargo-generate#291.
taurr referenced this issue in taurr/cargo-generate Jul 25, 2021
Using cargo generate --git <repo>#<sub-folder, this one
1) Allows to use a subfolder as the actual template (see #47)
2) Allows to e.g. generate a project from an example in a repo (see #78)
3) Avoids the conflicting requirements for cargo-generate#295 and cargo-generate#291, allowing for easier implementation of `--pick` for cargo-generate#291.
taurr referenced this issue in taurr/cargo-generate Jul 26, 2021
Using cargo generate --git <repo>#<sub-folder, this one
1) Allows to use a subfolder as the actual template (see #47)
2) Allows to e.g. generate a project from an example in a repo (see #78)
3) Avoids the conflicting requirements for cargo-generate#295 and cargo-generate#291, allowing for easier implementation of `--pick` for cargo-generate#291.
taurr referenced this issue in taurr/cargo-generate Jul 27, 2021
Using cargo generate --git <repo>#<sub-folder, this one
1) Allows to use a subfolder as the actual template (see #47)
2) Allows to e.g. generate a project from an example in a repo (see #78)
3) Avoids the conflicting requirements for cargo-generate#295 and cargo-generate#291, allowing for easier implementation of `--pick` for cargo-generate#291.
taurr referenced this issue in taurr/cargo-generate Jul 27, 2021
Using cargo generate --git <repo>#<sub-folder, this one
1) Allows to use a subfolder as the actual template (see #47)
2) Allows to e.g. generate a project from an example in a repo (see #78)
3) Avoids the conflicting requirements for cargo-generate#295 and cargo-generate#291, allowing for easier implementation of `--pick` for cargo-generate#291.
taurr referenced this issue in taurr/cargo-generate Jul 29, 2021
Using cargo generate --git <repo>#<sub-folder, this one
1) Allows to use a subfolder as the actual template (see #47)
2) Allows to e.g. generate a project from an example in a repo (see #78)
3) Avoids the conflicting requirements for cargo-generate#295 and cargo-generate#291, allowing for easier implementation of `--pick` for cargo-generate#291.
taurr referenced this issue in taurr/cargo-generate Jul 29, 2021
Using cargo generate --git <repo>#<sub-folder, this one
1) Allows to use a subfolder as the actual template (see #47)
2) Allows to e.g. generate a project from an example in a repo (see #78)
3) Avoids the conflicting requirements for cargo-generate#295 and cargo-generate#291, allowing for easier implementation of `--pick` for cargo-generate#291.
sassman pushed a commit that referenced this issue Jul 29, 2021
Using cargo generate --git <repo>#<sub-folder, this one
1) Allows to use a subfolder as the actual template (see #47)
2) Allows to e.g. generate a project from an example in a repo (see #78)
3) Avoids the conflicting requirements for #295 and #291, allowing for easier implementation of `--pick` for #291.
@taurr
Copy link
Collaborator

taurr commented Aug 16, 2021

What's needed in order to close this?

@sassman
Copy link
Member

sassman commented Aug 16, 2021

I wanted to start with an example folder to demonstrate certain use cases in order to provide some simple starting points for template authors.

@taurr
Copy link
Collaborator

taurr commented Sep 7, 2021

@sassman, I just added a folder example-templates to the root in #445, as I believe this will be the best way of showing/documenting that feature. If you decide to let #446 go through, then if a template contains cargo-generate.toml, users end up being able to expand simply by:

cargo generate --git <repo>

no matter where in the repo the templates are.

I don't know if you've had time to look into #370, but that one should allow use to use github actions to expand, then compile-test the templates.

I don't see how we could ever use regular examples as templates, as they simply do not contain the tags needed - thus it would end up being straight copies anyway, but #446 and #370 together will at least make it easy to expand embedded templates and help with the testability.

@taurr
Copy link
Collaborator

taurr commented Sep 25, 2021

@sassman what kind of examples should we provide in order to close this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants