|
#!/usr/bin/ruby |
|
require 'rubygems' |
|
require 'bundler' |
|
|
|
=begin |
|
This script was written to incorporate Bundler and Gemfile.lock into |
|
Vim's tag and file-finding tools. |
|
=end |
|
|
|
# This code generates ctags. If a Gemfile.lock is found |
|
# in the current directory, tags will be generated for |
|
# every gem referenced in the Gemfile. |
|
|
|
# Can now run the script from anywhere and pass in the project directory as a parameter |
|
Dir.chdir ARGV[0] |
|
|
|
bundler_paths = [] |
|
if File.exist?('Gemfile.lock') |
|
lockfile_contents = Bundler.read_file('Gemfile.lock') |
|
lockfile = Bundler::LockfileParser.new(lockfile_contents) |
|
bundler_paths = lockfile.specs.map do |spec| |
|
spec.__materialize__ |
|
spec.full_gem_path |
|
end |
|
end |
|
|
|
tags_file = ".tags" |
|
opts = "--extra=+f -R -f #{tags_file}" |
|
exclude_dirs = "--exclude=.git --exclude=log --exclude=.svn" |
|
ctag_paths = "* #{bundler_paths.map {|x| x + '/*'}.join(' ')}" |
|
command = "ctags #{opts} #{exclude_dirs} #{ctag_paths} 2> /dev/null" |
|
|
|
puts "Processing ctags..." |
|
system command |
|
puts "Wrote ctags to '#{tags_file}'" |
|
|
|
# This code generates a script that loads Bundler paths into vim. |
|
# To use this script in vim, put this command into your .vimrc: |
|
# |
|
# autocmd BufNewFile,BufRead *.rb silent! source .paths.vim<cr> |
|
|
|
system "rm -f .paths.vim" |
|
unless bundler_paths.empty? |
|
gem_paths = Gem.all_load_paths |
|
vim_paths = bundler_paths.map {|x| x = x + '/lib'} - gem_paths |
|
paths_file = ".paths.vim" |
|
File.open(paths_file, 'w') do |file| |
|
vim_paths.each {|x| file.puts "silent! set path+=#{x}" } |
|
end |
|
puts "Wrote #{vim_paths.length} vim paths to '#{paths_file}'" |
|
end |