-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
156 lines (127 loc) · 5.47 KB
/
setup.py
File metadata and controls
156 lines (127 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3
import logging
import os
import click
def init_logging(level: int) -> None:
assert isinstance(level, int)
logging.basicConfig(
format="%(asctime)s %(levelname)s [%(filename)s:%(lineno)d](%(funcName)s) - %(message)s"
if level <= logging.DEBUG
else "%(asctime)s %(levelname)s - %(message)s",
level=level,
handlers=[logging.StreamHandler()],
)
class JobLogger:
def __init__(self, message: str) -> None:
self.message = message
def __enter__(self):
logging.info(self.message)
def __exit__(self, exc_type, exc_val, exc_tb):
logging.info(f"{self.message} - done")
def replace_file(filename, old, new):
with open(filename, "r") as fp:
content = fp.read()
content = content.replace(old, new)
with open(filename, "w") as fp:
fp.write(content)
@click.command()
@click.option("-o", "--org", "org_opt", required=True, help="organization")
@click.option("-r", "--repo", "repo_opt", required=True, help="repository")
@click.option(
"--required-version",
"required_opt",
default="0.7",
help="required nvim version, by default is `0.7`",
)
@click.option(
"--indent-size", "indent_opt", default=2, help="indent size, by default is `2`"
)
@click.option(
"--debug",
"debug_opt",
is_flag=True,
help="enable debug, by default is `false`",
)
def setup(debug_opt, org_opt, repo_opt, required_opt, indent_opt):
init_logging(logging.DEBUG if debug_opt else logging.INFO)
logging.debug(
f"debug_opt:{debug_opt}, org_opt:{org_opt}, repo_opt:{repo_opt}, required_opt:{required_opt}, indent_opt:{indent_opt}"
)
org = org_opt
repo_nvim = repo_opt
assert isinstance(repo_nvim, str)
repo = repo_nvim[:-5] if repo_nvim.endswith(".nvim") else repo_nvim
repo_underscore = repo.replace("-", "_")
indent = int(indent_opt)
assert isinstance(required_opt, str)
required_version = (
f"{required_opt}.0" if required_opt.count(".") == 1 else required_opt
)
required_version = (
f"v{required_version}"
if not required_version.startswith("v")
else required_version
)
required_version_short = required_version[:-2]
logging.debug(
f"org:{org}, repo_nvim:{repo_nvim}, repo:{repo}, repo_underscore:{repo_underscore}, indent:{indent}, required_version:{required_version}, required_version_short:{required_version_short}"
)
# remove CHANGELOG.md
if os.path.exists("CHANGELOG.md"):
with JobLogger("remove CHANGELOG.md"):
os.remove("CHANGELOG.md")
# clear README.md
with JobLogger("update README.md"):
with open("README.md", "w") as fp:
fp.write(
f"""
# {repo_nvim}
<p align="center">
<a href="https://github.com/neovim/neovim/releases/{required_version}"><img alt="Neovim" src="https://img.shields.io/badge/require-{required_version_short}+-blue" /></a>
<a href="https://luarocks.org/modules/{org}/{repo_nvim}"><img alt="luarocks" src="https://img.shields.io/luarocks/v/{org}/{repo_nvim}" /></a>
<a href="https://github.com/{org}/{repo_nvim}/actions/workflows/ci.yml"><img alt="ci.yml" src="https://img.shields.io/github/actions/workflow/status/{org}/{repo_nvim}/ci.yml?label=ci" /></a>
<a href="https://app.codecov.io/github/{org}/{repo_nvim}"><img alt="codecov" src="https://img.shields.io/codecov/c/github/{org}/{repo_nvim}?label=codecov" /></a>
</p>
"""
)
# update LICENSE
with JobLogger("update LICENSE"):
replace_file("LICENSE", "linrongbin16", org)
replace_file("LICENSE", "ci-template", repo)
# update .github/workflows
with JobLogger("update .github/workflows"):
for action_file in os.listdir(".github/workflows"):
replace_file(f".github/workflows/{action_file}", "linrongbin16", org)
replace_file(f".github/workflows/{action_file}", "ci-template", repo)
replace_file(f".github/workflows/{action_file}", "v0.7.0", required_version)
# update .luacov
with JobLogger("update .luacov"):
replace_file(".luacov", "linrongbin16", org)
replace_file(".luacov", "ci-template", repo)
# rename lua/ci-template.lua
with JobLogger("rename lua/ci-template.lua"):
os.rename("lua/ci-template.lua", f"lua/{repo}.lua")
# rename spec/ci_template_spec.lua
with JobLogger("rename spec/ci_template_spec.lua"):
os.rename("spec/ci_template_spec.lua", f"spec/{repo_underscore}_spec.lua")
# update spec/{repo_underscore}_spec.lua
with JobLogger(f"update spec/{repo_underscore}_spec.lua"):
replace_file(f"spec/{repo_underscore}_spec.lua", "linrongbin16", org)
replace_file(f"spec/{repo_underscore}_spec.lua", "ci-template", repo)
replace_file(f"spec/{repo_underscore}_spec.lua", "ci_template", repo_underscore)
# clear .release-please-manifest.json
with JobLogger("update .release-please-manifest.json"):
with open(".release-please-manifest.json", "w") as fp:
fp.write('{\n ".": "0.0.1"\n}')
if indent != 2:
# update .editorconfig
with JobLogger("update .editorconfig"):
replace_file(".editorconfig", "= 2", f"= {indent}")
# update .stylua.toml
with JobLogger("update .stylua.toml"):
replace_file(".stylua.toml", "= 2", f"= {indent}")
# update .nvim.lua
with JobLogger("update .nvim.lua"):
replace_file(".nvim.lua", "= 2", f"= {indent}")
if __name__ == "__main__":
setup()