bullet_train-super_scaffolding 1.0.5 → 1.0.6
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 +4 -4
- data/lib/bullet_train/super_scaffolding/version.rb +1 -1
- data/lib/scaffolding/block_manipulator.rb +140 -0
- data/lib/scaffolding/script.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce5d6f3d2cb0ff2a0ed85aa5928e2a9e390b17709b41889c0fb6c25b9d7c4293
|
4
|
+
data.tar.gz: af398b0b3cbc631f42e38d8643899678399bf1d7e819a1261f0142980029ec97
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a1450d9449295670cd943ad637da3405b6b509c09905520c93bfdbab32eb471d93eedd3df5915db3cf325d77a5bd2d0ea8b4add6cf29b9811aba12b4242d74b
|
7
|
+
data.tar.gz: f5c3879c59b9ed29c0982856e206e569756f6861f5f80784ef6ae04cdc5cd315771adfebdb18bcf3b7ae0a7aa32482ddf7c9f025f90befc7f7632b7c38f18cda
|
@@ -0,0 +1,140 @@
|
|
1
|
+
class Scaffolding::BlockManipulator
|
2
|
+
attr_accessor :lines
|
3
|
+
|
4
|
+
def initialize(filepath)
|
5
|
+
@filepath = filepath
|
6
|
+
@lines = File.readlines(filepath)
|
7
|
+
end
|
8
|
+
|
9
|
+
#
|
10
|
+
# Wrap a block of ruby code inside another block
|
11
|
+
#
|
12
|
+
# @param [String] starting A string to search for at the start of the block. Eg "<%= updates_for context, collection do"
|
13
|
+
# @param [Array] with An array with two String elements. The text that should wrap the block. Eg ["<%= action_model_select_controller do %>", "<% end %>"]
|
14
|
+
#
|
15
|
+
def wrap_block(starting:, with:)
|
16
|
+
with[0] += "\n" unless with[0].match?(/\n$/)
|
17
|
+
with[1] += "\n" unless with[1].match?(/\n$/)
|
18
|
+
starting_line = find_block_start(starting)
|
19
|
+
end_line = find_block_end(starting_from: starting_line)
|
20
|
+
|
21
|
+
final = []
|
22
|
+
block_indent = ""
|
23
|
+
spacer = " "
|
24
|
+
@lines.each_with_index do |line, index|
|
25
|
+
line += "\n" unless line.match?(/\n$/)
|
26
|
+
if index < starting_line
|
27
|
+
final << line
|
28
|
+
elsif index == starting_line
|
29
|
+
block_indent = line.match(/^\s*/).to_s
|
30
|
+
final << block_indent + with[0]
|
31
|
+
final << (line.blank? ? "\n" : "#{spacer}#{line}")
|
32
|
+
elsif index > starting_line && index < end_line
|
33
|
+
final << (line.blank? ? "\n" : "#{spacer}#{line}")
|
34
|
+
elsif index == end_line
|
35
|
+
final << (line.blank? ? "\n" : "#{spacer}#{line}")
|
36
|
+
final << block_indent + with[1]
|
37
|
+
else
|
38
|
+
final << line
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
@lines = final
|
43
|
+
unless @lines.last.match?(/\n$/)
|
44
|
+
@lines.last += "\n"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def insert(content, within: nil, after: nil, before: nil, after_block: nil, append: false)
|
49
|
+
# Search for before like we do after, we'll just inject before it.
|
50
|
+
after ||= before
|
51
|
+
|
52
|
+
# If within is given, find the start and end lines of the block
|
53
|
+
content += "\n" unless content.match?(/\n$/)
|
54
|
+
start_line = 0
|
55
|
+
end_line = @lines.count - 1
|
56
|
+
if within.present?
|
57
|
+
start_line = find_block_start(within)
|
58
|
+
end_line = find_block_end(starting_from: start_line)
|
59
|
+
# start_line += 1 # ensure we actually insert the content _within_ the given block
|
60
|
+
# end_line += 1 if end_line == start_line
|
61
|
+
end
|
62
|
+
if after_block.present?
|
63
|
+
block_start = find_block_start(after_block)
|
64
|
+
block_end = find_block_end(starting_from: block_start)
|
65
|
+
start_line = block_end
|
66
|
+
end_line = @lines.count - 1
|
67
|
+
end
|
68
|
+
index = start_line
|
69
|
+
match = false
|
70
|
+
while index < end_line && !match
|
71
|
+
line = @lines[index]
|
72
|
+
if after.nil? || line.match?(after)
|
73
|
+
unless append
|
74
|
+
match = true
|
75
|
+
# We adjust the injection point if we really wanted to insert before.
|
76
|
+
insert_line(content, index - (before ? 1 : 0))
|
77
|
+
end
|
78
|
+
end
|
79
|
+
index += 1
|
80
|
+
end
|
81
|
+
|
82
|
+
return if match
|
83
|
+
|
84
|
+
# Match should always be false here.
|
85
|
+
if append && !match
|
86
|
+
insert_line(content, index - 1)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def insert_line(content, insert_at_index)
|
91
|
+
content += "\n" unless content.match?(/\n$/)
|
92
|
+
final = []
|
93
|
+
@lines.each_with_index do |line, index|
|
94
|
+
indent = line.match(/^\s*/).to_s
|
95
|
+
final << line
|
96
|
+
if index == insert_at_index
|
97
|
+
final << indent + content
|
98
|
+
end
|
99
|
+
end
|
100
|
+
@lines = final
|
101
|
+
end
|
102
|
+
|
103
|
+
def insert_block(block_content, after_block:)
|
104
|
+
block_start = find_block_start(after_block)
|
105
|
+
block_end = find_block_end(starting_from: block_start)
|
106
|
+
insert_line(block_content[0], block_end)
|
107
|
+
insert_line(block_content[1], block_end + 1)
|
108
|
+
end
|
109
|
+
|
110
|
+
def write
|
111
|
+
File.write(@filepath, @lines.join)
|
112
|
+
end
|
113
|
+
|
114
|
+
def find_block_start(starting_string)
|
115
|
+
matcher = Regexp.escape(starting_string)
|
116
|
+
starting_line = 0
|
117
|
+
|
118
|
+
@lines.each_with_index do |line, index|
|
119
|
+
if line.match?(matcher)
|
120
|
+
starting_line = index
|
121
|
+
break
|
122
|
+
end
|
123
|
+
end
|
124
|
+
starting_line
|
125
|
+
end
|
126
|
+
|
127
|
+
def find_block_end(starting_from:)
|
128
|
+
depth = 0
|
129
|
+
current_line = starting_from
|
130
|
+
@lines[starting_from..@lines.count].each_with_index do |line, index|
|
131
|
+
current_line = starting_from + index
|
132
|
+
depth += 1 if line.match?(/\s*<%.+ do .*%>/)
|
133
|
+
depth += 1 if line.match?(/\s*<% if .*%>/)
|
134
|
+
depth += 1 if line.match?(/\s*<% unless .*%>/)
|
135
|
+
depth -= 1 if line.match?(/\s*<%.* end .*%>/)
|
136
|
+
break current_line if depth == 0
|
137
|
+
end
|
138
|
+
current_line
|
139
|
+
end
|
140
|
+
end
|
data/lib/scaffolding/script.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bullet_train-super_scaffolding
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Culver
|
@@ -45,6 +45,7 @@ files:
|
|
45
45
|
- lib/bullet_train/super_scaffolding/scaffolders/oauth_provider_scaffolder.rb
|
46
46
|
- lib/bullet_train/super_scaffolding/version.rb
|
47
47
|
- lib/scaffolding.rb
|
48
|
+
- lib/scaffolding/block_manipulator.rb
|
48
49
|
- lib/scaffolding/class_names_transformer.rb
|
49
50
|
- lib/scaffolding/oauth_providers.rb
|
50
51
|
- lib/scaffolding/routes_file_manipulator.rb
|