Class: Gamerom::Cli

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

Overview

Cli - Main cli commands

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/gamerom/cli.rb', line 10

def self.exit_on_failure?
  true
end

Instance Method Details

#configObject



15
16
17
18
19
20
21
22
23
# File 'lib/gamerom/cli.rb', line 15

def config
  cfg = {
    ROM_ROOT: Gamerom::ROM_ROOT,
    CACHE_DIR: Gamerom::CACHE_DIR,
    GAME_DIR: Gamerom::GAME_DIR,
    LOG_DIR: Gamerom::LOG_DIR,
  }
  pp cfg
end

#info(*args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gamerom/cli.rb', line 28

def info(*args)
  game_identifier = args.join(' ')
  repo = Repo.new(options[:repo])
  validate_platform repo, options[:platform]
  puts "showing info for game #{game_identifier} on #{options[:platform]} platform on #{options[:repo]} repo..."
  game = repo.find(options[:platform], game_identifier)
  if !game.nil?
    puts game
    puts game.filenames if game.installed?
  else
    shell.say "Game #{game_identifier} not found", :red
  end
rescue StandardError => e
  render_error e, options
  exit 1
end

#install(*args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gamerom/cli.rb', line 48

def install(*args)
  game_identifier = args.join(' ')
  repo = Repo.new(options[:repo])
  validate_platform repo, options[:platform]
  game = repo.find(options[:platform], game_identifier)
  if game.nil?
    shell.say "Game #{game_identifier} not found", :red
    return
  end
  puts "installing game #{game.id} - #{game.name} - #{game.region} on #{options[:platform]} platform on #{options[:repo]} repo..."
  if game.installed?
    shell.say 'Game already installed', :yellow
    return
  end
  game.install
  shell.say 'Game installed', :green
rescue StandardError => e
  render_error e, options
  exit 1
end

#install_allObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gamerom/cli.rb', line 73

def install_all
  repo = Repo.new(options[:repo])
  validate_platform repo, options[:platform]
  games = repo.games options[:platform], region: options[:region]
  games.each do |game|
    install(game.id) unless game.installed?
  end
rescue StandardError => e
  render_error e, options
  exit 1
end

#listObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/gamerom/cli.rb', line 89

def list
  repo = Repo.new(options[:repo])
  validate_platform repo, options[:platform]
  puts "listing available games for #{options[:platform]} platform on #{options[:repo]} repo..."
  games = repo.games options[:platform], region: options[:region]
  print_game_table(games)
rescue StandardError => e
  render_error e, options
  exit 1
end

#platformsObject



102
103
104
105
106
107
108
109
# File 'lib/gamerom/cli.rb', line 102

def platforms
  puts "listing available platforms for #{options[:repo]} repo..."
  platforms = { platforms: Repo.new(options[:repo]).platforms }
  puts platforms.to_yaml
rescue StandardError => e
  render_error e, options
  exit 1
end

#recoverObject



114
115
116
117
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
# File 'lib/gamerom/cli.rb', line 114

def recover
  repo = Repo.new(options[:repo])
  validate_platform repo, options[:platform]
  puts "recovering state of roms for #{options[:platform]} platform on #{options[:repo]} repo..."
  games = repo.games options[:platform]
  games_not_found = []
  games.each do |game|
    filename = nil
    basename = "#{Gamerom::GAME_DIR}/#{repo.name}/#{options[:platform]}/#{game[:region]}/#{game[:name]}"
    %w[zip 7z rar].each do |ext|
      fullname = "#{basename}.#{ext}"
      filename = fullname if File.exist? fullname
    end

    if filename
      game.update_state File.basename(filename)
      puts "Found game #{game[:name]}"
    else
      games_not_found << game[:name]
    end
  end
  if games_not_found.count.positive?
    puts 'Games not found:'
    puts games_not_found
  end
rescue StandardError => e
  puts e.message
  exit 1
end

#regionsObject



147
148
149
150
151
152
153
154
155
# File 'lib/gamerom/cli.rb', line 147

def regions
  repo = Repo.new(options[:repo])
  validate_platform repo, options[:platform]
  puts "listing available regions for #{options[:platform]} platform on #{options[:repo]} repo..."
  puts repo.regions options[:platform]
rescue StandardError => e
  render_error e, options
  exit 1
end

#repoObject



158
159
160
161
162
163
164
# File 'lib/gamerom/cli.rb', line 158

