Skip to content

Instantly share code, notes, and snippets.

@jgandt
Forked from andyl/ctags_vim_paths_gemfile.rb
Created January 31, 2012 15:25
Show Gist options
  • Save jgandt/1711060 to your computer and use it in GitHub Desktop.
Save jgandt/1711060 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment