Class: YARD::RubygemsHook

Inherits:
Object
  • Object
show all
Extended by:
Gem::UserInteraction
Includes:
Gem::UserInteraction
Defined in:
lib/yard/rubygems/hook.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec, generate_yard = false, generate_yri = true) ⇒ RubygemsHook

Returns a new instance of RubygemsHook.



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/yard/rubygems/hook.rb', line 93

def initialize(spec, generate_yard = false, generate_yri = true)
  @doc_dir = spec.doc_dir
  @force = false
  @spec = spec

  @generate_yard = generate_yard
  @generate_yri = generate_yri

  @yard_dir = spec.doc_dir('yard')
  @yri_dir = spec.doc_dir('.yardoc')
end

Class Attribute Details

.yard_versionObject (readonly)

Loaded version of YARD. Set by ::load_yard



38
39
40
# File 'lib/yard/rubygems/hook.rb', line 38

def yard_version
  @yard_version
end

Instance Attribute Details

#forceObject

Force installation of documentation?



22
23
24
# File 'lib/yard/rubygems/hook.rb', line 22

def force
  @force
end

#generate_yardObject

Generate yard?



27
28
29
# File 'lib/yard/rubygems/hook.rb', line 27

def generate_yard
  @generate_yard
end

#generate_yriObject

Generate yri data?



32
33
34
# File 'lib/yard/rubygems/hook.rb', line 32

def generate_yri
  @generate_yri
end

Class Method Details

.generation_hook(installer, specs) ⇒ Object

Post installs hook that generates documentation for each specification in specs



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/yard/rubygems/hook.rb', line 45

def self.generation_hook(installer, specs)
  start = Time.now
  types = installer.document

  generate_yard = types.include?('yardoc') || types.include?('yard')
  generate_yri = types.include? 'yri'

  specs.each do |spec|
    gen_yard = generate_yard
    gen_yri = generate_yri
    gen_yri = false if gen_yard # never generate both, no need
    unless types.empty? # --no-document is not in effect
      # look at spec.metadata['yard.run'] for override
      run_yard = spec.['yard.run']
      gen_yard = true if run_yard && run_yard != 'yri'
      gen_yri = true if run_yard == 'yri'
    end

    new(spec, gen_yard, gen_yri).generate
  end

  return unless generate_yard || generate_yri

  duration = (Time.now - start).to_i
  names = specs.map(&:name).join ', '

  say "Done installing documentation for #{names} after #{duration} seconds"
end

.load_yardObject

Loads the YARD generator



85
86
87
88
89
90
91
# File 'lib/yard/rubygems/hook.rb', line 85

def self.load_yard
  return if @yard_version

  require 'yard'

  @yard_version = Gem::Version.new ::YARD::VERSION
end

.removal_hook(uninstaller) ⇒ Object

Pre uninstalls hook that removes documentation



78
79
80
# File 'lib/yard/rubygems/hook.rb', line 78

def self.removal_hook(uninstaller)
  new(uninstaller.spec).remove
end

Instance Method Details

#generateObject

Generates YARD and yri data



148
149
150
151
152
153
154
155
156
157
# File 'lib/yard/rubygems/hook.rb', line 148

def generate
  return if @spec.default_gem?
  return unless @generate_yri || @generate_yard

  setup

  install_yri if @generate_yri && (@force || !File.exist?(@yri_dir))

  install_yard if @generate_yard && (@force || !File.exist?(@yard_dir))
end

#install_yardObject



131
132
133
134
135
136
# File 'lib/yard/rubygems/hook.rb', line 131

def install_yard
  FileUtils.rm_rf @yard_dir

  say "Installing YARD documentation for #{@spec.full_name}..."
  run_yardoc '--no-progress', '--db', @yri_dir, '-o', @yard_dir
end

#install_yriObject



138
139
140
141
142
143
# File 'lib/yard/rubygems/hook.rb', line 138

def install_yri
  FileUtils.rm_rf @yri_dir

  say "Building YARD (yri) index for #{@spec.full_name}..."
  run_yardoc '--no-progress', '-c', '-n', '--db', @yri_dir
end

#removeObject

Removes YARD and yri data



189
190
191
192
# File 'lib/yard/rubygems/hook.rb', line 189

def remove
  uninstall_yri
  uninstall_yard
end

#run_yardoc(*args) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/yard/rubygems/hook.rb', line 105

def run_yardoc(*args)
  args << '--quiet' unless Gem.configuration.really_verbose
  args << '--backtrace' if Gem.configuration.backtrace
  unless File.file?(File.join(@spec.full_gem_path, '.yardopts'))
    args << @spec.require_paths
    unless @spec.extra_rdoc_files.empty?
      args << '-'
      args += @spec.extra_rdoc_files
    end
  end
  args = args.flatten.map(&:to_s)

  Dir.chdir(@spec.full_gem_path) do
    YARD::CLI::Yardoc.run(*args)
  end
rescue Errno::EACCES => e
  dirname = File.dirname e.message.split("-")[1].strip
  raise Gem::FilePermissionError, dirname
rescue => ex
  alert_error "While generating documentation for #{@spec.full_name}"
  ui.errs.puts "... MESSAGE:   #{ex}"
  ui.errs.puts "... YARDOC args: #{args.join(' ')}"
  ui.errs.puts "\t#{ex.backtrace.join("\n\t")}" if Gem.configuration.backtrace
  ui.errs.puts "(continuing with the rest of the installation)"
end

#setupObject

Prepares the spec for documentation generation



162
163
164
165
166
167
168
169
170
# File 'lib/yard/rubygems/hook.rb', line 162

def setup
  self.class.load_yard

  if File.exist?(@doc_dir)
    raise Gem::FilePermissionError, @doc_dir unless File.writable?(@doc_dir)
  else
    FileUtils.mkdir_p @doc_dir
  end
end

#uninstall_yardObject



172
173
174
175
176
177
# File 'lib/yard/rubygems/hook.rb', line 172

def uninstall_yard
  if File.exist?(@yard_dir)
    raise Gem::FilePermissionError, @yard_dir unless File.writable?(@yard_dir)
    FileUtils.rm_rf @yard_dir
  end
end

#uninstall_yriObject



179
180
181
182
183
184
# File 'lib/yard/rubygems/hook.rb', line 179

def uninstall_yri
  if File.exist?(@yri_dir)
    raise Gem::FilePermissionError, @yri_dir unless File.writable?(@yri_dir)
    FileUtils.rm_rf @yri_dir
  end
end