main 3.0.3 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -457,6 +457,14 @@ DOCS
457
457
  API section below
458
458
 
459
459
  HISTORY
460
+ 4.0.0
461
+ - avoid duping ios. new methods Main.push_ios! and Main.pop_ios! are
462
+ utilized for testing. this was done to make it simple to wrap
463
+ daemon/servolux programs around main, althought not strictly required.
464
+ not the version bump - there is not reason to expect existing main
465
+ programs to break, but it *is* and interface change which requires a major
466
+ version bump.
467
+
460
468
  3.0.0
461
469
  - major refactor to support modes via module/extend vs. subclassing.
462
470
  MIGHT NOT be backward compatible, though no known issues thus far.
data/README.erb CHANGED
@@ -186,6 +186,14 @@ DOCS
186
186
  API section below
187
187
 
188
188
  HISTORY
189
+ 4.0.0
190
+ - avoid duping ios. new methods Main.push_ios! and Main.pop_ios! are
191
+ utilized for testing. this was done to make it simple to wrap
192
+ daemon/servolux programs around main, althought not strictly required.
193
+ not the version bump - there is not reason to expect existing main
194
+ programs to break, but it *is* and interface change which requires a major
195
+ version bump.
196
+
189
197
  3.0.0
190
198
  - major refactor to support modes via module/extend vs. subclassing.
191
199
  MIGHT NOT be backward compatible, though no known issues thus far.
@@ -2,7 +2,7 @@ module Main
2
2
  #
3
3
  # top level constants
4
4
  #
5
- Main::VERSION = '3.0.3' unless
5
+ Main::VERSION = '4.0.0' unless
6
6
  defined? Main::VERSION
7
7
  def self.version() Main::VERSION end
8
8
 
@@ -44,4 +44,25 @@ module Main
44
44
  require libdir + 'mode'
45
45
  require libdir + 'program'
46
46
  require libdir + 'factories'
47
+
48
+ class << Main
49
+ def push_ios!
50
+ @ios ||= []
51
+ @ios.push({
52
+ :STDIN => STDIN.dup, :STDOUT => STDOUT.dup, :STDERR => STDERR.dup
53
+ })
54
+ end
55
+
56
+ def pop_ios!
57
+ @ios ||= []
58
+ (@ios.pop||{}).each do |const, dup|
59
+ dst = eval(const.to_s)
60
+ begin
61
+ dst.reopen(dup)
62
+ rescue
63
+ ::Object.const_set(const, dup)
64
+ end
65
+ end
66
+ end
67
+ end
47
68
  end
@@ -8,11 +8,14 @@ module Main
8
8
  end
9
9
 
10
10
  def Main.new(*args, &block)
11
- factory(&block).build(*args).new()
11
+ main_class = factory(&block).build(*args)
12
+ main_class.new()
12
13
  end
13
14
 
14
15
  def Main.run(*args, &block)
15
- new(*args, &block).run()
16
+ main_class = factory(&block).build(*args)
17
+ main = main_class.new()
18
+ main.run()
16
19
  end
17
20
  end
18
21
 
@@ -21,6 +24,5 @@ private
21
24
  def Main(*args, &block)
22
25
  Main.run(*args, &block)
23
26
  end
24
-
25
27
  alias_method 'main', 'Main'
26
28
  end
@@ -48,24 +48,25 @@ module Main
48
48
  opts = (args.shift || {}).to_hash.dup
49
49
 
50
50
  factory = self
51
+
51
52
  program = Class.new(Program)
52
- program.evaluate(&factory)
53
53
 
54
- program.module_eval do
55
- program.factory = factory
56
- program.argv = argv
57
- program.env = env
58
- program.opts = opts
54
+ program.factory = factory
55
+ program.argv = argv
56
+ program.env = env
57
+ program.opts = opts
59
58
 
59
+ program.module_eval(&factory)
60
+
61
+ program.module_eval do
60
62
  dynamically_extend_via_commandline_modes!
61
63
  program.set_default_options!
62
-
63
64
  define_method(:run, &block) if block
64
-
65
65
  wrap_run!
66
66
  end
67
67
  program
68
68
  end
69
+
69
70
  end
70
71
 
71
72
  def new()
@@ -41,7 +41,6 @@ module Main
41
41
  def before_initialize() :hook end
42
42
  def main_initialize()
43
43
  setup_finalizers
44
- setup_io_restoration
45
44
  setup_io_redirection
46
45
  setup_logging
47
46
  end
@@ -50,13 +49,14 @@ module Main
50
49
  def post_initialize() :hook end
51
50
 
52
51
  def setup_finalizers
53
- @finalizers = finalizers = []
52
+ @finalizers ||= []
54
53
  ObjectSpace.define_finalizer(self) do
55
54
  while((f = finalizers.pop)); f.call; end
56
55
  end
57
56
  end
58
57
 
59
58
  def finalize
59
+ @finalizers ||= []
60
60
  while((f = @finalizers.pop)); f.call; end
61
61
  end
62
62
 
