lib/jim/cli.rb in jim-0.1.2 vs lib/jim/cli.rb in jim-0.2.0
- old
+ new
@@ -6,11 +6,11 @@
# CLI handles the command line interface for the `jim` binary.
# The layout is farily simple. Options are parsed using optparse.rb and
# the different public methods represent 1-1 the commands provided by the bin.
class CLI
- attr_accessor :jimfile, :jimhome, :force
+ attr_accessor :jimfile, :jimhome, :force, :stdout
# create a new instance with the args passed from the command line i.e. ARGV
def initialize(args)
@output = ""
# set the default jimhome
@@ -30,10 +30,12 @@
cheat
else
@output << "No action found for #{command}. Run -h for help."
end
@output
+ rescue ArgumentError => e
+ @output << "#{e.message} for #{command}"
rescue Jim::FileExists => e
@output << "#{e.message} already exists, bailing. Use --force if you're sure"
rescue => e
@output << e.message + " (#{e.class})"
end
@@ -47,23 +49,24 @@
# list the possible commands without detailed descriptions
def cheat
logger.info "Usage: jim [options] [command] [args]\n"
logger.info "Commands:"
- logger.info template('commands').find_all {|l| l.match(/^\w/) }.join("")
+ logger.info [*template('commands')].grep(/^\w/).join
logger.info "run commands for details"
end
+ alias :help :cheat
# initialize the current dir with a new Jimfile
def init(dir = nil)
dir = Pathname.new(dir || '')
jimfile_path = dir + 'Jimfile'
if jimfile_path.readable? && !force
- raise Jim::FileExists(jimfile_path)
+ raise Jim::FileExists.new(jimfile_path)
else
File.open(jimfile_path, 'w') do |f|
- f << template('Jimfile')
+ f << template('jimfile')
end
logger.info "wrote Jimfile to #{jimfile_path}"
end
end
@@ -72,28 +75,43 @@
Jim::Installer.new(url, jimhome, :force => force, :name => name, :version => version).install
end
# bundle the files specified in Jimfile into `to`
def bundle(to = nil)
+ to = STDOUT if stdout
path = bundler.bundle!(to)
+ logger.info "Wrote #{File.size(path) / 1024}kb"
end
# compress the files specified in Jimfile into `to`
def compress(to = nil)
+ to = STDOUT if stdout
path = bundler.compress!(to)
+ logger.info "Wrote #{File.size(path) / 1024}kb"
end
# copy/vendor all the files specified in Jimfile to `dir`
def vendor(dir = nil)
- bundler.vendor!(dir)
+ bundler.vendor!(dir, force)
end
- # list the installed projects and versions
+ # list the only the _installed_ projects and versions
def list
- logger.info "Getting list of installed files in #{index.directories.join(':')}"
+ logger.info "Getting list of installed files in\n#{installed_index.directories.join(':')}"
+ list = installed_index.list
+ logger.info "Installed:"
+ print_version_list(list)
+ end
+ alias :installed :list
+
+ # list all available projects and versions including those in the local path, or
+ # paths specified in a jimfile
+ def available
+ logger.info "Getting list of all available files in\n#{index.directories.join("\n")}"
list = index.list
- logger.info "Installed:\n#{list.collect {|i| "#{i[0]} (#{i[1].join(', ')})"}.join("\n")}"
+ logger.info "Available:"
+ print_version_list(list)
end
# Iterates over matching files and prompts for removal
def remove(name, version = nil)
logger.info "Looking for files matching #{name} #{version}"
@@ -114,17 +132,29 @@
logger.info "Removed #{removed} files."
else
logger.info "No installed files matched."
end
end
+ alias :uninstall :remove
# list the files and their resolved paths specified in the Jimfile
def resolve
resolved = bundler.resolve!
- logger.info "Files:\n#{resolved.join("\n")}"
+ logger.info "Files:"
+ resolved.each do |r|
+ logger.info r.join(" | ")
+ end
resolved
end
+
+ # vendor to dir, then bundle and compress the Jimfile contents
+ def pack(dir = nil)
+ logger.info "packing the Jimfile for this project"
+ vendor(dir)
+ bundle
+ compress
+ end
private
def parse_options(runtime_args)
OptionParser.new("", 24, ' ') do |opts|
opts.banner = "Usage: jim [options] [command] [args]"
@@ -146,14 +176,20 @@
opts.on("-d", "--debug", "set log level to debug") {|d|
logger.level = Logger::DEBUG
}
+ opts.on("-o", "--stdout", "write output of commands (like bundle and compress to STDOUT)") {|o|
+ logger.level = Logger::ERROR
+ self.stdout = true
+ }
+
opts.on("-v", "--version", "print version") {|d|
puts "jim #{Jim::VERSION}"
exit
}
+
opts.on_tail("-h", "--help", "Show this message. Run jim commands for list of commands.") do
puts opts.help
exit
end
@@ -184,9 +220,15 @@
(Pathname.new(__FILE__).dirname + 'templates' + path).read
end
def logger
Jim.logger
+ end
+
+ def print_version_list(list)
+ list.each do |file, versions|
+ logger.info "#{file} (#{VersionSorter.rsort(versions.collect {|v| v[0] }).join(',')})"
+ end
end
end
end
\ No newline at end of file