Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Introduced a shim construct to marshal outputs from the api as approp…

…riate.

Now the --json switch works!

Also added another special case to the --version switch to handle -v too (when it is the only arg)
  • Loading branch information...
commit 37051364c64add4eda96a465bb26a9e8e1166e4c 1 parent f350a13
@brendanjerwin brendanjerwin authored
View
44 lib/nu/api.rb
@@ -8,18 +8,18 @@
module Nu
class Api
- def self.out(out)
- @stdout = out
+ def self.set_log(logger)
+ @log = logger
end
- def self.verbose(verbose)
- @verbose = verbose
+ def self.set_out(outer)
+ @out = outer
end
def self.load_project_settings(settings_file)
log "Load Project Settings called: settings_file=#{settings_file}"
@settings_file = settings_file
- @logger = lambda {|msg| log(msg)}
+
@platforms =
['net1_0', 'net1_1', 'net2_0', 'net3_0', 'net3_5', 'net4_0',
'mono1_0', 'mono2_0',
@@ -35,9 +35,9 @@ def self.load_project_settings(settings_file)
@project_settings.lib.use_long_names = false if @project_settings.lib.use_long_names == nil
if @verbose
- disp "Project Settings:"
- disp YAML.dump(@project_settings).gsub('!ruby/object:OpenStruct','').gsub(/\s*table:/,'').gsub('---','')
- disp ""
+ log "Project Settings:"
+ log YAML.dump(@project_settings).gsub('!ruby/object:OpenStruct','').gsub(/\s*table:/,'').gsub('---','')
+ log ""
end
end
@@ -46,7 +46,7 @@ def self.store_setting(name, value)
log "Before:"
load_project_settings(@settings_file) if @verbose
- @project_settings.set_setting_by_path(name, value, @logger)
+ @project_settings.set_setting_by_path(name, value, @log)
assert_project_settings
File.open(@settings_file, 'w') do |out|
@@ -60,10 +60,10 @@ def self.assert_project_settings
if @project_settings.platform
@project_settings.platform = @project_settings.platform.gsub('.','_')
if !@platforms.index(@project_settings.platform)
- disp "'#{@project_settings.platform}' is not a valid platform."
- disp "\nChoose one of these:"
+ out "'#{@project_settings.platform}' is not a valid platform."
+ out "\nChoose one of these:"
@platforms.each {|p| disp " #{p}"}
- disp ""
+ out ""
exit 1
end
end
@@ -71,7 +71,7 @@ def self.assert_project_settings
def self.get_setting(name)
log "Get Setting called: name=#{name}"
- @project_settings.get_setting_by_path(name, @logger)
+ @project_settings.get_setting_by_path(name, @log)
end
def self.version_string
@@ -82,32 +82,26 @@ def self.version_string
def self.install_package(package_name, package_version)
log "Install called: package_name=#{package_name} package_version=#{package_version}."
- loader = Nu::Loader.new(package_name, package_version, @project_settings.lib.location, @project_settings.lib.use_long_names)
+ loader = Nu::Loader.new(package_name, package_version, @project_settings.lib.location, @project_settings.lib.use_long_names, @out, @log)
if loader.load_gem
loader.copy_to_lib
end
end
- def self.output_report()
+ def self.report()
log "Report called."
-
- disp "\n"
- disp "The following packages are installed:"
- disp "====================================="
- Nu::LibTools.read_specs_from_lib(@project_settings.lib.location).each{|i| disp " " + i.full_name}
- disp "====================================="
- disp ""
+ Nu::LibTools.read_specs_from_lib(@project_settings.lib.location)
end
private
def self.log(msg)
- disp(msg) if @verbose
+ @log.call(msg)
end
- def self.disp(msg)
- @stdout << msg + "\n"
+ def self.out(msg)
+ @out.call(msg)
end
end
View
135 lib/nu/app.rb
@@ -5,62 +5,70 @@
require 'date'
require File.expand_path(File.dirname(__FILE__) + "/api.rb")
require File.expand_path(File.dirname(__FILE__) + "/loader.rb")
+require File.expand_path(File.dirname(__FILE__) + "/cli_shim.rb")
+require File.expand_path(File.dirname(__FILE__) + "/json_shim.rb")
class App
attr_reader :options
def initialize(arguments, stdin, stdout)
-
+
+ # Set defaults
+ @options = OpenStruct.new
+ @options.verbose = false
+ @options.quiet = false
+ @commands = []
+
+ @arguments = arguments
+
#special case, they want to know our version
- if arguments.length == 1 && arguments[0] == '--version'
+ if arguments.length == 1 && (arguments[0] == '--version' || arguments[0] == '-v')
output_version
exit 0
end
-
- Nu::Api.out(stdout)
- @arguments = arguments
-
- # Set defaults
- @options = OpenStruct.new
- @options.verbose = false
- @options.quiet = false
- @commands = []
-
- begin
- OptionParser.new do |opts|
-
- opts.banner = "\nUsage:" +
- "\n nu -h/--help" +
- "\n nu --version" +
- "\n nu COMMAND [arguments...] [options...]" +
- "\n\nExamples:" +
- "\n nu install fluentnhibernate" +
- "\n nu install nunit --version 2.5.7.10213.20100801" +
- "\n nu config lib.location ./lib" +
- "\n nu report" +
- "\n nu install fluentnhibernate --report" +
- "\n\nOptions and Switches:"
+ Nu::Api.set_log(lambda {|msg| log msg})
+ Nu::Api.set_out(lambda {|msg| disp msg})
+ @shim = CliShim.new(lambda {|msg| disp msg}, lambda {|msg| log msg})
- opts.on('-v', '--version VERSION','Specify version of package to install' ) do |ver|
- @options.package_version = ver
- end
-
- opts.on('-r','--report', 'Report on the packages currently installed in the lib folder. When called as a switch it will run the report AFTER executing the requested command.') do
- @commands << lambda {Nu::Api.output_report}
- end
+ begin
+ OptionParser.new do |opts|
- # Specify options
- opts.on('-V', '--verbose') { @options.verbose = true }
- opts.on('-q', '--quiet') { @options.quiet = true }
+ opts.banner = "\nUsage:" +
+ "\n nu -h/--help" +
+ "\n nu -v/--version" +
+ "\n nu COMMAND [arguments...] [options...]" +
+ "\n\nExamples:" +
+ "\n nu install fluentnhibernate" +
+ "\n nu install nunit --version 2.5.7.10213.20100801" +
+ "\n nu config lib.location ./lib" +
+ "\n nu report" +
+ "\n nu install fluentnhibernate --report" +
+ "\n\nOptions and Switches:"
- opts.on_tail( '-h', '--help', 'Display this screen' ) do
- output_help(opts)
- end
-
- @help_command = lambda{output_help(opts)}
-
+ opts.on('-v', '--version VERSION','Specify version of package to install' ) do |ver|
+ @options.package_version = ver
+ end
+
+ opts.on('-r','--report', 'Report on the packages currently installed in the lib folder. When called as a switch it will run the report AFTER executing the requested command.') do
+ @commands << lambda {@shim.report}
+ end
+
+ # Specify options
+ opts.on('-V', '--verbose') { @options.verbose = true }
+ opts.on('-q', '--quiet') { @options.quiet = true }
+ opts.on('--json', 'Run in JSON mode. All outputs will be in JSON, status messages silenced.') do
+ @options.json = true
+ @shim = JsonShim.new(lambda {|json| puts json}, lambda {|msg| log msg})
+ end
+
+ opts.on_tail( '-h', '--help', 'Display this screen' ) do
+ output_help(opts)
+ end
+
+ @help_command = lambda{output_help(opts)}
+
end.parse!
rescue
@help_command.call
@@ -74,16 +82,15 @@ def run
if arguments_valid?
- puts "Start at #{DateTime.now}\n\n" if @options.verbose
+ disp "Start at #{DateTime.now}\n\n" if @options.verbose
output_version if @options.verbose
output_inputs if @options.verbose
- Nu::Api.verbose(@options.verbose)
Nu::Api.load_project_settings('nuniverse.yaml')
@commands.reverse.each {|command| command.call}
-
- puts "\nFinished at #{DateTime.now}" if @options.verbose
+
+ disp "\nFinished at #{DateTime.now}" if @options.verbose
else
@help_command.call
@@ -98,23 +105,23 @@ def extract_commands
@options.command = @arguments[0].downcase
case @options.command
when 'report'
- @commands << lambda {Nu::Api.output_report}
+ @commands << lambda {@shim.report}
when 'install'
assert_param_count(2)
@options.package = @arguments[1]
- @commands << lambda {Nu::Api.install_package(@options.package, @options.package_version)}
+ @commands << lambda {@shim.install_package(@options.package, @options.package_version)}
# when 'uninstall'
# assert_param_count(2)
# @options.package = @arguments[1]
- # @commands << lambda {Nu::Api.uninstall_package(@options.package, @options.package_version)}
+ # @commands << lambda {@shim.uninstall_package(@options.package, @options.package_version)}
when 'config'
if @arguments.length == 2
- @commands << lambda {puts "#{@arguments[1]} = #{Nu::Api.get_setting(@arguments[1])}"}
+ @commands << lambda {@shim.get_setting(@arguments[1])}
else
assert_param_count(3)
@commands << lambda do
Nu::Api.store_setting(@arguments[1], @arguments[2])
- puts "#{@arguments[1]} = #{Nu::Api.get_setting(@arguments[1])}"
+ @shim.get_setting(@arguments[1])
end if @arguments.length == 3
end
end
@@ -128,6 +135,7 @@ def assert_param_count(count)
end
def post_process_options
+ @options.quiet = true if @options.json
@options.verbose = false if !@options.verbose
@options.verbose = false if @options.quiet
end
@@ -138,30 +146,39 @@ def arguments_valid?
end
def output_inputs
- puts "Inputs:\n"
+ disp "Inputs:\n"
@options.marshal_dump.each do |name, val|
- puts " #{name} = #{val}"
+ disp " #{name} = #{val}"
end
- puts ""
+ disp ""
end
def output_help(opts)
+ @options.quiet = false
output_version
output_description
- puts opts
- puts "\n\nFurther Information:" +
+ disp opts
+ disp "\n\nFurther Information:" +
"\n http://nu.wikispot.org" +
"\n http://groups.google.com/group/nu-net"
- puts ''
+ disp ''
exit 0
end
def output_version
- puts Nu::Api.version_string
+ disp Nu::Api.version_string
end
def output_description
- puts "Nu will automatically download and install specified library packages into your lib folder, along with all their dependencies.\n"
+ disp "Nu will automatically download and install specified library packages into your lib folder, along with all their dependencies.\n"
+ end
+
+ def disp(msg)
+ puts msg unless @options.quiet
+ end
+
+ def log(msg)
+ puts msg if @options.verbose
end
end
View
26 lib/nu/cli_shim.rb
@@ -0,0 +1,26 @@
+require File.expand_path(File.dirname(__FILE__) + "/has_out_and_log.rb")
+require File.expand_path(File.dirname(__FILE__) + "/api.rb")
+
+class CliShim < HasOutAndLog
+
+ def report(details=false)
+ log "Report called."
+
+ out "\n"
+ out "The following packages are installed:"
+ out "====================================="
+ #TODO: render differently if details==true
+ Nu::Api.report.each{|i| out " #{i.name} (#{i.version})"}
+ out "====================================="
+ out ""
+ end
+
+ def install_package(package, package_version)
+ Nu::Api.install_package(package, package_version)
+ out "Installed package #{(package + " #{package_version}").strip}."
+ end
+
+ def get_setting(name)
+ out "#{name} = #{Nu::Api.get_setting(name)}"
+ end
+end
View
17 lib/nu/has_out_and_log.rb
@@ -0,0 +1,17 @@
+
+class HasOutAndLog
+ def initialize(out, log)
+ @out = out
+ @log = log
+ end
+
+ protected
+
+ def out(msg)
+ @out.call(msg)
+ end
+
+ def log(msg)
+ @log.call(msg)
+ end
+end
View
19 lib/nu/json_shim.rb
@@ -0,0 +1,19 @@
+require 'rubygems'
+require 'active_support'
+require File.expand_path(File.dirname(__FILE__) + "/has_out_and_log.rb")
+
+class JsonShim < HasOutAndLog
+
+ def report
+ out Nu::Api.report.to_json
+ end
+
+ def install_package(package, package_version)
+ Nu::Api.install_package(package, package_version)
+ end
+
+ def get_setting(name)
+ out ({:name => name, :value => Nu::Api.get_setting(name) }.to_json)
+ end
+
+end
View
23 lib/nu/loader.rb
@@ -2,33 +2,36 @@
require 'rubygems/dependency_installer'
require File.expand_path(File.dirname(__FILE__) + "/lib_tools.rb")
require File.expand_path(File.dirname(__FILE__) + "/gem_tools.rb")
+require File.expand_path(File.dirname(__FILE__) + "/has_out_and_log.rb")
module Nu
- class Loader
+ class Loader < HasOutAndLog
attr :gem_name
attr :location
attr :version
- def initialize(name, version, location, long_names)
+ def initialize(name, version, location, long_names, out, log)
#improve the crap out of this
@long_names = long_names
@gem_name = name
@location = location
@version = version
+ super(out, log)
end
def load_gem
+ log "Load Gem #{(@gem_name + " #{@version}").strip}."
if !gem_available?
- puts "Gem #{(@gem_name + " #{@version}").strip} is not installed locally - I am now going to try and install it"
+ out "Gem #{(@gem_name + " #{@version}").strip} is not installed locally - I am now going to try and install it"
begin
inst = Gem::DependencyInstaller.new
inst.install @gem_name, @version
inst.installed_gems.each do |spec|
- puts "Successfully installed #{spec.full_name}"
+ out "Successfully installed #{spec.full_name}"
end
rescue Gem::GemNotFoundException => e
- puts "ERROR: #{e.message}"
+ out "ERROR: #{e.message}"
return false
end
else
@@ -38,10 +41,10 @@ def load_gem
def copy_to_lib
start_here = copy_source
- puts "Copy From: #{start_here}"
+ log "Copy From: #{start_here}"
to = copy_dest
- puts "Copy To: #{to}"
+ log "Copy To: #{to}"
FileUtils.copy_entry start_here, to
Nu::GemTools.write_spec(gemspec, to)
@@ -72,12 +75,12 @@ def copy_dest
def process_dependencies
gemspec.dependencies.each do |d|
if Gem.available? d.name
- puts "Loading dependency: #{d.name} #{d.requirement}"
+ out "Loading dependency: #{d.name} #{d.requirement}"
loader = Loader.new(d.name, d.requirement, @location, @long_names)
loader.copy_to_lib
else
- puts "#{d.name} is not installed locally"
- puts "please run 'gem install #{d.name}"
+ out "#{d.name} is not installed locally"
+ out "please run 'gem install #{d.name}"
end
end
end
View
2  specs/loader.rspec
@@ -4,7 +4,7 @@ require 'lib/nu/loader'
describe Nu::Loader do
before(:all) do
- @loader = Nu::Loader.new('rspec', nil, 'spec_lib', false)
+ @loader = Nu::Loader.new('rspec', nil, 'spec_lib', false, lambda{|o|},lambda{|o|})
@loader.load_gem
end
Please sign in to comment.
Something went wrong with that request. Please try again.