annotaterb 4.1.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +68 -0
  3. data/README.md +4 -5
  4. data/VERSION +1 -1
  5. data/exe/annotaterb +7 -7
  6. data/lib/annotate_rb/active_record_patch.rb +2 -0
  7. data/lib/annotate_rb/commands/annotate_models.rb +0 -1
  8. data/lib/annotate_rb/commands/annotate_routes.rb +0 -1
  9. data/lib/annotate_rb/commands/print_help.rb +0 -1
  10. data/lib/annotate_rb/commands/print_version.rb +0 -1
  11. data/lib/annotate_rb/commands.rb +4 -4
  12. data/lib/annotate_rb/config_finder.rb +1 -1
  13. data/lib/annotate_rb/config_loader.rb +2 -2
  14. data/lib/annotate_rb/core.rb +3 -3
  15. data/lib/annotate_rb/helper.rb +16 -0
  16. data/lib/annotate_rb/model_annotator/{annotation_generator.rb → annotation_builder.rb} +18 -14
  17. data/lib/annotate_rb/model_annotator/annotation_decider.rb +10 -12
  18. data/lib/annotate_rb/model_annotator/annotation_diff.rb +19 -0
  19. data/lib/annotate_rb/model_annotator/annotation_diff_generator.rb +44 -0
  20. data/lib/annotate_rb/model_annotator/annotation_pattern_generator.rb +3 -3
  21. data/lib/annotate_rb/model_annotator/annotator.rb +12 -53
  22. data/lib/annotate_rb/model_annotator/column_annotation/annotation_builder.rb +135 -0
  23. data/lib/annotate_rb/model_annotator/column_annotation/attributes_builder.rb +104 -0
  24. data/lib/annotate_rb/model_annotator/column_annotation/column_wrapper.rb +103 -0
  25. data/lib/annotate_rb/model_annotator/column_annotation/type_builder.rb +54 -0
  26. data/lib/annotate_rb/model_annotator/column_annotation.rb +12 -0
  27. data/lib/annotate_rb/model_annotator/file_builder.rb +58 -0
  28. data/lib/annotate_rb/model_annotator/file_components.rb +78 -0
  29. data/lib/annotate_rb/model_annotator/file_name_resolver.rb +3 -3
  30. data/lib/annotate_rb/model_annotator/foreign_key_annotation/annotation_builder.rb +57 -0
  31. data/lib/annotate_rb/model_annotator/foreign_key_annotation.rb +9 -0
  32. data/lib/annotate_rb/model_annotator/index_annotation/annotation_builder.rb +113 -0
  33. data/lib/annotate_rb/model_annotator/index_annotation.rb +9 -0
  34. data/lib/annotate_rb/model_annotator/magic_comment_parser.rb +32 -0
  35. data/lib/annotate_rb/model_annotator/model_class_getter.rb +6 -6
  36. data/lib/annotate_rb/model_annotator/model_files_getter.rb +12 -10
  37. data/lib/annotate_rb/model_annotator/model_wrapper.rb +44 -37
  38. data/lib/annotate_rb/model_annotator/pattern_getter.rb +142 -10
  39. data/lib/annotate_rb/model_annotator/project_annotation_remover.rb +65 -0
  40. data/lib/annotate_rb/model_annotator/project_annotator.rb +63 -0
  41. data/lib/annotate_rb/model_annotator/related_files_list_builder.rb +16 -18
  42. data/lib/annotate_rb/model_annotator/single_file_annotation_remover.rb +37 -0
  43. data/lib/annotate_rb/model_annotator/single_file_annotator.rb +49 -0
  44. data/lib/annotate_rb/model_annotator/{file_annotator_instruction.rb → single_file_annotator_instruction.rb} +2 -2
  45. data/lib/annotate_rb/model_annotator/single_file_remove_annotation_instruction.rb +15 -0
  46. data/lib/annotate_rb/model_annotator.rb +25 -24
  47. data/lib/annotate_rb/options.rb +20 -25
  48. data/lib/annotate_rb/parser.rb +150 -142
  49. data/lib/annotate_rb/rake_bootstrapper.rb +8 -8
  50. data/lib/annotate_rb/route_annotator/annotation_processor.rb +7 -8
  51. data/lib/annotate_rb/route_annotator/annotator.rb +2 -2
  52. data/lib/annotate_rb/route_annotator/base_processor.rb +3 -3
  53. data/lib/annotate_rb/route_annotator/header_generator.rb +15 -15
  54. data/lib/annotate_rb/route_annotator/helper.rb +9 -9
  55. data/lib/annotate_rb/route_annotator/removal_processor.rb +4 -4
  56. data/lib/annotate_rb/route_annotator.rb +6 -6
  57. data/lib/annotate_rb/runner.rb +0 -4
  58. data/lib/annotate_rb/tasks/annotate_models_migrate.rake +5 -5
  59. data/lib/annotate_rb.rb +19 -19
  60. data/lib/generators/annotate_rb/install/install_generator.rb +3 -2
  61. data/lib/generators/annotate_rb/install/templates/annotate_rb.rake +1 -1
  62. metadata +24 -16
  63. data/lib/annotate_rb/model_annotator/column_annotation_builder.rb +0 -92
  64. data/lib/annotate_rb/model_annotator/column_attributes_builder.rb +0 -102
  65. data/lib/annotate_rb/model_annotator/column_type_builder.rb +0 -51
  66. data/lib/annotate_rb/model_annotator/column_wrapper.rb +0 -84
  67. data/lib/annotate_rb/model_annotator/constants.rb +0 -22
  68. data/lib/annotate_rb/model_annotator/file_annotation_remover.rb +0 -25
  69. data/lib/annotate_rb/model_annotator/file_annotator.rb +0 -83
  70. data/lib/annotate_rb/model_annotator/file_patterns.rb +0 -129
  71. data/lib/annotate_rb/model_annotator/foreign_key_annotation_builder.rb +0 -55
  72. data/lib/annotate_rb/model_annotator/helper.rb +0 -107
  73. data/lib/annotate_rb/model_annotator/index_annotation_builder.rb +0 -74
  74. data/lib/annotate_rb/model_annotator/model_file_annotator.rb +0 -55
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1c77ca5a809b63c94e69b937732b8f8f531807cc332627791b649576e8ed583e
4
- data.tar.gz: c2fb3ac84bad2e0cd108c801ca162a6a17b34ba800af8f5e885e03c8338d636d
3
+ metadata.gz: 5864a6030a7b4cfbf7751761c07136690ebafe15393d67147789f70319036205
4
+ data.tar.gz: a1dc2b99206b342bbd0dbc66795db4156abddfee307acecf6c2c383e8dbe89b1
5
5
  SHA512:
