bahuvrihi-tap 0.10.2 → 0.10.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,182 +0,0 @@
1
- require 'tap/support/command_line'
2
-
3
- module Tap
4
- module Support
5
-
6
- # FrameworkClass encapsulates class methods related to Framework.
7
- module FrameworkClass
8
-
9
- # Returns the default name for the class: to_s.underscore
10
- attr_accessor :default_name
11
-
12
- def self.extended(base)
13
- caller.each do |line|
14
- case line
15
- when /\/framework.rb/ then next
16
- when Lazydoc::CALLER_REGEXP
17
- base.instance_variable_set(:@source_file, File.expand_path($1))
18
- break
19
- end
20
- end
21
-
22
- base.instance_variable_set(:@default_name, base.to_s.underscore)
23
- end
24
-
25
- def inherited(child)
26
- unless child.instance_variable_defined?(:@source_file)
27
- caller.first =~ Lazydoc::CALLER_REGEXP
28
- child.instance_variable_set(:@source_file, File.expand_path($1))
29
- end
30
-
31
- child.instance_variable_set(:@default_name, child.to_s.underscore)
32
- super
33
- end
34
-
35
- def subclass(const_name, configs={}, options={}, &block)
36
- # Generate the nesting module
37
- current, constants = const_name.to_s.constants_split
38
- raise ArgumentError, "#{current} is already defined!" if constants.empty?
39
-
40
- subclass_const = constants.pop
41
- constants.each {|const| current = current.const_set(const, Module.new)}
42
-
43
- # Generate the subclass
44
- subclass = Class.new(self)
45
- configs = configs[0] if configs.kind_of?(Array) && configs.length == 1 && configs[0].kind_of?(Hash)
46
-
47
- case configs
48
- when Hash
49
- subclass.send(:attr_accessor, *configs.keys)
50
- configs.each_pair do |key, value|
51
- subclass.configurations.add(key, value)
52
- end
53
- when Array
54
- configs.each do |method, key, value, opts, config_block|
55
- subclass.send(method, key, value, opts, &config_block)
56
- end
57
- end
58
-
59
- block_method = options[:block_method] || :process
60
- subclass.send(:define_method, block_method, &block)
61
- subclass.default_name = const_name
62
-
63
- const_name = current == Object ? subclass_const : "#{current}::#{subclass_const}"
64
- caller.each_with_index do |line, index|
65
- case line
66
- when /\/tap\/support\/declarations.rb/ then next
67
- when Lazydoc::CALLER_REGEXP
68
- subclass.source_file = File.expand_path($1)
69
- lzd = subclass.lazydoc(false)
70
- lzd[const_name, false]['manifest'] = lzd.register($3.to_i - 1)
71
- break
72
- end
73
- end
74
-
75
- arity = options[:arity] || block.arity
76
- comment = Comment.new
77
- comment.subject = case
78
- when arity > 0
79
- Array.new(arity, "INPUT").join(' ')
80
- when arity < 0
81
- array = Array.new(-1 * arity - 1, "INPUT")
82
- array << "INPUTS..."
83
- array.join(' ')
84
- else ""
85
- end
86
- subclass.lazydoc(false)[const_name, false]['args'] ||= comment
87
-
88
- # Set the subclass constant
89
- current.const_set(subclass_const, subclass)
90
- end
91
-
92
- DEFAULT_HELP_TEMPLATE = %Q{<% manifest = task_class.manifest %>
93
- <%= task_class %><%= manifest.subject.to_s.strip.empty? ? '' : ' -- ' %><%= manifest.subject %>
94
-
95
- <% unless manifest.empty? %>
96
- <%= '-' * 80 %>
97
-
98
- <% manifest.wrap(77, 2, nil).each do |line| %>
99
- <%= line %>
100
- <% end %>
101
- <%= '-' * 80 %>
102
- <% end %>
103
-
104
- }
105
-
106
- def instantiate(argv, app=Tap::App.instance) # => instance, argv
107
- opts = OptionParser.new
108
-
109
- # Add configurations
110
- config = {}
111
- unless configurations.empty?
112
- opts.separator ""
113
- opts.separator "configurations:"
114
- end
115
-
116
- configurations.each do |receiver, key, configuration|
117
- opts.on(*CommandLine.configv(configuration)) do |value|
118
- config[key] = value
119
- end
120
- end
121
-
122
- # Add options on_tail, giving priority to configurations
123
- opts.separator ""
124
- opts.separator "options:"
125
-
126
- opts.on_tail("-h", "--help", "Print this help") do
127
- opts.banner = "#{help}usage: tap run -- #{to_s.underscore} #{args.subject}"
128
- puts opts
129
- exit
130
- end
131
-
132
- # Add option for name
133
- name = default_name
134
- opts.on_tail('--name NAME', /^[^-].*/, 'Specify a name') do |value|
135
- name = value
136
- end
137
-
138
- # Add option to add args
139
- use_args = []
140
- opts.on_tail('--use FILE', /^[^-].*/, 'Loads inputs from file') do |value|
141
- obj = YAML.load_file(value)
142
- case obj
143
- when Hash
144
- obj.values.each do |array|
145
- # error if value isn't an array
146
- use_args.concat(array)
147
- end
148
- when Array
149
- use_args.concat(obj)
150
- else
151
- use_args << obj
152
- end
153
- end
154
-
155
- opts.parse!(argv)
156
- obj = new({}, name, app)
157
-
158
- path_configs = load_config(app.config_filepath(name))
159
- if path_configs.kind_of?(Array)
160
- path_configs.each_with_index do |path_config, i|
161
- obj.initialize_batch_obj(path_config, "#{name}_#{i}") unless i == 0
162
- end
163
- path_configs = path_configs[0]
164
- end
165
-
166
- argv = (argv + use_args).collect {|str| str =~ /\A---\s*\n/ ? YAML.load(str) : str }
167
-
168
- [obj.reconfigure(path_configs).reconfigure(config), argv]
169
- end
170
-
171
- def lazydoc(resolve=true)
172
- lazydoc = super(false)
173
- lazydoc.register_method_pattern('args', :process) unless lazydoc.resolved?
174
- super
175
- end
176
-
177
- def help
178
- Tap::Support::Templater.new(DEFAULT_HELP_TEMPLATE, :task_class => self).build
179
- end
180
- end
181
- end
182
- end