2019-12-22 02:45:19 +01:00
|
|
|
from AbstractModule import AbstractModule
|
2019-12-16 12:08:22 +01:00
|
|
|
from subprocess import call
|
2019-12-22 03:59:38 +01:00
|
|
|
from PyInquirer import prompt
|
2019-12-16 12:08:22 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
2019-12-22 04:23:03 +01:00
|
|
|
import json
|
2019-12-14 04:48:57 +01:00
|
|
|
|
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__()
|
2019-12-22 04:23:03 +01:00
|
|
|
self.plugins = {}
|
|
|
|
with open("modules/vim/vim_plugins.json") as plugins:
|
|
|
|
self.plugins = json.load(plugins)['plugins']
|
2019-12-22 03:59:38 +01:00
|
|
|
|
|
|
|
def do_module_selection(self):
|
|
|
|
mapped_list = []
|
|
|
|
for choice in self.plugins.keys():
|
|
|
|
mapped_list.append({
|
|
|
|
"name": choice
|
|
|
|
})
|
|
|
|
|
|
|
|
print("Available Plugins:")
|
|
|
|
module_selection = [
|
|
|
|
{
|
|
|
|
'type': 'checkbox',
|
|
|
|
'message': 'Select plugins',
|
|
|
|
'name': 'modules',
|
|
|
|
'choices': mapped_list
|
|
|
|
}
|
2019-12-14 04:22:34 +01:00
|
|
|
]
|
2019-12-22 04:23:03 +01:00
|
|
|
return prompt(module_selection)['modules']
|
2019-12-14 04:22:34 +01:00
|
|
|
|
2019-12-22 03:59:38 +01:00
|
|
|
def run(self):
|
2019-12-14 04:22:34 +01:00
|
|
|
|
2019-12-22 04:23:03 +01:00
|
|
|
selected_modules = self.do_module_selection()
|
2019-12-15 07:30:20 +01:00
|
|
|
|
|
|
|
# print out selected plugins
|
2019-12-22 03:59:38 +01:00
|
|
|
[print(x) for x in selected_modules]
|
2019-12-15 07:30:20 +01:00
|
|
|
|
|
|
|
# Create vimrc file
|
|
|
|
vimrc_exists = os.path.isfile("testfile")
|
|
|
|
|
|
|
|
if vimrc_exists:
|
2019-12-16 12:11:30 +01:00
|
|
|
temp_file = open('temp', 'w')
|
|
|
|
with open("testfile", "r") as vimrc_out_file:
|
|
|
|
for line in vimrc_out_file:
|
|
|
|
if line.startswith("call plug#begin('~/.vim/plugged')"):
|
2019-12-22 03:59:38 +01:00
|
|
|
for element in selected_modules:
|
|
|
|
line = line.strip() + "\n" + self.plugins[element] + "\n"
|
2019-12-16 12:11:30 +01:00
|
|
|
temp_file.write(line)
|
|
|
|
temp_file.close()
|
|
|
|
shutil.move('temp', 'testfile')
|
2019-12-19 14:03:31 +01:00
|
|
|
|
|
|
|
# Merge duplicates to first ocassion with awk
|
2019-12-16 12:11:30 +01:00
|
|
|
sorted_file = "awk -i inplace ' !x[$0]++' ~/Gitlab/linux-tools/testfile"
|
|
|
|
call(sorted_file, shell=True)
|
2019-12-15 07:30:20 +01:00
|
|
|
|
|
|
|
else:
|
2019-12-16 12:10:08 +01:00
|
|
|
with open("testfile", "w+") as vimrc_file:
|
|
|
|
vimrc_file.write("\n" + "call plug#begin('~/.vim/plugged')\n")
|
2019-12-22 03:59:38 +01:00
|
|
|
for element in selected_modules:
|
|
|
|
vimrc_file.write(self.plugins[element] + "\n")
|
2019-12-17 23:36:42 +01:00
|
|
|
vimrc_file.write("call plug#end()\n")
|
|
|
|
|
|
|
|
vimdir_exists = os.path.isdir("/home/clay/Gitlab/linux-tools/testdirectory/")
|
2019-12-19 14:03:31 +01:00
|
|
|
autoloaddir_exists = os.path.isdir("/home/clay/Gitlab/linux-tools/testdirectory/autoload/")
|
2019-12-17 23:36:42 +01:00
|
|
|
|
2019-12-19 14:03:31 +01:00
|
|
|
# Create necessary folder structure for Plugin Manager
|
2019-12-22 04:23:03 +01:00
|
|
|
if not autoloaddir_exists:
|
2019-12-19 14:03:31 +01:00
|
|
|
if vimdir_exists:
|
2019-12-22 04:23:03 +01:00
|
|
|
install_vimplug = "cd ~/Gitlab/linux-tools/testdirectory/ " \
|
|
|
|
"&& mkdir autoload " \
|
|
|
|
"&& cd autoload " \
|
|
|
|
"&& touch test.txt"
|
2019-12-19 14:03:31 +01:00
|
|
|
call(install_vimplug, shell=True)
|
2019-12-17 23:36:42 +01:00
|
|
|
|
2019-12-19 14:03:31 +01:00
|
|
|
else:
|
2019-12-22 04:23:03 +01:00
|
|
|
make_vimdir = "cd ~/Gitlab/linux-tools/ " \
|
|
|
|
"&& mkdir testdirectory " \
|
|
|
|
"&& cd testdirectory " \
|
|
|
|
"&& mkdir autoload " \
|
|
|
|
"&& cd autoload " \
|
|
|
|
"&& touch test.txt"
|
2019-12-22 02:45:19 +01:00
|
|
|
call(make_vimdir, shell=True)
|
2019-12-17 23:36:42 +01:00
|
|
|
else:
|
2019-12-22 02:45:19 +01:00
|
|
|
print("Vim-Plug already installed!")
|