-
Notifications
You must be signed in to change notification settings - Fork 13
/
setup.py
280 lines (223 loc) · 8.19 KB
/
setup.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# importing required module
# -------------------------------
import subprocess
from shutil import which
import argparse
from pathlib import Path
from datetime import datetime
# import pynvim and install it if it's not already installed
try:
from pynvim import attach
except ImportError:
# install pip if it's not installed
try:
import pip
except ModuleNotFoundError:
command = ["python3", "-m", "ensurepip", "--default-pip"]
subprocess.run(command)
command = ['pip', 'install', 'pynvim']
subprocess.run(command)
from pynvim import attach
# -------------------------------
# argument handeling
# -------------------------------
parser = argparse.ArgumentParser()
parser.add_argument(
"--delete",
"-d",
type=int,
default=0,
help="0 or 1, 0 means don't delete(Default) and 1 means delete the no \
required file/directory (.git, LICENSE, README.md)",
)
parser.add_argument(
"--backup",
"-b",
type=int,
default=1,
help="0 or 1, 0 means don't backup and 1 means backup(Default) the \
config before any configuration",
)
parser.add_argument(
"--update",
"-u",
type=int,
default=0,
help="0 or 1, 1 means update and 0 means don't update(Default) the \
Abstract",
)
args = parser.parse_args()
delete = args.delete
backup = args.backup
update = args.update
# -------------------------------
# directory locations
# -------------------------------
nvim = attach('child', argv=[which("env"), "nvim", "--embed", "--headless"])
HOME = Path.home()
NVIM_DATA_DIR = nvim.funcs.stdpath('data')
NVIM_CONF_PATH = nvim.funcs.stdpath('config')
CONFIG = NVIM_CONF_PATH.split("nvim")[0]
CACHE = f"{NVIM_DATA_DIR}/.cache"
CUSTOM_TOOLS_DIR = f"{NVIM_DATA_DIR}/custom_tools"
CACHE_BUILD_PATH = f"{HOME}/.cache/build_files"
SCRIPT_PATH = Path(__file__).parent.absolute()
# -------------------------------
# directories we must have
# -------------------------------
require_dir = [
f"{CONFIG}",
f"{CACHE}/swap",
f"{CACHE}/view",
f"{CACHE}/shada",
f"{CACHE}/backedUP",
f"{CACHE}/undos",
CUSTOM_TOOLS_DIR,
CACHE_BUILD_PATH,
]
# -------------------------------
# -------------------------------
def create_require_dir(dirs):
need_to_inform = False
once = True
for dir in dirs:
if not Path(dir).exists():
need_to_inform = True
if need_to_inform and once:
print("creating required directories...")
once = False
Path(dir).mkdir(parents=True)
print(" ", dir)
# -------------------------------
# -------------------------------
def clone_repro(path, repository, name):
subprocess.run(['git', 'clone', repository, name], cwd=path)
# -------------------------------
# -------------------------------
def update_abstract():
subprocess.run(["git", "pull"], cwd=NVIM_CONF_PATH)
# -------------------------------
# -------------------------------
def backup_nvim():
current_date = datetime.today().strftime("%Y-%m-%d_%T")
if Path(NVIM_CONF_PATH).exists():
subprocess.run(['cp', '-rf', 'nvim', f'nvim-old_{current_date}'], cwd=CONFIG)
print(f"\nyour old config: {NVIM_CONF_PATH}_{current_date}\n")
# -------------------------------
# -------------------------------
def clean():
if Path(NVIM_DATA_DIR+"/site/pack/packer").exists():
subprocess.run(["rm", "-rf", NVIM_DATA_DIR+"/site/pack/packer"])
print("\nREMOVED: ", NVIM_DATA_DIR+"/site/pack/packer")
if Path(NVIM_CONF_PATH+"/plugin").exists():
subprocess.run(["rm", "-rf", NVIM_CONF_PATH+"/plugin"])
print("\nREMOVED: ", NVIM_CONF_PATH+"/plugin")
# -------------------------------
# -------------------------------
def abstract_git():
"""check if abstract exist as a git project"""
if Path(NVIM_CONF_PATH).exists():
if Path(f"{NVIM_CONF_PATH}/.__abstract__").is_file():
if Path(f"{NVIM_CONF_PATH}/.git").exists():
return True
return False
# -------------------------------
# -------------------------------
def need_to_clone_abstract():
"""check if we need to clone the abstract repro,
if setup.py run without cloning abstract
"""
if Path(f"{SCRIPT_PATH}/setup.py").is_file() and \
Path(f"{SCRIPT_PATH}/.__abstract__").is_file():
return False
if abstract_git():
return False
return True
# -------------------------------
# -------------------------------
def compile_nvim():
print("\nIf it's taking long time then press CTRL+C and run setup.py file again \n (2-5 minutes would be enough) \n")
packer_compile_cmd = ["nvim", "--headless", "-c", "autocmd User PackerComplete quitall", "-c", "PackerSync"]
subprocess.run(packer_compile_cmd)
# -------------------------------
# -------------------------------
def setup_packer():
nvim_plugin_dir = str(f"{NVIM_DATA_DIR}/site/pack/packer/start")
packer_dir = nvim_plugin_dir+"/packer.nvim"
print("\nsetting up packer...")
if not Path(nvim_plugin_dir).exists():
Path(nvim_plugin_dir).mkdir(parents=True)
if not Path(packer_dir).exists():
print(packer_dir)
repository = "https://github.com/wbthomason/packer.nvim"
subprocess.run(["git", "clone", "--depth", "1", repository], cwd=nvim_plugin_dir)
# -------------------------------
# -------------------------------
def remove_no_require():
subprocess.run(["rm", "-rf", ".git*", "LICENSE", "README.md", "setup.py", ".__*"], cwd=NVIM_CONF_PATH)
print("\nREMOVED: .git ,LICENSE ,README.md ,setup.py ,.__abstract__")
# -------------------------------
# -------------------------------
def main():
print("--------------------------------")
print("installing...this may take some time.")
print("--------------------------------\n")
# backup config if backup argument is 1
if backup == 1:
backup_nvim()
# create required directories
create_require_dir(require_dir)
if update == 1 and abstract_git():
update_abstract()
else:
if need_to_clone_abstract():
print("\n")
clone_repro(CONFIG, "https://github.com/Abstract-IDE/Abstract", "nvim")
else:
# prevent copying or removing if setup.up is running from ~/.config/nvim/
if str(SCRIPT_PATH) != str(NVIM_CONF_PATH):
if Path(f"{SCRIPT_PATH}/setup.py").is_file() and Path(f"{SCRIPT_PATH}/.__abstract__").is_file():
# remove ~/.config/nvim/ to prevent depth parent copy(eg: ~/.config/nvim/nvim)
subprocess.run(["rm", "-rf", NVIM_CONF_PATH])
print("\ncopying config...")
subprocess.run(["cp", "-rf", SCRIPT_PATH, NVIM_CONF_PATH])
else:
update_abstract()
else:
update_abstract()
# compile configs
try:
clean()
setup_packer()
print("\ncompiling config and plugins...")
compile_nvim()
except KeyboardInterrupt:
print("\n\n")
print("!!!someting went wrong!!!!\n please re-run setup.py file")
if delete == 1:
print("\ncleaning config...")
remove_no_require()
# cloning lazy-builder tool (https://github.com/shaeinst/lazy-builder)
lazy_builder_path = f"{CUSTOM_TOOLS_DIR}/lazy-builder"
if not Path(lazy_builder_path).exists():
try:
print("\ninstalling additional...")
repository = "https://github.com/Abstract-IDE/lazy-builder"
clone_repro(CUSTOM_TOOLS_DIR, repository, 'lazy-builder')
except KeyboardInterrupt:
print("additional tools didn't install\n")
else:
subprocess.run(["git", "pull"], cwd=lazy_builder_path)
msg = """\n\n
!!!WARNING!!!
we try our best to auto setup as much as possible.
you may get some errors during installation (maybe it's your network problem?)
so, open nvim (from your terminal ) and if nvim throws any errors,
it means that installaton wasn't sucessfull.
In that case, please re-run setup.py or
python <(curl -s https://raw.githubusercontent.com/Abstract-IDE/Abstract/main/setup.py)
"""
print(msg)
# -------------------------------
if __name__ == '__main__':
main()