bullet_train-super_scaffolding 1.0.3 → 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bullet_train/super_scaffolding/engine.rb +8 -0
- data/lib/bullet_train/super_scaffolding/version.rb +1 -1
- data/lib/bullet_train/super_scaffolding.rb +0 -2
- data/lib/scaffolding/block_manipulator.rb +140 -0
- data/lib/scaffolding/script.rb +6 -0
- data/lib/scaffolding/transformer.rb +4 -3
- data/lib/tasks/bullet_train/super_scaffolding_tasks.rake +7 -1
- 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: 3bcd9e0be2aa17210990067b3a2d43f624450d380d50503be510bd2744d7da11
|
4
|
+
data.tar.gz: 2550d7faf22d005b3997d185f05dd6a4cb7c8d26c97130497e84b5dc5b3c34ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 951c8ae79449a58036bd66b87cfba7d07b6a5e33c6b2fbac99f32cf9d81ea58bc53046a793dee952d7d1455f44f2c6e970605b0ff7c0f6a6bdf17c4c602bd347
|
7
|
+
data.tar.gz: 222c2cd02aa95cc849449130a1d59647d6abf3ad5ed3e4c12524f46138432388c1e33d67c57a60c10fecc0586695477eb6c710f5ed3440960974c65f7888fe38
|
@@ -1,6 +1,14 @@
|
|
1
1
|
module BulletTrain
|
2
2
|
module SuperScaffolding
|
3
3
|
class Engine < ::Rails::Engine
|
4
|
+
initializer "bullet_train.super_scaffolding.register_template_path" do |app|
|
5
|
+
# Templates from the application itself should always be highest priority.
|
6
|
+
# This allows application developers to locally overload any template from any package.
|
7
|
+
BulletTrain::SuperScaffolding.template_paths << Rails.root.to_s
|
8
|
+
|
9
|
+
# Register the base path of this package with the Super Scaffolding engine.
|
10
|
+
BulletTrain::SuperScaffolding.template_paths << File.expand_path('../../../..', __FILE__)
|
11
|
+
end
|
4
12
|
end
|
5
13
|
end
|
6
14
|
end
|
@@ -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
@@ -3,6 +3,7 @@
|
|
3
3
|
|
4
4
|
require "scaffolding"
|
5
5
|
require "scaffolding/transformer"
|
6
|
+
require "scaffolding/block_manipulator"
|
6
7
|
require "scaffolding/class_names_transformer"
|
7
8
|
require "scaffolding/oauth_providers"
|
8
9
|
require "scaffolding/routes_file_manipulator"
|
@@ -23,6 +24,11 @@ ARGV.each do |arg|
|
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
27
|
+
def standard_protip
|
28
|
+
puts "🏆 Protip: Commit your other changes before running Super Scaffolding so it's easy to undo if you (or we) make any mistakes."
|
29
|
+
puts "If you do that, you can reset to your last commit state by using `git checkout .` and `git clean -d -f` ."
|
30
|
+
end
|
31
|
+
|
26
32
|
def check_required_options_for_attributes(scaffolding_type, attributes, child, parent = nil)
|
27
33
|
attributes.each do |attribute|
|
28
34
|
parts = attribute.split(":")
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require "indefinite_article"
|
2
2
|
require "yaml"
|
3
|
+
require "scaffolding/class_names_transformer"
|
3
4
|
|
4
5
|
class Scaffolding::Transformer
|
5
6
|
attr_accessor :child, :parent, :parents, :class_names_transformer, :cli_options, :additional_steps, :namespace, :suppress_could_not_find
|
@@ -111,11 +112,11 @@ class Scaffolding::Transformer
|
|
111
112
|
# Originally all the potential source files were in the repository alongside the application.
|
112
113
|
# Now the files could be provided by an included Ruby gem, so we allow those Ruby gems to register their base
|
113
114
|
# path and then we check them in order to see which template we should use.
|
114
|
-
BulletTrain::SuperScaffolding.template_paths.
|
115
|
+
BulletTrain::SuperScaffolding.template_paths.map do |base_path|
|
115
116
|
base_path = Pathname.new(base_path)
|
116
117
|
resolved_path = base_path.join(file).to_s
|
117
|
-
File.
|
118
|
-
end.compact.first
|
118
|
+
File.exist?(resolved_path) ? resolved_path : nil
|
119
|
+
end.compact.first || raise("Couldn't find the Super Scaffolding template for `#{file}` in any of the following locations:\n\n#{BulletTrain::SuperScaffolding.template_paths.join("\n")}")
|
119
120
|
end
|
120
121
|
|
121
122
|
def get_transformed_file_content(file)
|
@@ -1,6 +1,12 @@
|
|
1
1
|
namespace :bullet_train do
|
2
2
|
desc "Next-level code generation"
|
3
|
-
task :super_scaffolding => :environment do
|
3
|
+
task :super_scaffolding, [:all_options] => :environment do |t, arguments|
|
4
|
+
ARGV.pop while ARGV.any?
|
5
|
+
|
6
|
+
arguments[:all_options]&.split&.each do |argument|
|
7
|
+
ARGV.push(argument)
|
8
|
+
end
|
9
|
+
|
4
10
|
BulletTrain::SuperScaffolding::Runner.new.run
|
5
11
|
end
|
6
12
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Culver
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-02-
|
11
|
+
date: 2022-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 6.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 6.0.0
|
27
27
|
description: Bullet Train Super Scaffolding
|
28
28
|
email:
|
29
29
|
- andrew.culver@gmail.com
|
@@ -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
|