rails-annotate-solargraph 0.3.0 → 0.4.0

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: 4ca6f0073923760a583abb52af713540b2f53cf6e8dd896584e3a76b97444c8b
4
- data.tar.gz: a07141ca737ee0c60153cabd5e28540d3507ba09401b4cc31d30ea491bc32eb2
3
+ metadata.gz: 80d2fcb8fc2090d6a1eabc8f510b3b9fec1608da1870c7d88b301ce64907920a
4
+ data.tar.gz: 9553090d359f8f829e955349cac6c527c43335433e38d91152862780cce3ab63
5
5
  SHA512:
6
- metadata.gz: f3a40df235de87414059053670f344e0e6606b9538ecba40b7fbb2c849c07f8009e5cec055e485033bae5529bce9146d2369d76b8a6ab4288664703bef29da2f
7
- data.tar.gz: d920e8b59c64f57d22fba128b4db1acffd88b718799e24e2f361d43e99ae6505258faa2665f57fafdee9d9cd6d4dc47231c9e5e7bb99a7e687707c2642e211e5
6
+ metadata.gz: 8b2415d929d939454a4f65277d85baa03935219226c6ad5b35c35264cd2de94bb1b1d55a55a55ee8cc737523210e3653c648bea50798dd53b580767fbb6a2eec
7
+ data.tar.gz: 9436e870a6cbc28e636151b76717cde5bf4186f44bc8af4293ada785f1c50e039895fcf5cb38dd27868c5e88027df9f52be8ee070591e0635de30fdba7437aef
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "cSpell.words": [
3
3
  "activerecord",
4
+ "klass",
4
5
  "rakefile",
5
6
  "solargraph",
6
7
  "yieldparam"
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.0] - 2022-04-17
4
+
5
+ - Annotations get saved to a schema file by default `app/models/annotate_solargraph_schema.rb`
6
+
3
7
  ## [0.3.0] - 2022-04-17
4
8
 
