Sha256: 7acc7558cbcabef49e62a1133b2ff4198061947102b30e83d98440b362c19397

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

# 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
organize_gemfile-0.1.1 lib/organize_gemfile.rb