# Cookiecutter ## 簡介 以往在建置專案時,會要重新 setup 許多東西,其中也包括了目錄文件架構、文件內容、ignore 檔、lint 檔等等,但每次在開發專案時都要重複這樣的動作**非常耗時且容易出錯**,因此 Cookiecutter 的出現解決了這項問題,讓使用者可以下**一個 command 完成所有建置步驟**。 [Cookiecutter](https://cookiecutter.readthedocs.io/en/latest/readme.html) 就像是快速做餅乾的工具,而 templates 是餅乾的模型,Cookiecutter 依照這個 templates 做出來的餅乾都會是一樣的,唯一的差異在於每個專案可以有自己的變數,例如:專案名稱、路徑等,都透過 Jinja2 來替換這些變數。 > `100% of templating is done with Jinja2. This includes file and directory names.` > > ⎯⎯ [Cookiecutter Features](https://cookiecutter.readthedocs.io/en/latest/readme.html?highlight=jinja2#features) 另外, templates 並**不局限於任何程式語言**,也可以在同一個專案下使用多種語言。 ## 安裝 [安裝 Cookiecutter](https://cookiecutter.readthedocs.io/en/latest/installation.html) 。 ```shell $ pip install cookiecutter ``` ## 以 Cookiecutter 建置專案 以 [Python Project Cookiecutter](https://gitlab.kkinternal.com/pet/cookiecutter-pyproject/tree/master) 為例,本專案包含一個最簡單打招呼的程式,並且包含了一個有使用者名稱的資料庫。 1. 從 Gitlab 上抓取該 Cookiecutter ,並設置成自己的專案。 ```shell $ cookiecutter git@gitlab.kkinternal.com:pet/cookiecutter-pyproject.git ``` 會被要求輸入一些專案的變數,而這些變數就是對應到 [`cookiecutter.json`](https://gitlab.kkinternal.com/pet/cookiecutter-pyproject/blob/master/cookiecutter.json) 內的變數。 ```shell python_version [3.7.0]: project_name [My Awesome Project]: project_slug [my-awesome-project]: project_path [pet/my-awesome-project]: project_description [A great project!]: package_name [my_awesome_project]: init_version [0.1.0]: ``` 2. 資料夾下會出現以 [`{{cookiecutter.project_slug}}`](https://gitlab.kkinternal.com/pet/cookiecutter-pyproject/tree/master/%7B%7Bcookiecutter.project_slug%7D%7D) 為 templates 的目錄文件架構。可以依照檔案對應看 Wiki 文件,詳細請看[備註](#備註)。 ## 參考資料 * [audreyr/cookiecutter - GitHub](https://github.com/audreyr/cookiecutter) * [Cookiecutter - Read the Docs](https://cookiecutter.readthedocs.io/en/latest/) ## 備註 [Python Project Cookiecutter](https://gitlab.kkinternal.com/pet/cookiecutter-pyproject/tree/master) 專案下的檔案,各自對應的 Wiki 文件: * `.bumpversion.cfg` :[bumpversion](bumpversion) * `.gitlab-ci.yml` :[gitlab-ci](gitlab-ci) * `Dockerfile` 、 `docker-compose.yml` 、 `.dockerignore` :[docker](docker) * `Makefile` :[Makefile](makefile) * `Pipfile` :[Pipenv](pipenv) * `pylintrc` :[pylint](PyLint) * `tests/` :[pytest](pytest) 、 [mock](mock) * `{{package_name}}/devtools.py` 、 `{{package_name}}/cli.py` :[Click](click) * `{{package_name}}/main.py` :[logging](logging)
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.