annotate 2.7.4 → 2.7.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f0d9e7c58d8f45bb9c817977235df81eea4ade06e3fa63d19a71e7b5486360f6
4
- data.tar.gz: 04a020ce1fb7a0e3e9709f9897d3f71881b6c2fe6cb60f2be8b2fb85edfbc7f0
3
+ metadata.gz: 19c80b25d2326ba60d78120276156ea6223fd07024dd9b9c771d4fa405f68204
4
+ data.tar.gz: 6e8409e098cdecbc97053480e8948f04e4cba6c758b8035255d5e027ed46097d
5
5
  SHA512:
6
- metadata.gz: 23fe870dc38870547da39aca1aa88049999acd0bf37225db6d7f96227e9af7a5d5c515e1500b02736b1ca0f689a186ef4667ac934f38405c6f8bae1eb8922dc7
7
- data.tar.gz: 8e8a680c399ce6c68b9d1d889d14a45584eb93150cda7dabf1c348e83db9d89d5accf94df48451485ec2576ea0204d668f8782eab414789f88e362ed56a0ac35
6
+ metadata.gz: 0b8dcb130221c519f1639d8fbea17f0047e915ad6fc7cb3a95cdb9f20b6d25088b04f8cd181519f8ef4ce39e62986d9edee7cabd2824673a4c41878e27d6c24d
7
+ data.tar.gz: f0b1f5b74e818782c6ecda0aafe926a055734f5aaaf9ef82592a3dfcfe96d4589b82f6253b4cc72595e6aeef0fa8e07c177c6a0ef70f8511ceeea81ef167b122
@@ -1,5 +1,5 @@
1
- == 2.7.4
2
- See https://github.com/ctran/annotate_models/releases/tag/v2.7.4
1
+ == 2.7.5
2
+ See https://github.com/ctran/annotate_models/releases/tag/v2.7.5
3
3
 
4
4
  == 2.7.3
5
5
  See https://github.com/ctran/annotate_models/releases/tag/v2.7.3
@@ -203,6 +203,7 @@ you can do so with a simple environment variable, instead of editing the
203
203
  -f [bare|rdoc|markdown], Render Schema Infomation as plain/RDoc/Markdown
204
204
  --format
205
205
  --force Force new annotations even if there are no changes.
206
+ --frozen Do not allow to change annotations. Exits non-zero if there are going to be changes to files.
206
207
  --timestamp Include timestamp in (routes) annotation
207
208
  --trace If unable to annotate a file, print the full stack trace, not just the exception message.
