diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..60a04bd --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include ohmyvim/config.ini diff --git a/buildout.cfg b/buildout.cfg index 4053775..cf071d2 100644 --- a/buildout.cfg +++ b/buildout.cfg @@ -8,4 +8,7 @@ develop = . recipe = z3c.recipe.scripts eggs = oh-my-vim + unittest2 + coverage + nose interpreter = python diff --git a/ohmyvim/config.ini b/ohmyvim/config.ini new file mode 100644 index 0000000..c723348 --- /dev/null +++ b/ohmyvim/config.ini @@ -0,0 +1,13 @@ +[bundles] +bufexplorer=https://github.com/vim-scripts/bufexplorer.zip.git +django=https://github.com/vim-scripts/django.vim.git +gundo=https://github.com/sjl/gundo.vim.git +nerdtree=https://github.com/scrooloose/nerdtree.git +python=https://github.com/vim-scripts/python.vim.git +snipmate=https://github.com/msanders/snipmate.vim.git +syntastic=https://github.com/scrooloose/syntastic.git +vim-bundle-mako=https://github.com/sophacles/vim-bundle-mako.git +vim-trailing-whitespace=https://github.com/bronson/vim-trailing-whitespace.git + +[themes] +github-theme=https://github.com/vim-scripts/github-theme.git diff --git a/ohmyvim/scripts.py b/ohmyvim/scripts.py index 3bf6ded..df84620 100644 --- a/ohmyvim/scripts.py +++ b/ohmyvim/scripts.py @@ -3,6 +3,7 @@ from os.path import isfile from os.path import basename from os.path import expanduser +from ConfigObject import ConfigObject from urllib import urlopen from subprocess import Popen from subprocess import PIPE @@ -26,42 +27,55 @@ class Manager(object): - runtime = expanduser('~/.vim/bundle') - autoload = expanduser('~/.vim/autoload') - ohmyvim = expanduser('~/.vim/ohmyvim') - dependencies = { 'vim-pathogen': 'https://github.com/tpope/vim-pathogen.git', 'oh-my-vim': 'https://github.com/gawel/oh-my-vim.git', } def __init__(self): + self.output = [] + + self.runtime = expanduser('~/.vim/bundle') + self.autoload = expanduser('~/.vim/autoload') + self.ohmyvim = expanduser('~/.vim/ohmyvim') + for dirname in (self.runtime, self.autoload, self.ohmyvim, expanduser('~/.vim/swp')): if not isdir(dirname): os.makedirs(dirname) + for name, url in self.dependencies.items(): if not isdir(join(self.runtime, name)): Popen(['git', 'clone', '-q', url, join(self.runtime, name)]).wait() + if not isfile(join(self.ohmyvim, 'theme.vim')): with open(join(self.ohmyvim, 'theme.vim'), 'w') as fd: fd.write('') + if not isfile(join(self.ohmyvim, 'ohmyvim.vim')): with open(join(self.ohmyvim, 'ohmyvim.vim'), 'w') as fd: fd.write('source %s\n' % join(self.runtime, 'vim-pathogen', 'autoload', 'pathogen.vim')) fd.write('call pathogen#runtime_append_all_bundles()\n') fd.write('source %s\n' % join(self.ohmyvim, 'theme.vim')) - binary = os.path.abspath(sys.argv[0]) + + kw = dict(ohmyvim=join(self.ohmyvim, 'ohmyvim.vim'), + binary=os.path.abspath(sys.argv[0])) if not isfile(expanduser('~/.vimrc')): with open(expanduser('~/.vimrc'), 'w') as fd: - fd.write(VIMRC % locals()) + fd.write(VIMRC % kw) else: with open(expanduser('~/.vimrc')) as fd: - if binary not in fd.read(): + if kw['binary'] not in fd.read(): with open(expanduser('~/.vimrc'), 'a') as fd: - fd.write(VIMRC % locals()) + fd.write(VIMRC % kw) + + def log(self, value, *args): + if args: + value = value % args + self.output.append(value) + print(value) def get_plugins(self): plugins = [] @@ -82,14 +96,18 @@ def search(self, args): if not terms: terms = ['language%3AVimL'] terms = '%20'.join(terms) - webbrowser.open_new(("https://github.com/search?" - "langOverride=&repo=&start_value=1&" - "type=Repositories&language=VimL&q=") + terms) + url = ("https://github.com/search?" + "langOverride=&repo=&start_value=1&" + "type=Repositories&language=VimL&q=") + terms + if '__test__' not in os.environ: + webbrowser.open_new(url) + else: + self.log(url) def list(self, args): for plugin, dirname, themes in self.get_plugins(): - if args.raw: - print plugin + if args.complete: + self.log(plugin) else: os.chdir(dirname) p = Popen(['git', 'remote', '-v'], stdout=PIPE) @@ -98,9 +116,9 @@ def list(self, args): remote = remote.split('\t')[1].split(' ')[0] if args.urls: if plugin not in self.dependencies: - print remote + self.log(remote) else: - print '* %s (%s)' % (plugin, remote) + self.log('* %s (%s)', plugin, remote) def install_url(self, url): url = url.strip() @@ -112,94 +130,90 @@ def install_url(self, url): name = basename(url)[:-4] dirname = join(self.runtime, name) if os.path.isdir(dirname): - print '%s already installed. Upgrading...' % name + self.log('%s already installed. Upgrading...', name) os.chdir(dirname) Popen(['git', 'pull', '-n']).wait() else: - print 'Installing bundle %s...' % name + self.log('Installing bundle %s...', name) Popen(['git', 'clone', '-q', url, dirname]).wait() if isfile(join(dirname, 'requires.txt')): with open(join(dirname, 'requires.txt')) as fd: dependencies = [d for d in fd.readlines()] else: - print '%s is not a git url' % url + self.log('%s is not a git url', url) return dirname, dependencies def install(self, args): - dependencies = set() - for url in args.url: - if url.endswith('requires.txt'): - if isfile(url): - with open(url) as fd: + filename = join(os.path.dirname(__file__), 'config.ini') + config = ConfigObject(filename=filename) + if args.complete: + for name in sorted(config.bundles.keys()): + self.log(name) + for name in sorted(config.themes.keys()): + self.log(name) + else: + dependencies = set() + for url in args.url: + url = config.bundles.get(url, url) + url = config.themes.get(url, url) + if url.endswith('.txt'): + if isfile(url): + with open(url) as fd: + dependencies = [d for d in fd.readlines()] + elif url.startswith('http'): + fd = urlopen(url) dependencies = [d for d in fd.readlines()] - elif url.startswith('http'): - fd = urlopen(url) - dependencies = [d for d in fd.readlines()] - else: - _, deps = self.install_url(url) - for d in deps: - if d.strip(): - dependencies.add(d) - if dependencies: - print 'Processing dependencies...' - for url in dependencies: - self.install_url(url) + else: + _, deps = self.install_url(url) + for d in deps: + if d.strip(): + dependencies.add(d) + if dependencies: + self.log('Processing dependencies...') + for url in dependencies: + self.install_url(url) def upgrade(self, args): - if not args.bundle: - print 'all' for plugin, dirname, themes in self.get_plugins(): if plugin in args.bundle or 'all' in args.bundle: - print 'Upgrading %s...' % plugin + self.log('Upgrading %s...', plugin) os.chdir(dirname) Popen(['git', 'pull', '-n']).wait() - elif args.raw: - print plugin def remove(self, args): - for plugin, dirname, themes in self.get_plugins(): - if not args.bundle: - print plugin - elif plugin in args.bundle: - if plugin in self.dependencies: - print "Don't remove %s!" % plugin - print 'Removing %s...' % plugin - dirname = join(self.runtime, plugin) - if isdir(join(dirname, '.git')): - shutil.rmtree(dirname) + if args.bundle: + for plugin, dirname, themes in self.get_plugins(): + if plugin in args.bundle: + if plugin in self.dependencies: + self.log("Don't remove %s!", plugin) + self.log('Removing %s...', plugin) + dirname = join(self.runtime, plugin) + if isdir(join(dirname, '.git')): + shutil.rmtree(dirname) def theme(self, args): theme = args.theme if theme: - if theme.startswith('http'): - theme_dir, _ = self.install_url(theme) - for plugin, dirname, themes in self.get_plugins(): - if theme_dir == dirname and len(themes) == 1: - theme = themes[0] - print 'Activate %s theme...' % theme - with open(join(self.ohmyvim, 'theme.vim'), 'w') as fd: - fd.write(':colo %s\n' % theme) - else: - for plugin, dirname, themes in self.get_plugins(): - if theme in themes: - print 'Activate %s theme...' % theme - with open(join(self.ohmyvim, 'theme.vim'), 'w') as fd: - fd.write(':colo %s\n' % theme) - return - for plugin, dirname, themes in self.get_plugins(): - if isdir(join(dirname, '.git')): - os.chdir(dirname) - p = Popen(['git', 'remote', '-v'], stdout=PIPE) - p.wait() - remote = p.stdout.read().split('\n')[0] - remote = remote.split('\t')[1].split(' ')[0] - if themes: - if args.raw: - for theme in themes: - print theme - else: - print '* %s (%s)' % (plugin, remote) - print '\t- %s' % ', '.join(themes) + for plugin, dirname, themes in self.get_plugins(): + if theme in themes: + self.log('Activate %s theme...', theme) + with open(join(self.ohmyvim, 'theme.vim'), 'w') as fd: + fd.write(':colo %s\n' % theme) + else: + for plugin, dirname, themes in self.get_plugins(): + if isdir(join(dirname, '.git')): + os.chdir(dirname) + p = Popen(['git', 'remote', '-v'], stdout=PIPE) + p.wait() + remote = p.stdout.read().split('\n')[0] + remote = remote.split('\t')[1].split(' ')[0] + if themes: + if args.complete: + for theme in themes: + self.log(theme) + else: + self.log('* %s (%s)', plugin, remote) + self.log('\t- %s', ', '.join(themes)) def profiles(self, args): profiles = join(self.runtime, 'oh-my-vim', 'profiles') @@ -214,9 +228,9 @@ def profiles(self, args): if line.startswith('"'): desc += line.strip(' "\n') if desc: - print '* %s - %s' % (name, desc) + self.log('* %s - %s', name, desc) else: - print '* %s' % name + self.log('* %s', name) def main(*args): @@ -233,16 +247,16 @@ def main(*args): p.set_defaults(action=manager.search) p = subparsers.add_parser('list') - p.add_argument('--raw', action='store_true', default=False) + p.add_argument('--complete', action='store_true', default=False) p.add_argument('-u', '--urls', action='store_true', default=False) p.set_defaults(action=manager.list) p = subparsers.add_parser('install', help='install a script or bundle') + p.add_argument('--complete', action='store_true', default=False) p.add_argument('url', nargs='*', default='') p.set_defaults(action=manager.install) p = subparsers.add_parser('upgrade', help='upgrade bundles') - p.add_argument('--raw', action='store_true', default=False) p.add_argument('bundle', nargs='*', default='') p.set_defaults(action=manager.upgrade) @@ -251,7 +265,7 @@ def main(*args): p.set_defaults(action=manager.remove) p = subparsers.add_parser('theme', help='list or activate a theme') - p.add_argument('--raw', action='store_true', default=False) + p.add_argument('--complete', action='store_true', default=False) p.add_argument('theme', nargs='?', default='') p.set_defaults(action=manager.theme) @@ -262,4 +276,7 @@ def main(*args): args = parser.parse_args(args) else: args = parser.parse_args() + args.action(args) + + return manager.output diff --git a/ohmyvim/tests.py b/ohmyvim/tests.py new file mode 100644 index 0000000..715956b --- /dev/null +++ b/ohmyvim/tests.py @@ -0,0 +1,103 @@ +# -*- coding: utf-8 -*- +import unittest2 as unittest +from ohmyvim.scripts import main +from os.path import isdir +from os.path import isfile +from os.path import join +import shutil +import tempfile +import os + + +class TestOhMyVim(unittest.TestCase): + + def setUp(self): + self.wd = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, self.wd) + os.environ['HOME'] = self.wd + os.environ['__test__'] = '1' + self.requires = join(self.wd, 'deps.txt') + with open(self.requires, 'w') as fd: + fd.write('https://github.com/vim-scripts/github-theme.git\n\n') + + def assertIsFile(self, *args): + filename = join(self.wd, *args) + if not isfile(filename): + print(os.listdir(os.path.dirname(filename))) + self.assertTrue(isfile(filename), filename) + + def assertIsDir(self, *args): + dirname = join(self.wd, *args) + if not isdir(dirname): + print(os.listdir(os.path.dirname(dirname))) + self.assertTrue(isfile(dirname), dirname) + + def assertResp(self, resp): + self.assertTrue(len(resp) > 0, resp) + + def main(self, args): + return main(*args.split(' ')) + + def test_ohmyvim(self): + self.main('install') + self.assertIn(self.wd, os.path.expanduser('~/')) + self.assertIsFile('.vim/ohmyvim/ohmyvim.vim') + self.assertIsDir('.vim/bundle/oh-my-vim') + + url = self.main('search')[0] + self.assertIn('language%3AVimL', url) + + url = self.main('search -t')[0] + self.assertIn('colorschemes', url) + + url = self.main('search -t mytheme')[0] + self.assertIn('colorschemes', url) + self.assertIn('mytheme', url) + + resp = self.main('profiles') + self.assertIn('* defaults - some defaults settings', resp) + + resp = self.main('install --complete') + self.assertIn('github-theme', resp) + + self.main('install') + resp = self.main('install github-theme') + self.assertResp(resp) + + resp = self.main( + 'install https://github.com/vim-scripts/github-theme.git') + self.assertIn('github-theme already installed. Upgrading...', resp) + + resp = self.main('install %s' % self.requires) + self.assertIn('github-theme already installed. Upgrading...', resp) + + resp = self.main('list --complete') + self.assertIn('github-theme', resp) + + resp = self.main('list') + self.assertIn( + '* github-theme (https://github.com/vim-scripts/github-theme.git)', + resp) + + resp = self.main('list -u') + self.assertIn('https://github.com/vim-scripts/github-theme.git', resp) + + resp = self.main('theme --complete') + self.assertIn('github', resp) + + resp = self.main('theme') + self.assertIn('\t- github', resp) + + resp = self.main('theme github') + self.assertIn('Activate github theme...', resp) + + resp = self.main('remove') + self.assertTrue(len(resp) == 0) + + resp = self.main('remove github-theme') + self.assertNotIn('github-theme', resp) + + resp = self.main('upgrade oh-my-vim') + self.assertIn('Upgrading oh-my-vim...', resp) + + diff --git a/plugin/ohmyvim.vim b/plugin/ohmyvim.vim index 01c041c..8345de8 100644 --- a/plugin/ohmyvim.vim +++ b/plugin/ohmyvim.vim @@ -15,7 +15,6 @@ function! OhMyVim(args) endfunction function! OhMyVimCmpl(A,L,P) - " a lot of improvment can be done here... let a:splitted = split(a:L, ' ') let a:cmds=sort(split('search,upgrade,list,remove,theme,profiles,install', ',')) if len(a:splitted) == 1 @@ -32,11 +31,13 @@ function! OhMyVimCmpl(A,L,P) endif if len(a:splitted) >= 3 if a:splitted[1] == 'theme' - return system(g:ohmyvim.' theme --raw') + return system(g:ohmyvim.' theme --complete') + elseif a:splitted[1] == 'install' + return system(g:ohmyvim.' install --complete') elseif a:splitted[1] == 'upgrade' - return system('ls ~/.vim/bundle/') + return system(g:ohmyvim.' list --complete') elseif a:splitted[1] == 'remove' - return system('ls ~/.vim/bundle/') + return system(g:ohmyvim.' list --complete') endif return "\n" endif diff --git a/profiles/defaults.vim b/profiles/defaults.vim index 43e7913..afc621e 100644 --- a/profiles/defaults.vim +++ b/profiles/defaults.vim @@ -1,18 +1,23 @@ " some defaults settings syntax on -set ls=2 -set noai -set mouse=a +filetype plugin on +set noerrorbells +set visualbell t_vb= set hlsearch set autoread set incsearch set expandtab + +set ls=2 +set noai +set mouse=a set tabstop=4 -set noerrorbells set shiftwidth=4 -filetype plugin on -set visualbell t_vb= set ignorecase smartcase + +set wildmenu +set wildmode=list:full + set statusline=%F%m%r%h%w\ format=%{&ff}\ type=%Y\ x=%l\ y=%v\ %p%%\ %{strftime(\"%d/%m/%y\ -\%H:%M\")} set directory=~/.vim/swp/ diff --git a/setup.cfg b/setup.cfg index bc97c0b..c9f79a8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,8 @@ +[nosetests] +verbosity=2 +with-coverage=1 +cover-package=ohmyvim + [aliases] release = sdist register upload diff --git a/setup.py b/setup.py index 448c6fc..039ff00 100644 --- a/setup.py +++ b/setup.py @@ -35,6 +35,7 @@ def read(*args): install_requires=[ # -*- Extra requirements: -*- 'argparse', + 'ConfigObject', ], entry_points=""" # -*- Entry points: -*-