Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Commit

Permalink
WIP - build methods repo
Browse files Browse the repository at this point in the history
  • Loading branch information
floehopper committed Mar 15, 2014
1 parent e6a34ef commit c204b23
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
40 changes: 40 additions & 0 deletions build_methods_repo.rb
@@ -0,0 +1,40 @@
require 'bundler/setup'

# specifically for Mocha
require 'parser/ruby18'
Parser::CurrentRuby = Parser::Ruby18

require 'method_log/repository'
require 'method_log/method_finder'
require 'method_log/source_file'

def unindent(code)
lines = code.split($/)
indent = lines.reject { |l| l.strip.length == 0 }.map { |l| l[/^ */].length }.min
lines.map { |l| l.sub(Regexp.new(' ' * indent), '') }.join($/)
end

new_repository_path = File.expand_path('.git/methods.git')
Rugged::Repository.init_at(new_repository_path, :bare)
new_repository = MethodLog::Repository.new(new_repository_path)

repository_path = File.expand_path('~/Code/freerange/mocha')
repository = MethodLog::Repository.new(repository_path)
repository.commits(sorting: Rugged::SORT_TOPO | Rugged::SORT_REVERSE).each do |commit|
puts commit.sha
new_commit = new_repository.build_commit
commit.source_files.each do |source_file|
next if source_file.path[%r{^(vendor|test)}]
begin
method_finder = MethodLog::MethodFinder.new(source_file)
method_finder.methods.each do |method_signature, method_definition|
_, namespace, name = method_signature.match(/^(.*)([#.].*)$/).to_a
path = namespace.split('::').push(name).join(File::SEPARATOR) + '.rb'
new_commit.add(MethodLog::SourceFile.new(path: path, source: unindent(method_definition.source) + $/))
end
rescue Parser::SyntaxError => e
p e
end
end
new_commit.apply(user: commit.author, message: commit.message)
end
2 changes: 2 additions & 0 deletions lib/method_log/method_finder.rb
Expand Up @@ -7,6 +7,8 @@

module MethodLog
class MethodFinder < Parser::AST::Processor
attr_reader :methods

def initialize(source_file)
@source_file = source_file
@scope = Scope::Root.new
Expand Down
3 changes: 2 additions & 1 deletion lib/method_log/repository.rb
Expand Up @@ -25,9 +25,10 @@ def commit(*source_files)
end

def commits(options = {})
options[:sorting] ||= Rugged::SORT_TOPO
Enumerator.new do |yielder|
if @repository.ref('refs/heads/master')
@repository.walk(@repository.last_commit).with_index do |commit, index|
@repository.walk(@repository.last_commit, options[:sorting]).with_index do |commit, index|
break if options[:max_count] && index >= options[:max_count] - 1
yielder << build_commit(commit.oid)
end
Expand Down

0 comments on commit c204b23

Please sign in to comment.