5
9
  - `has_many :through` and `has_one :through` relations get documented
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails-annotate-solargraph (0.3.0)
4
+ rails-annotate-solargraph (0.4.0)
5
5
  rails (>= 5.0, < 8.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -63,11 +63,31 @@ You can change the gem's default configuration like so:
63
63
  ```ruby
64
64
  # config/initializers/rails_annotate_solargraph.rb
65
65
 
66
- Rails::Annotate::Solargraph.configure do |conf|
67
- conf.annotation_position = :top # `:bottom` by default
66
+ if ::Rails.env.development?
67
+ ::Rails::Annotate::Solargraph.configure do |conf|
68
+ conf.annotation_position = :top # `:schema_file` by default
69
+ end
68
70
  end
69
71
  ```
70
72
 
73
+ #### annotation_position
74
+
75
+ There are a few values for this option:
76
+
77
+ - `:schema_file` -- default value, annotations get saved to a special file `app/models/annotate_solargraph_schema.rb`
78
+ - `:bottom` -- annotations are appended to the model files
79
+ - `:top` -- annotations are prepended to the model files
80
+
81
+ ### Update
82
+
83
+ To update this gem you should generate the rakefiles once again. Overwrite them.
84
+
85
+ ```sh
86
+ $ rails g annotate:solargraph:install
87
+ $ rake annotate:solargraph:remove
88
+ $ rake annotate:solargraph:generate
89
+ ```
90
+
71
91
  ## Development
72
92
 
73
93
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -9,10 +9,10 @@ module Rails
9
9
  # @return [Symbol]
10
10
  attr_reader :annotation_position
11
11
 
12
- ANNOTATION_POSITIONS = ::Set[:bottom, :top].freeze
12
+ ANNOTATION_POSITIONS = ::Set[:bottom, :top, :schema_file].freeze
13
13
 
14
14
  def initialize
15
- @annotation_position = :bottom
15
+ @annotation_position = :schema_file
16
16
  end
17
17
 
18
18
  # @param val [Symbol]
@@ -22,6 +22,10 @@ module Rails
22
22
 
23
23
  @annotation_position = val
24
24
  end
25
+
26
+ def schema_file?
27
+ @annotation_position == :schema_file
28
+ end
25
29
  end
26
30
  end
27
31
  end
@@ -1,17 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'fileutils'
4
+
3
5
  module Rails
4
6
  module Annotate
5
7
  module Solargraph
6
8
  class Model
7
9
  using TerminalColors::Refinement
8
10
 
9
- # @return [String]
10
- ANNOTATION_START = "\n# %%<RailsAnnotateSolargraph:Start>%%"
11
- # @return [String]
12
- ANNOTATION_END = "%%<RailsAnnotateSolargraph:End>%%\n\n"
13
- # @return [Regexp]
14
- ANNOTATION_REGEXP = /#{ANNOTATION_START}.*#{ANNOTATION_END}/m.freeze
15
11
  # @return [Regexp]
16
12
  MAGIC_COMMENT_REGEXP = /(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/.freeze
17
13
 
@@ -40,6 +36,26 @@ module Rails
40
36
  ::Object.to_s
41
37
  end
42
38
  end
39
+
40
+ # @param klass [Class]
41
+ # @return [String]
42
+ def annotation_start(klass = nil)
43
+ table_name = klass && CONFIG.schema_file? ? ":#{klass.table_name}" : ''
44
+ "\n# %%<RailsAnnotateSolargraph:Start#{table_name}>%%"
45
+ end
46
+
47
+ # @param klass [Class]
48
+ # @return [String]
49
+ def annotation_end(klass = nil)
50
+ table_name = klass && CONFIG.schema_file? ? ":#{klass.table_name}" : ''
51
+ "%%<RailsAnnotateSolargraph:End#{table_name}>%%\n\n"
52
+ end
53
+
54
+ # @param klass [Class]
55
+ # @return [Regexp]
56
+ def annotation_regexp(klass = nil)
57
+ /#{annotation_start(klass)}.*#{annotation_end(klass)}/m
58
+ end
43
59
  end
44
60
 
45
61
  # @return [String]
@@ -51,7 +67,23 @@ module Rails
51
67
  # @param klass [Class]
52
68
  def initialize(klass)
53
69
  @klass = klass
54
- @file_name = ::File.join(::Rails.root, MODEL_DIR, "#{klass.to_s.underscore}.rb")
70
+ base_file_name = CONFIG.schema_file? ? SCHEMA_FILE_NAME : "#{klass.to_s.underscore}.rb"
71
+ @file_name = ::File.join(::Rails.root, MODEL_DIR, base_file_name)
72
+ end
73
+
74
+ # @return [String]
75
+ def annotation_start
76
+ self.class.annotation_start(@klass)
77
+ end
78
+
79
+ # @return [String]
80
+ def annotation_end
81
+ self.class.annotation_end(@klass)
82
+ end
83
+
84
+ # @return [Regexp]
85
+ def annotation_regexp
86
+ self.class.annotation_regexp(@klass)
55
87
  end
56
88
 
57
89
  # @param :write [Boolean]
@@ -79,8 +111,10 @@ module Rails
79
111
  # @param :write [Boolean]
80
112
  # @return [Array<String>] Old file content followed by new content.
81
113
  def remove_annotation(write: true)
114
+ return ['', ''] unless ::File.exist?(@file_name)
115
+
82
116
  file_content = ::File.read(@file_name)
83
- new_file_content = file_content.sub(ANNOTATION_REGEXP, '')
117
+ new_file_content = file_content.sub(annotation_regexp, '')
84
118
  result = [file_content, new_file_content]
85
119
  return result unless write
86
120
  return result if file_content == new_file_content
@@ -93,7 +127,7 @@ module Rails
93
127
  def annotation
94
128
  doc_string = ::String.new
95
129
  doc_string << <<~DOC
96
- #{ANNOTATION_START}
130
+ #{annotation_start}
97
131
  # @!parse
98
132
  # class #{@klass} < #{@klass.superclass}
99
133
  DOC
@@ -117,7 +151,7 @@ module Rails
117
151
 
118
152
  doc_string << <<~DOC.chomp
119
153
  # end
120
- # #{ANNOTATION_END}
154
+ # #{annotation_end}
121
155
  DOC
122
156
  end
123
157
 
@@ -133,6 +167,7 @@ module Rails
133
167
  # @param content [String]
134
168
  # @return [void]
135
169
  def write_file(file_name, content)
170
+ ::FileUtils.touch(file_name) unless ::File.exists?(file_name)
136
171
  ::File.write(file_name, content)
137
172
  puts "modify".rjust(12).with_styles(:bold, :green) + " #{relative_file_name(file_name)}"
138
173
  end
@@ -3,7 +3,7 @@
3
3
  module Rails
4
4
  module Annotate
5
5
  module Solargraph
6
- VERSION = '0.3.0'
6
+ VERSION = '0.4.0'
7
7
  end
8
8
  end
9
9
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'set'
4
+ require 'fileutils'
4
5
 
5
6
  require_relative "solargraph/version"
6
7
  require_relative "solargraph/configuration"
@@ -19,11 +20,16 @@ module Rails
19
20
  CONFIG = Configuration.new
20
21
  # @return [Set<Symbol>]
21
22
  VALID_MODIFICATION_METHODS = ::Set[:annotate, :remove_annotation].freeze
23
+ # @return [String]
24
+ SCHEMA_CLASS_NAME = 'AnnotateSolargraphSchema'
25
+ # @return [String]
26
+ SCHEMA_FILE_NAME = "annotate_solargraph_schema.rb"
22
27
 
23
28
  class << self
24
29
  # @return [Array<String>] Array of changed files.
25
30
  def generate
26
31
  title 'Generating model schema annotations'
32
+ create_schema_file
27
33
  modify_models :annotate
28
34
  end
29
35
 
@@ -49,6 +55,16 @@ module Rails
49
55
 
50
56
  include TerminalColors
51
57
 
58
+ def create_schema_file
59
+ schema_file = ::File.join(::Rails.root, MODEL_DIR, SCHEMA_FILE_NAME)
60
+ return unless CONFIG.schema_file? && !::File.exist?(schema_file)
61
+
62
+ ::FileUtils.touch(schema_file)
63
+ ::File.write schema_file, <<~SCHEMA
64
+ module AnnotateSolargraphSchema; end
65
+ SCHEMA
66
+ end
67
+
52
68
  # @param method [Symbol] Name of the method that will be called on every loaded Model
53
69
  # @return [Array<String>] Array of changed files.
54
70
  def modify_models(method)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-annotate-solargraph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateusz Drewniak