6
- metadata.gz: fb88fa8b511e0ccbbadce0db7d0ae941234967eb7358fea1146e48309939b1b35e51f8db9f15d544a6af4b3827decb65e51f4546d73feb2ab2f404c67f3c0f05
7
- data.tar.gz: 562cd430feba785e8f53ab90f79dfa1c3765f46a8e048bbe379955c8f696dd4e01ce167245cf17e31de4704dc0e22da859cba2f6f00c87d10782e637c2faad65
6
+ metadata.gz: b7cbd03e31fdc24f27d23d700773c157eca461078cddcd87739605980d6f5f07887b59f345ef41d91994547d8efbc2b82595e454f9e4619a5fe4e0fb506d29cd
7
+ data.tar.gz: f162544608cce0746a92250287ca4a5c7d25d151fe5bfe8bf4d18075834e98ce68939dc8b176dbe97d16f1f039a1a001cbb71066b26280f9fb5e83df763ba158
data/CHANGELOG.md CHANGED
@@ -0,0 +1,68 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased](https://github.com/drwl/annotaterb/tree/HEAD)
4
+
5
+ [Full Changelog](https://github.com/drwl/annotaterb/compare/v4.1.1...HEAD)
6
+
7
+ **Merged pull requests:**
8
+
9
+ - Specify `standard` gem version [\#39](https://github.com/drwl/annotaterb/pull/39) ([drwl](https://github.com/drwl))
10
+ - Bump version to v4.2.0 [\#37](https://github.com/drwl/annotaterb/pull/37) ([drwl](https://github.com/drwl))
11
+ - Generate changelog for 4.2 [\#36](https://github.com/drwl/annotaterb/pull/36) ([drwl](https://github.com/drwl))
12
+ - Improve tests for `ColumnAnnotation::*` [\#35](https://github.com/drwl/annotaterb/pull/35) ([drwl](https://github.com/drwl))
13
+ - Change instances of `Options.from` =\> `Options.new` in tests [\#34](https://github.com/drwl/annotaterb/pull/34) ([drwl](https://github.com/drwl))
14
+ - Add `Standard` linter to project [\#33](https://github.com/drwl/annotaterb/pull/33) ([drwl](https://github.com/drwl))
15
+ - Support Sorbet `typed` magic comment [\#32](https://github.com/drwl/annotaterb/pull/32) ([drwl](https://github.com/drwl))
16
+ - Add `position_in_additional_file_patterns` to Options and Parser [\#31](https://github.com/drwl/annotaterb/pull/31) ([drwl](https://github.com/drwl))
17
+ - Remove `Files` gem as a dependency [\#30](https://github.com/drwl/annotaterb/pull/30) ([drwl](https://github.com/drwl))
18
+ - Refactor `ModelAnnotator` again [\#28](https://github.com/drwl/annotaterb/pull/28) ([drwl](https://github.com/drwl))
19
+ - Add initial change log [\#27](https://github.com/drwl/annotaterb/pull/27) ([drwl](https://github.com/drwl))
20
+
21
+ ## [v4.1.1](https://github.com/drwl/annotaterb/tree/v4.1.1) (2023-05-20)
22
+
23
+ [Full Changelog](https://github.com/drwl/annotaterb/compare/v4.1.0...v4.1.1)
24
+
25
+ **Merged pull requests:**
26
+
27
+ - Bump version to v4.1.1 [\#26](https://github.com/drwl/annotaterb/pull/26) ([drwl](https://github.com/drwl))
28
+ - Tidy the github repo [\#25](https://github.com/drwl/annotaterb/pull/25) ([drwl](https://github.com/drwl))
29
+ - Add guide for migrating from Annotate gem [\#24](https://github.com/drwl/annotaterb/pull/24) ([drwl](https://github.com/drwl))
30
+ - Update column pattern regex to incorporate special column comments [\#23](https://github.com/drwl/annotaterb/pull/23) ([drwl](https://github.com/drwl))
31
+
32
+ ## [v4.1.0](https://github.com/drwl/annotaterb/tree/v4.1.0) (2023-05-17)
33
+
34
+ [Full Changelog](https://github.com/drwl/annotaterb/compare/v4.0.0...v4.1.0)
35
+
36
+ **Merged pull requests:**
37
+
38
+ - Bump version to 4.1.0 [\#22](https://github.com/drwl/annotaterb/pull/22) ([drwl](https://github.com/drwl))
39
+ - Refactor ModelAnnotator; Fix instances of incorrect `exclude_*` keys [\#21](https://github.com/drwl/annotaterb/pull/21) ([drwl](https://github.com/drwl))
40
+ - Fix the default behavior for model annotator [\#20](https://github.com/drwl/annotaterb/pull/20) ([drwl](https://github.com/drwl))
41
+ - Refactor model annotator [\#19](https://github.com/drwl/annotaterb/pull/19) ([drwl](https://github.com/drwl))
42
+ - Removed unused helper methods and `Env` class [\#18](https://github.com/drwl/annotaterb/pull/18) ([drwl](https://github.com/drwl))
43
+ - Update dummy app depdencies [\#17](https://github.com/drwl/annotaterb/pull/17) ([drwl](https://github.com/drwl))
44
+
45
+ ## [v4.0.0](https://github.com/drwl/annotaterb/tree/v4.0.0) (2023-05-03)
46
+
47
+ [Full Changelog](https://github.com/drwl/annotaterb/compare/1da0386bf9e1ca3fbd0d9d3ae69cdc7a8cdc26fa...v4.0.0)
48
+
49
+ **Merged pull requests:**
50
+
51
+ - Last project polish for 4.0.0 release [\#14](https://github.com/drwl/annotaterb/pull/14) ([drwl](https://github.com/drwl))
52
+ - Add configuration instructions to README [\#13](https://github.com/drwl/annotaterb/pull/13) ([drwl](https://github.com/drwl))
53
+ - Prepare gem for beta release [\#12](https://github.com/drwl/annotaterb/pull/12) ([drwl](https://github.com/drwl))
54
+ - Make annotaterb usable [\#11](https://github.com/drwl/annotaterb/pull/11) ([drwl](https://github.com/drwl))
55
+ - Move old annotate code into AnnotateRb namespace [\#10](https://github.com/drwl/annotaterb/pull/10) ([drwl](https://github.com/drwl))
56
+ - Fix CodeQL action [\#9](https://github.com/drwl/annotaterb/pull/9) ([drwl](https://github.com/drwl))
57
+ - Regularly run CI [\#8](https://github.com/drwl/annotaterb/pull/8) ([drwl](https://github.com/drwl))
58
+ - Get CI consistently green [\#7](https://github.com/drwl/annotaterb/pull/7) ([drwl](https://github.com/drwl))
59
+ - More work [\#6](https://github.com/drwl/annotaterb/pull/6) ([drwl](https://github.com/drwl))
60
+ - Make CI green [\#5](https://github.com/drwl/annotaterb/pull/5) ([drwl](https://github.com/drwl))
61
+ - Make it work for Rails 7 [\#4](https://github.com/drwl/annotaterb/pull/4) ([drwl](https://github.com/drwl))
62
+ - Tidy up project [\#3](https://github.com/drwl/annotaterb/pull/3) ([drwl](https://github.com/drwl))
63
+ - Set spec run order to random [\#2](https://github.com/drwl/annotaterb/pull/2) ([drwl](https://github.com/drwl))
64
+ - Tidy rspec configuration [\#1](https://github.com/drwl/annotaterb/pull/1) ([drwl](https://github.com/drwl))
65
+
66
+
67
+
68
+ \* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*
data/README.md CHANGED
@@ -60,10 +60,7 @@ $ bin/rails g annotate_rb:install
60
60
  This will copy a rake task into your Rails project's `lib/tasks` directory that will hook into the Rails project rake tasks, automatically running AnnotateRb after database migration rake tasks.
61
61
 
62
62
  ## Migrating from the annotate gem
63
- The old [annotate gem](https://github.com/ctran/annotate_models) relied on environment variables and hardcoded values in rake files for configuration. AnnotateRb is different. It reads from an optional configuration yml file and options from the CLI to function.
64
-
65
- * Remove the following files `lib/tasks/annotate_models.rake`, `lib/tasks/annotate_models_migrate.rake`, `lib/tasks/annotate_routes.rake`.
66
- * Run the generator install command above.
63
+ Refer to the [migration guide](MIGRATION_GUIDE.md).
67
64
 
68
65
  ## Usage
69
66
 
@@ -160,7 +157,9 @@ Previously in the [Annotate](https://github.com/ctran/annotate_models) you could
160
157
  position: after
161
158
  ```
162
159
 
163
- Annotaterb reads first from the configuration file, if it exists, then merges it with any options passed into the CLI.
160
+ Annotaterb reads first from the configuration file, if it exists, then merges it with any options passed into the CLI.
161
+
162
+ For further details visit the [section in the migration guide](MIGRATION_GUIDE.md#automatic-annotations-after-running-database-migration-commands).
164
163
 
165
164
  ### How to skip annotating a particular model
166
165
  If you want to always skip annotations on a particular model, add this string
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.1.0
1
+ 4.2.0
data/exe/annotaterb CHANGED
@@ -1,21 +1,21 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- unless File.exist?('./Rakefile') || File.exist?('./Gemfile')
5
- abort 'Please run annotaterb from the root of the project.'
4
+ unless File.exist?("./Rakefile") || File.exist?("./Gemfile")
5
+ abort "Please run annotaterb from the root of the project."
6
6
  end
7
7
 
8
8
  begin
9
- require 'bundler'
9
+ require "bundler"
10
10
  Bundler.setup
11
- rescue StandardError
11
+ rescue
12
12
  end
13
13
 
14
14
  $LOAD_PATH.unshift("#{__dir__}/../lib")
15
15
 
16
- require 'annotate_rb'
16
+ require "annotate_rb"
17
17
 
18
- exit_status = ::AnnotateRb::Runner.run(ARGV)
18
+ _exit_status = ::AnnotateRb::Runner.run(ARGV)
19
19
 
20
20
  # TODO: Return exit status
21
- # exit exit_status
21
+ # exit exit_status
@@ -1,5 +1,6 @@
1
1
  # monkey patches
2
2
 
3
+ # standard:disable Style/MissingRespondToMissing
3
4
  module ::ActiveRecord
4
5
  class Base
5
6
  def self.method_missing(_name, *_args)
@@ -7,3 +8,4 @@ module ::ActiveRecord
7
8
  end
8
9
  end
9
10
  end
11
+ # standard:enable Style/MissingRespondToMissing
@@ -19,4 +19,3 @@ module AnnotateRb
19
19
  end
20
20
  end
21
21
  end
22
-
@@ -16,4 +16,3 @@ module AnnotateRb
16
16
  end
17
17
  end
18
18
  end
19
-
@@ -13,4 +13,3 @@ module AnnotateRb
13
13
  end
14
14
  end
15
15
  end
16
-
@@ -9,4 +9,3 @@ module AnnotateRb
9
9
  end
10
10
  end
11
11
  end
12
-
@@ -2,9 +2,9 @@
2
2
 
3
3
  module AnnotateRb
4
4
  module Commands
5
- autoload :PrintVersion, 'annotate_rb/commands/print_version'
6
- autoload :PrintHelp, 'annotate_rb/commands/print_help'
7
- autoload :AnnotateModels, 'annotate_rb/commands/annotate_models'
8
- autoload :AnnotateRoutes, 'annotate_rb/commands/annotate_routes'
5
+ autoload :PrintVersion, "annotate_rb/commands/print_version"
6
+ autoload :PrintHelp, "annotate_rb/commands/print_help"
7
+ autoload :AnnotateModels, "annotate_rb/commands/annotate_models"
8
+ autoload :AnnotateRoutes, "annotate_rb/commands/annotate_routes"
9
9
  end
10
10
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module AnnotateRb
4
4
  class ConfigFinder
5
- DOTFILE = '.annotaterb.yml'
5
+ DOTFILE = ".annotaterb.yml"
6
6
 
7
7
  class << self
8
8
  def find_project_root
@@ -44,9 +44,9 @@ module AnnotateRb
44
44
  # Method from Rubocop::ConfigLoader
45
45
  def yaml_safe_load(yaml_code, filename)
46
46
  yaml_safe_load!(yaml_code, filename)
47
- rescue ::StandardError
47
+ rescue
48
48
  if defined?(::SafeYAML)
49
- raise 'SafeYAML is unmaintained, no longer needed and should be removed'
49
+ raise "SafeYAML is unmaintained, no longer needed and should be removed"
50
50
  end
51
51
 
52
52
  raise
@@ -4,13 +4,13 @@ module AnnotateRb
4
4
  module Core
5
5
  class << self
6
6
  def version
7
- @version ||= File.read(File.expand_path('../../VERSION', __dir__)).strip
7
+ @version ||= File.read(File.expand_path("../../VERSION", __dir__)).strip
8
8
  end
9
9
 
10
10
  def load_rake_tasks
11
11
  return if @load_rake_tasks
12
12
 
13
- rake_tasks = Dir[File.join(File.dirname(__FILE__), 'tasks', '**/*.rake')]
13
+ rake_tasks = Dir[File.join(File.dirname(__FILE__), "tasks", "**/*.rake")]
14
14
 
15
15
  rake_tasks.each do |task|
16
16
  load task
@@ -20,4 +20,4 @@ module AnnotateRb
20
20
  end
21
21
  end
22
22
  end
23
- end
23
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module Helper
5
+ class << self
6
+ def width(string)
7
+ string.chars.inject(0) { |acc, elem| acc + ((elem.bytesize == 3) ? 2 : 1) }
8
+ end
9
+
10
+ # TODO: Find another implementation that doesn't depend on ActiveSupport
11
+ def fallback(*args)
12
+ args.compact.detect(&:present?)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -2,12 +2,12 @@
2
2
 
3
3
  module AnnotateRb
4
4
  module ModelAnnotator
5
- class AnnotationGenerator
5
+ class AnnotationBuilder
6
6
  # Annotate Models plugin use this header
7
- PREFIX = '== Schema Information'.freeze
8
- PREFIX_MD = '## Schema Information'.freeze
7
+ PREFIX = "== Schema Information"
8
+ PREFIX_MD = "## Schema Information"
9
9
 
10
- END_MARK = '== Schema Information End'.freeze
10
+ END_MARK = "== Schema Information End"
11
11
 
12
12
  MD_NAMES_OVERHEAD = 6
13
13
  MD_TYPE_ALLOWANCE = 18
@@ -18,7 +18,7 @@ module AnnotateRb
18
18
  @info = "" # TODO: Make array and build string that way
19
19
  end
20
20
 
21
- def generate
21
+ def build
22
22
  @info = "# #{header}\n"
23
23
  @info += schema_header_text
24
24
 
@@ -26,22 +26,22 @@ module AnnotateRb
26
26
 
27
27
  if @options[:format_markdown]
28
28
  @info += format("# %-#{max_size + MD_NAMES_OVERHEAD}.#{max_size + MD_NAMES_OVERHEAD}s | %-#{MD_TYPE_ALLOWANCE}.#{MD_TYPE_ALLOWANCE}s | %s\n",
29
- 'Name',
30
- 'Type',
31
- 'Attributes')
32
- @info += "# #{'-' * (max_size + MD_NAMES_OVERHEAD)} | #{'-' * MD_TYPE_ALLOWANCE} | #{'-' * 27}\n"
29
+ "Name",
30
+ "Type",
31
+ "Attributes")
32
+ @info += "# #{"-" * (max_size + MD_NAMES_OVERHEAD)} | #{"-" * MD_TYPE_ALLOWANCE} | #{"-" * 27}\n"
33
33
  end
34
34
 
35
35
  @info += @model.columns.map do |col|
36
- ColumnAnnotationBuilder.new(col, @model, max_size, @options).build
36
+ ColumnAnnotation::AnnotationBuilder.new(col, @model, max_size, @options).build
37
37
  end.join
38
38
 
39
39
  if @options[:show_indexes] && @model.table_exists?
40
- @info += IndexAnnotationBuilder.new(@model, @options).build
40
+ @info += IndexAnnotation::AnnotationBuilder.new(@model, @options).build
41
41
  end
42
42
 
43
43
  if @options[:show_foreign_keys] && @model.table_exists?
44
- @info += ForeignKeyAnnotationBuilder.new(@model, @options).build
44
+ @info += ForeignKeyAnnotation::AnnotationBuilder.new(@model, @options).build
45
45
  end
46
46
 
47
47
  @info += schema_footer_text
@@ -51,7 +51,11 @@ module AnnotateRb
51
51
 
52
52
  def header
53
53
  header = @options[:format_markdown] ? PREFIX_MD.dup : PREFIX.dup
54
- version = ActiveRecord::Migrator.current_version rescue 0
54
+ version = begin
55
+ ActiveRecord::Migrator.current_version
56
+ rescue
57
+ 0
58
+ end
55
59
 
56
60
  if @options[:include_version] && version > 0
57
61
  header += "\n# Schema version: #{version}"
@@ -91,4 +95,4 @@ module AnnotateRb
91
95
  end
92
96
  end
93
97
  end
94
- end
98
+ end
@@ -4,6 +4,8 @@ module AnnotateRb
4
4
  module ModelAnnotator
5
5
  # Class that encapsulates the logic to decide whether to annotate a model file and its related files or not.
6
6
  class AnnotationDecider
7
+ SKIP_ANNOTATION_PREFIX = '# -\*- SkipSchemaAnnotations'
8
+
7
9
  def initialize(file, options)
8
10
  @file = file
9
11
  @options = options
@@ -35,12 +37,12 @@ module AnnotateRb
35
37
  return to_annotate
36
38
  rescue BadModelFileError => e
37
39
  unless @options[:ignore_unknown_models]
38
- $stderr.puts "Unable to annotate #{@file}: #{e.message}"
39
- $stderr.puts "\t" + e.backtrace.join("\n\t") if @options[:trace]
40
+ warn "Unable to process #{@file}: #{e.message}"
41
+ warn "\t" + e.backtrace.join("\n\t") if @options[:trace]
40
42
  end
41
- rescue StandardError => e
42
- $stderr.puts "Unable to annotate #{@file}: #{e.message}"
43
- $stderr.puts "\t" + e.backtrace.join("\n\t") if @options[:trace]
43
+ rescue => e
44
+ warn "Unable to process #{@file}: #{e.message}"
45
+ warn "\t" + e.backtrace.join("\n\t") if @options[:trace]
44
46
  end
45
47
 
46
48
  false
@@ -49,14 +51,10 @@ module AnnotateRb
49
51
  private
50
52
 
51
53
  def file_contains_skip_annotation
52
- file_string = File.exist?(@file) ? File.read(@file) : ''
54
+ file_string = File.exist?(@file) ? File.read(@file) : ""
53
55
 
54
- if /#{Constants::SKIP_ANNOTATION_PREFIX}.*/ =~ file_string
55
- true
56
- else
57
- false
58
- end
56
+ /#{SKIP_ANNOTATION_PREFIX}.*/o.match?(file_string)
59
57
  end
60
58
  end
61
59
  end
62
- end
60
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ # Plain old Ruby object for holding the differences
6
+ class AnnotationDiff
7
+ attr_reader :current_columns, :new_columns
8
+
9
+ def initialize(current_columns, new_columns)
10
+ @current_columns = current_columns.dup.freeze
11
+ @new_columns = new_columns.dup.freeze
12
+ end
13
+
14
+ def changed?
15
+ @changed ||= @current_columns != @new_columns
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ # Compares the current file content and new annotation block and generates the column annotation differences
6
+ class AnnotationDiffGenerator
7
+ HEADER_PATTERN = /(^# Table name:.*?\n(#.*\r?\n)*\r?)/
8
+ COLUMN_PATTERN = /^#[\t ]+[\w*.`\[\]():]+[\t ]+.+$/
9
+
10
+ class << self
11
+ def call(file_content, annotation_block)
12
+ new(file_content, annotation_block).generate
13
+ end
14
+ end
15
+
16
+ # @param [String] file_content The current file content of the model file we intend to annotate
17
+ # @param [String] annotation_block The annotation block we intend to write to the model file
18
+ def initialize(file_content, annotation_block)
19
+ @file_content = file_content
20
+ @annotation_block = annotation_block
21
+ end
22
+
23
+ def generate
24
+ # Ignore the Schema version line because it changes with each migration
25
+ current_annotations = @file_content.match(HEADER_PATTERN).to_s
26
+ new_annotations = @annotation_block.match(HEADER_PATTERN).to_s
27
+
28
+ current_annotation_columns = if current_annotations.present?
29
+ current_annotations.scan(COLUMN_PATTERN).sort
30
+ else
31
+ []
32
+ end
33
+
34
+ new_annotation_columns = if new_annotations.present?
35
+ new_annotations.scan(COLUMN_PATTERN).sort
36
+ else
37
+ []
38
+ end
39
+
40
+ _result = AnnotationDiff.new(current_annotation_columns, new_annotation_columns)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -3,15 +3,15 @@
3
3
  module AnnotateRb
4
4
  module ModelAnnotator
5
5
  class AnnotationPatternGenerator
6
- COMPAT_PREFIX = '== Schema Info'.freeze
7
- COMPAT_PREFIX_MD = '## Schema Info'.freeze
6
+ COMPAT_PREFIX = "== Schema Info"
7
+ COMPAT_PREFIX_MD = "## Schema Info"
8
8
 
9
9
  class << self
10
10
  def call(options = Options.from({}))
11
11
  if options[:wrapper_open]
12
12
  return /(?:^(\n|\r\n)?# (?:#{options[:wrapper_open]}).*(\n|\r\n)?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?(\n|\r\n)(#.*(\n|\r\n))*(\n|\r\n)*)|^(\n|\r\n)?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?(\n|\r\n)(#.*(\n|\r\n))*(\n|\r\n)*/
13
13
  end
14
- /^(\n|\r\n)?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?(\n|\r\n)(#.*(\n|\r\n))*(\n|\r\n)*/
14
+ /^(\n|\r\n)?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?(\n|\r\n)(#.*(\n|\r\n))*(\n|\r\n)*/o
15
15
  end
16
16
  end
17
17
  end
@@ -5,66 +5,25 @@ module AnnotateRb
5
5
  class Annotator
6
6
  class << self
7
7
  def do_annotations(options = {})
8
- annotated = []
9
-
10
- model_files_to_consider = ModelFilesGetter.call(options)
11
-
12
- model_files_to_consider.each do |path, filename|
13
- file = File.join(path, filename)
14
-
15
- if AnnotationDecider.new(file, options).annotate?
16
- ModelFileAnnotator.call(annotated, file, options)
17
- end
18
- end
19
-
20
- if annotated.empty?
21
- puts 'Model files unchanged.'
22
- else
23
- puts "Annotated (#{annotated.length}): #{annotated.join(', ')}"
24
- end
8
+ new(options).do_annotations
25
9
  end
26
10
 
27
11
  def remove_annotations(options = {})
28
- deannotated = []
29
-
30
- model_files_to_consider = ModelFilesGetter.call(options)
31
-
32
- model_files_to_consider.each do |path, filename|
33
- deannotated_klass = false
34
- file = File.join(path, filename)
35
-
36
- begin
37
- klass = ModelClassGetter.call(file, options)
38
- if klass < ActiveRecord::Base && !klass.abstract_class?
39
- model_name = klass.name.underscore
40
- table_name = klass.table_name
41
-
42
- if FileAnnotationRemover.call(file, options)
43
- deannotated_klass = true
44
- end
45
-
46
- related_files = RelatedFilesListBuilder.new(file, model_name, table_name, options).build
12
+ new(options).remove_annotations
13
+ end
14
+ end
47
15
 
48
- related_files.each do |f, _position_key|
49
- if File.exist?(f)
50
- FileAnnotationRemover.call(f, options)
51
- end
52
- end
53
- end
16
+ def initialize(options)
17
+ @options = options
18
+ end
54
19
 
55
- if deannotated_klass
56
- deannotated << klass
57
- end
58
- rescue StandardError => e
59
- $stderr.puts "Unable to deannotate #{File.join(file)}: #{e.message}"
60
- $stderr.puts "\t" + e.backtrace.join("\n\t") if options[:trace]
61
- end
62
- end
20
+ def do_annotations
21
+ ProjectAnnotator.new(@options).annotate
22
+ end
63
23
 
64
- puts "Removed annotations from: #{deannotated.join(', ')}"
65
- end
24
+ def remove_annotations
25
+ ProjectAnnotationRemover.new(@options).remove_annotations
66
26
  end
67
27
  end
68
28
  end
69
29
  end
70
-