Skip to content
This repository has been archived by the owner on Oct 9, 2018. It is now read-only.

Commit

Permalink
Refactored CLI using thor, improving usability by miles
Browse files Browse the repository at this point in the history
  • Loading branch information
timkurvers committed Dec 10, 2012
1 parent 5738d1c commit e58de17
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
1 change: 1 addition & 0 deletions app-store-emigrant.gemspec
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |s|
s.add_dependency 'rubyzip', '~> 0.9'
s.add_dependency 'rainbow', '~> 1.1'
s.add_dependency 'CFPropertyList', '~> 2.0'
s.add_dependency 'thor', '~> 0.16.0'

s.add_development_dependency 'rake'

Expand Down
2 changes: 1 addition & 1 deletion bin/ase
Expand Up @@ -2,4 +2,4 @@

require 'app-store-emigrant'

AppStore::Emigrant::CLI.new ARGV
AppStore::Emigrant::CLI.start
69 changes: 57 additions & 12 deletions lib/app-store-emigrant/cli.rb
@@ -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

0 comments on commit e58de17

Please sign in to comment.