rails-annotate-solargraph 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/Gemfile.lock +1 -1
- data/README.md +2 -2
- data/{readme_assets → assets}/annotation_generation_demo.gif +0 -0
- data/{readme_assets → assets}/automatic_annotations_demo.gif +0 -0
- data/lib/generators/annotate/solargraph/templates/rails_annotate_solargraph.rake +2 -2
- data/lib/rails/annotate/solargraph/model.rb +93 -12
- data/lib/rails/annotate/solargraph/terminal_colors.rb +75 -0
- data/lib/rails/annotate/solargraph/version.rb +1 -1
- data/lib/rails/annotate/solargraph.rb +12 -2
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a66619cade62b6636a6110b2b6f61fce7c721ed467f1aece9e04276e8a02f3b
|
4
|
+
data.tar.gz: 0ee08a9c5c439826ce36b7f502a75789e57894329ea4d8f77d1bfcade1972101
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65f3e9b439dacbe375872efe1b1d792c97227818f4673ae45de7638e6ec015f96f69f89f8e8062700700d113441db05adc65f614d9716d325236fcb8a79855b6
|
7
|
+
data.tar.gz: 8edbd94f4559ed11b5566aa07e5c16a4e9641df3ae40bbec7a6bbeeb5351cdccd31570f448376b103be6b57b8682704f77f0e055bd67ab2ce588998a6037f5a3
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,19 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.2.1] - 2022-04-16
|
4
|
+
|
5
|
+
- Rakefile template refactoring and bug fix
|
6
|
+
|
7
|
+
## [0.2.0] - 2022-04-16
|
8
|
+
|
9
|
+
- Associations get fully documented
|
10
|
+
|
11
|
+
## [0.1.1] - 2022-04-15
|
12
|
+
|
13
|
+
- Minor bug fix after the initial release
|
14
|
+
|
3
15
|
## [0.1.0] - 2022-04-15
|
4
16
|
|
5
17
|
- Initial release
|
18
|
+
- Automatic generation of annotations after migrations
|
19
|
+
- Manual rake tasks `annotate:solargraph:generate`, `annotate:solargraph:remove`
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -11,12 +11,12 @@ as a Ruby language server for your IDE).
|
|
11
11
|
|
12
12
|
Here's how you can generate and use these annotations.
|
13
13
|
|
14
|
-
![Annotation Generation Gif](
|
14
|
+
![Annotation Generation Gif](assets/annotation_generation_demo.gif)
|
15
15
|
|
16
16
|
They're automatically updated and generated when you execute migrations
|
17
17
|
in the development environment.
|
18
18
|
|
19
|
-
![Automatic Annotations Gif](
|
19
|
+
![Automatic Annotations Gif](assets/automatic_annotations_demo.gif)
|
20
20
|
|
21
21
|
## Installation
|
22
22
|
|
File without changes
|
File without changes
|
@@ -5,12 +5,12 @@ if ::Rails.env.development?
|
|
5
5
|
namespace :solargraph do
|
6
6
|
desc "Add YARD comments documenting the models' schemas"
|
7
7
|
task generate: :environment do
|
8
|
-
|
8
|
+
::Rails::Annotate::Solargraph.generate
|
9
9
|
end
|
10
10
|
|
11
11
|
desc "Remove YARD comments documenting the models' schemas"
|
12
12
|
task remove: :environment do
|
13
|
-
|
13
|
+
::Rails::Annotate::Solargraph.remove
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -4,9 +4,15 @@ module Rails
|
|
4
4
|
module Annotate
|
5
5
|
module Solargraph
|
6
6
|
class Model
|
7
|
+
using TerminalColors::Refinement
|
8
|
+
|
9
|
+
# @return [String]
|
7
10
|
ANNOTATION_START = "\n# %%<RailsAnnotateSolargraph:Start>%%"
|
11
|
+
# @return [String]
|
8
12
|
ANNOTATION_END = "%%<RailsAnnotateSolargraph:End>%%\n\n"
|
13
|
+
# @return [Regexp]
|
9
14
|
ANNOTATION_REGEXP = /#{ANNOTATION_START}.*#{ANNOTATION_END}/m.freeze
|
15
|
+
# @return [Regexp]
|
10
16
|
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
|
11
17
|
|
12
18
|
class << self
|
@@ -51,7 +57,7 @@ module Rails
|
|
51
57
|
# @param :write [Boolean]
|
52
58
|
# @return [String] New file content.
|
53
59
|
def annotate(write: true)
|
54
|
-
file_content = remove_annotation write: false
|
60
|
+
old_content, file_content = remove_annotation write: false
|
55
61
|
|
56
62
|
if CONFIG.annotation_position == :top
|
57
63
|
magic_comments = file_content.scan(MAGIC_COMMENT_REGEXP).flatten.compact.join
|
@@ -63,35 +69,43 @@ module Rails
|
|
63
69
|
end
|
64
70
|
|
65
71
|
return new_file_content unless write
|
66
|
-
|
72
|
+
# debugger
|
73
|
+
return new_file_content if old_content == new_file_content
|
67
74
|
|
68
|
-
|
75
|
+
write_file @file_name, new_file_content
|
69
76
|
new_file_content
|
70
77
|
end
|
71
78
|
|
72
79
|
# @param :write [Boolean]
|
73
|
-
# @return [String]
|
80
|
+
# @return [Array<String>] Old file content followed by new content.
|
74
81
|
def remove_annotation(write: true)
|
75
82
|
file_content = ::File.read(@file_name)
|
76
83
|
new_file_content = file_content.sub(ANNOTATION_REGEXP, '')
|
77
|
-
|
78
|
-
return
|
84
|
+
result = [file_content, new_file_content]
|
85
|
+
return result unless write
|
86
|
+
return result if file_content == new_file_content
|
79
87
|
|
80
|
-
|
81
|
-
|
88
|
+
write_file @file_name, new_file_content
|
89
|
+
result
|
82
90
|
end
|
83
91
|
|
84
92
|
# @return [String]
|
85
93
|
def annotation
|
86
|
-
|
87
|
-
|
94
|
+
doc_string = ::String.new
|
95
|
+
doc_string << <<~DOC
|
88
96
|
#{ANNOTATION_START}
|
89
97
|
# @!parse
|
90
98
|
# class #{@klass} < #{@klass.superclass}
|
91
99
|
DOC
|
92
100
|
|
101
|
+
@klass.reflections.sort.each do |attr_name, reflection|
|
102
|
+
next document_polymorphic_relation(doc_string, attr_name, reflection) if reflection.polymorphic?
|
103
|
+
|
104
|
+
document_relation(doc_string, attr_name, reflection)
|
105
|
+
end
|
106
|
+
|
93
107
|
@klass.attribute_types.each do |name, attr_type|
|
94
|
-
|
108
|
+
doc_string << <<~DOC
|
95
109
|
# # Database column `#{@klass.table_name}.#{name}`, type: `#{attr_type.type}`.
|
96
110
|
# # @param val [#{yard_type attr_type}, nil]
|
97
111
|
# def #{name}=(val); end
|
@@ -101,7 +115,7 @@ module Rails
|
|
101
115
|
DOC
|
102
116
|
end
|
103
117
|
|
104
|
-
|
118
|
+
doc_string << <<~DOC.chomp
|
105
119
|
# end
|
106
120
|
# #{ANNOTATION_END}
|
107
121
|
DOC
|
@@ -109,6 +123,73 @@ module Rails
|
|
109
123
|
|
110
124
|
private
|
111
125
|
|
126
|
+
# @param file_name [String]
|
127
|
+
# @return [String]
|
128
|
+
def relative_file_name(file_name)
|
129
|
+
file_name.delete_prefix("#{::Rails.root}/")
|
130
|
+
end
|
131
|
+
|
132
|
+
# @param file_name [String]
|
133
|
+
# @param content [String]
|
134
|
+
# @return [void]
|
135
|
+
def write_file(file_name, content)
|
136
|
+
::File.write(file_name, content)
|
137
|
+
puts "modify".rjust(12).with_styles(:bold, :green) + " #{relative_file_name(file_name)}"
|
138
|
+
end
|
139
|
+
|
140
|
+
# @return [String]
|
141
|
+
def klass_relation_name
|
142
|
+
@klass.table_name[..-2]
|
143
|
+
end
|
144
|
+
|
145
|
+
# @param doc_string [String]
|
146
|
+
# @param attr_name [String]
|
147
|
+
# @param reflection [ActiveRecord::Reflection::AbstractReflection]
|
148
|
+
# @return [void]
|
149
|
+
def document_relation(doc_string, attr_name, reflection)
|
150
|
+
db_description = \
|
151
|
+
case reflection
|
152
|
+
when ::ActiveRecord::Reflection::BelongsToReflection
|
153
|
+
type_docstring = reflection.klass
|
154
|
+
"`belongs_to` relation with `#{reflection.klass}`. Database column `#{@klass.table_name}.#{reflection.foreign_key}`."
|
155
|
+
when ::ActiveRecord::Reflection::HasOneReflection
|
156
|
+
type_docstring = reflection.klass
|
157
|
+
"`has_one` relation with `#{reflection.klass}`. Database column `#{reflection.klass.table_name}.#{reflection.foreign_key}`."
|
158
|
+
when ::ActiveRecord::Reflection::HasManyReflection
|
159
|
+
type_docstring = "Array<#{reflection.klass}>"
|
160
|
+
"`has_many` relation with `#{reflection.klass}`. Database column `#{reflection.klass.table_name}.#{reflection.foreign_key}`."
|
161
|
+
end
|
162
|
+
|
163
|
+
doc_string << <<~DOC
|
164
|
+
# # #{db_description}
|
165
|
+
# # @param val [#{type_docstring}, nil]
|
166
|
+
# def #{attr_name}=(val); end
|
167
|
+
# # #{db_description}
|
168
|
+
# # @return [#{type_docstring}, nil]
|
169
|
+
# def #{attr_name}; end
|
170
|
+
DOC
|
171
|
+
end
|
172
|
+
|
173
|
+
# @param doc_string [String]
|
174
|
+
# @param attr_name [String]
|
175
|
+
# @param reflection [ActiveRecord::Reflection::AbstractReflection]
|
176
|
+
# @return [void]
|
177
|
+
def document_polymorphic_relation(doc_string, attr_name, reflection)
|
178
|
+
classes = Solargraph.model_classes.select do |model_class|
|
179
|
+
model_class.reflections[klass_relation_name]&.options&.[](:as)&.to_sym == attr_name.to_sym
|
180
|
+
end
|
181
|
+
|
182
|
+
classes_string = classes.join(', ')
|
183
|
+
doc_string << <<~DOC
|
184
|
+
# # Polymorphic relation. Database columns `#{@klass.table_name}.#{attr_name}_id` and `#{@klass.table_name}.#{attr_name}_type`.
|
185
|
+
# # @param val [#{classes_string}, nil]
|
186
|
+
# def #{attr_name}=(val); end
|
187
|
+
# # Polymorphic relation. Database columns `#{@klass.table_name}.#{attr_name}_id` and `#{@klass.table_name}.#{attr_name}_type`.
|
188
|
+
# # @return [#{classes_string}, nil]
|
189
|
+
# def #{attr_name}; end
|
190
|
+
DOC
|
191
|
+
end
|
192
|
+
|
112
193
|
# @param attr_type [ActiveModel::Type::Value]
|
113
194
|
# @return [String]
|
114
195
|
def yard_type(attr_type)
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Annotate
|
5
|
+
module Solargraph
|
6
|
+
module TerminalColors
|
7
|
+
extend self
|
8
|
+
|
9
|
+
# @return [Hash{Symbol => String}]
|
10
|
+
MAP = {
|
11
|
+
blue: (BLUE = "\033[94m"),
|
12
|
+
cyan: (CYAN = "\033[96m"),
|
13
|
+
green: (GREEN = "\033[92m"),
|
14
|
+
yellow: (YELLOW = "\033[93m"),
|
15
|
+
red: (RED = "\033[91m"),
|
16
|
+
terminate: (TERMINATE = "\033[0m"),
|
17
|
+
bold: (BOLD = "\033[1m"),
|
18
|
+
italic: (ITALIC = "\033[3m"),
|
19
|
+
underline: (UNDERLINE = "\033[4m")
|
20
|
+
}.freeze
|
21
|
+
|
22
|
+
class << self
|
23
|
+
# Style a string with an ASCII escape code
|
24
|
+
#
|
25
|
+
# @param string [String]
|
26
|
+
# @param style [Symbol]
|
27
|
+
# @return [String]
|
28
|
+
def with_style(string, style)
|
29
|
+
"#{MAP[style]}#{string}#{TERMINATE}"
|
30
|
+
end
|
31
|
+
|
32
|
+
# Style a string with multiple ASCII escape codes
|
33
|
+
#
|
34
|
+
# @param string [String]
|
35
|
+
# @param styles [Array<Symbol>]
|
36
|
+
# @return [String]
|
37
|
+
def with_styles(string, *styles)
|
38
|
+
result = ::String.new
|
39
|
+
styles.each do |style|
|
40
|
+
result << MAP[style]
|
41
|
+
end
|
42
|
+
|
43
|
+
result << "#{string}#{TERMINATE}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def title_string(string)
|
48
|
+
TerminalColors.with_styles "== #{string} ==", :cyan, :underline, :italic
|
49
|
+
end
|
50
|
+
|
51
|
+
def error_string(string)
|
52
|
+
TerminalColors.with_styles "!! #{string} !!", :bold, :red
|
53
|
+
end
|
54
|
+
|
55
|
+
def title(string)
|
56
|
+
puts "\n", title_string(string)
|
57
|
+
end
|
58
|
+
|
59
|
+
def error(string)
|
60
|
+
puts "\n", error_string(string)
|
61
|
+
end
|
62
|
+
|
63
|
+
module Refinement
|
64
|
+
refine ::String do
|
65
|
+
# @param styles [Array<Symbol]>
|
66
|
+
# @return [self] Colored string
|
67
|
+
def with_styles(*styles)
|
68
|
+
::Rails::Annotate::Solargraph::TerminalColors.with_styles(self, *styles)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -4,6 +4,7 @@ require 'set'
|
|
4
4
|
|
5
5
|
require_relative "solargraph/version"
|
6
6
|
require_relative "solargraph/configuration"
|
7
|
+
require_relative "solargraph/terminal_colors"
|
7
8
|
require_relative "solargraph/model"
|
8
9
|
|
9
10
|
module Rails
|
@@ -16,15 +17,19 @@ module Rails
|
|
16
17
|
RAKEFILE_NAME = 'rails_annotate_solargraph.rake'
|
17
18
|
# @return [Configuration]
|
18
19
|
CONFIG = Configuration.new
|
20
|
+
# @return [Set<Symbol>]
|
21
|
+
VALID_MODIFICATION_METHODS = ::Set[:annotate, :remove_annotation].freeze
|
19
22
|
|
20
23
|
class << self
|
21
24
|
# @return [Array<String>] Array of changed files.
|
22
25
|
def generate
|
26
|
+
title 'Generating model schema annotations'
|
23
27
|
modify_models :annotate
|
24
28
|
end
|
25
29
|
|
26
30
|
# @return [Array<String>] Array of changed files.
|
27
31
|
def remove
|
32
|
+
title 'Removing model schema annotations'
|
28
33
|
modify_models :remove_annotation
|
29
34
|
end
|
30
35
|
|
@@ -35,10 +40,15 @@ module Rails
|
|
35
40
|
|
36
41
|
alias call generate
|
37
42
|
|
38
|
-
|
43
|
+
# @return [Array<ActiveRecord::Base>]
|
44
|
+
def model_classes
|
45
|
+
@model_classes ||= (::ApplicationRecord rescue ::ActiveRecord::Base).subclasses.sort_by(&:name)
|
46
|
+
end
|
39
47
|
|
40
48
|
private
|
41
49
|
|
50
|
+
include TerminalColors
|
51
|
+
|
42
52
|
# @param method [Symbol] Name of the method that will be called on every loaded Model
|
43
53
|
# @return [Array<String>] Array of changed files.
|
44
54
|
def modify_models(method)
|
@@ -49,7 +59,7 @@ module Rails
|
|
49
59
|
model_files = ::Dir[::File.join(::Rails.root, MODEL_DIR, '**/*.rb')].map { |file| file.sub("#{::Rails.root}/", '') }.to_set
|
50
60
|
|
51
61
|
::Rails.application.eager_load!
|
52
|
-
|
62
|
+
model_classes.each do |subclass|
|
53
63
|
subclass_file = ::File.join MODEL_DIR, "#{subclass.to_s.underscore}.rb"
|
54
64
|
next unless model_files.include? subclass_file
|
55
65
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-annotate-solargraph
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mateusz Drewniak
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-04-
|
11
|
+
date: 2022-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -48,6 +48,8 @@ files:
|
|
48
48
|
- LICENSE.txt
|
49
49
|
- README.md
|
50
50
|
- Rakefile
|
51
|
+
- assets/annotation_generation_demo.gif
|
52
|
+
- assets/automatic_annotations_demo.gif
|
51
53
|
- bin/console
|
52
54
|
- bin/setup
|
53
55
|
- lib/generators/annotate/solargraph/install_generator.rb
|
@@ -55,10 +57,9 @@ files:
|
|
55
57
|
- lib/rails/annotate/solargraph.rb
|
56
58
|
- lib/rails/annotate/solargraph/configuration.rb
|
57
59
|
- lib/rails/annotate/solargraph/model.rb
|
60
|
+
- lib/rails/annotate/solargraph/terminal_colors.rb
|
58
61
|
- lib/rails/annotate/solargraph/version.rb
|
59
62
|
- rails-annotate-solargraph.gemspec
|
60
|
-
- readme_assets/annotation_generation_demo.gif
|
61
|
-
- readme_assets/automatic_annotations_demo.gif
|
62
63
|
- sig/rails/annotate/solargraph.rbs
|
63
64
|
homepage: https://gitlab.com/mateuszdrewniak/rails-annotate-solargraph
|
64
65
|
licenses:
|