Class: Herve::CLI::RubyCommands::List

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/herve/cli/ruby_commands/list.rb

Defined Under Namespace

Classes: Entry, Release

Constant Summary collapse

DEFAULT_MAX_AGE =

Max age to use when there is no info in HTTP response

60
MINIMUM_CACHE_TTL =

Minimum cache duration to not check less than every minute

60
ARCH_REGEX =
/portable-ruby-[\d.]+\.(?<arch>[a-zA-Z0-9_]+)\.bottle\.tar\.gz/

Instance Attribute Summary

Attributes inherited from BaseCommand

#config, #logger

Instance Method Summary collapse

Methods inherited from BaseCommand

inherited

Instance Method Details

#call(**options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/herve/cli/ruby_commands/list.rb', line 40

def call(**options)
  installed_rubies = config.rubies
  active_ruby = config.project_ruby

  if options[:installed_only]
    if installed_rubies.empty?
      logger.warn { "No Ruby installation found." }
      logger.info do
        "Try installin Ruby with '#{Rainbow("herve ruby install").cyan}' or check your configuration"
      end
      return
    end

    print_rubies(installed_rubies, active_ruby, options[:format])
    return
  end

  all_releases = fetch_available_releases(config.cache)

  rubies_map = {}
  installed_rubies.each do |ruby|
    rubies_map[ruby.display_name] ||= []
    rubies_map[ruby.display_name] << ruby
  end

  desired_od, desired_arch = parse_arch_string(RubyCommands.current_platform_string)
  available_rubies = all_releases.map do |release|
    release["assets"].map do |asset|
      ruby_from_release(release, asset)
    end
  end.flatten
  available_rubies.filter! { |ruby| (ruby.os == desired_od) && (ruby.arch == desired_arch) }

  available_rubies.each do |ruby|
    display_name = ruby.display_name
    next if rubies_map.key?(display_name)

    rubies_map[display_name] ||= []
    rubies_map[display_name] << ruby
  end

  if rubies_map.empty? && options[:format] == "text"
    logger.warn { "No rubies found for your platform" }
    return
  end

  print_rubies(rubies_map.values.flatten, active_ruby, options[:format])
end