gruntwork-io / boilerplate

A tool for generating files and folders ("boilerplate") from a set of templates
https://www.gruntwork.io
Mozilla Public License 2.0
157 stars 12 forks source link

Question: obtain the relative path boilerplate working dir #171

Open xNok opened 3 months ago

xNok commented 3 months ago

Assuming that you run boilerplate at the root of a repo and want to start building a relative path to the root. How would you go about obtaining those paths?

Here is an example structure

. .boilerplate
| ` testfile.txt
+ subfolder
  ` + subsubfolder
     ` testfile.txt

Then then then I run

boilerplate --template-url ./.boilerplates/test --output-folder ./subfolder/subsubfolder

I would want testfile.txt to contain ./subfolder/subsubfolder

I tried some hacks using shell, but interpolation does seem to occur in that way

{{ relPath outputFolder (shell echo "$PWD") }}

Maybe We could consider providing this out of the box in a variable.

{{ relPath outputFolder cmdPwd }}
xNok commented 3 months ago

I ran some more experiments

{{ relPath (shell "bash" "-c" "echo $PWD") outputFolder }}

This results in ../../subfolder/subsubfolder, so my conclusion is that $PWD is actually the template folder. So there is know what to obtain the path from where the command Is executed atm.

brikis98 commented 3 months ago

Ah, adding a workingDir param to the context, which contains the path where you ran boilerplate, is a good idea. A PR for that is very welcome!

In the meantime, if you're looking for the root of a Git repo, perhaps with a shell command, you could use git rev-parse --show-toplevel?

xNok commented 3 months ago

I will look into making a PR, just need to put my head into the code at some point.

The best working solution so far

{{ relPath (env "PWD" "NO_FOUND") outputFolder }}