Class: Makit::Ruby::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/ruby/cli.rb

Class Method Summary collapse

Class Method Details

.configObject

Get Ruby configuration information



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/makit/ruby/cli.rb', line 46

def self.config
  return {} unless installed?

  config_output = `ruby -e "puts RbConfig::CONFIG" 2>/dev/null`
  return {} if config_output.empty?

  config = {}
  config_output.lines.each do |line|
    key, value = line.strip.split("=", 2)
    config[key] = value if key && value
  end
  config
end

.current_versionObject

Get the current Ruby version using the ‘ruby’ CLI



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/makit/ruby/cli.rb', line 7

def self.current_version
  version_output = `ruby --version 2>/dev/null`.strip
  raise "Ruby is not installed or not available in PATH" if version_output.empty?

  # Extract version number from output like "ruby 3.2.0p123 (2023-01-01 revision abc123) [x86_64-darwin22]"

  version_match = version_output.match(/ruby (\d+\.\d+\.\d+)/)
  if version_match
    version_match[1]
  else
    # Fallback: return the full version string if parsing fails

    version_output
  end
end

.engineObject

Get Ruby engine information



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/makit/ruby/cli.rb', line 68

def self.engine
  return nil unless installed?

  engine_info = `ruby -e "puts RUBY_ENGINE" 2>/dev/null`.strip
  engine_version = `ruby -e "puts RUBY_ENGINE_VERSION" 2>/dev/null`.strip

  {
    name: engine_info,
    version: engine_version,
  }
end

.environment_summaryObject

Get Ruby environment summary



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/makit/ruby/cli.rb', line 151

def self.environment_summary
  return { error: "Ruby is not installed" } unless installed?

  {
    version: current_version,
    full_version: version_info[:full_version],
    platform: platform,
    engine: engine,
    executable_path: executable_path,
    installation_dir: installation_dir,
    gem_dirs: gem_dirs,
    library_dirs: library_dirs,
  }
end

.executable_pathObject

Get the Ruby executable path



41
42
43
# File 'lib/makit/ruby/cli.rb', line 41

def self.executable_path
  `which ruby 2>/dev/null`.strip
end

.extract_patch_level(version_string) ⇒ Object

Extract patch level from version string



167
168
169
170
# File 'lib/makit/ruby/cli.rb', line 167

def self.extract_patch_level(version_string)
  match = version_string.match(/p(\d+)/)
  match ? match[1] : nil
end

.extract_platform(version_string) ⇒ Object

Extract platform from version string



179
180
181
182
# File 'lib/makit/ruby/cli.rb', line 179

def self.extract_platform(version_string)
  match = version_string.match(/\[([^\]]+)\]/)
  match ? match[1] : nil
end

.extract_release_date(version_string) ⇒ Object

Extract release date from version string



173
174
175
176
# File 'lib/makit/ruby/cli.rb', line 173

def self.extract_release_date(version_string)
  match = version_string.match(/\((\d{4}-\d{2}-\d{2})/)
  match ? match[1] : nil
end

.gem_dirsObject

Get Ruby gem directories



98
99
100
101
102
103
104
105
# File 'lib/makit/ruby/cli.rb', line 98

def self.gem_dirs
  return [] unless installed?

  gem_dirs_output = `ruby -e "puts Gem.path" 2>/dev/null`
  return [] if gem_dirs_output.empty?

  gem_dirs_output.strip.split("\n")
end

.installation_dirObject

Get Ruby installation directory



81
82
83
84
85
# File 'lib/makit/ruby/cli.rb', line 81

def self.installation_dir
  return nil unless installed?

  `ruby -e "puts RbConfig::CONFIG['prefix']" 2>/dev/null`.strip
end

.installed?Boolean

Check if Ruby is installed and available

Returns:

  • (Boolean)


36
37
38
# File 'lib/makit/ruby/cli.rb', line 36

def self.installed?
  system("ruby --version > /dev/null 2>&1")
end

.library_dirsObject

Get Ruby library directories



88
89
90
91
92
93
94
95
# File 'lib/makit/ruby/cli.rb', line 88

def self.library_dirs
  return [] unless installed?

  lib_dirs = `ruby -e "puts RbConfig::CONFIG['libdir']" 2>/dev/null`.strip
  arch_lib_dirs = `ruby -e "puts RbConfig::CONFIG['archdir']" 2>/dev/null`.strip

  [lib_dirs, arch_lib_dirs].compact.uniq
end

.load_pathObject

Get Ruby load path



108
109
110
111
112
113
114
115
# File 'lib/makit/ruby/cli.rb', line 108

def self.load_path
  return [] unless installed?

  load_path_output = `ruby -e "puts $LOAD_PATH" 2>/dev/null`
  return [] if load_path_output.empty?

  load_path_output.strip.split("\n")
end

.platformObject

Get Ruby platform information



61
62
63
64
65
# File 'lib/makit/ruby/cli.rb', line 61

def self.platform
  return nil unless installed?

  `ruby -e "puts RUBY_PLATFORM" 2>/dev/null`.strip
end

.version_available?(version) ⇒ Boolean

Check if a specific Ruby version is available

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/makit/ruby/cli.rb', line 118

def self.version_available?(version)
  return false unless installed?

  current = current_version
  case version
  when /^(\d+)\.(\d+)\.(\d+)$/
    major = ::Regexp.last_match(1).to_i
    minor = ::Regexp.last_match(2).to_i
    patch = ::Regexp.last_match(3).to_i
    current_major, current_minor, current_patch = current.split(".").map(&:to_i)

    if major != current_major
      major == current_major
    elsif minor != current_minor
      minor == current_minor
    else
      patch == current_patch
    end
  when /^(\d+)\.(\d+)$/
    major = ::Regexp.last_match(1).to_i
    minor = ::Regexp.last_match(2).to_i
    current_major, current_minor = current.split(".").map(&:to_i)
    major == current_major && minor == current_minor
  when /^(\d+)$/
    major = ::Regexp.last_match(1).to_i
    current_major = current.split(".").first.to_i
    major == current_major
  else
    false
  end
end

.version_infoObject

Get detailed Ruby version information



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/makit/ruby/cli.rb', line 22

def self.version_info
  version_output = `ruby --version 2>/dev/null`.strip
  raise "Ruby is not installed or not available in PATH" if version_output.empty?

  {
    full_version: version_output,
    version: current_version,
    patch_level: extract_patch_level(version_output),
    release_date: extract_release_date(version_output),
    platform: extract_platform(version_output),
  }
end