class Supercop::Actions::Clean

Public Class Methods

call() click to toggle source
# File lib/supercop/actions/clean.rb, line 5
def call
  if clean_gemfile[:changed]
    restore_gemfile

    update_bundle
  else
    notify_nothing_changed
  end

  remove_config
end

Private Class Methods

clean_gemfile() click to toggle source
# File lib/supercop/actions/clean.rb, line 43
def clean_gemfile
  clean_gemfile = []
  changed = false

  File.open(gemfile, 'r') do |f|
    until (line = f.gets).nil?
      if line.include?(start_comment)
        changed = true
        until (line = f.gets).nil?
          if line.include?(end_comment)
            line = f.gets
            break
          end
        end
      end

      clean_gemfile << line
    end
  end
  @_clean_gemfile = { gemfile: clean_gemfile.join, changed: changed }
end
end_comment() click to toggle source
# File lib/supercop/actions/clean.rb, line 87
def end_comment
  Supercop::Actions::Loaders::Dependency::END_COMMENT
end
gemfile() click to toggle source
# File lib/supercop/actions/clean.rb, line 73
def gemfile
  @_gemfile ||= File.join(Dir.pwd, 'Gemfile')
end
notify_nothing_changed() click to toggle source
# File lib/supercop/actions/clean.rb, line 19
def notify_nothing_changed
  puts 'Your Gemfile was not changed. Nothing to restore.'
end
remove_config() click to toggle source
# File lib/supercop/actions/clean.rb, line 65
def remove_config
  if File.file?(supercop_config)
    puts 'Removing your supercop.yml'

    FileUtils.rm(supercop_config)
  end
end
restore_gemfile() click to toggle source
# File lib/supercop/actions/clean.rb, line 23
def restore_gemfile
  puts 'Restoring your Gemfile'

  temp_file = "#{gemfile}.tmp"

  File.open(temp_file, 'w') do |f|
    f.print(clean_gemfile[:gemfile])
  end

  FileUtils.mv temp_file, gemfile
end
start_comment() click to toggle source
# File lib/supercop/actions/clean.rb, line 83
def start_comment
  Supercop::Actions::Loaders::Dependency::START_COMMENT
end
supercop_config() click to toggle source
# File lib/supercop/actions/clean.rb, line 77
def supercop_config
  root = defined?(Rails) ? File.join(Dir.pwd, 'config') : Dir.pwd

  @_supercop_config ||= File.join(root, 'supercop.yml')
end
update_bundle() click to toggle source
# File lib/supercop/actions/clean.rb, line 35
def update_bundle
  puts 'Updating your bundle, please wait'

  Bundler.with_clean_env do
    `bundle install`
  end
end