# frozen_string_literal: true require_relative 'organize_gemfile/bundle_parser.rb' require_relative 'organize_gemfile/gemfile_group.rb' require_relative 'organize_gemfile/builder.rb' require_relative 'organize_gemfile/version' require 'optparse' module OrganizeGemfile class Error < StandardError end def self.execute(*args) options = parse_options(args) gemfile_path = options[:gemfile] ||= File.expand_path('Gemfile') unless File.exist?(gemfile_path) puts "Gemfile not found at #{gemfile_path}" return end gemfile_lock_path = Pathname.new(gemfile_path).parent.join('Gemfile.lock') unless gemfile_lock_path.exist? puts "Gemfile.lock not found at #{gemfile_lock_path}. Be sure to run `bundle install` before running this command." return end gemfile_lock_path = File.expand_path('Gemfile.lock') raise 'Gemfile.lock not found' unless File.exist?(gemfile_lock_path) parser = BundleParser.new(gemfile_path, gemfile_lock_path) ruby_version = parser.ruby_version groups = parser.groups builder = Builder.new(gemfile_path, ruby_version: ruby_version, groups: groups) revised_gemfile = builder.build File.open(gemfile_path, 'w') { |f| f.write(revised_gemfile) } puts 'Gemfile organized!' end def self.parse_options(args) options = {} opts = OptionParser.new opts.banner = 'Usage: organize_gemfile [options]' opts.on('-h', '--help', 'Prints this help') do puts opts exit end opts.on('-gPATH', '--gemfile=PATH', 'Path to Gemfile') do |path| options[:gemfile] = File.expand_path(path) end opts.parse!(into: options) options end end