Skip to content

Commit cdd192c

Browse files
drwlryanwjackson
authored andcommitted
Refactor Parser (#641)
Refactored Parser to isolate changes being made to ENV. This way we have an intermediate step where we know the environment variables being set.
1 parent 494697e commit cdd192c

File tree

2 files changed

+172
-158
lines changed

2 files changed

+172
-158
lines changed

lib/annotate/parser.rb

Lines changed: 170 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -3,224 +3,238 @@
33
module Annotate
44
# Class for handling command line arguments
55
class Parser # rubocop:disable Metrics/ClassLength
6-
def self.parse(args)
7-
new(args).parse
6+
def self.parse(args, env = {})
7+
new(args, env).parse
88
end
99

10-
attr_reader :args
10+
attr_reader :args, :options, :env
1111

1212
ANNOTATION_POSITIONS = %w[before top after bottom].freeze
1313
FILE_TYPE_POSITIONS = %w[position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer].freeze
1414
EXCLUSION_LIST = %w[tests fixtures factories serializers].freeze
1515
FORMAT_TYPES = %w[bare rdoc markdown].freeze
1616

17-
def initialize(args)
17+
def initialize(args, env)
1818
@args = args
19+
@options = default_options
20+
@env = env
1921
end
2022

2123
def parse
22-
options = default_options
24+
# To split up because right now this method parses and commits
25+
parser.parse!(args)
2326

24-
parser(options).parse!(args)
27+
commit
2528

2629
options
2730
end
2831

2932
private
3033

31-
def parser(options) # rubocop:disable Metrics/MethodLength
34+
def commit
35+
env.each_pair do |key, value|
36+
ENV[key] = value
37+
end
38+
end
39+
40+
def parser
41+
OptionParser.new do |option_parser|
42+
add_options_to_parser(option_parser)
43+
end
44+
end
45+
46+
def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength
3247
has_set_position = {}
3348
positions = ANNOTATION_POSITIONS
3449

35-
OptionParser.new do |opts|
36-
opts.banner = 'Usage: annotate [options] [model_file]*'
50+
option_parser.banner = 'Usage: annotate [options] [model_file]*'
3751

38-
opts.on('--additional_file_patterns path1,path2,path3', Array, "Additional file paths or globs to annotate") do |additional_file_patterns|
39-
ENV['additional_file_patterns'] = additional_file_patterns
40-
end
4152

42-
opts.on('-d', '--delete', 'Remove annotations from all model files or the routes.rb file') do
43-
options[:target_action] = :remove_annotations
44-
end
53+
option_parser.on('--additional_file_patterns path1,path2,path3', Array, "Additional file paths or globs to annotate") do |additional_file_patterns|
54+
ENV['additional_file_patterns'] = additional_file_patterns
55+
end
4556

46-
opts.on('-p', '--position [before|top|after|bottom]', positions,
47-
'Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)') do |p|
48-
ENV['position'] = p
57+
option_parser.on('-d', '--delete', 'Remove annotations from all model files or the routes.rb file') do
58+
@options[:target_action] = :remove_annotations
59+
end
4960

50-
FILE_TYPE_POSITIONS.each do |key|
51-
ENV[key] = p unless has_set_position[key]
52-
end
53-
end
61+
option_parser.on('-p', '--position [before|top|after|bottom]', positions,
62+
'Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)') do |p|
63+
env['position'] = p
5464

55-
opts.on('--pc', '--position-in-class [before|top|after|bottom]', positions,
56-
'Place the annotations at the top (before) or the bottom (after) of the model file') do |p|
57-
ENV['position_in_class'] = p
58-
has_set_position['position_in_class'] = true
65+
FILE_TYPE_POSITIONS.each do |key|
66+
env[key] = p unless has_set_position[key]
5967
end
68+
end
6069

61-
opts.on('--pf', '--position-in-factory [before|top|after|bottom]', positions,
62-
'Place the annotations at the top (before) or the bottom (after) of any factory files') do |p|
63-
ENV['position_in_factory'] = p
64-
has_set_position['position_in_factory'] = true
65-
end
70+
option_parser.on('--pc', '--position-in-class [before|top|after|bottom]', positions,
71+
'Place the annotations at the top (before) or the bottom (after) of the model file') do |p|
72+
env['position_in_class'] = p
73+
has_set_position['position_in_class'] = true
74+
end
6675

67-
opts.on('--px', '--position-in-fixture [before|top|after|bottom]', positions,
68-
'Place the annotations at the top (before) or the bottom (after) of any fixture files') do |p|
69-
ENV['position_in_fixture'] = p
70-
has_set_position['position_in_fixture'] = true
71-
end
76+
option_parser.on('--pf', '--position-in-factory [before|top|after|bottom]', positions,
77+
'Place the annotations at the top (before) or the bottom (after) of any factory files') do |p|
78+
env['position_in_factory'] = p
79+
has_set_position['position_in_factory'] = true
80+
end
7281

73-
opts.on('--pt', '--position-in-test [before|top|after|bottom]', positions,
74-
'Place the annotations at the top (before) or the bottom (after) of any test files') do |p|
75-
ENV['position_in_test'] = p
76-
has_set_position['position_in_test'] = true
77-
end
82+
option_parser.on('--px', '--position-in-fixture [before|top|after|bottom]', positions,
83+
'Place the annotations at the top (before) or the bottom (after) of any fixture files') do |p|
84+
env['position_in_fixture'] = p
85+
has_set_position['position_in_fixture'] = true
86+
end
7887

79-
opts.on('--pr', '--position-in-routes [before|top|after|bottom]', positions,
80-
'Place the annotations at the top (before) or the bottom (after) of the routes.rb file') do |p|
81-
ENV['position_in_routes'] = p
82-
has_set_position['position_in_routes'] = true
83-
end
88+
option_parser.on('--pt', '--position-in-test [before|top|after|bottom]', positions,
89+
'Place the annotations at the top (before) or the bottom (after) of any test files') do |p|
90+
env['position_in_test'] = p
91+
has_set_position['position_in_test'] = true
92+
end
8493

85-
opts.on('--ps', '--position-in-serializer [before|top|after|bottom]', positions,
86-
'Place the annotations at the top (before) or the bottom (after) of the serializer files') do |p|
87-
ENV['position_in_serializer'] = p
88-
has_set_position['position_in_serializer'] = true
89-
end
94+
option_parser.on('--pr', '--position-in-routes [before|top|after|bottom]', positions,
95+
'Place the annotations at the top (before) or the bottom (after) of the routes.rb file') do |p|
96+
env['position_in_routes'] = p
97+
has_set_position['position_in_routes'] = true
98+
end
9099

91-
opts.on('--w', '--wrapper STR', 'Wrap annotation with the text passed as parameter.',
92-
'If --w option is used, the same text will be used as opening and closing') do |p|
93-
ENV['wrapper'] = p
94-
end
100+
option_parser.on('--ps', '--position-in-serializer [before|top|after|bottom]', positions,
101+
'Place the annotations at the top (before) or the bottom (after) of the serializer files') do |p|
102+
env['position_in_serializer'] = p
103+
has_set_position['position_in_serializer'] = true
104+
end
95105

96-
opts.on('--wo', '--wrapper-open STR', 'Annotation wrapper opening.') do |p|
97-
ENV['wrapper_open'] = p
98-
end
106+
option_parser.on('--w', '--wrapper STR', 'Wrap annotation with the text passed as parameter.',
107+
'If --w option is used, the same text will be used as opening and closing') do |p|
108+
env['wrapper'] = p
109+
end
99110

100-
opts.on('--wc', '--wrapper-close STR', 'Annotation wrapper closing') do |p|
101-
ENV['wrapper_close'] = p
102-
end
111+
option_parser.on('--wo', '--wrapper-open STR', 'Annotation wrapper opening.') do |p|
112+
env['wrapper_open'] = p
113+
end
103114

104-
opts.on('-r', '--routes', "Annotate routes.rb with the output of 'rake routes'") do
105-
ENV['routes'] = 'true'
106-
end
115+
option_parser.on('--wc', '--wrapper-close STR', 'Annotation wrapper closing') do |p|
116+
env['wrapper_close'] = p
117+
end
107118

108-
opts.on('-a', '--active-admin', 'Annotate active_admin models') do
109-
ENV['active_admin'] = 'true'
110-
end
119+
option_parser.on('-r', '--routes', "Annotate routes.rb with the output of 'rake routes'") do
120+
env['routes'] = 'true'
121+
end
111122

112-
opts.on('-v', '--version', 'Show the current version of this gem') do
113-
puts "annotate v#{Annotate.version}"
114-
options[:exit] = true
115-
end
123+
option_parser.on('-a', '--active-admin', 'Annotate active_admin models') do
124+
env['active_admin'] = 'true'
125+
end
116126

117-
opts.on('-m', '--show-migration', 'Include the migration version number in the annotation') do
118-
ENV['include_version'] = 'yes'
119-
end
127+
option_parser.on('-v', '--version', 'Show the current version of this gem') do
128+
puts "annotate v#{Annotate.version}"
129+
@options[:exit] = true
130+
end
120131

121-
opts.on('-k', '--show-foreign-keys',
122-
"List the table's foreign key constraints in the annotation") do
123-
ENV['show_foreign_keys'] = 'yes'
124-
end
132+
option_parser.on('-m', '--show-migration', 'Include the migration version number in the annotation') do
133+
env['include_version'] = 'yes'
134+
end
125135

126-
opts.on('--ck',
127-
'--complete-foreign-keys', 'Complete foreign key names in the annotation') do
128-
ENV['show_foreign_keys'] = 'yes'
129-
ENV['show_complete_foreign_keys'] = 'yes'
130-
end
136+
option_parser.on('-k', '--show-foreign-keys',
137+
"List the table's foreign key constraints in the annotation") do
138+
env['show_foreign_keys'] = 'yes'
139+
end
131140

132-
opts.on('-i', '--show-indexes',
133-
"List the table's database indexes in the annotation") do
134-
ENV['show_indexes'] = 'yes'
135-
end
141+
option_parser.on('--ck',
142+
'--complete-foreign-keys', 'Complete foreign key names in the annotation') do
143+
env['show_foreign_keys'] = 'yes'
144+
env['show_complete_foreign_keys'] = 'yes'
145+
end
136146

137-
opts.on('-s', '--simple-indexes',
138-
"Concat the column's related indexes in the annotation") do
139-
ENV['simple_indexes'] = 'yes'
140-
end
147+
option_parser.on('-i', '--show-indexes',
148+
"List the table's database indexes in the annotation") do
149+
env['show_indexes'] = 'yes'
150+
end
141151

142-
opts.on('--model-dir dir',
143-
"Annotate model files stored in dir rather than app/models, separate multiple dirs with commas") do |dir|
144-
ENV['model_dir'] = dir
145-
end
152+
option_parser.on('-s', '--simple-indexes',
153+
"Concat the column's related indexes in the annotation") do
154+
env['simple_indexes'] = 'yes'
155+
end
146156

147-
opts.on('--root-dir dir',
148-
"Annotate files stored within root dir projects, separate multiple dirs with commas") do |dir|
149-
ENV['root_dir'] = dir
150-
end
157+
option_parser.on('--model-dir dir',
158+
"Annotate model files stored in dir rather than app/models, separate multiple dirs with commas") do |dir|
159+
env['model_dir'] = dir
160+
end
151161

152-
opts.on('--ignore-model-subdirects',
153-
"Ignore subdirectories of the models directory") do |_dir|
154-
ENV['ignore_model_sub_dir'] = 'yes'
155-
end
162+
option_parser.on('--root-dir dir',
163+
"Annotate files stored within root dir projects, separate multiple dirs with commas") do |dir|
164+
env['root_dir'] = dir
165+
end
156166

157-
opts.on('--sort',
158-
"Sort columns alphabetically, rather than in creation order") do |_dir|
159-
ENV['sort'] = 'yes'
160-
end
167+
option_parser.on('--ignore-model-subdirects',
168+
"Ignore subdirectories of the models directory") do |_dir|
169+
env['ignore_model_sub_dir'] = 'yes'
170+
end
161171

162-
opts.on('--classified-sort',
163-
"Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do |_dir|
164-
ENV['classified_sort'] = 'yes'
165-
end
172+
option_parser.on('--sort',
173+
"Sort columns alphabetically, rather than in creation order") do |_dir|
174+
env['sort'] = 'yes'
175+
end
166176

167-
opts.on('-R', '--require path',
168-
"Additional file to require before loading models, may be used multiple times") do |path|
169-
ENV['require'] = if !ENV['require'].blank?
170-
ENV['require'] + ",#{path}"
171-
else
172-
path
173-
end
174-
end
177+
option_parser.on('--classified-sort',
178+
"Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do |_dir|
179+
env['classified_sort'] = 'yes'
180+
end
175181

176-
opts.on('-e', '--exclude [tests,fixtures,factories,serializers]', Array, "Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions|
177-
exclusions ||= EXCLUSION_LIST
178-
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = 'yes' }
179-
end
182+
option_parser.on('-R', '--require path',
183+
"Additional file to require before loading models, may be used multiple times") do |path|
184+
env['require'] = if !env['require'].blank?
185+
env['require'] + ",#{path}"
186+
else
187+
path
188+
end
189+
end
180190

181-
opts.on('-f', '--format [bare|rdoc|markdown]', FORMAT_TYPES, 'Render Schema Infomation as plain/RDoc/Markdown') do |fmt|
182-
ENV["format_#{fmt}"] = 'yes'
183-
end
191+
option_parser.on('-e', '--exclude [tests,fixtures,factories,serializers]', Array, "Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions|
192+
exclusions ||= EXCLUSION_LIST
193+
exclusions.each { |exclusion| env["exclude_#{exclusion}"] = 'yes' }
194+
end
184195

185-
opts.on('--force', 'Force new annotations even if there are no changes.') do |_force|
186-
ENV['force'] = 'yes'
187-
end
196+
option_parser.on('-f', '--format [bare|rdoc|markdown]', FORMAT_TYPES, 'Render Schema Infomation as plain/RDoc/Markdown') do |fmt|
197+
env["format_#{fmt}"] = 'yes'
198+
end
188199

189-
opts.on('--frozen', 'Do not allow to change annotations. Exits non-zero if there are going to be changes to files.') do
190-
ENV['frozen'] = 'yes'
191-
end
200+
option_parser.on('--force', 'Force new annotations even if there are no changes.') do |_force|
201+
env['force'] = 'yes'
202+
end
192203

193-
opts.on('--timestamp', 'Include timestamp in (routes) annotation') do
194-
ENV['timestamp'] = 'true'
195-
end
204+
option_parser.on('--frozen', 'Do not allow to change annotations. Exits non-zero if there are going to be changes to files.') do
205+
env['frozen'] = 'yes'
206+
end
196207

197-
opts.on('--trace', 'If unable to annotate a file, print the full stack trace, not just the exception message.') do |_value|
198-
ENV['trace'] = 'yes'
199-
end
208+
option_parser.on('--timestamp', 'Include timestamp in (routes) annotation') do
209+
env['timestamp'] = 'true'
210+
end
200211

201-
opts.on('-I', '--ignore-columns REGEX', "don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`") do |regex|
202-
ENV['ignore_columns'] = regex
203-
end
212+
option_parser.on('--trace', 'If unable to annotate a file, print the full stack trace, not just the exception message.') do |_value|
213+
env['trace'] = 'yes'
214+
end
204215

205-
opts.on('--ignore-routes REGEX', "don't annotate routes that match a given REGEX (i.e., `annotate -I '(mobile|resque|pghero)'`") do |regex|
206-
ENV['ignore_routes'] = regex
207-
end
216+
option_parser.on('-I', '--ignore-columns REGEX', "don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`") do |regex|
217+
env['ignore_columns'] = regex
218+
end
208219

209-
opts.on('--hide-limit-column-types VALUES', "don't show limit for given column types, separated by commas (i.e., `integer,boolean,text`)") do |values|
210-
ENV['hide_limit_column_types'] = values.to_s
211-
end
220+
option_parser.on('--ignore-routes REGEX', "don't annotate routes that match a given REGEX (i.e., `annotate -I '(mobile|resque|pghero)'`") do |regex|
221+
env['ignore_routes'] = regex
222+
end
212223

213-
opts.on('--hide-default-column-types VALUES', "don't show default for given column types, separated by commas (i.e., `json,jsonb,hstore`)") do |values|
214-
ENV['hide_default_column_types'] = values.to_s
215-
end
224+
option_parser.on('--hide-limit-column-types VALUES', "don't show limit for given column types, separated by commas (i.e., `integer,boolean,text`)") do |values|
225+
env['hide_limit_column_types'] = values.to_s
226+
end
216227

217-
opts.on('--ignore-unknown-models', "don't display warnings for bad model files") do |_values|
218-
ENV['ignore_unknown_models'] = 'true'
219-
end
228+
option_parser.on('--hide-default-column-types VALUES', "don't show default for given column types, separated by commas (i.e., `json,jsonb,hstore`)") do |values|
229+
env['hide_default_column_types'] = values.to_s
230+
end
220231

221-
opts.on('--with-comment', "include database comments in model annotations") do |_values|
222-
ENV['with_comment'] = 'true'
223-
end
232+
option_parser.on('--ignore-unknown-models', "don't display warnings for bad model files") do |_values|
233+
env['ignore_unknown_models'] = 'true'
234+
end
235+
236+
option_parser.on('--with-comment', "include database comments in model annotations") do |_values|
237+
env['with_comment'] = 'true'
224238
end
225239
end
226240

0 commit comments

Comments
 (0)