Skip to content

Commit e531293

Browse files
committed
Copy parser options into Parser
1 parent 5ac56e4 commit e531293

File tree

3 files changed

+742
-0
lines changed

3 files changed

+742
-0
lines changed

lib/parser.rb

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
require 'optparse'
2+
3+
# Class for handling command line arguments
4+
class Parser # rubocop:disable Metrics/ClassLength
5+
def self.parse(args)
6+
new(args).parse
7+
end
8+
9+
attr_reader :args
10+
11+
ANNOTATION_POSITIONS = %w[before top after bottom].freeze
12+
FILE_TYPE_POSITIONS = %w[position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer].freeze
13+
EXCLUSION_LIST = %w[tests fixtures factories serializers].freeze
14+
FORMAT_TYPES = %w[bare rdoc markdown].freeze
15+
16+
def initialize(args)
17+
@args = args
18+
end
19+
20+
def parse
21+
options = default_options
22+
23+
parser(options).parse!(args)
24+
25+
options
26+
end
27+
28+
private
29+
30+
def parser(options) # rubocop:disable Metrics/MethodLength
31+
has_set_position = {}
32+
positions = ANNOTATION_POSITIONS
33+
34+
OptionParser.new do |opts|
35+
opts.banner = 'Usage: annotate [options] [model_file]*'
36+
37+
opts.on('-d', '--delete', 'Remove annotations from all model files or the routes.rb file') do
38+
options[:target_action] = :remove_annotations
39+
end
40+
41+
opts.on('-p', '--position [before|top|after|bottom]', positions,
42+
'Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)') do |p|
43+
ENV['position'] = p
44+
45+
FILE_TYPE_POSITIONS.each do |key|
46+
ENV[key] = p unless has_set_position[key]
47+
end
48+
end
49+
50+
opts.on('--pc', '--position-in-class [before|top|after|bottom]', positions,
51+
'Place the annotations at the top (before) or the bottom (after) of the model file') do |p|
52+
ENV['position_in_class'] = p
53+
has_set_position['position_in_class'] = true
54+
end
55+
56+
opts.on('--pf', '--position-in-factory [before|top|after|bottom]', positions,
57+
'Place the annotations at the top (before) or the bottom (after) of any factory files') do |p|
58+
ENV['position_in_factory'] = p
59+
has_set_position['position_in_factory'] = true
60+
end
61+
62+
opts.on('--px', '--position-in-fixture [before|top|after|bottom]', positions,
63+
'Place the annotations at the top (before) or the bottom (after) of any fixture files') do |p|
64+
ENV['position_in_fixture'] = p
65+
has_set_position['position_in_fixture'] = true
66+
end
67+
68+
opts.on('--pt', '--position-in-test [before|top|after|bottom]', positions,
69+
'Place the annotations at the top (before) or the bottom (after) of any test files') do |p|
70+
ENV['position_in_test'] = p
71+
has_set_position['position_in_test'] = true
72+
end
73+
74+
opts.on('--pr', '--position-in-routes [before|top|after|bottom]', positions,
75+
'Place the annotations at the top (before) or the bottom (after) of the routes.rb file') do |p|
76+
ENV['position_in_routes'] = p
77+
has_set_position['position_in_routes'] = true
78+
end
79+
80+
opts.on('--ps', '--position-in-serializer [before|top|after|bottom]', positions,
81+
'Place the annotations at the top (before) or the bottom (after) of the serializer files') do |p|
82+
ENV['position_in_serializer'] = p
83+
has_set_position['position_in_serializer'] = true
84+
end
85+
86+
opts.on('--w', '--wrapper STR', 'Wrap annotation with the text passed as parameter.',
87+
'If --w option is used, the same text will be used as opening and closing') do |p|
88+
ENV['wrapper'] = p
89+
end
90+
91+
opts.on('--wo', '--wrapper-open STR', 'Annotation wrapper opening.') do |p|
92+
ENV['wrapper_open'] = p
93+
end
94+
95+
opts.on('--wc', '--wrapper-close STR', 'Annotation wrapper closing') do |p|
96+
ENV['wrapper_close'] = p
97+
end
98+
99+
opts.on('-r', '--routes', "Annotate routes.rb with the output of 'rake routes'") do
100+
ENV['routes'] = 'true'
101+
end
102+
103+
opts.on('-a', '--active-admin', 'Annotate active_admin models') do
104+
ENV['active_admin'] = 'true'
105+
end
106+
107+
opts.on('-v', '--version', 'Show the current version of this gem') do
108+
puts "annotate v#{Annotate.version}"
109+
end
110+
111+
opts.on('-m', '--show-migration', 'Include the migration version number in the annotation') do
112+
ENV['include_version'] = 'yes'
113+
end
114+
115+
opts.on('-k', '--show-foreign-keys',
116+
"List the table's foreign key constraints in the annotation") do
117+
ENV['show_foreign_keys'] = 'yes'
118+
end
119+
120+
opts.on('--ck',
121+
'--complete-foreign-keys', 'Complete foreign key names in the annotation') do
122+
ENV['show_foreign_keys'] = 'yes'
123+
ENV['show_complete_foreign_keys'] = 'yes'
124+
end
125+
126+
opts.on('-i', '--show-indexes',
127+
"List the table's database indexes in the annotation") do
128+
ENV['show_indexes'] = 'yes'
129+
end
130+
131+
opts.on('-s', '--simple-indexes',
132+
"Concat the column's related indexes in the annotation") do
133+
ENV['simple_indexes'] = 'yes'
134+
end
135+
136+
opts.on('--model-dir dir',
137+
"Annotate model files stored in dir rather than app/models, separate multiple dirs with commas") do |dir|
138+
ENV['model_dir'] = dir
139+
end
140+
141+
opts.on('--root-dir dir',
142+
"Annotate files stored within root dir projects, separate multiple dirs with commas") do |dir|
143+
ENV['root_dir'] = dir
144+
end
145+
146+
opts.on('--ignore-model-subdirects',
147+
"Ignore subdirectories of the models directory") do |_dir|
148+
ENV['ignore_model_sub_dir'] = 'yes'
149+
end
150+
151+
opts.on('--sort',
152+
"Sort columns alphabetically, rather than in creation order") do |_dir|
153+
ENV['sort'] = 'yes'
154+
end
155+
156+
opts.on('--classified-sort',
157+
"Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do |_dir|
158+
ENV['classified_sort'] = 'yes'
159+
end
160+
161+
opts.on('-R', '--require path',
162+
"Additional file to require before loading models, may be used multiple times") do |path|
163+
ENV['require'] = if !ENV['require'].blank?
164+
ENV['require'] + ",#{path}"
165+
else
166+
path
167+
end
168+
end
169+
170+
opts.on('-e', '--exclude [tests,fixtures,factories,serializers]', Array, "Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions|
171+
exclusions ||= EXCLUSION_LIST
172+
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = 'yes' }
173+
end
174+
175+
opts.on('-f', '--format [bare|rdoc|markdown]', FORMAT_TYPES, 'Render Schema Infomation as plain/RDoc/Markdown') do |fmt|
176+
ENV["format_#{fmt}"] = 'yes'
177+
end
178+
179+
opts.on('--force', 'Force new annotations even if there are no changes.') do |_force|
180+
ENV['force'] = 'yes'
181+
end
182+
183+
opts.on('--frozen', 'Do not allow to change annotations. Exits non-zero if there are going to be changes to files.') do
184+
ENV['frozen'] = 'yes'
185+
end
186+
187+
opts.on('--timestamp', 'Include timestamp in (routes) annotation') do
188+
ENV['timestamp'] = 'true'
189+
end
190+
191+
opts.on('--trace', 'If unable to annotate a file, print the full stack trace, not just the exception message.') do |_value|
192+
ENV['trace'] = 'yes'
193+
end
194+
195+
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|
196+
ENV['ignore_columns'] = regex
197+
end
198+
199+
opts.on('--ignore-routes REGEX', "don't annotate routes that match a given REGEX (i.e., `annotate -I '(mobile|resque|pghero)'`") do |regex|
200+
ENV['ignore_routes'] = regex
201+
end
202+
203+
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|
204+
ENV['hide_limit_column_types'] = values.to_s
205+
end
206+
207+
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|
208+
ENV['hide_default_column_types'] = values.to_s
209+
end
210+
211+
opts.on('--ignore-unknown-models', "don't display warnings for bad model files") do |_values|
212+
ENV['ignore_unknown_models'] = 'true'
213+
end
214+
215+
opts.on('--with-comment', "include database comments in model annotations") do |_values|
216+
ENV['with_comment'] = 'true'
217+
end
218+
end
219+
end
220+
221+
def default_options
222+
{
223+
target_action: :do_annotations
224+
}
225+
end
226+
end

0 commit comments

Comments
 (0)