aws / industry-toolkit

Apache License 2.0
10 stars 0 forks source link

How to build a cookiecutter project from template in repo? #10

Closed mws-github closed 2 months ago

mws-github commented 2 months ago

Just tried to build a new project with the Spring template, but failed. Am I referencing the the template the wrong way from the command line?

Local Build Log

C:\temp\cookie-playground>cookiecutter https://github.com/aws/industry-toolkit/tree/main/templates/servers/java-spring-boot/%7B%7Bcookiecutter.project_name%7D%7D The repository https://github.com/aws/industry-toolkit/tree/main/templates/servers/java-spring-boot/%7B%7Bcookiecutter.project_name%7D%7D could not be found, have you made a typo?

paulfryer commented 2 months ago

When the cookiecutter template is nested within a repository, like in your example (https://github.com/aws/industry-toolkit/tree/main/templates/servers/java-spring-boot/{{cookiecutter.project_name}}), there are a few ways to work around this, as cookiecutter expects the template to be at the root of the repository by default.

Here are some approaches you can use to access the nested template:

  1. Cloning the Repository Locally and Running Locally The simplest approach is to clone the repository, navigate to the nested template directory, and run cookiecutter from there:

bash Copy code git clone https://github.com/aws/industry-toolkit cd industry-toolkit/templates/servers/java-spring-boot cookiecutter . This approach works well if you’re okay with cloning the whole repository locally.

  1. Use a Local Path with Cookiecutter If the repository is too large or you don't want to clone everything, you can manually download just the relevant template files from the repository using GitHub’s Download ZIP feature, extract it, and then run cookiecutter locally:

Navigate to the nested template folder on GitHub (e.g., templates/servers/java-spring-boot). Download that directory as a ZIP, or clone just that part of the repo using sparse checkout. Run: bash Copy code cookiecutter /path/to/extracted/folder

  1. Use git sparse-checkout for Just the Template Directory If cloning the entire repository isn’t ideal, but you want to work from a specific subdirectory, you can use Git sparse-checkout to clone only the relevant directory. Here's how to do it:

bash Copy code git init industry-toolkit cd industry-toolkit git remote add origin https://github.com/aws/industry-toolkit.git git config core.sparseCheckout true echo "templates/servers/java-spring-boot/" >> .git/info/sparse-checkout git pull origin main cd templates/servers/java-spring-boot cookiecutter . This will only pull the templates/servers/java-spring-boot directory, allowing you to avoid downloading the entire repository.

  1. Manual Download of Specific Files Manually download the nested template files and organize them on your local system. You can download the directory directly via GitHub, or use wget or curl for individual files. Once you have the structure locally, you can run cookiecutter on it as if it were a local template.
  2. Script to Automate Downloading and Running You could also create a script that downloads the required files automatically, unpacks them, and runs cookiecutter. This could be a Python or Bash script that uses GitHub’s API or wget/curl to download only the required parts.

Summary If the template is nested in a repository, the key options are:

Clone the repository and navigate to the nested folder to run cookiecutter. Use git sparse-checkout to clone only the template folder. Manually download the template files and run cookiecutter locally. Each option ensures that you can properly use a nested template without needing to restructure the repository.

paulfryer commented 2 months ago

Our toolkit will take 2 parameters:

  1. A repository name
  2. The directory path to the template (if applicable).

The tooling will handle the downloading of the template.