1
1
require 'optparse'
2
2
3
3
# Class for handling command line arguments
4
- class Parser
4
+ class Parser # rubocop:disable Metrics/ClassLength
5
5
def self . parse ( args )
6
6
new ( args ) . parse
7
7
end
@@ -10,6 +10,8 @@ def self.parse(args)
10
10
11
11
ANNOTATION_POSITIONS = %w[ before top after bottom ] . freeze
12
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
13
15
14
16
def initialize ( args )
15
17
@args = args
@@ -25,7 +27,7 @@ def parse
25
27
26
28
private
27
29
28
- def parser ( options )
30
+ def parser ( options ) # rubocop:disable Metrics/MethodLength
29
31
has_set_position = { }
30
32
positions = ANNOTATION_POSITIONS
31
33
@@ -93,6 +95,126 @@ def parser(options)
93
95
opts . on ( '--wc' , '--wrapper-close STR' , 'Annotation wrapper closing' ) do |p |
94
96
ENV [ 'wrapper_close' ] = p
95
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
96
218
end
97
219
end
98
220
0 commit comments