208
209
  -I, --ignore-columns REGEX don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`
@@ -44,5 +44,5 @@ Gem::Specification.new do |s|
44
44
 
45
45
  s.specification_version = 4 if s.respond_to? :specification_version
46
46
  s.add_runtime_dependency(%q<rake>, ['>= 10.4', '< 13.0'])
47
- s.add_runtime_dependency(%q<activerecord>, ['>= 3.2', '< 6.0'])
47
+ s.add_runtime_dependency(%q<activerecord>, ['>= 3.2', '< 7.0'])
48
48
  end
@@ -170,6 +170,10 @@ OptionParser.new do |opts|
170
170
  ENV['force'] = 'yes'
171
171
  end
172
172
 
173
+ opts.on('--frozen', 'Do not allow to change annotations. Exits non-zero if there are going to be changes to files.') do
174
+ ENV['frozen'] = 'yes'
175
+ end
176
+
173
177
  opts.on('--timestamp', 'Include timestamp in (routes) annotation') do
174
178
  ENV['timestamp'] = 'true'
175
179
  end
@@ -29,8 +29,8 @@ module Annotate
29
29
  FLAG_OPTIONS = [
30
30
  :show_indexes, :simple_indexes, :include_version, :exclude_tests,
31
31
  :exclude_fixtures, :exclude_factories, :ignore_model_sub_dir,
32
- :format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace,
33
- :timestamp, :exclude_serializers, :classified_sort,
32
+ :format_bare, :format_rdoc, :format_markdown, :sort, :force, :frozen,
33
+ :trace, :timestamp, :exclude_serializers, :classified_sort,
34
34
  :show_foreign_keys, :show_complete_foreign_keys,
35
35
  :exclude_scaffolds, :exclude_controllers, :exclude_helpers,
36
36
  :exclude_sti_subclasses, :ignore_unknown_models, :with_comment
@@ -62,7 +62,7 @@ module AnnotateModels
62
62
 
63
63
  # Don't show limit (#) on these column types
64
64
  # Example: show "integer" instead of "integer(4)"
65
- NO_LIMIT_COL_TYPES = %w(integer boolean).freeze
65
+ NO_LIMIT_COL_TYPES = %w(integer bigint boolean).freeze
66
66
 
67
67
  # Don't show default value for these column types
68
68
  NO_DEFAULT_COL_TYPES = %w(json jsonb hstore).freeze
@@ -301,7 +301,7 @@ module AnnotateModels
301
301
  type_remainder = (md_type_allowance - 2) - col_type.length
302
302
  info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
303
303
  else
304
- info << sprintf("# %-#{max_size}.#{max_size}s:%-#{bare_type_allowance}.#{bare_type_allowance}s %s", col_name, col_type, attrs.join(", ")).rstrip + "\n"
304
+ info << format_default(col_name, max_size, col_type, bare_type_allowance, attrs)
305
305
  end
306
306
  end
307
307
 
@@ -362,7 +362,7 @@ module AnnotateModels
362
362
  end
363
363
 
364
364
  def get_col_type(col)
365
- if col.respond_to?(:bigint?) && col.bigint?
365
+ if (col.respond_to?(:bigint?) && col.bigint?) || /\Abigint\b/ =~ col.sql_type
366
366
  'bigint'
367
367
  else
368
368
  (col.type || col.sql_type).to_s
@@ -498,53 +498,56 @@ module AnnotateModels
498
498
  # :before, :top, :after or :bottom. Default is :before.
499
499
  #
500
500
  def annotate_one_file(file_name, info_block, position, options = {})
501
- if File.exist?(file_name)
502
- old_content = File.read(file_name)
503
- return false if old_content =~ /#{SKIP_ANNOTATION_PREFIX}.*\n/
501
+ return false unless File.exist?(file_name)
502
+ old_content = File.read(file_name)
503
+ return false if old_content =~ /#{SKIP_ANNOTATION_PREFIX}.*\n/
504
504
 
505
- # Ignore the Schema version line because it changes with each migration
506
- header_pattern = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?)/
507
- old_header = old_content.match(header_pattern).to_s
508
- new_header = info_block.match(header_pattern).to_s
505
+ # Ignore the Schema version line because it changes with each migration
506
+ header_pattern = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?)/
507
+ old_header = old_content.match(header_pattern).to_s
508
+ new_header = info_block.match(header_pattern).to_s
509
509
 
510
- column_pattern = /^#[\t ]+[\w\*`]+[\t ]+.+$/
511
- old_columns = old_header && old_header.scan(column_pattern).sort
512
- new_columns = new_header && new_header.scan(column_pattern).sort
510
+ column_pattern = /^#[\t ]+[\w\*`]+[\t ]+.+$/
511
+ old_columns = old_header && old_header.scan(column_pattern).sort
512
+ new_columns = new_header && new_header.scan(column_pattern).sort
513
513
 
514
- magic_comments_block = magic_comments_as_string(old_content)
514
+ return false if old_columns == new_columns && !options[:force]
515
515
 
516
- if old_columns == new_columns && !options[:force]
517
- return false
518
- else
519
- # Replace inline the old schema info with the new schema info
520
- new_content = old_content.sub(annotate_pattern(options), info_block + "\n")
516
+ abort "annotate error. #{file_name} needs to be updated, but annotate was run with `--frozen`." if options[:frozen]
521
517
 
522
- if new_content.end_with?(info_block + "\n")
523
- new_content = old_content.sub(annotate_pattern(options), "\n" + info_block)
524
- end
518
+ # Replace inline the old schema info with the new schema info
519
+ wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : ""
520
+ wrapper_close = options[:wrapper_close] ? "# #{options[:wrapper_close]}\n" : ""
521
+ wrapped_info_block = "#{wrapper_open}#{info_block}#{wrapper_close}"
525
522
 
