#!/usr/bin/env ruby # Script to automatically install Vim bundles to ~/.vim/bundle/ # # See vim/bundles for a sample file require 'getoptlong' # Shut up about warnings BUNDLE_ROOT = '/.vim/bundle/' def vim_bundle(bundle, tag=nil) if $transport == 'ssh' url = 'git@github.com:' elsif $transport == 'https' url = 'https://github.com/' elsif $transport == 'git' url = 'git://github.com/' else $stderr.puts "#{$an}: Unrecognized transport layer '#{transport}' (try --help)" exit 1 end if bundle =~ /^[A-Za-z0-9-]+\/[A-Za-z0-9._-]+$/ url += bundle + '.git' elsif bundle =~ /^[A-Za-z0-9._-]+$/ url += 'vim-scripts/' + bundle + '.git' else # We are using a service other than GitHub. Specifying the URL directly url = bundle end bundle_name = bundle.sub(/^.*\//, '').sub(/\.git$/, '') if $dry_run puts "URL #{url} Bundle #{bundle_name} Tag #{tag.to_s}" return else dir = ENV['HOME'] + BUNDLE_ROOT + bundle_name pwd = Dir.pwd $VERBOSE = nil if File.directory?(dir) if $update # Don't update if the no-update option was passed if File.directory?(dir + '/.git') puts "Updating bundle #{bundle_name}..." Dir.chdir(dir) `git fetch` Dir.chdir(pwd) puts else $stderr.puts "#{$an}: #{bundle_name} is not a Git repository." return end else $stderr.puts "#{$an}: Skipping update for #{bundle_name}" return end else # Directory doesn't exist, need to pull puts "Installing bundle #{bundle_name}..." Dir.chdir(ENV['HOME'] + BUNDLE_ROOT) `git clone #{url} #{bundle_name}` Dir.chdir(pwd) puts end tag ||= 'origin/HEAD' Dir.chdir(dir) `git stash` `git checkout #{tag}` `git stash pop` Dir.chdir(pwd) $VERBOSE = false end end opts = GetoptLong.new( [ '--help', '-h', GetoptLong::NO_ARGUMENT ], [ '--transport', '-t', GetoptLong::REQUIRED_ARGUMENT ], [ '--no-update', GetoptLong::NO_ARGUMENT ], [ '--dry-run', '-n', GetoptLong::NO_ARGUMENT ] ) $an = $0.sub(/^.*\//, '') file = nil $transport = 'ssh' $update = true $dry_run = false opts.each do |opt, arg| case opt when '--help' puts <<-EOF #{$an} manages vim bundles in ~/.vim/bundles Usage: #{$an} [OPTIONS] FILE Options: --transport, Use the specified transport layer to download git bundles. -t