Class: RubyOnEtags::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_on_etags/core.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.install_tasksObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby_on_etags/core.rb', line 27

def self.install_tasks

  namespace :ruby_on_etags do

    desc "Build TAGS for whole project. GEMS='some_gem-1.0.0, another_gem-0.2.1'."
    task :build do
      self.new.build_tags
    end

    desc "Watch for changes in project directory and build TAGS when needed."
    task :watch do
      system "watchr -e 'watch(\"\.rb\") { system \"rake ruby_on_etags:build &>/dev/null\" }'"
    end

  end

end

Instance Method Details

#build_tagsObject



45
46
47
48
49
50
51
52
# File 'lib/ruby_on_etags/core.rb', line 45

def build_tags
  remove_tags
  tags_files = []
  tags_files << build_tags_for_standard_library
  tags_files += build_tags_for_gems
  tags_files << build_tags_current_dir
  concatenate_tags_files(tags_files)
end

#build_tags_current_dirObject



72
73
74
75
76
77
# File 'lib/ruby_on_etags/core.rb', line 72

def build_tags_current_dir
  tags_file = Tempfile.new('ruby_on_etags-project-TAGS', '.')
  tags_file.close
  etags(["."], tags_file.path)
  tags_file.path
end

#build_tags_for_gemsObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby_on_etags/core.rb', line 54

def build_tags_for_gems
  gems_in_use.map do |gem_spec|
    tags_filename = File.join(cache_dir,
                              'gems',
                              "#{gem_spec.name}-#{gem_spec.version.version}",
                              'TAGS')
    etags(gem_spec.load_paths, tags_filename, :ctags_flags => "--tag-relative=no")
    tags_filename
  end
end

#build_tags_for_standard_libraryObject



65
66
67
68
69
70
# File 'lib/ruby_on_etags/core.rb', line 65

def build_tags_for_standard_library
  File.join(cache_dir, 'rubies', RUBY_VERSION, 'TAGS').tap do |tags_filename|
    dirs = $:.delete_if { |path| path =~ %r|/gems/| }
    etags(dirs, tags_filename, :ctags_flags => "--tag-relative=no")
  end
end

#concatenate_tags_files(tags_files) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/ruby_on_etags/core.rb', line 79

def concatenate_tags_files(tags_files)
  File.open("TAGS", 'w') do |tags|
    tags_files.each do |tags_filename|
      tags << IO.read(tags_filename)
    end
  end
end

#gems_in_useObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/ruby_on_etags/core.rb', line 91

def gems_in_use
  if File.exists?("Gemfile")
    # Bundler.load.specs
    Bundler::Definition.build(Bundler.default_gemfile,
                              Bundler.default_lockfile,
                              nil).specs
  else
    []
  end
end

#remove_tagsObject



87
88
89
# File 'lib/ruby_on_etags/core.rb', line 87

def remove_tags
  FileUtils.rm_f("TAGS")
end