class GemfileArranger::CLI

Public Instance Methods

arrange() click to toggle source
# File lib/gemfile_arranger/cli.rb, line 39
def arrange
  code = gemfile_contents(options[:gemfile])

  buffer        = Parser::Source::Buffer.new('(gemfile_arranger arrange)')
  buffer.source = code
  parser        = Parser::CurrentRuby.new
  ast           = parser.parse(buffer)

  sort_gems_in_group = Traverse::SortGemsInGroup.new
  rewrited_ast = sort_gems_in_group.process(ast)

  sort_priority_gems_in_group = Traverse::SortPriorityGemsInGroup.new(config['priority_gem'])
  rewrited_ast = sort_priority_gems_in_group.process(rewrited_ast)

  sort_block = Traverse::SortBlock.new(config['block_order'])
  rewrited_ast = sort_block.process(rewrited_ast)

  rewrited_gemfile = Unparser.unparse(rewrited_ast)
  if options[:auto_correct]
    rewrite_gemfile(rewrited_gemfile, options[:gemfile])
    puts "Rewrite Gemfile compete! #{options[:gemfile]}"
  else
    puts rewrited_gemfile
  end
end
show_base_config() click to toggle source
# File lib/gemfile_arranger/cli.rb, line 71
def show_base_config
  puts YAML.dump(base_config)
end
show_config() click to toggle source
# File lib/gemfile_arranger/cli.rb, line 66
def show_config
  puts YAML.dump(config)
end
version() click to toggle source
# File lib/gemfile_arranger/cli.rb, line 31
def version
  puts "Gemfile Arranger version #{VERSION}"
end

Private Instance Methods

base_config() click to toggle source
# File lib/gemfile_arranger/cli.rb, line 79
def base_config
  base_config_path = file_path
                     .dirname
                     .join('..', '..', 'config', '.gemfile_arranger.base.yml')
                     .expand_path
  fail "Can not read base config: #{base_config_path}" unless base_config_path.file?

  base_config_contents = base_config_path.read
  SafeYAML.load(base_config_contents) || {}
end
config() click to toggle source
# File lib/gemfile_arranger/cli.rb, line 100
def config
  base_config.merge(user_config)
end
file_path() click to toggle source
# File lib/gemfile_arranger/cli.rb, line 116
def file_path
  Pathname.new(__FILE__)
end
gemfile_contents(filename) click to toggle source
# File lib/gemfile_arranger/cli.rb, line 104
def gemfile_contents(filename)
  gemfile_path = root_path
                 .join(filename)
                 .expand_path
  fail "Can not read Gemfile: #{gemfile_path}" unless gemfile_path.file?
  gemfile_path.read
end
rewrite_gemfile(contents, filename) click to toggle source
# File lib/gemfile_arranger/cli.rb, line 120
def rewrite_gemfile(contents, filename)
  root_path
    .join(filename)
    .expand_path
    .write(contents + "\n")
end
root_path() click to toggle source
# File lib/gemfile_arranger/cli.rb, line 112
def root_path
  Pathname.pwd
end
user_config() click to toggle source
# File lib/gemfile_arranger/cli.rb, line 90
def user_config
  user_config_path = root_path
                     .join('.gemfile_arranger.yml')
                     .expand_path
  return {} unless user_config_path.file?

  user_config_contents = user_config_path.read
  SafeYAML.load(user_config_contents) || {}
end