class Gamerom::Cli
Cli
- Main cli commands
Public Class Methods
exit_on_failure?()
click to toggle source
# File lib/gamerom/cli.rb, line 10 def self.exit_on_failure? true end
Public Instance Methods
config()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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_all()
click to toggle source
# 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
list()
click to toggle source
# 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
platforms()
click to toggle source
# 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
recover()
click to toggle source
# 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
regions()
click to toggle source
# 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
repo()
click to toggle source
# 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)
click to toggle source
# 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
stats()
click to toggle source
# 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_all()
click to toggle source
# 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)
click to toggle source
# 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_all()
click to toggle source
# 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_databases()
click to toggle source
# 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_database()
click to toggle source
# 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
version()
click to toggle source
# File lib/gamerom/cli.rb, line 304 def version puts Gamerom::VERSION end
Private Instance Methods
print_game_table(games)
click to toggle source
# File lib/gamerom/cli.rb, line 310 def print_game_table(games) results = [] games.each do |game| results << [ game.id, game.name, game.region, game.installed? ? shell.set_color('installed', :green) : '-', game.respond_to?(:tags) ? game.tags.join(', ') : '-' ] end results.sort_by! { |columns| columns[1] } results.unshift %w[ID NAME REGION INSTALLED TAGS] shell.print_table(results) end
render_error(exception, options)
click to toggle source
# File lib/gamerom/cli.rb, line 327 def render_error(exception, options) shell.say exception.message, :red shell.say exception.full_message.force_encoding('utf-8'), :red if options[:verbose] end
validate_platform(repo, platform)
click to toggle source
# File lib/gamerom/cli.rb, line 332 def validate_platform(repo, platform) return if repo.platforms.keys.include? options[:platform] raise "Expected '--platform' to be one of #{repo.platforms.keys.join(", ")}; got #{platform}" end