526
- wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : ""
527
- wrapper_close = options[:wrapper_close] ? "# #{options[:wrapper_close]}\n" : ""
528
- wrapped_info_block = "#{wrapper_open}#{info_block}#{wrapper_close}"
529
- # if there *was* no old schema info (no substitution happened) or :force was passed,
530
- # we simply need to insert it in correct position
531
- if new_content == old_content || options[:force]
532
- old_content.gsub!(magic_comment_matcher, '')
533
- old_content.sub!(annotate_pattern(options), '')
534
-
535
- new_content = if %w(after bottom).include?(options[position].to_s)
536
- magic_comments_block + (old_content.rstrip + "\n\n" + wrapped_info_block)
537
- else
538
- magic_comments_block + wrapped_info_block + "\n" + old_content
539
- end
540
- end
523
+ old_annotation = old_content.match(annotate_pattern(options)).to_s
541
524
 
542
- File.open(file_name, 'wb') { |f| f.puts new_content }
543
- return true
544
- end
525
+ # if there *was* no old schema info or :force was passed, we simply
526
+ # need to insert it in correct position
527
+ if old_annotation.empty? || options[:force]
528
+ magic_comments_block = magic_comments_as_string(old_content)
529
+ old_content.gsub!(magic_comment_matcher, '')
530
+ old_content.sub!(annotate_pattern(options), '')
531
+
532
+ new_content = if %w(after bottom).include?(options[position].to_s)
533
+ magic_comments_block + (old_content.rstrip + "\n\n" + wrapped_info_block)
534
+ elsif magic_comments_block.empty?
535
+ magic_comments_block + wrapped_info_block + "\n" + old_content
536
+ else
537
+ magic_comments_block + "\n" + wrapped_info_block + "\n" + old_content
538
+ end
545
539
  else
546
- false
540
+ # replace the old annotation with the new one
541
+
542
+ # keep the surrounding whitespace the same
543
+ space_match = old_annotation.match(/\A(?<start>\s*).*?\n(?<end>\s*)\z/m)
544
+ new_annotation = space_match[:start] + wrapped_info_block + space_match[:end]
545
+
546
+ new_content = old_content.sub(annotate_pattern(options), new_annotation)
547
547
  end
548
+
549
+ File.open(file_name, 'wb') { |f| f.puts new_content }
550
+ true
548
551
  end
549
552
 
550
553
  def magic_comment_matcher
@@ -555,7 +558,7 @@ module AnnotateModels
555
558
  magic_comments = content.scan(magic_comment_matcher).flatten.compact
556
559
 
557
560
  if magic_comments.any?
558
- magic_comments.join + "\n"
561
+ magic_comments.join
559
562
  else
560
563
  ''
561
564
  end
@@ -885,7 +888,7 @@ module AnnotateModels
885
888
  def max_schema_info_width(klass, options)
886
889
  if with_comments?(klass, options)
887
890
  max_size = klass.columns.map do |column|
888
- column.name.size + (column.comment ? column.comment.size : 0)
891
+ column.name.size + (column.comment ? width(column.comment) : 0)
889
892
  end.max || 0
890
893
  max_size += 2
891
894
  else
@@ -895,6 +898,20 @@ module AnnotateModels
895
898
 
896
899
  max_size
897
900
  end
901
+
902
+ def format_default(col_name, max_size, col_type, bare_type_allowance, attrs)
903
+ sprintf("# %s:%s %s", mb_chars_ljust(col_name, max_size), mb_chars_ljust(col_type, bare_type_allowance), attrs.join(", ")).rstrip + "\n"
904
+ end
905
+
906
+ def width(string)
907
+ string.chars.inject(0) { |acc, elem| acc + (elem.bytesize == 1 ? 1 : 2) }
908
+ end
909
+
910
+ def mb_chars_ljust(string, length)
911
+ string = string.to_s
912
+ padding = length - width(string)
913
+ string + (' ' * padding)
914
+ end
898
915
  end
899
916
 
900
917
  class BadModelFileError < LoadError
@@ -1,5 +1,5 @@
1
1
  module Annotate
2
2
  def self.version
3
- '2.7.4'
3
+ '2.7.5'
4
4
  end
5
5
  end
@@ -42,11 +42,12 @@ if Rails.env.development?
42
42
  'format_markdown' => 'false',
43
43
  'sort' => 'false',
44
44
  'force' => 'false',
