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 templating feature for "from" file/template #2312

Merged
merged 1 commit into from
Jul 12, 2020

Conversation

niamtokik
Copy link

@niamtokik niamtokik commented Jul 1, 2020

Hi,

I wanted to have an easy way to select different kind of license for my projects based on defined template. Unfortunately, rebar3 supports variable templating only on the target files and templates' names. I added the support of templating in the From part of the execution. That means it is now possible to add a variable in the source and not only the destination. Here an exemple:

{description, "Custom app with ct test and c_src directory"}.

{variables, [
  {license, "apache-2.0", "default license"}
]}.

{dir, "{{name}}"}.
{dir, "{{name}}/src"}.
{dir, "{{name}}/test"}.
{dir, "{{name}}/c_src"}.

{template, "_app/LICENSE-{{license}}", "{{name}}/LICENSE"}.
{chmod, "{{name}}/LICENSE", 8#644}.

{template, "_app/README.md", "{{name}}/README.md"}.
{chmod, "{{name}}/README.md", 8#644}.

By default, the license variable is set to apache-2.0 but we can select any other variables. If the file _app/LICENSE-{{license}} exists, the file will be copied in {{name}}/LICENSE. A whole working example exist in my rebar3_templates own's repository. By adding the template _app in ${HOME}/.config/rebar3, I can know use different available license:

# you can skip this step if you already
# have your own templates.
cd ${HOME}/.config/rebar3
git clone https://github.com/niamtokik/rebar3_templates.git .

# will create a project with default apache-2.0 license
rebar3 new _app name=apache 
ls apache/LICENSE && cat apache/LICENSE

# will create a project with BSD0 license
rebar3 new _app name=bsd license=BSD0
ls bsd/LICENSE && cat bsd/LICENSE

# will create a project with MIT license
rebar3 new _app name=mit license=MIT
ls mit/LICENSE && cat mit/LICENSE

I did not add a check if the file exist or not. If the license does not exist, rebar3 will crash.

EDIT: tested on OpenBSD-current.

@ferd
Copy link
Collaborator

ferd commented Jul 4, 2020

Seems good enough to me. Reuses old functions so it should keep working okay.

The hard crash is probably something we'd ideally want to avoid since templates is possibly the first feature a user tries when following a tutorial. Being able to detect and properly fail would be ideal there.

I'm guessing that if we don't expect to use the feature internally, doing a direct file check around the filename:join would work. Otherwise, we'd probably need to make a load_file fix that checks and errors by explaining that a given file can't be found before crashing to at least let the user understand what goes on.

@niamtokik
Copy link
Author

niamtokik commented Jul 12, 2020

I'm guessing that if we don't expect to use the feature internally, doing a direct file check around the filename:join would work. Otherwise, we'd probably need to make a load_file fix that checks and errors by explaining that a given file can't be found before crashing to at least let the user understand what goes on.

fixed on the last commit. Rebar3 aborts when a file does not exist or if there is an error with it. Don't know if it could generate side effect, but work on my side. It should return something like:

rebar3 new _app name=test LICENSE=imnothere
# ===> Failed to load file "/home/user/.config/rebar3/templates/_app/LICENSE-imnothere": enoent

Modify also the load_file function by adding a
way to check an error, and abort the program
smoothly.
@niamtokik niamtokik force-pushed the from-file-with-template branch from 0940b37 to 9f03c34 Compare July 12, 2020 07:38
@ferd ferd merged commit 60652e6 into erlang:master Jul 12, 2020
@ferd
Copy link
Collaborator

ferd commented Jul 12, 2020

Thanks for the contribution!

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

Successfully merging this pull request may close these issues.

2 participants