annotate 2.7.0 → 2.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/AUTHORS.rdoc +1 -0
- data/CHANGELOG.rdoc +3 -0
- data/README.rdoc +9 -5
- data/annotate.gemspec +2 -2
- data/bin/annotate +47 -44
- data/lib/annotate.rb +59 -45
- data/lib/annotate/active_record_patch.rb +1 -1
- data/lib/annotate/annotate_models.rb +212 -156
- data/lib/annotate/annotate_routes.rb +94 -80
- data/lib/annotate/version.rb +1 -1
- data/lib/generators/annotate/install_generator.rb +2 -3
- data/lib/generators/annotate/templates/auto_annotate_models.rake +6 -5
- data/lib/tasks/annotate_models.rake +12 -10
- data/lib/tasks/annotate_routes.rake +1 -0
- metadata +11 -5
@@ -18,139 +18,153 @@
|
|
18
18
|
# Released under the same license as Ruby. No Support. No Warranty.
|
19
19
|
#
|
20
20
|
module AnnotateRoutes
|
21
|
-
PREFIX =
|
21
|
+
PREFIX = '# == Route Map'
|
22
22
|
|
23
23
|
def self.do_annotations(options={})
|
24
|
-
return unless
|
24
|
+
return unless routes_exists?
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
routes_map = `rake routes`.split(/\n/, -1)
|
29
|
-
|
30
|
-
# In old versions of Rake, the first line of output was the cwd. Not so
|
31
|
-
# much in newer ones. We ditch that line if it exists, and if not, we
|
32
|
-
# keep the line around.
|
33
|
-
routes_map.shift if(routes_map.first =~ /^\(in \//)
|
26
|
+
routes_map = AnnotateRoutes.app_routes_map(options)
|
34
27
|
|
35
28
|
header = [
|
36
|
-
|
37
|
-
"#"
|
29
|
+
"#{PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : ''), '#'
|
38
30
|
] + routes_map.map { |line| "# #{line}".rstrip }
|
39
31
|
|
40
32
|
existing_text = File.read(routes_file)
|
41
|
-
(content, where_header_found) = strip_annotations(existing_text)
|
42
|
-
changed = where_header_found != 0 # This will either be :before, :after, or
|
43
|
-
# a number. If the number is > 0, the
|
44
|
-
# annotation was found somewhere in the
|
45
|
-
# middle of the file. If the number is
|
46
|
-
# zero, no annotation was found.
|
47
|
-
|
48
|
-
if(position_after)
|
49
|
-
# Ensure we have adequate trailing newlines at the end of the file to
|
50
|
-
# ensure a blank line separating the content from the annotation.
|
51
|
-
content << '' if(content.last != '')
|
52
33
|
|
53
|
-
|
54
|
-
# the spacer we put in the first time around.
|
55
|
-
if(changed && where_header_found == :before)
|
56
|
-
content.shift if(content.first == '')
|
57
|
-
end
|
58
|
-
else
|
59
|
-
header = header << '' if(content.first != '' || changed)
|
60
|
-
end
|
61
|
-
|
62
|
-
content = position_after ? (content + header) : header + content
|
63
|
-
|
64
|
-
if write_contents(existing_text, content)
|
34
|
+
if write_contents(existing_text, header, options)
|
65
35
|
puts "#{routes_file} annotated."
|
66
|
-
else
|
67
|
-
puts "#{routes_file} unchanged."
|
68
36
|
end
|
69
37
|
end
|
70
38
|
|
71
39
|
def self.remove_annotations(options={})
|
72
|
-
return unless
|
40
|
+
return unless routes_exists?
|
73
41
|
existing_text = File.read(routes_file)
|
74
|
-
|
42
|
+
content, where_header_found = strip_annotations(existing_text)
|
75
43
|
|
76
44
|
content = strip_on_removal(content, where_header_found)
|
77
45
|
|
78
|
-
if write_contents(existing_text, content)
|
46
|
+
if write_contents(existing_text, content, options)
|
79
47
|
puts "Removed annotations from #{routes_file}."
|
80
|
-
else
|
81
|
-
puts "#{routes_file} unchanged."
|
82
48
|
end
|
83
49
|
end
|
84
50
|
|
85
|
-
|
51
|
+
private
|
52
|
+
|
53
|
+
def self.app_routes_map(options)
|
54
|
+
routes_map = `rake routes`.split(/\n/, -1)
|
55
|
+
|
56
|
+
# In old versions of Rake, the first line of output was the cwd. Not so
|
57
|
+
# much in newer ones. We ditch that line if it exists, and if not, we
|
58
|
+
# keep the line around.
|
59
|
+
routes_map.shift if routes_map.first =~ /^\(in \//
|
60
|
+
|
61
|
+
# Skip routes which match given regex
|
62
|
+
# Note: it matches the complete line (route_name, path, controller/action)
|
63
|
+
if options[:ignore_routes]
|
64
|
+
routes_map.reject! { |line| line =~ /#{options[:ignore_routes]}/ }
|
65
|
+
end
|
66
|
+
|
67
|
+
routes_map
|
68
|
+
end
|
86
69
|
|
87
70
|
def self.routes_file
|
88
|
-
@routes_rb ||= File.join(
|
71
|
+
@routes_rb ||= File.join('config', 'routes.rb')
|
89
72
|
end
|
90
73
|
|
91
74
|
def self.routes_exists?
|
92
75
|
routes_exists = File.exists?(routes_file)
|
93
|
-
puts "Can
|
94
|
-
|
76
|
+
puts "Can't find routes.rb" unless routes_exists
|
77
|
+
|
78
|
+
routes_exists
|
95
79
|
end
|
96
80
|
|
97
|
-
def self.write_contents(existing_text,
|
81
|
+
def self.write_contents(existing_text, header, options = {})
|
82
|
+
content, where_header_found = strip_annotations(existing_text)
|
83
|
+
new_content = annotate_routes(header, content, where_header_found, options)
|
84
|
+
|
98
85
|
# Make sure we end on a trailing newline.
|
99
|
-
new_content << '' unless
|
86
|
+
new_content << '' unless new_content.last == ''
|
100
87
|
new_text = new_content.join("\n")
|
101
88
|
|
102
|
-
|
89
|
+
if existing_text == new_text
|
90
|
+
puts "#{routes_file} unchanged."
|
91
|
+
false
|
92
|
+
else
|
93
|
+
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
|
94
|
+
true
|
95
|
+
end
|
96
|
+
end
|
103
97
|
|
104
|
-
|
105
|
-
|
98
|
+
def self.annotate_routes(header, content, where_header_found, options = {})
|
99
|
+
if %w(before top).include?(options[:position_in_routes])
|
100
|
+
header = header << '' if content.first != ''
|
101
|
+
new_content = header + content
|
102
|
+
else
|
103
|
+
# Ensure we have adequate trailing newlines at the end of the file to
|
104
|
+
# ensure a blank line separating the content from the annotation.
|
105
|
+
content << '' unless content.last == ''
|
106
|
+
|
107
|
+
# We're moving something from the top of the file to the bottom, so ditch
|
108
|
+
# the spacer we put in the first time around.
|
109
|
+
content.shift if where_header_found == :before && content.first == ''
|
110
|
+
|
111
|
+
new_content = content + header
|
112
|
+
end
|
113
|
+
|
114
|
+
new_content
|
106
115
|
end
|
107
116
|
|
117
|
+
# TODO: write the method doc using ruby rdoc formats
|
118
|
+
# where_header_found => This will either be :before, :after, or
|
119
|
+
# a number. If the number is > 0, the
|
120
|
+
# annotation was found somewhere in the
|
121
|
+
# middle of the file. If the number is
|
122
|
+
# zero, no annotation was found.
|
108
123
|
def self.strip_annotations(content)
|
109
124
|
real_content = []
|
110
125
|
mode = :content
|
111
|
-
line_number = 0
|
112
126
|
header_found_at = 0
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
mode = :header
|
125
|
-
else
|
126
|
-
real_content << line
|
127
|
-
end
|
127
|
+
|
128
|
+
content.split(/\n/, -1).each_with_index do |line, line_number|
|
129
|
+
if mode == :header && line !~ /\s*#/
|
130
|
+
mode = :content
|
131
|
+
next unless line == ''
|
132
|
+
elsif mode == :content
|
133
|
+
if line =~ /^\s*#\s*== Route.*$/
|
134
|
+
header_found_at = line_number + 1 # index start's at 0
|
135
|
+
mode = :header
|
136
|
+
else
|
137
|
+
real_content << line
|
128
138
|
end
|
129
|
-
rescue
|
130
|
-
retry
|
131
139
|
end
|
132
140
|
end
|
133
|
-
content_lines = real_content.count
|
134
141
|
|
135
|
-
|
136
|
-
|
142
|
+
where_header_found(real_content, header_found_at)
|
143
|
+
end
|
144
|
+
|
145
|
+
def self.where_header_found(real_content, header_found_at)
|
146
|
+
# By default assume the annotation was found in the middle of the file
|
147
|
+
|
137
148
|
# ... unless we have evidence it was at the beginning ...
|
138
|
-
|
149
|
+
return real_content, :before if header_found_at == 1
|
150
|
+
|
139
151
|
# ... or that it was at the end.
|
140
|
-
|
152
|
+
return real_content, :after if header_found_at >= real_content.count
|
141
153
|
|
142
|
-
|
154
|
+
# and the default
|
155
|
+
return real_content, header_found_at
|
143
156
|
end
|
144
157
|
|
145
158
|
def self.strip_on_removal(content, where_header_found)
|
146
|
-
if
|
147
|
-
content.shift while
|
148
|
-
elsif
|
149
|
-
content.pop while
|
159
|
+
if where_header_found == :before
|
160
|
+
content.shift while content.first == ''
|
161
|
+
elsif where_header_found == :after
|
162
|
+
content.pop while content.last == ''
|
150
163
|
end
|
164
|
+
|
151
165
|
# TODO: If the user buried it in the middle, we should probably see about
|
152
166
|
# TODO: preserving a single line of space between the content above and
|
153
167
|
# TODO: below...
|
154
|
-
|
168
|
+
content
|
155
169
|
end
|
156
170
|
end
|
data/lib/annotate/version.rb
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
module Annotate
|
2
2
|
module Generators
|
3
3
|
class InstallGenerator < Rails::Generators::Base
|
4
|
-
desc
|
4
|
+
desc 'Copy annotate_models rakefiles for automatic annotation'
|
5
5
|
source_root File.expand_path('../templates', __FILE__)
|
6
6
|
|
7
7
|
# copy rake tasks
|
8
8
|
def copy_tasks
|
9
|
-
template
|
9
|
+
template 'auto_annotate_models.rake', 'lib/tasks/auto_annotate_models.rake'
|
10
10
|
end
|
11
|
-
|
12
11
|
end
|
13
12
|
end
|
14
13
|
end
|
@@ -24,13 +24,14 @@ if Rails.env.development?
|
|
24
24
|
'exclude_fixtures' => 'false',
|
25
25
|
'exclude_factories' => 'false',
|
26
26
|
'exclude_serializers' => 'false',
|
27
|
-
'exclude_scaffolds' => '
|
28
|
-
'exclude_controllers' => '
|
29
|
-
'exclude_helpers' => '
|
27
|
+
'exclude_scaffolds' => 'true',
|
28
|
+
'exclude_controllers' => 'true',
|
29
|
+
'exclude_helpers' => 'true',
|
30
30
|
'ignore_model_sub_dir' => 'false',
|
31
31
|
'ignore_columns' => nil,
|
32
|
+
'ignore_routes' => nil,
|
32
33
|
'ignore_unknown_models' => 'false',
|
33
|
-
'hide_limit_column_types' => '<%= AnnotateModels::NO_LIMIT_COL_TYPES.join(
|
34
|
+
'hide_limit_column_types' => '<%= AnnotateModels::NO_LIMIT_COL_TYPES.join(",") %>',
|
34
35
|
'skip_on_db_migrate' => 'false',
|
35
36
|
'format_bare' => 'true',
|
36
37
|
'format_rdoc' => 'false',
|
@@ -39,7 +40,7 @@ if Rails.env.development?
|
|
39
40
|
'force' => 'false',
|
40
41
|
'trace' => 'false',
|
41
42
|
'wrapper_open' => nil,
|
42
|
-
'wrapper_close' => nil
|
43
|
+
'wrapper_close' => nil
|
43
44
|
)
|
44
45
|
end
|
45
46
|
|
@@ -1,16 +1,16 @@
|
|
1
1
|
annotate_lib = File.expand_path(File.dirname(File.dirname(__FILE__)))
|
2
2
|
|
3
|
-
|
3
|
+
unless ENV['is_cli']
|
4
4
|
task :set_annotation_options
|
5
|
-
task :
|
5
|
+
task annotate_models: :set_annotation_options
|
6
6
|
end
|
7
7
|
|
8
|
-
desc
|
9
|
-
task :
|
8
|
+
desc 'Add schema information (as comments) to model and fixture files'
|
9
|
+
task annotate_models: :environment do
|
10
10
|
require "#{annotate_lib}/annotate/annotate_models"
|
11
11
|
require "#{annotate_lib}/annotate/active_record_patch"
|
12
12
|
|
13
|
-
options={
|
13
|
+
options={is_rake: true}
|
14
14
|
ENV['position'] = options[:position] = Annotate.fallback(ENV['position'], 'before')
|
15
15
|
options[:position_in_class] = Annotate.fallback(ENV['position_in_class'], ENV['position'])
|
16
16
|
options[:position_in_fixture] = Annotate.fallback(ENV['position_in_fixture'], ENV['position'])
|
@@ -29,8 +29,8 @@ task :annotate_models => :environment do
|
|
29
29
|
options[:exclude_fixtures] = Annotate.true?(ENV['exclude_fixtures'])
|
30
30
|
options[:exclude_serializers] = Annotate.true?(ENV['exclude_serializers'])
|
31
31
|
options[:exclude_scaffolds] = Annotate.true?(ENV['exclude_scaffolds'])
|
32
|
-
options[:exclude_controllers] = Annotate.true?(ENV
|
33
|
-
options[:exclude_helpers] = Annotate.true?(ENV
|
32
|
+
options[:exclude_controllers] = Annotate.true?(ENV.fetch('exclude_controllers', 'true'))
|
33
|
+
options[:exclude_helpers] = Annotate.true?(ENV.fetch('exclude_helpers', 'true'))
|
34
34
|
options[:ignore_model_sub_dir] = Annotate.true?(ENV['ignore_model_sub_dir'])
|
35
35
|
options[:format_bare] = Annotate.true?(ENV['format_bare'])
|
36
36
|
options[:format_rdoc] = Annotate.true?(ENV['format_rdoc'])
|
@@ -42,16 +42,18 @@ task :annotate_models => :environment do
|
|
42
42
|
options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper'])
|
43
43
|
options[:wrapper_close] = Annotate.fallback(ENV['wrapper_close'], ENV['wrapper'])
|
44
44
|
options[:ignore_columns] = ENV.fetch('ignore_columns', nil)
|
45
|
+
options[:ignore_routes] = ENV.fetch('ignore_routes', nil)
|
46
|
+
options[:hide_limit_column_types] = Annotate.fallback(ENV['hide_limit_column_types'], '')
|
45
47
|
|
46
48
|
AnnotateModels.do_annotations(options)
|
47
49
|
end
|
48
50
|
|
49
|
-
desc
|
50
|
-
task :
|
51
|
+
desc 'Remove schema information from model and fixture files'
|
52
|
+
task remove_annotation: :environment do
|
51
53
|
require "#{annotate_lib}/annotate/annotate_models"
|
52
54
|
require "#{annotate_lib}/annotate/active_record_patch"
|
53
55
|
|
54
|
-
options={
|
56
|
+
options={is_rake: true}
|
55
57
|
options[:model_dir] = ENV['model_dir']
|
56
58
|
options[:root_dir] = ENV['root_dir']
|
57
59
|
options[:require] = ENV['require'] ? ENV['require'].split(',') : []
|
@@ -6,6 +6,7 @@ task :annotate_routes => :environment do
|
|
6
6
|
options={}
|
7
7
|
ENV['position'] = options[:position] = Annotate.fallback(ENV['position'], 'before')
|
8
8
|
options[:position_in_routes] = Annotate.fallback(ENV['position_in_routes'], ENV['position'])
|
9
|
+
options[:ignore_routes] = Annotate.fallback(ENV['ignore_routes'], nil)
|
9
10
|
options[:require] = ENV['require'] ? ENV['require'].split(',') : []
|
10
11
|
AnnotateRoutes.do_annotations(options)
|
11
12
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: annotate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.7.
|
4
|
+
version: 2.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Chaffee
|
@@ -12,22 +12,28 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date:
|
15
|
+
date: 2016-05-09 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: rake
|
19
19
|
requirement: !ruby/object:Gem::Requirement
|
20
20
|
requirements:
|
21
|
-
- - "
|
21
|
+
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: '10.4'
|
24
|
+
- - "<"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '12.0'
|
24
27
|
type: :runtime
|
25
28
|
prerelease: false
|
26
29
|
version_requirements: !ruby/object:Gem::Requirement
|
27
30
|
requirements:
|
28
|
-
- - "
|
31
|
+
- - ">="
|
29
32
|
- !ruby/object:Gem::Version
|
30
33
|
version: '10.4'
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '12.0'
|
31
37
|
- !ruby/object:Gem::Dependency
|
32
38
|
name: activerecord
|
33
39
|
requirement: !ruby/object:Gem::Requirement
|
@@ -103,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
109
|
version: '0'
|
104
110
|
requirements: []
|
105
111
|
rubyforge_project: annotate
|
106
|
-
rubygems_version: 2.5.
|
112
|
+
rubygems_version: 2.5.1
|
107
113
|
signing_key:
|
108
114
|
specification_version: 4
|
109
115
|
summary: Annotates Rails Models, routes, fixtures, and others based on the database
|