45
+ 'frozen' => 'false',
45
46
  'classified_sort' => 'true',
46
47
  'trace' => 'false',
47
48
  'wrapper_open' => nil,
48
49
  'wrapper_close' => nil,
49
- 'with_comment' => true
50
+ 'with_comment' => 'true'
50
51
  )
51
52
  end
52
53
 
@@ -39,6 +39,7 @@ task annotate_models: :environment do
39
39
  options[:format_markdown] = Annotate.true?(ENV['format_markdown'])
40
40
  options[:sort] = Annotate.true?(ENV['sort'])
41
41
  options[:force] = Annotate.true?(ENV['force'])
42
+ options[:frozen] = Annotate.true?(ENV['frozen'])
42
43
  options[:classified_sort] = Annotate.true?(ENV['classified_sort'])
43
44
  options[:trace] = Annotate.true?(ENV['trace'])
44
45
  options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper'])
@@ -48,6 +49,7 @@ task annotate_models: :environment do
48
49
  options[:hide_limit_column_types] = Annotate.fallback(ENV['hide_limit_column_types'], '')
49
50
  options[:hide_default_column_types] = Annotate.fallback(ENV['hide_default_column_types'], '')
50
51
  options[:with_comment] = Annotate.true?(ENV['with_comment'])
52
+ options[:ignore_unknown_models] = Annotate.true?(ENV.fetch('ignore_unknown_models', 'false'))
51
53
 
52
54
  AnnotateModels.do_annotations(options)
53
55
  end
@@ -4,21 +4,17 @@
4
4
  # Append annotations to Rake tasks for ActiveRecord, so annotate automatically gets
5
5
  # run after doing db:migrate.
6
6
 
7
- namespace :db do
8
- [:migrate, :rollback].each do |cmd|
9
- task cmd do
10
- Rake::Task['set_annotation_options'].invoke
7
+ %w(db:migrate db:migrate:up db:migrate:down db:migrate:reset db:migrate:redo db:rollback).each do |task|
8
+ Rake::Task[task].enhance do
9
+ Rake::Task[Rake.application.top_level_tasks.last].enhance do
10
+ annotation_options_task = if Rake::Task.task_defined?('app:set_annotation_options')
11
+ 'app:set_annotation_options'
12
+ else
13
+ 'set_annotation_options'
14
+ end
15
+ Rake::Task[annotation_options_task].invoke
11
16
  Annotate::Migration.update_annotations
12
17
  end
13
-
14
- namespace cmd do
15
- [:change, :up, :down, :reset, :redo].each do |t|
16
- task t do
17
- Rake::Task['set_annotation_options'].invoke
18
- Annotate::Migration.update_annotations
19
- end
20
- end
21
- end
22
18
  end
23
19
  end
24
20
 
@@ -46,6 +42,8 @@ module Annotate
46
42
  def self.update_routes
47
43
  if Rake::Task.task_defined?("annotate_routes")
48
44
  Rake::Task["annotate_routes"].invoke
45
+ elsif Rake::Task.task_defined?("app:annotate_routes")
46
+ Rake::Task["app:annotate_routes"].invoke
49
47
  end
50
48
  end
51
49
  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
4
+ version: 2.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Chaffee
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2018-06-03 00:00:00.000000000 Z
15
+ date: 2019-04-27 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: rake
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '3.2'
44
44
  - - "<"
45
45
  - !ruby/object:Gem::Version
46
- version: '6.0'
46
+ version: '7.0'
47
47
  type: :runtime
48
48
  prerelease: false
49
49
  version_requirements: !ruby/object:Gem::Requirement
@@ -53,7 +53,7 @@ dependencies:
53
53
  version: '3.2'
54
54
  - - "<"
55
55
  - !ruby/object:Gem::Version
56
- version: '6.0'
56
+ version: '7.0'
57
57
  description: Annotates Rails/ActiveRecord Models, routes, fixtures, and others based
58
58
  on the database schema.
59
59
  email:
@@ -108,8 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  requirements: []
111
- rubyforge_project: annotate
112
- rubygems_version: 2.7.7
111
+ rubygems_version: 3.0.3
113
112
  signing_key:
114
113
  specification_version: 4
115
114
  summary: Annotates Rails Models, routes, fixtures, and others based on the database