@@ -76,39 +76,43 @@ module Main
76
76
  case log
77
77
  when ::Logger, Logger
78
78
  @logger = log
79
- when IO, StringIO
80
- @logger = Logger.new log
81
- @logger.level = logger_level
82
79
  else
83
80
  @logger = Logger.new(*log)
84
- @logger.level = logger_level
81
+ @logger.level = logger_level
85
82
  end
86
83
  end
87
84
  @logger
88
85
  end
89
86
 
90
87
  def setup_io_restoration
88
+ return
89
+ @finalizers ||= []
91
90
  [STDIN, STDOUT, STDERR].each do |io|
92
- dup = io.dup and @finalizers.push lambda{ io.reopen dup rescue nil }
91
+ dup = io.dup
92
+ @finalizers.push(
93
+ lambda do
94
+ io.reopen(dup)
95
+ end
96
+ )
93
97
  end
94
98
  end
95
-
99
+
96
100
  undef_method 'stdin='
97
101
  def stdin= io
98
102
  unless(defined?(@stdin) and (@stdin == io))
99
103
  @stdin =
100
- if io.respond_to? 'read'
104
+ if io.respond_to?('read')
101
105
  io
102
106
  else
103
- fd = open io.to_s, 'r+'
104
- @finalizers.push lambda{ fd.close }
107
+ fd = open(io.to_s, 'r+')
108
+ @finalizers.push(lambda{ fd.close })
105
109
  fd
106
110
  end
107
111
  begin
108
- STDIN.reopen @stdin
112
+ STDIN.reopen(@stdin)
109
113
  rescue
110
114
  $stdin = @stdin
111
- ::Object.const_set 'STDIN', @stdin
115
+ ::Object.const_set('STDIN', @stdin)
112
116
  end
113
117
  end
114
118
  end
@@ -117,14 +121,19 @@ module Main
117
121
  def stdout= io
118
122
  unless(defined?(@stdout) and (@stdout == io))
119
123
  @stdout =
120
- if io.respond_to? 'write'
124
+ if io.respond_to?('write')
121
125
  io
122
126
  else
123
- fd = open io.to_s, 'w+'
124
- @finalizers.push lambda{ fd.close }
127
+ fd = open(io.to_s, 'w+')
128
+ @finalizers.push(lambda{ fd.close })
125
129
  fd
126
130
  end
127
- STDOUT.reopen @stdout rescue($stdout = @stdout)
131
+ begin
132
+ STDOUT.reopen(@stdout)
133
+ rescue
134
+ $stdout = @stdout
135
+ ::Object.const_set('STDOUT', @stdout)
136
+ end
128
137
  end
129
138
  end
130
139
 
@@ -132,14 +141,19 @@ module Main
132
141
  def stderr= io
133
142
  unless(defined?(@stderr) and (@stderr == io))
134
143
  @stderr =
135
- if io.respond_to? 'write'
144
+ if io.respond_to?('write')
136
145
  io
137
146
  else
138
- fd = open io.to_s, 'w+'
139
- @finalizers.push lambda{ fd.close }
147
+ fd = open(io.to_s, 'w+')
148
+ @finalizers.push(lambda{ fd.close })
140
149
  fd
141
150
  end
142
- STDERR.reopen @stderr rescue($stderr = @stderr)
151
+ begin
152
+ STDERR.reopen(@stderr)
153
+ rescue
154
+ $stderr = @stderr
155
+ ::Object.const_set('STDERR', @stderr)
156
+ end
143
157
  end
144
158
  end
145
159
 
@@ -4,7 +4,7 @@
4
4
  Gem::Specification::new do |spec|
5
5
  spec.name = "main"
6
6
  spec.description = 'a class factory and dsl for generating command line programs real quick'
7
- spec.version = "3.0.3"
7
+ spec.version = "4.0.0"
8
8
  spec.platform = Gem::Platform::RUBY
9
9
  spec.summary = "main"
10
10
 
@@ -30,6 +30,8 @@ class T < Test::Unit::TestCase
30
30
  ENV.clear
31
31
  env.each{|k,v| ENV[k.to_s]=v.to_s}
32
32
 
33
+ Main.push_ios!
34
+
33
35
  test = self
34
36
 
35
37
  factory = Main.factory(&block)
@@ -58,6 +60,8 @@ class T < Test::Unit::TestCase
58
60
 
59
61
  test.status ||= main.exit_status
60
62
 
63
+ Main.pop_ios!
64
+
61
65
  main
62
66
  end
63
67
 
@@ -599,8 +603,8 @@ class T < Test::Unit::TestCase
599
603
  end
600
604
  }
601
605
  }
602
- assert test(?e, sout.path)
603
- assert IO.read(sout.path) == "42\n"
606
+ assert test(?e, sout.path), 'sout exists'
607
+ assert IO.read(sout.path) == "42\n", 'sout has correct output'
604
608
  end
605
609
  def test_0370
606
610
  m = nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: main
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.3
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ara T. Howard
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-12 00:00:00 -06:00
12
+ date: 2009-10-20 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency