nu 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,18 +8,18 @@ require File.expand_path(File.dirname(__FILE__) + "/settings.rb")
8
8
  module Nu
9
9
  class Api
10
10
 
11
- def self.out(out)
12
- @stdout = out
11
+ def self.set_log(logger)
12
+ @log = logger
13
13
  end
14
14
 
15
- def self.verbose(verbose)
16
- @verbose = verbose
15
+ def self.set_out(outer)
16
+ @out = outer
17
17
  end
18
18
 
19
19
  def self.load_project_settings(settings_file)
20
20
  log "Load Project Settings called: settings_file=#{settings_file}"
21
21
  @settings_file = settings_file
22
- @logger = lambda {|msg| log(msg)}
22
+
23
23
  @platforms =
24
24
  ['net1_0', 'net1_1', 'net2_0', 'net3_0', 'net3_5', 'net4_0',
25
25
  'mono1_0', 'mono2_0',
@@ -35,24 +35,21 @@ module Nu
35
35
  @project_settings.lib.use_long_names = false if @project_settings.lib.use_long_names == nil
36
36
 
37
37
  if @verbose
38
- disp "Project Settings:"
39
- disp YAML.dump(@project_settings).gsub('!ruby/object:OpenStruct','').gsub(/\s*table:/,'').gsub('---','')
40
- disp ""
38
+ log "Project Settings:"
39
+ log YAML.dump(@project_settings).gsub('!ruby/object:OpenStruct','').gsub(/\s*table:/,'').gsub('---','')
40
+ log ""
41
41
  end
42
42
  end
43
43
 
44
44
  def self.store_setting(name, value)
45
45
  log "Store Setting called: name=#{name} value=#{value}"
46
- log "Before:"
47
- load_project_settings(@settings_file) if @verbose
48
46
 
49
- @project_settings.set_setting_by_path(name, value, @logger)
47
+ @project_settings.set_setting_by_path(name, value, @log)
50
48
  assert_project_settings
51
49
 
52
50
  File.open(@settings_file, 'w') do |out|
53
51
  YAML.dump(@project_settings, out)
54
52
  end
55
- log "After:"
56
53
  load_project_settings(@settings_file)
57
54
  end
58
55
 
@@ -60,10 +57,10 @@ module Nu
60
57
  if @project_settings.platform
61
58
  @project_settings.platform = @project_settings.platform.gsub('.','_')
62
59
  if !@platforms.index(@project_settings.platform)
63
- disp "'#{@project_settings.platform}' is not a valid platform."
64
- disp "\nChoose one of these:"
60
+ out "'#{@project_settings.platform}' is not a valid platform."
61
+ out "\nChoose one of these:"
65
62
  @platforms.each {|p| disp " #{p}"}
66
- disp ""
63
+ out ""
67
64
  exit 1
68
65
  end
69
66
  end
@@ -71,7 +68,7 @@ module Nu
71
68
 
72
69
  def self.get_setting(name)
73
70
  log "Get Setting called: name=#{name}"
74
- @project_settings.get_setting_by_path(name, @logger)
71
+ @project_settings.get_setting_by_path(name, @log)
75
72
  end
76
73
 
77
74
  def self.version_string
@@ -82,32 +79,26 @@ module Nu
82
79
  def self.install_package(package_name, package_version)
83
80
  log "Install called: package_name=#{package_name} package_version=#{package_version}."
84
81
 
85
- loader = Nu::Loader.new(package_name, package_version, @project_settings.lib.location, @project_settings.lib.use_long_names)
82
+ loader = Nu::Loader.new(package_name, package_version, @project_settings.lib.location, @project_settings.lib.use_long_names, @out, @log)
86
83
  if loader.load_gem
87
84
  loader.copy_to_lib
88
85
  end
89
86
 
90
87
  end
91
88
 
92
- def self.output_report()
89
+ def self.report()
93
90
  log "Report called."
94
-
95
- disp "\n"
96
- disp "The following packages are installed:"
97
- disp "====================================="
98
- Nu::LibTools.read_specs_from_lib(@project_settings.lib.location).each{|i| disp " " + i.full_name}
99
- disp "====================================="
100
- disp ""
91
+ Nu::LibTools.read_specs_from_lib(@project_settings.lib.location)
101
92
  end
102
93
 
103
94
  private
104
95
 
105
96
  def self.log(msg)
106
- disp(msg) if @verbose
97
+ @log.call(msg)
107
98
  end
108
99
 
109
- def self.disp(msg)
110
- @stdout << msg + "\n"
100
+ def self.out(msg)
101
+ @out.call(msg)
111
102
  end
112
103
 
113
104
  end
@@ -3,64 +3,76 @@
3
3
  require 'optparse'
4
4
  require 'ostruct'
5
5
  require 'date'
6
+ require 'logger'
6
7
  require File.expand_path(File.dirname(__FILE__) + "/api.rb")
7
8
  require File.expand_path(File.dirname(__FILE__) + "/loader.rb")
9
+ require File.expand_path(File.dirname(__FILE__) + "/cli_shim.rb")
10
+ require File.expand_path(File.dirname(__FILE__) + "/json_shim.rb")
8
11
 
9
12
  class App
10
13
 
11
14
  attr_reader :options
12
15
 
13
16
  def initialize(arguments, stdin, stdout)
14
-
17
+
18
+ # Set defaults
19
+ @options = OpenStruct.new
20
+ @options.verbose = false
21
+ @options.quiet = false
22
+ @commands = []
23
+
24
+ @arguments = arguments
25
+
15
26
  #special case, they want to know our version
16
- if arguments.length == 1 && arguments[0] == '--version'
27
+ if arguments.length == 1 && (arguments[0] == '--version' || arguments[0] == '-v')
17
28
  output_version
18
29
  exit 0
19
30
  end
20
-
21
- Nu::Api.out(stdout)
22
31
 
23
- @arguments = arguments
24
-
25
- # Set defaults
26
- @options = OpenStruct.new
27
- @options.verbose = false
28
- @options.quiet = false
29
- @commands = []
30
-
31
- begin
32
- OptionParser.new do |opts|
33
-
34
- opts.banner = "\nUsage:" +
35
- "\n nu -h/--help" +
36
- "\n nu --version" +
37
- "\n nu COMMAND [arguments...] [options...]" +
38
- "\n\nExamples:" +
39
- "\n nu install fluentnhibernate" +
40
- "\n nu install nunit --version 2.5.7.10213.20100801" +
41
- "\n nu config lib.location ./lib" +
42
- "\n nu report" +
43
- "\n nu install fluentnhibernate --report" +
44
- "\n\nOptions and Switches:"
45
-
46
- opts.on('-v', '--version VERSION','Specify version of package to install' ) do |ver|
47
- @options.package_version = ver
48
- end
32
+ Nu::Api.set_log(lambda {|msg| log msg})
33
+ Nu::Api.set_out(lambda {|msg| disp msg})
34
+ @shim = CliShim.new(lambda {|msg| disp msg}, lambda {|msg| log msg})
49
35
 
50
- 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
51
- @commands << lambda {Nu::Api.output_report}
52
- end
36
+ begin
37
+ OptionParser.new do |opts|
53
38
 
54
- # Specify options
55
- opts.on('-V', '--verbose') { @options.verbose = true }
56
- opts.on('-q', '--quiet') { @options.quiet = true }
39
+ opts.banner = "\nUsage:" +
40
+ "\n nu -h/--help" +
41
+ "\n nu -v/--version" +
42
+ "\n nu COMMAND [arguments...] [options...]" +
43
+ "\n\nExamples:" +
44
+ "\n nu install fluentnhibernate" +
45
+ "\n nu install nunit --version 2.5.7.10213.20100801" +
46
+ "\n nu config lib.location ./lib" +
47
+ "\n nu report" +
48
+ "\n nu install fluentnhibernate --report" +
49
+ "\n\nOptions and Switches:"
57
50
 
58
- opts.on_tail( '-h', '--help', 'Display this screen' ) do
59
- output_help(opts)
60
- end
61
-
62
- @help_command = lambda{output_help(opts)}
63
-
51
+ opts.on('-v', '--version VERSION','Specify version of package to install' ) do |ver|
52
+ @options.package_version = ver
53
+ end
54
+
55
+ 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
56
+ @commands << lambda {@shim.report}
57
+ end
58
+
59
+ # Specify options
60
+ opts.on('-V', '--verbose') { @options.verbose = true }
61
+ opts.on('-q', '--quiet') { @options.quiet = true }
62
+ opts.on('--json', 'Run in JSON mode. All outputs will be in JSON, status messages silenced.') do
63
+ @options.json = true
64
+ @shim = JsonShim.new(lambda do |json|
65
+ puts json
66
+ log_to_file(json)
67
+ end, lambda {|msg| log msg})
68
+ end
69
+
70
+ opts.on_tail( '-h', '--help', 'Display this screen' ) do
71
+ output_help(opts)
72
+ end
73
+
74
+ @help_command = lambda{output_help(opts)}
75
+
64
76
  end.parse!
65
77
  rescue
66
78
  @help_command.call
@@ -74,16 +86,15 @@ class App
74
86
 
75
87
  if arguments_valid?
76
88
 
77
- puts "Start at #{DateTime.now}\n\n" if @options.verbose
89
+ log "Start at #{DateTime.now}\n\n"
78
90
  output_version if @options.verbose
79
91
  output_inputs if @options.verbose
80
92
 
81
- Nu::Api.verbose(@options.verbose)
82
93
  Nu::Api.load_project_settings('nuniverse.yaml')
83
94
 
84
95
  @commands.reverse.each {|command| command.call}
85
-
86
- puts "\nFinished at #{DateTime.now}" if @options.verbose
96
+
97
+ log "\nFinished at #{DateTime.now}"
87
98
 
88
99
  else
89
100
  @help_command.call
@@ -98,23 +109,23 @@ class App
98
109
  @options.command = @arguments[0].downcase
99
110
  case @options.command
100
111
  when 'report'
101
- @commands << lambda {Nu::Api.output_report}
112
+ @commands << lambda {@shim.report}
102
113
  when 'install'
103
114
  assert_param_count(2)
104
115
  @options.package = @arguments[1]
105
- @commands << lambda {Nu::Api.install_package(@options.package, @options.package_version)}
116
+ @commands << lambda {@shim.install_package(@options.package, @options.package_version)}
106
117
  # when 'uninstall'
107
118
  # assert_param_count(2)
108
119
  # @options.package = @arguments[1]
109
- # @commands << lambda {Nu::Api.uninstall_package(@options.package, @options.package_version)}
120
+ # @commands << lambda {@shim.uninstall_package(@options.package, @options.package_version)}
110
121
  when 'config'
111
122
  if @arguments.length == 2
112
- @commands << lambda {puts "#{@arguments[1]} = #{Nu::Api.get_setting(@arguments[1])}"}
123
+ @commands << lambda {@shim.get_setting(@arguments[1])}
113
124
  else
114
125
  assert_param_count(3)
115
126
  @commands << lambda do
116
127
  Nu::Api.store_setting(@arguments[1], @arguments[2])
117
- puts "#{@arguments[1]} = #{Nu::Api.get_setting(@arguments[1])}"
128
+ @shim.get_setting(@arguments[1])
118
129
  end if @arguments.length == 3
119
130
  end
120
131
  end
@@ -138,30 +149,49 @@ class App
138
149
  end
139
150
 
140
151
  def output_inputs
141
- puts "Inputs:\n"
152
+ disp "Inputs:\n"
142
153
 
143
154
  @options.marshal_dump.each do |name, val|
144
- puts " #{name} = #{val}"
155
+ disp " #{name} = #{val}"
145
156
  end
146
- puts ""
157
+ disp ""
147
158
  end
148
159
 
149
160
  def output_help(opts)
161
+ @options.quiet = false
150
162
  output_version
151
163
  output_description
152
- puts opts
153
- puts "\n\nFurther Information:" +
164
+ disp opts
165
+ disp "\n\nFurther Information:" +
154
166
  "\n http://nu.wikispot.org" +
155
167
  "\n http://groups.google.com/group/nu-net"
156
- puts ''
168
+ disp ''
157
169
  exit 0
158
170
  end
159
171
 
160
172
  def output_version
161
- puts Nu::Api.version_string
173
+ disp Nu::Api.version_string
162
174
  end
163
175
 
164
176
  def output_description
165
- puts "Nu will automatically download and install specified library packages into your lib folder, along with all their dependencies.\n"
177
+ disp "Nu will automatically download and install specified library packages into your lib folder, along with all their dependencies.\n"
178
+ end
179
+
180
+ def disp(msg)
181
+ puts msg unless @options.quiet || @options.json
182
+ end
183
+
184
+ def log(msg)
185
+ log_to_file(msg) if @options.json
186
+ if @options.verbose
187
+ puts msg unless @options.json
188
+ end
189
+ end
190
+
191
+ def log_to_file(msg)
192
+ if @options.verbose
193
+ @file_logger ||= Logger.new('nu.log', 5, 1024000)
194
+ @file_logger.debug("#{DateTime.now} - #{msg.strip}")
195
+ end
166
196
  end
167
197
  end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/has_out_and_log.rb")
2
+ require File.expand_path(File.dirname(__FILE__) + "/api.rb")
3
+
4
+ class CliShim < HasOutAndLog
5
+
6
+ def report(details=false)
7
+ log "Report called."
8
+
9
+ out "\n"
10
+ out "The following packages are installed:"
11
+ out "====================================="
12
+ #TODO: render differently if details==true
13
+ Nu::Api.report.each{|i| out " #{i.name} (#{i.version})"}
14
+ out "====================================="
15
+ out ""
16
+ end
17
+
18
+ def install_package(package, package_version)
19
+ Nu::Api.install_package(package, package_version)
20
+ out "Installed package #{(package + " #{package_version}").strip}."
21
+ end
22
+
23
+ def get_setting(name)
24
+ out "#{name} = #{Nu::Api.get_setting(name)}"
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+
2
+ class HasOutAndLog
3
+ def initialize(out, log)
4
+ @out = out
5
+ @log = log
6
+ end
7
+
8
+ protected
9
+
10
+ def out(msg)
11
+ @out.call(msg)
12
+ end
13
+
14
+ def log(msg)
15
+ @log.call(msg)
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require File.expand_path(File.dirname(__FILE__) + "/has_out_and_log.rb")
4
+
5
+ class JsonShim < HasOutAndLog
6
+
7
+ def report
8
+ out Nu::Api.report.to_json
9
+ end
10
+
11
+ def install_package(package, package_version)
12
+ Nu::Api.install_package(package, package_version)
13
+ end
14
+
15
+ def get_setting(name)
16
+ out ({:name => name, :value => Nu::Api.get_setting(name) }.to_json)
17
+ end
18
+
19
+ end
@@ -2,33 +2,36 @@ require 'rubygems'
2
2
  require 'rubygems/dependency_installer'
3
3
  require File.expand_path(File.dirname(__FILE__) + "/lib_tools.rb")
4
4
  require File.expand_path(File.dirname(__FILE__) + "/gem_tools.rb")
5
+ require File.expand_path(File.dirname(__FILE__) + "/has_out_and_log.rb")
5
6
 
6
7
  module Nu
7
- class Loader
8
+ class Loader < HasOutAndLog
8
9
 
9
10
  attr :gem_name
10
11
  attr :location
11
12
  attr :version
12
13
 
13
- def initialize(name, version, location, long_names)
14
+ def initialize(name, version, location, long_names, out, log)
14
15
  #improve the crap out of this
15
16
  @long_names = long_names
16
17
  @gem_name = name
17
18
  @location = location
18
19
  @version = version
20
+ super(out, log)
19
21
  end
20
22
 
21
23
  def load_gem
24
+ log "Load Gem #{(@gem_name + " #{@version}").strip}."
22
25
  if !gem_available?
23
- puts "Gem #{(@gem_name + " #{@version}").strip} is not installed locally - I am now going to try and install it"
26
+ out "Gem #{(@gem_name + " #{@version}").strip} is not installed locally - I am now going to try and install it"
24
27
  begin
25
28
  inst = Gem::DependencyInstaller.new
26
29
  inst.install @gem_name, @version
27
30
  inst.installed_gems.each do |spec|
28
- puts "Successfully installed #{spec.full_name}"
31
+ out "Successfully installed #{spec.full_name}"
29
32
  end
30
33
  rescue Gem::GemNotFoundException => e
31
- puts "ERROR: #{e.message}"
34
+ out "ERROR: #{e.message}"
32
35
  return false
33
36
  end
34
37
  else
@@ -38,10 +41,10 @@ module Nu
38
41
 
39
42
  def copy_to_lib
40
43
  start_here = copy_source
41
- puts "Copy From: #{start_here}"
44
+ log "Copy From: #{start_here}"
42
45
 
43
46
  to = copy_dest
44
- puts "Copy To: #{to}"
47
+ log "Copy To: #{to}"
45
48
 
46
49
  FileUtils.copy_entry start_here, to
47
50
  Nu::GemTools.write_spec(gemspec, to)
@@ -72,12 +75,12 @@ module Nu
72
75
  def process_dependencies
73
76
  gemspec.dependencies.each do |d|
74
77
  if Gem.available? d.name
75
- puts "Loading dependency: #{d.name} #{d.requirement}"
76
- loader = Loader.new(d.name, d.requirement, @location, @long_names)
78
+ out "Loading dependency: #{d.name} #{d.requirement}"
79
+ loader = Loader.new(d.name, d.requirement, @location, @long_names, @out, @log)
77
80
  loader.copy_to_lib
78
81
  else
79
- puts "#{d.name} is not installed locally"
80
- puts "please run 'gem install #{d.name}"
82
+ out "#{d.name} is not installed locally"
83
+ out "please run 'gem install #{d.name}"
81
84
  end
82
85
  end
83
86
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nu
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dru Sellers
@@ -18,7 +18,7 @@ autorequire:
18
18
  bindir: bin
19
19
  cert_chain: []
20
20
 
21
- date: 2010-08-30 00:00:00 -05:00
21
+ date: 2010-08-31 00:00:00 -05:00
22
22
  default_executable:
23
23
  dependencies: []
24
24
 
@@ -35,7 +35,10 @@ files:
35
35
  - bin/nu
36
36
  - lib/nu/api.rb
37
37
  - lib/nu/app.rb
38
+ - lib/nu/cli_shim.rb
38
39
  - lib/nu/gem_tools.rb
40
+ - lib/nu/has_out_and_log.rb
41
+ - lib/nu/json_shim.rb
39
42
  - lib/nu/lib_tools.rb
40
43
  - lib/nu/loader.rb
41
44
  - lib/nu/settings.rb