def repo
  puts 'listing available repo...'
  puts Repo.list
rescue StandardError => e
  render_error e, options
  exit 1
end

#search(*args) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/gamerom/cli.rb', line 171

def search(*args)
  keyword = args.join(' ')
  repo = Repo.new(options[:repo])
  validate_platform repo, options[:platform]
  puts "searching available games for #{options[:platform]} platform on #{options[:repo]} repo..."
  games = repo.games options[:platform], region: options[:region], keyword: keyword
  if options[:install]
    games.each do |game|
      install(game.id) unless game.installed?
    end
  else
    print_game_table(games)
  end
rescue StandardError => e
  render_error e, options
  exit 1
end

#statsObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/gamerom/cli.rb', line 192

def stats
  repo = Repo.new(options[:repo])
  validate_platform repo, options[:platform]
  puts "stats for #{options[:platform]} platform on #{options[:repo]} repo..."
  games = repo.games options[:platform]
  total = games.count
  installed = games.select(&:installed?).count
  size = 0
  if File.exist? "#{Gamerom::GAME_DIR}/#{repo.name}/#{options[:platform]}"
    size = `du -hs "#{Gamerom::GAME_DIR}/#{repo.name}/#{options[:platform]}/"|awk '{ print $1 }'`
  end
  puts "  All: #{installed}/#{total} - size: #{size}"
  repo.regions(options[:platform]).each do |region|
    games = repo.games(options[:platform], region: region)
    total = games.count
    installed = games.select(&:installed?).count
    size = 0
    if File.exist? "#{Gamerom::GAME_DIR}/#{repo.name}/#{options[:platform]}/#{region}"
      size = `du -hs "#{Gamerom::GAME_DIR}/#{repo.name}/#{options[:platform]}/#{region}/"|awk '{ print $1 }'`
    end
    puts "  #{region}: #{installed}/#{total} - size: #{size}"
  end
  puts
rescue StandardError => e
  render_error e, options
  exit 1
end

#stats_allObject



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/gamerom/cli.rb', line 222

def stats_all
  repo = Repo.new(options[:repo])
  repo.platforms.each_key do |platform|
    a = Gamerom::Cli.new
    a.options = { platform: platform, repo: options[:repo] }
    a.stats
  end
rescue StandardError => e
  render_error e, options
  exit 1
end

#uninstall(*args) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/gamerom/cli.rb', line 237

def uninstall(*args)
  game_identifier = args.join(' ')
  repo = Repo.new(options[:repo])
  validate_platform repo, options[:platform]
  game = repo.find(options[:platform], game_identifier)
  if game.nil?
    shell.say "Game #{game_identifier} not found", :red
    return
  end
  puts "uninstalling game #{game.id} - #{game.name} - #{game.region} on #{options[:platform]} platform..."
  unless game.installed?
    shell.say 'Game is not installed', :yellow
    return
  end
  game.uninstall
  shell.say 'Game uninstalled', :green
rescue StandardError => e
  render_error e, options
  exit 1
end

#uninstall_allObject



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/gamerom/cli.rb', line 262

def uninstall_all
  repo = Repo.new(options[:repo])
  validate_platform repo, options[:platform]
  games = repo.games options[:platform], region: options[:region]
  games.each do |game|
    uninstall(game.id) if game.installed?
  end
rescue StandardError => e
  render_error e, options
  exit 1
end

#update_all_databasesObject



276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/gamerom/cli.rb', line 276

def update_all_databases
  puts "updating all databases on #{options[:repo]} repo..."
  repo = Repo.new(options[:repo])
  repo.platforms.each_key do |platform|
    repo = Repo.new(options[:repo])
    repo.update_database platform
  end
  shell.say 'All game databases updated', :green
rescue StandardError => e
  render_error e, options
  exit 1
end

#update_databaseObject



292
293
294
295
296
297
298
299
300
301
# File 'lib/gamerom/cli.rb', line 292

def update_database
  repo = Repo.new(options[:repo])
  validate_platform repo, options[:platform]
  puts "updating #{options[:platform]} platform on #{options[:repo]} repo..."
  repo.update_database options[:platform]
  shell.say "Game database updated for platform #{options[:platform]} on #{options[:repo]} repo", :green
rescue StandardError => e
  render_error e, options
  exit 1
end

#versionObject



304
305
306
# File 'lib/gamerom/cli.rb', line 304

def version
  puts Gamerom::VERSION
end