This repository has been archived by the owner on Oct 9, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored CLI using thor, improving usability by miles
- Loading branch information
1 parent
5738d1c
commit e58de17
Showing
3 changed files
with
59 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
require 'app-store-emigrant' | ||
|
||
AppStore::Emigrant::CLI.new ARGV | ||
AppStore::Emigrant::CLI.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,89 @@ | ||
# encoding: utf-8 | ||
|
||
require 'rainbow' | ||
require 'thor' | ||
|
||
module AppStore; end | ||
|
||
module AppStore::Emigrant | ||
|
||
# Represents the command line interface | ||
class CLI | ||
class CLI < Thor | ||
|
||
# Initializes CLI instance with given arguments | ||
def initialize args | ||
desc 'scan [LIBRARY PATH]', 'Scans an iTunes library and reports which of its mobile applications are out of date' | ||
method_option :clear_cache, :type => :boolean, :default => false, :aliases => '-c', :desc => 'Clears application cache prior to scanning' | ||
def scan path = nil | ||
|
||
# Extract the path (if any) | ||
path, = args | ||
# Ensure all output is immediately flushed | ||
$stdout.sync = true | ||
|
||
# Use the default library on this system when no path is specified | ||
library = path ? Library.new(path) : Library.default | ||
|
||
# Load cloud data in bulk | ||
# Clear cache if requested | ||
if options[:clear_cache] | ||
Cache.clear! | ||
end | ||
|
||
# Verify cache integrity | ||
puts 'Verifying cache integrity..' | ||
|
||
# Forcefully cache metadata for each application that is not yet cached | ||
library.apps.each do |app| | ||
|
||
unless app.cached? | ||
begin | ||
app.cache! | ||
puts " + #{app.name} v#{app.version}" | ||
rescue App::Invalid => e | ||
puts " ! Cannot cache #{app.filename}: #{e.message}".foreground(:red).bright | ||
end | ||
end | ||
|
||
end | ||
|
||
# Print cache statistics | ||
puts "Done. Currently caching #{Cache.count} applications on your system." | ||
|
||
puts | ||
|
||
# Since all apps are cached, load cloud data in bulk | ||
library.clouddata! | ||
|
||
# Print library statistics | ||
puts "Your library contains #{library.valid_apps.length} valid applications, of which #{library.outdated_apps.length} are outdated:" | ||
|
||
# Loop through all applications | ||
library.apps.each do |app| | ||
|
||
if app.valid? | ||
|
||
# Print their name, version and whether it's outdated | ||
print app.name + ' @ ' + "v#{app.version}".foreground(app.outdated? ? :red : :green) | ||
# Generate color-coded version | ||
version = "v#{app.version}".foreground(app.outdated? ? :red : :green) | ||
if app.outdated? | ||
version = version.bright | ||
end | ||
|
||
# Print application name, version and whether it's outdated | ||
print " · #{app.name} #{version}" | ||
if app.outdated? | ||
print " (v#{app.cloudversion} available in the cloud)" | ||
print " · v#{app.cloudversion} available".foreground(:white) | ||
end | ||
|
||
puts | ||
else | ||
print app.filename + ' is invalid (metadata, id or name missing)' | ||
puts " ! #{app.filename} is invalid (metadata, id or name missing)".foreground(:red).bright | ||
end | ||
|
||
puts | ||
|
||
end | ||
|
||
end | ||
|
||
desc 'version', 'Prints version information' | ||
def version | ||
puts "App Store Emigrant v#{AppStore::Emigrant::VERSION}" | ||
end | ||
|
||
end | ||
|
||
end |