2019-12-22 04:23:03 +01:00
|
|
|
import json
|
2019-12-28 05:07:41 +01:00
|
|
|
import os
|
2019-12-28 09:12:08 +01:00
|
|
|
import subprocess
|
2019-12-28 05:07:41 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
from PyInquirer import prompt
|
|
|
|
|
|
|
|
from AbstractModule import AbstractModule
|
|
|
|
|
|
|
|
|
2019-12-22 02:45:19 +01:00
|
|
|
class VimModule(AbstractModule):
|
2019-12-14 04:22:34 +01:00
|
|
|
def __init__(self):
|
2019-12-14 05:47:33 +01:00
|
|
|
super().__init__()
|
2020-01-18 05:56:19 +01:00
|
|
|
self._debugFlag = False
|
|
|
|
self._json_config = {}
|
2019-12-28 03:35:40 +01:00
|
|
|
with open("modules/vim/vimrc_conf.json") as plugins:
|
2020-01-18 05:56:19 +01:00
|
|
|
self._json_config = json.load(plugins)
|
|
|
|
|
|
|
|
def _get_vim_root(self):
|
|
|
|
if self._debugFlag:
|
|
|
|
return Path(os.getcwd()).joinpath("devenv", ".vim")
|
|
|
|
else:
|
|
|
|
return Path().home().joinpath(".vim")
|
2019-12-22 03:59:38 +01:00
|
|
|
|
2020-01-18 05:56:19 +01:00
|
|
|
def _create_folder_structure(self):
|
|
|
|
os.makedirs(str(self._get_vim_root().joinpath("autoload")), exist_ok=True)
|
|
|
|
os.makedirs(str(self._get_vim_root().joinpath("plugged")), exist_ok=True)
|
2019-12-28 06:04:27 +01:00
|
|
|
|
2020-01-18 05:56:19 +01:00
|
|
|
def _get_vimplug_file(self):
|
|
|
|
vimplug_filepath = Path(str(self._get_vim_root().joinpath("autoload")))
|
2019-12-28 09:12:08 +01:00
|
|
|
print(vimplug_filepath)
|
2020-01-18 05:56:19 +01:00
|
|
|
if os.path.isfile(str(self._get_vim_root().joinpath("autoload", "plug.vim"))):
|
2019-12-28 09:12:08 +01:00
|
|
|
print("Vimplug already installed!")
|
|
|
|
else:
|
2020-01-11 05:12:05 +01:00
|
|
|
curl_request = "curl {} -o {}".format(
|
2020-01-18 05:56:19 +01:00
|
|
|
self._json_config['pluginmanager_url'],
|
|
|
|
str(self._get_vim_root().joinpath("autoload", "plug.vim"))
|
2020-01-11 05:12:05 +01:00
|
|
|
)
|
2019-12-28 09:12:08 +01:00
|
|
|
subprocess.call(curl_request, shell=True)
|
|
|
|
|
2020-01-18 05:56:19 +01:00
|
|
|
def _create_plugin_section(self):
|
2019-12-28 06:13:25 +01:00
|
|
|
vimrc_content = []
|
2020-01-18 05:56:19 +01:00
|
|
|
plugins = self._json_config['plugins']
|
2019-12-22 03:59:38 +01:00
|
|
|
print("Available Plugins:")
|
2019-12-30 02:07:37 +01:00
|
|
|
plugin_selection = [
|
2019-12-22 03:59:38 +01:00
|
|
|
{
|
|
|
|
'type': 'checkbox',
|
|
|
|
'message': 'Select plugins',
|
|
|
|
'name': 'modules',
|
2020-01-11 04:13:36 +01:00
|
|
|
'choices': list(map(lambda x: {"name": x}, plugins.keys()))
|
2019-12-22 03:59:38 +01:00
|
|
|
}
|
2019-12-14 04:22:34 +01:00
|
|
|
]
|
2019-12-30 02:07:37 +01:00
|
|
|
selected_plugins = prompt(plugin_selection)['modules']
|
2020-01-11 05:12:05 +01:00
|
|
|
if len(selected_plugins) > 0:
|
2020-01-18 05:56:19 +01:00
|
|
|
self._get_vimplug_file()
|
2019-12-27 03:18:39 +01:00
|
|
|
print("\033[4mYour selection:\033[0m")
|
2019-12-22 07:49:51 +01:00
|
|
|
[print(x) for x in selected_plugins]
|
2019-12-15 07:30:20 +01:00
|
|
|
|
2020-01-12 19:00:19 +01:00
|
|
|
vimrc_content.append("call plug#begin('~/.vim/plugged')\n")
|
|
|
|
for element in selected_plugins:
|
|
|
|
vimrc_content.append(plugins[element] + "\n")
|
|
|
|
vimrc_content.append("call plug#end()\n")
|
2019-12-19 14:03:31 +01:00
|
|
|
|
2019-12-28 06:13:25 +01:00
|
|
|
return vimrc_content
|
|
|
|
|
2020-01-18 05:56:19 +01:00
|
|
|
def _create_setting_section(self):
|
|
|
|
default_settings = list(self._json_config['settings'].values())
|
2020-01-11 04:13:36 +01:00
|
|
|
|
2019-12-28 06:04:27 +01:00
|
|
|
print("\n\033[4mDefault settings:\033[0m")
|
|
|
|
[print(i) for i in default_settings]
|
|
|
|
|
2020-01-11 04:13:36 +01:00
|
|
|
vimrc_content = list(map(lambda x: x + "\n", default_settings))
|
2019-12-28 06:13:25 +01:00
|
|
|
return vimrc_content
|
2019-12-28 05:19:34 +01:00
|
|
|
|
2020-01-18 05:56:19 +01:00
|
|
|
def _get_vimfile_working_copy(self):
|
|
|
|
vimrc_path = str(self._get_vim_root().joinpath(".vimrc"))
|
2019-12-28 06:04:27 +01:00
|
|
|
try:
|
|
|
|
with open(vimrc_path, "r") as vimrc_file:
|
|
|
|
vimrc_content = vimrc_file.readlines()
|
|
|
|
except FileNotFoundError:
|
|
|
|
vimrc_content = []
|
|
|
|
|
|
|
|
return vimrc_content
|
|
|
|
|
2020-01-18 05:56:19 +01:00
|
|
|
def _write_vimfile(self, vimrc_content):
|
2019-12-22 07:49:51 +01:00
|
|
|
seen = set()
|
|
|
|
seen_add = seen.add
|
|
|
|
vimrc_content = [x1 for x1 in vimrc_content if not (x1 in seen or seen_add(x1))]
|
2019-12-15 07:30:20 +01:00
|
|
|
|
2020-01-18 05:56:19 +01:00
|
|
|
vimrc_path = str(self._get_vim_root().joinpath(".vimrc"))
|
2019-12-28 05:07:41 +01:00
|
|
|
with open(vimrc_path, "w") as vimrc_file:
|
2019-12-22 07:49:51 +01:00
|
|
|
for line in vimrc_content:
|
|
|
|
vimrc_file.write(line)
|
2019-12-17 23:36:42 +01:00
|
|
|
|
2020-01-18 05:56:19 +01:00
|
|
|
def _exec_plugin_manager(self):
|
2019-12-30 02:25:05 +01:00
|
|
|
install_plugins = "vim +PlugInstall +qall +silent"
|
|
|
|
subprocess.call(install_plugins, shell=True)
|
|
|
|
|
2020-01-18 05:56:19 +01:00
|
|
|
def run(self, debug=False):
|
|
|
|
if debug:
|
|
|
|
self._debugFlag = True
|
|
|
|
self._create_folder_structure()
|
|
|
|
plugin_section = self._create_plugin_section()
|
|
|
|
settings_section = self._create_setting_section()
|
|
|
|
self._write_vimfile(settings_section + plugin_section)
|
|
|
|
self._exec_plugin_manager()
|