lib/yard/cli/diff.rb in yard-0.8.2.1 vs lib/yard/cli/diff.rb in yard-0.8.3
- old
+ new
@@ -10,10 +10,13 @@
class Diff < Command
def initialize
super
@list_all = false
@use_git = false
+ @compact = false
+ @modified = true
+ @verifier = Verifier.new
@old_git_commit = nil
@old_path = Dir.pwd
log.show_backtraces = true
end
@@ -23,59 +26,93 @@
def run(*args)
registry = optparse(*args).map do |gemfile|
if @use_git
load_git_commit(gemfile)
- Registry.all.map {|o| o.path }
+ all_objects
else
if load_gem_data(gemfile)
log.info "Found #{gemfile}"
- Registry.all.map {|o| o.path }
+ all_objects
else
log.error "Cannot find gem #{gemfile}"
nil
end
end
end.compact
return if registry.size != 2
- [ ["Added objects", registry[1] - registry[0]],
- ["Removed objects", registry[0] - registry[1]]].each do |name, objects|
+ first_object = nil
+ [ ["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
- puts name + ":"
- objects.sort.each do |object|
- if !@list_all && last_object && object =~ /#{Regexp.quote last_object}(::|\.|#)/
- print " (...)" unless all_objects_notice
+ log.puts name + ":" unless @compact
+ objects.sort_by {|o| o.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
+ log.puts if first_object
else
- puts
+ log.puts
end
all_objects_notice = false
- print " " + object
+ log.print "" + (@compact ? "#{short} " : " ") +
+ object.path + " (#{object.file}:#{object.line})"
last_object = object
+ first_object = true
end
- puts
- puts
+ unless @compact
+ log.puts; log.puts
+ end
end
+ log.puts if @compact
end
private
+ def all_objects
+ return Registry.all if @verifier.expressions.empty?
+ @verifier.run(Registry.all)
+ end
+
+ def added_objects(registry1, registry2)
+ registry2.reject {|o| registry1.find {|o2| o2.path == o.path } }
+ end
+
+ def modified_objects(registry1, registry2)
+ registry1.select do |obj|
+ case obj
+ when CodeObjects::MethodObject
+ registry2.find {|o| obj == o && o.source != obj.source }
+ when CodeObjects::ConstantObject
+ registry2.find {|o| obj == o && o.value != obj.value }
+ end
+ end.compact
+ end
+
+ def removed_objects(registry1, registry2)
+ registry1.reject {|o| registry2.find {|o2| o2.path == o.path } }
+ end
+
def load_git_commit(commit)
commit_path = 'git_commit' + commit.gsub(/\W/, '_')
tmpdir = File.join(Dir.tmpdir, commit_path)
log.info "Expanding #{commit} to #{tmpdir}..."
Dir.chdir(@old_path)
FileUtils.mkdir_p(tmpdir)
FileUtils.cp_r('.', tmpdir)
Dir.chdir(tmpdir)
log.info("git says: " + `git reset --hard #{commit}`.chomp)
generate_yardoc(tmpdir)
+ ensure
Dir.chdir(@old_path)
cleanup(commit_path)
end
def load_gem_data(gemfile)
@@ -138,13 +175,13 @@
cleanup(gemfile)
end
def generate_yardoc(dir)
olddir = Dir.pwd
- Dir.chdir(dir)
- log.enter_level(Logger::ERROR) { Yardoc.run('-n', '--no-save') }
- Dir.chdir(olddir)
+ Dir.chdir(dir) do
+ log.enter_level(Logger::ERROR) { Yardoc.run('-n', '--no-save') }
+ end
end
def expand_gem(gemfile, io)
tmpdir = File.join(Dir.tmpdir, gemfile)
log.info "Expanding #{gemfile} to #{tmpdir}..."
@@ -182,16 +219,23 @@
opts.separator "gem name matches an installed gem (full name-version syntax), that gem will be used."
opts.on('-a', '--all', 'List all objects, even if they are inside added/removed module/class') do
@list_all = true
end
+ opts.on('--compact', 'Show compact results') { @compact = true }
opts.on('--git', 'Compare versions from two git commit/branches') do
@use_git = true
end
+ opts.on('--query QUERY', 'Only diff filtered objects') do |query|
+ @verifier.add_expressions(query)
+ end
+ opts.on('--no-modified', 'Ignore modified objects') do
+ @modified = false
+ end
common_options(opts)
parse_options(opts, args)
unless args.size == 2
- puts opts.banner
+ log.puts opts.banner
exit(0)
end
args
end
\ No newline at end of file