|
1 | 1 | import shutil |
2 | 2 | import subprocess |
3 | 3 | import sys |
| 4 | +from functools import partial |
4 | 5 |
|
| 6 | +run = partial(subprocess.run, check=False) |
5 | 7 |
|
6 | | -def possibly_install_uv() -> None: |
7 | | - if shutil.which("uv") is None: |
8 | | - # install uv |
9 | | - subprocess.run( |
10 | | - [sys.executable, "-m", "pip", "install", "--user", "uv"], check=False |
11 | | - ) |
12 | 8 |
|
| 9 | +def possibly_install_pipx() -> None: |
| 10 | + if shutil.which("pipx") is None: |
| 11 | + # install pipx |
| 12 | + run([sys.executable, "-m", "pip", "install", "--user", "pipx"]) |
| 13 | + # add pipx to PATH |
| 14 | + run([sys.executable, "-m", "pipx", "ensurepath"]) |
13 | 15 |
|
14 | | -def possibly_install_nox() -> None: |
15 | | - if shutil.which("nox") is None: |
16 | | - # possibly install uv |
17 | | - possibly_install_uv() |
18 | 16 |
|
19 | | - # install nox |
20 | | - subprocess.run(["uv", "tool", "install", "nox"], check=False) |
| 17 | +def possibly_install_uv() -> None: |
| 18 | + if shutil.which("uv") is None: |
| 19 | + # possibly install pipx |
| 20 | + possibly_install_pipx() |
21 | 21 |
|
| 22 | + # install uv |
| 23 | + run([sys.executable, "-m", "pipx", "install", "uv"]) |
22 | 24 |
|
23 | | -def possibly_install_pre_commit() -> None: |
24 | | - if shutil.which("pre-commit") is None: |
25 | | - # possibly install uv |
26 | | - possibly_install_uv() |
27 | 25 |
|
28 | | - # install pre-commit |
29 | | - subprocess.run( |
30 | | - ["uv", "tool", "install", "pre-commit", "--with", "pre-commit-uv"], |
31 | | - check=False, |
32 | | - ) |
| 26 | +def initialize_venv() -> None: |
| 27 | + # create virtual environment and install dependencies |
| 28 | + run(["uv", "sync", "--dev"]) |
33 | 29 |
|
34 | 30 |
|
35 | 31 | def initialize_git_repository() -> None: |
36 | 32 | # initialize Git repository |
37 | | - subprocess.run(["git", "init", "-b", "main"], check=False) |
| 33 | + run(["git", "init", "-b", "main"]) |
38 | 34 |
|
39 | 35 | # update and install pre-commit hooks |
40 | | - subprocess.run(["pre-commit", "autoupdate"], check=False) |
41 | | - subprocess.run(["pre-commit", "install", "--install-hooks"], check=False) |
| 36 | + run(["uv", "run", "prek", "update"]) |
| 37 | + run(["uv", "run", "prek", "install", "--prepare-hooks"]) |
42 | 38 |
|
43 | 39 | # add files |
44 | | - subprocess.run(["git", "add", "."], check=False) |
| 40 | + run(["git", "add", "."]) |
45 | 41 |
|
46 | | - # run nox "cog" and "pre-commit" sessions |
47 | | - subprocess.run(["nox", "--session", "cog", "pre-commit"], check=False) |
| 42 | + # run the pre-commit hooks (which, amongst others, runs cog) |
| 43 | + run(["uv", "run", "prek", "run", "--all-files"]) |
48 | 44 |
|
49 | 45 | # possibly re-add files |
50 | | - subprocess.run(["git", "add", "."], check=False) |
| 46 | + run(["git", "add", "."]) |
51 | 47 |
|
52 | 48 | # commit |
53 | | - subprocess.run( |
| 49 | + run( |
54 | 50 | [ |
55 | 51 | "git", |
56 | 52 | "commit", |
57 | 53 | "-m", |
58 | | - ":cookie: Initial commit from `cookiecutter-python-cli-app`", |
| 54 | + "Initial commit from `cookiecutter-python-cli-app`", |
59 | 55 | ], |
60 | | - check=False, |
61 | 56 | ) |
62 | 57 |
|
63 | 58 |
|
64 | | -def initialize_venv() -> None: |
65 | | - # run nox "dev" session |
66 | | - subprocess.run(["nox", "--session", "dev"], check=False) |
67 | | - |
68 | | - |
69 | 59 | if __name__ == "__main__": |
70 | | - # possibly install nox (and uv) |
71 | | - possibly_install_nox() |
72 | | - # possibly install pre-commit (and uv) |
73 | | - possibly_install_pre_commit() |
| 60 | + # possibly install uv (and pipx) |
| 61 | + possibly_install_uv() |
| 62 | + # create venv and install dependencies |
| 63 | + initialize_venv() |
74 | 64 | # perform git initialization |
75 | 65 | if "{{ cookiecutter.init_git }}" == "True": |
76 | 66 | initialize_git_repository() |
77 | | - # create venv and install dependencies |
78 | | - if "{{ cookiecutter.init_venv }}" == "True": |
79 | | - initialize_venv() |
0 commit comments