planter 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b66f9b16f720b193d4dee6e0a23382b22a073982634bccbad2d52092516090eb
4
- data.tar.gz: 5d66d7905379b7b148586b52b85afb2d715501543e88f0d84a88973b734d9f6b
3
+ metadata.gz: d5ec6b0fc5c0ec804e4a1ea2f1c050bcad9eadcc4cea08a8816a6b3ff17a1c37
4
+ data.tar.gz: 3639096bccfaf6aac225c78c7acfc919610802f4569fa239ffe4ffe7b648edcc
5
5
  SHA512:
6
- metadata.gz: dde579bb6c055ce0c819148d1d818b39bf44ef9e75c692799dd760c7f9a96a0828d959f3204e6741fa55c41a6b9c2d8045f746142c139b5f20f1ea450ba7b259
7
- data.tar.gz: 9f208c04796ae9f80ce6d46b4ec94e56da4fc2c62c36abf3198d806621bf2c7d79d9f52fd2d30ad0ee73c94206e7f0997b953ff93dbc93b83ff8064ee7ab85ed
6
+ metadata.gz: 2da58e993a59e4019e83f9885826672158bf7483b7310ef07f86b54d93ceb3675bf7509c656a976372debac167aa2c12d6e12214d71be0acff253e0dd3d682b5
7
+ data.tar.gz: 80ce1e443cc6eafaf9eb1746f3d617fcf4b33bae7621d28e1f36a2a00a1f57a2e2638a2a629e02b0bdda049208b458a72e3bf43b4098d054e17f41ff8351d9de
data/README.md CHANGED
@@ -136,8 +136,8 @@ things to note.
136
136
 
137
137
  - The seeder will always be appended at the end of the array. If this is not the
138
138
  correct order, you'll need to adjust the array manually.
139
- - When adjusting the array, always keep the closing bracket on its own line, or
140
- the generator won't know where to put the new seeders.
139
+ - The generator can append to either multiline or inline `%i[...]` seeders
140
+ arrays.
141
141
 
142
142
  You can also tell the generator which seeding style to use.
143
143
 
@@ -50,9 +50,7 @@ module Planter
50
50
 
51
51
  create_csv(seeder) if selected_seeding_method == :csv
52
52
 
53
- inject_into_file "config/initializers/planter.rb",
54
- " #{seeder}\n",
55
- before: /^\s*\]\s*$/
53
+ register_seeder(seeder)
56
54
  end
57
55
 
58
56
  def seeder_contents
@@ -94,6 +92,110 @@ module Planter
94
92
  end.join
95
93
  end
96
94
 
95
+ def register_seeder(seeder)
96
+ contents = ::File.read(initializer_full_path)
97
+ ::File.write(initializer_full_path, add_seeder_to_initializer(contents, seeder))
98
+ end
99
+
100
+ def add_seeder_to_initializer(contents, seeder)
101
+ assignment = contents.match(seeders_assignment_pattern)
102
+ unless assignment
103
+ raise Thor::Error, "Could not find config.seeders = %i[...] in #{initializer_path}"
104
+ end
105
+
106
+ opening_bracket_index = assignment.end(0) - 1
107
+ closing_bracket_index = closing_bracket_index_for(contents, opening_bracket_index)
108
+ unless closing_bracket_index
109
+ raise Thor::Error,
110
+ "Could not find the closing bracket for config.seeders in #{initializer_path}"
111
+ end
112
+
113
+ insert_seeder(contents, seeder, assignment[1], opening_bracket_index, closing_bracket_index)
114
+ end
115
+
116
+ def insert_seeder(
117
+ contents,
118
+ seeder,
119
+ assignment_indent,
120
+ opening_bracket_index,
121
+ closing_bracket_index
122
+ )
123
+ body = contents[(opening_bracket_index + 1)...closing_bracket_index]
124
+ updated = contents.dup
125
+
126
+ if body.include?("\n")
127
+ insert_multiline_seeder(
128
+ updated,
129
+ contents,
130
+ seeder,
131
+ assignment_indent,
132
+ body,
133
+ closing_bracket_index
134
+ )
135
+ elsif body.strip.empty?
136
+ updated.insert(closing_bracket_index, seeder)
137
+ else
138
+ updated.insert(closing_bracket_index, " #{seeder}")
139
+ end
140
+
141
+ updated
142
+ end
143
+
144
+ def insert_multiline_seeder(
145
+ updated,
146
+ contents,
147
+ seeder,
148
+ assignment_indent,
149
+ body,
150
+ closing_bracket_index
151
+ )
152
+ closing_line_start = contents.rindex("\n", closing_bracket_index) + 1
153
+ closing_line_prefix = contents[closing_line_start...closing_bracket_index]
154
+
155
+ if closing_line_prefix.match?(/\A[ \t]*\z/)
156
+ updated.insert(closing_line_start, "#{seeder_indent(body, assignment_indent)}#{seeder}\n")
157
+ else
158
+ updated.insert(closing_bracket_index, " #{seeder}")
159
+ end
160
+ end
161
+
162
+ def seeder_indent(body, assignment_indent)
163
+ item_line = body.lines.find { |line| line.match?(/\S/) }
164
+ indent = item_line&.match(/\A[ \t]*/).to_s
165
+ indent.empty? ? "#{assignment_indent} " : indent
166
+ end
167
+
168
+ def closing_bracket_index_for(contents, opening_bracket_index)
169
+ depth = 1
170
+ index = opening_bracket_index + 1
171
+
172
+ while index < contents.length
173
+ if contents[index] == "\\"
174
+ index += 2
175
+ next
176
+ elsif contents[index] == "["
177
+ depth += 1
178
+ elsif contents[index] == "]"
179
+ depth -= 1
180
+ return index if depth.zero?
181
+ end
182
+
183
+ index += 1
184
+ end
185
+ end
186
+
187
+ def seeders_assignment_pattern
188
+ /^([ \t]*)config\.seeders\s*=\s*%i\[/
189
+ end
190
+
191
+ def initializer_path
192
+ "config/initializers/planter.rb"
193
+ end
194
+
195
+ def initializer_full_path
196
+ ::File.join(destination_root, initializer_path)
197
+ end
198
+
97
199
  def selected_seeding_method
98
200
  return unless options["seeding_method"]
99
201
 
@@ -21,7 +21,7 @@ module Planter
21
21
  # Patch version.
22
22
  #
23
23
  # @return [Integer]
24
- PATCH = 0
24
+ PATCH = 1
25
25
 
26
26
  module_function
27
27
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: planter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Gray