lib/yard/cli/diff.rb in yard-0.9.5 vs lib/yard/cli/diff.rb in yard-0.9.6

- old
+ new

@@ -1,5 +1,6 @@ +# frozen_string_literal: true require 'tmpdir' require 'fileutils' require 'open-uri' module YARD @@ -27,33 +28,31 @@ def run(*args) registry = optparse(*args).map do |gemfile| if @use_git load_git_commit(gemfile) all_objects + elsif load_gem_data(gemfile) + log.info "Found #{gemfile}" + all_objects else - if load_gem_data(gemfile) - log.info "Found #{gemfile}" - all_objects - else - log.error "Cannot find gem #{gemfile}" - nil - end + log.error "Cannot find gem #{gemfile}" + nil end end.compact return if registry.size != 2 first_object = nil - [ ["Added objects", "A", added_objects(*registry)], + [["Added objects", "A", added_objects(*registry)], ["Modified objects", "M", modified_objects(*registry)], ["Removed objects", "D", removed_objects(*registry)]].each do |name, short, objects| next if short == "M" && @modified == false next if objects.empty? last_object = nil all_objects_notice = false log.puts name + ":" unless @compact - objects.sort_by {|o| o.path }.each do |object| + objects.sort_by(&:path).each do |object| if !@list_all && last_object && object.parent == last_object log.print " (...)" unless all_objects_notice all_objects_notice = true next elsif @compact @@ -61,11 +60,11 @@ else log.puts end all_objects_notice = false log.print "" + (@compact ? "#{short} " : " ") + - object.path + " (#{object.file}:#{object.line})" + object.path + " (#{object.file}:#{object.line})" last_object = object first_object = true end unless @compact log.puts; log.puts @@ -121,34 +120,33 @@ Registry.clear # First check for argument as .yardoc file [File.join(gemfile, '.yardoc'), gemfile].each do |yardoc| log.info "Searching for .yardoc db at #{yardoc}" - if File.directory?(yardoc) - Registry.load_yardoc(yardoc) - Registry.load_all - return true - end + next unless File.directory?(yardoc) + Registry.load_yardoc(yardoc) + Registry.load_all + return true end # Next check installed RubyGems gemfile_without_ext = gemfile.sub(/\.gem$/, '') log.info "Searching for installed gem #{gemfile_without_ext}" - Gem.source_index.find_name('').find do |spec| - if spec.full_name == gemfile_without_ext - if yardoc = Registry.yardoc_file_for_gem(spec.name, "= #{spec.version}") - Registry.load_yardoc(yardoc) - Registry.load_all - else - log.enter_level(Logger::ERROR) do - olddir = Dir.pwd - Gems.run(spec.name, spec.version.to_s) - Dir.chdir(olddir) - end + YARD::GemIndex.find_all_by_name('').find do |spec| + next unless spec.full_name == gemfile_without_ext + yardoc = Registry.yardoc_file_for_gem(spec.name, "= #{spec.version}") + if yardoc + Registry.load_yardoc(yardoc) + Registry.load_all + else + log.enter_level(Logger::ERROR) do + olddir = Dir.pwd + Gems.run(spec.name, spec.version.to_s) + Dir.chdir(olddir) end - return true end + return true end # Look for local .gem file gemfile += '.gem' unless gemfile =~ /\.gem$/ log.info "Searching for local gem file #{gemfile}" @@ -164,10 +162,11 @@ log.info "Searching for remote gem file #{url}" begin open(url) {|io| expand_and_parse(gemfile, io) } return true rescue OpenURI::HTTPError + nil # noop end false end def expand_and_parse(gemfile, io) @@ -175,11 +174,10 @@ generate_yardoc(dir) cleanup(gemfile) end def generate_yardoc(dir) - olddir = Dir.pwd Dir.chdir(dir) do log.enter_level(Logger::ERROR) { Yardoc.run('-n', '--no-save') } end end @@ -190,24 +188,26 @@ if Gem::VERSION >= '2.0.0' require 'rubygems/package/tar_reader' reader = Gem::Package::TarReader.new(io) reader.each do |pkg| - if pkg.full_name == 'data.tar.gz' - Zlib::GzipReader.wrap(pkg) do |gzio| - tar = Gem::Package::TarReader.new(gzio) - tar.each do |entry| - mode = entry.header.mode - file = File.join(tmpdir, entry.full_name) - FileUtils.mkdir_p(File.dirname(file)) - File.open(file, 'wb') do |out| - out.write(entry.read) - out.fsync rescue nil + next unless pkg.full_name == 'data.tar.gz' + Zlib::GzipReader.wrap(pkg) do |gzio| + tar = Gem::Package::TarReader.new(gzio) + tar.each do |entry| + file = File.join(tmpdir, entry.full_name) + FileUtils.mkdir_p(File.dirname(file)) + File.open(file, 'wb') do |out| + out.write(entry.read) + begin + out.fsync + rescue NotImplementedError + nil # noop end end end - break end + break end else Gem::Package.open(io) do |pkg| pkg.each do |entry| pkg.extract_entry(tmpdir, entry)