Skip to content

Commit 15d1d30

Browse files
committed
✨feat: init repo automatically in post hooks
1 parent 4d1e2ff commit 15d1d30

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

hooks/post_gen_project.py

+28-15
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,36 @@
55

66
PROJECT_DIRECTORY = os.path.realpath(os.path.curdir)
77

8-
98
def remove_file(filepath):
10-
os.remove(os.path.join(PROJECT_DIRECTORY, filepath))
9+
try:
10+
os.remove(os.path.join(PROJECT_DIRECTORY, filepath))
11+
except FileNotFoundError:
12+
pass
13+
14+
def execute(*args, supress_exception = False, cwd=None):
15+
cur_dir = os.getcwd()
1116

12-
def execute(*args, supress_exception = False):
13-
proc = subprocess.Popen(args, stdout = subprocess.PIPE, stderr= subprocess.PIPE)
17+
try:
18+
if cwd:
19+
os.chdir(cwd)
1420

15-
out, err = proc.communicate()
16-
out = out.decode('utf-8')
17-
err = err.decode('utf-8')
18-
if err and not supress_exception:
19-
raise Exception(err)
20-
else:
21-
return out
21+
proc = subprocess.Popen(args, stdout = subprocess.PIPE, stderr= subprocess.PIPE)
22+
23+
out, err = proc.communicate()
24+
out = out.decode('utf-8')
25+
err = err.decode('utf-8')
26+
if err and not supress_exception:
27+
raise Exception(err)
28+
else:
29+
return out
30+
finally:
31+
os.chdir(cur_dir)
2232

2333
def init_git():
2434
# workaround for issue #1
2535
if not os.path.exists(os.path.join(PROJECT_DIRECTORY, ".git")):
26-
execute("git", "init")
27-
execute("git", "tag", "0.1.0")
36+
execute("git", "config", "--global", "init.defaultBranch", "main", cwd = PROJECT_DIRECTORY)
37+
execute("git", "init", cwd=PROJECT_DIRECTORY)
2838

2939
def install_pre_commit_hooks():
3040
execute(sys.executable, "-m", "pip", "install", "pre-commit==2.12.0")
@@ -43,8 +53,11 @@ def install_pre_commit_hooks():
4353
if 'Not open source' == '{{ cookiecutter.open_source_license }}':
4454
remove_file('LICENSE')
4555

46-
init_git()
47-
56+
try:
57+
init_git()
58+
except Exception as e:
59+
print(e)
60+
4861
if '{{ cookiecutter.install_precommit_hooks }}' == 'y':
4962
try:
5063
install_pre_commit_hooks()

0 commit comments

Comments
 (0)