Skip to content

Instantly share code, notes, and snippets.

@andyl
Created February 18, 2011 19:08
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save andyl/834215 to your computer and use it in GitHub Desktop.
Save andyl/834215 to your computer and use it in GitHub Desktop.
Generates Ctags and Vim Paths from Gemfile.lock
#!/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.
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
@andyl
Copy link
Author

andyl commented Aug 25, 2011

Thanks for that explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment