Class: Mnenv::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/mnenv/cli.rb

Constant Summary collapse

PLATFORM_REPOSITORIES =

General commands

{
  'gemfile' => GemfileRepository,
  'snap' => SnapRepository,
  'homebrew' => HomebrewRepository,
  'chocolatey' => ChocolateyRepository,
  'binary' => BinaryRepository
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Cli

Returns a new instance of Cli.



42
43
44
45
46
# File 'lib/mnenv/cli.rb', line 42

def initialize(*args)
  super
  self.class.verbose = options[:verbose]
  self.class.data_dir = options[:'data-dir']
end

Class Attribute Details

.data_dirObject

Returns the value of attribute data_dir.



37
38
39
# File 'lib/mnenv/cli.rb', line 37

def data_dir
  @data_dir
end

.verbose=(value) ⇒ Object (writeonly)

Sets the attribute verbose

Parameters:

  • value

    the value to set the attribute verbose to.



39
40
41
# File 'lib/mnenv/cli.rb', line 39

def verbose=(value)
  @verbose = value
end

Class Method Details

.verbose?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/mnenv/cli.rb', line 33

def verbose?
  @verbose ||= false
end

Instance Method Details

#global(version = nil) ⇒ Object



91
92
93
94
95
# File 'lib/mnenv/cli.rb', line 91

def global(version = nil)
  cmd = VersionCommand.new
  cmd.options = options
  cmd.global(version)
end

#info(platform, version_number) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mnenv/cli.rb', line 134

def info(platform, version_number)
  repo_class = PLATFORM_REPOSITORIES[platform]
  unless repo_class
    puts "Error: Unknown platform '#{platform}'. Available: #{PLATFORM_REPOSITORIES.keys.join(', ')}"
    exit 1
  end

  repo = repo_class.new
  version_obj = repo.find(version_number)

  unless version_obj
    puts "Error: Version '#{version_number}' not found for platform '#{platform}'"
    exit 1
  end

  case options[:format]
  when 'json'
    output = JsonFormatter.format_version(version_obj)
    output['platform'] = platform
    puts JSON.pretty_generate(output)
  else
    puts "#{platform.capitalize} version #{version_obj.display_name}:"
    puts "  Version: #{version_obj.version}"
    puts "  Published: #{version_obj.published_at || 'N/A'}"
    puts "  Parsed: #{version_obj.parsed_at || 'N/A'}"
    version_obj.to_h.each do |k, v|
      next if %w[version published_at parsed_at].include?(k)

      puts "  #{k}: #{v}"
    end
  end
end

#install(version = nil) ⇒ Object



69
70
71
72
73
# File 'lib/mnenv/cli.rb', line 69

def install(version = nil)
  cmd = InstallCommand.new
  cmd.options = Thor::CoreExt::HashWithIndifferentAccess.new(options.merge(version: version))
  cmd.install(version)
end

#list(platform) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/mnenv/cli.rb', line 169

def list(platform)
  repo_class = PLATFORM_REPOSITORIES[platform]
  unless repo_class
    puts "Error: Unknown platform '#{platform}'. Available: #{PLATFORM_REPOSITORIES.keys.join(', ')}"
    exit 1
  end

  repo = repo_class.new
  versions = repo.all

  case options[:format]
  when 'json'
    output = JsonFormatter.format_versions(versions)
    output['platform'] = platform
    puts JSON.pretty_generate(output)
  else
    puts "#{platform.capitalize} versions (#{versions.size}):"
    versions.each do |v|
      published = v.published_at ? " (#{v.published_at.strftime('%Y-%m-%d')})" : ''
      puts "  #{v.display_name}#{published}"
    end
  end
end

#list_allObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/mnenv/cli.rb', line 195

def list_all
  case options[:format]
  when 'json'
    output = {}
    PLATFORM_REPOSITORIES.each do |name, repo_class|
      repo = repo_class.new
      output[name] = JsonFormatter.format_versions(repo.all)
    end

    puts JSON.pretty_generate(output)
  else
    PLATFORM_REPOSITORIES.each_key do |platform|
      list(platform)
    end
  end
end

#local(version = nil) ⇒ Object



102
103
104
105
106
# File 'lib/mnenv/cli.rb', line 102

def local(version = nil)
  cmd = VersionCommand.new
  cmd.options = options
  cmd.local(version)
end

#uninstall(version) ⇒ Object



117
118
119
120
121
# File 'lib/mnenv/cli.rb', line 117

def uninstall(version)
  cmd = UninstallCommand.new
  cmd.options = options
  cmd.uninstall(version)
end

#updateObject



218
219
220
221
222
# File 'lib/mnenv/cli.rb', line 218

def update
  puts 'Updating version data...'
  Repository.update_versions
  puts 'Version data updated successfully.'
end

#use(version = nil) ⇒ Object



80
81
82
83
84
# File 'lib/mnenv/cli.rb', line 80

def use(version = nil)
  cmd = VersionCommand.new
  cmd.options = options
  cmd.use(version)
end

#versionObject



213
214
215
# File 'lib/mnenv/cli.rb', line 213

def version
  puts "mnenv #{Mnenv::VERSION}"
end

#versionsObject



109
110
111
112
# File 'lib/mnenv/cli.rb', line 109

def versions
  cmd = VersionCommand.new
  cmd.versions
end