vimenv (2471B)
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 | #!/usr/bin/env ruby # # name : vimenv, configure vim environment # author : Xu Xiaodong <[email protected]> # license : GPL # created : 2012 Apr 14 # modified : 2012 Jul 08 # require 'fileutils' require 'optparse' def init conf = %w[vimrc vim] conf.each do |item| orig = "#{Dir.pwd}/_#{item}" target = "#{ENV['HOME']}/.#{item}" # oops... 1.8.5 does not support Dir.home bak = "#{target}.old" if File.exist?(target) && ! File.symlink?(target) puts "Backuping #{target} to #{bak}" FileUtils.mv(target, bak) end FileUtils.rm(target) if File.symlink?(target) puts "Symlinking #{orig} to #{target}" FileUtils.symlink(orig, target) end end def edit puts "Editing Vim configuration file" system "vim _vimrc" end def add(author_slash_name) name = author_slash_name.gsub(/^.*\/(?:vim-)?(.*?)(?:[-\.]vim)?(?:-theme)?$/, '\1').downcase src = "git://github.com/#{author_slash_name}.git" desc = "_vim/bundle/#{name}" puts "Adding #{src} to #{desc}" system "git submodule add #{src} #{desc}" end def remove(name) path = "_vim/bundle/#{name.downcase}" git_path = ".git/modules/#{path}" files = [".git/config", ".gitmodules"] puts "Removing #{path}" system "git rm --cached #{path} >/dev/null" files.each { |file| system "git config -f #{file} --remove-section submodule.#{path}" } FileUtils.rmtree(path) FileUtils.rmtree(git_path) end def update puts "Updating Vim plugins" arg = ["init", "update", "foreach git pull origin master"] arg.each { |elt| system "git submodule #{elt}" } end def list bundle = Dir.entries('_vim/bundle') plugins = bundle[2, bundle.size].sort plugins.each { |name| puts " * Installed #{name} ..." } total = plugins.size puts ">>> Total #{total} plugins" end opts = OptionParser.new opts.on("-i", "--init" , "Initialize Vim environment") { init } opts.on("-e", "--edit" , "Edit Vim configuration file") { edit } opts.on("-a", "--add NAME" , "Add Vim plugins") { |name| add(name) } opts.on("-r", "--remove NAME", "Remove Vim plugins") { |name| remove(name) } opts.on("-u", "--update" , "Update Vim plugins") { update } opts.on("-l", "--list" , "List Vim plugins") { list } opts.on("-h", "--help" , "Show help") { puts opts.to_s } if ARGV.empty? puts opts.to_s else opts.parse(*ARGV) end # vim: ai:et:ts=2:sw=2:sts=2:tw=78:ft=ruby |