lib/rocktumbler/tumbler.rb in rocktumbler-0.1.3 vs lib/rocktumbler/tumbler.rb in rocktumbler-0.2.0
- old
+ new
@@ -2,27 +2,50 @@
module Rocktumbler
# The Tumbler class is responsible for executing the top level tumble method
# and reads, parses, cleans, writes and verifies the new gemfile.
class Tumbler
- def initialize(location = Bundler.default_gemfile)
- @opts = {}
+
+ def initialize(opts, location = Bundler.default_gemfile)
+ @opts = opts
@gemfile_location = location
@bundler_dependencies = parse_gemfile(@gemfile_location)
+ @original_gemfile_str = File.open(@gemfile_location).read
@gemfile = Rocktumbler::Gemfile.new(location)
end
- def tumble(args = ARGV)
- @opts = Option.parse(args)
+ def tumble
groups = Rocktumbler::GroupFilter.new(@bundler_dependencies, @opts).filter
clean_gemfile_str = @gemfile.print_source_and_ruby
clean_gemfile_str << groups.map(&:print).join
- compare_to_original_gemfile(clean_gemfile_str)
- write(clean_gemfile_str) unless @opts.skip_write
- clean_gemfile_str
+ if clean_gemfile_str == @original_gemfile_str
+ print Rainbow("No changes required to Gemfile\n").yellow
+ else
+ compare_to_original_gemfile(clean_gemfile_str)
+ write(clean_gemfile_str) unless @opts.skip_write
+ print Rainbow("New Gemfile generated and written to \
+#{@gemfile_location}\n").green
+ verbose_highlight(clean_gemfile_str) if @opts.verbose
+ end
end
+ private
+
+ def verbose_highlight(source)
+ print "\n"
+ formatter = Rouge::Formatters::Terminal256.new(theme: 'github')
+ lexer = Rouge::Lexers::Ruby.new
+ print formatter.format(lexer.lex(source))
+ print "\n"
+ end
+
+ def write(clean_gemfile_str)
+ gemfile = File.open(@gemfile_location, 'w')
+ gemfile.write(clean_gemfile_str)
+ gemfile.close
+ end
+
def compare_to_original_gemfile(clean_gemfile_str)
temp_gemfile = write_temp_gemfile(clean_gemfile_str)
temp_bundler_dependencies = parse_gemfile(temp_gemfile.path)
diff = @bundler_dependencies - temp_bundler_dependencies
@@ -31,17 +54,9 @@
else
fail IncomparableGemfileError, "Clean Gemfile is not comparable to the\
existing Gemfile. The following gems are missing : #{diff.map(&:name)}."
end
end
-
- def write(clean_gemfile_str)
- gemfile = File.open(@gemfile_location, 'w')
- gemfile.write(clean_gemfile_str)
- gemfile.close
- end
-
- private
def write_temp_gemfile(clean_gemfile_str)
temp_gemfile = Tempfile.new('Gemfile.temp')
temp_gemfile.write(clean_gemfile_str)
temp_gemfile.close