lib/bcupgrade/cask.rb in bcupgrade-0.3.4 vs lib/bcupgrade/cask.rb in bcupgrade-0.9.0

- old
+ new

@@ -1,101 +1,65 @@ # frozen_string_literal: true + require 'yaml' module Bcupgrade class Cask - attr_reader :installed_casks, :error_casks + attr_reader :config, :args, :target def initialize(options, args) - @config = config + @config = load_config @options = options - @args = args.uniq - @list = upgrade_target - @installed_casks = @list[0] - @error_casks = @list[1] + @args = args.uniq + @target = upgrade_target end - def check_version - update_casks = [] - - @installed_casks.each do |name| - cask_info = BrewCask.info(name) - version_number = trim_latest_version(cask_info) - - string = "/#{name}/#{version_number}" - latest_version = cask_info.include?(string) ? nil : version_number - - if latest_version - puts "#{name} (#{latest_version})" - update_casks.push(name) - end - end - - update_casks - end - def upgrade_version(casks) return if @options[:dry_run] casks.each do |cask| - input = if @options[:install] - 'y' - else - Readline.readline("\nUpgrade #{cask}? [y/n] ") - end - next unless input == 'y' + next unless prompt_answer_yes?(cask) - if @options[:remove] - puts "\n==> Remove #{cask}" - BrewCask.remove(cask) - end - - puts "\n==> Install #{cask}" + puts "\n==> Upgrade #{cask}" BrewCask.install(cask) end end private - def config + def load_config file = File.join(ENV['HOME'], '.bcupgrade') YAML.load_file(file) if File.exist?(file) end def upgrade_target if @args.any? - trim_target_to_a(@args) + @args else - check_target = trim_target_to_a(BrewCask.list.split(/\n/)) - [trim_ignore_casks(check_target[0]), check_target[1]] + outdated = BrewCask.outdated.split(/\n/) + exclude_ignore_casks(outdated) end end - def trim_target_to_a(array) - installed_casks = [] - error_casks = [] - - array.each do |cask| - if cask.include?(' (!)') - error_casks.push(cask.delete(' (!)')) - else - installed_casks.push(cask) - end - end - - [installed_casks, error_casks] - end - - def trim_ignore_casks(casks) + def exclude_ignore_casks(casks) if @config.nil? casks else - casks - Array(@config['ignore']) + ignore = Array(@config['ignore']) + casks - ignore end end - def trim_latest_version(cask_info) - lines = cask_info.split(/\n/) - lines[0]&.gsub(/.+: (.+)/, '\1') + def prompt_answer_yes?(cask) + input = if @options[:install] + 'y' + else + Readline.readline("\nUpgrade #{cask}? [y/n] ") + end + if input.casecmp('y').zero? || input.casecmp('yes').zero? + true + else + false + end end end end