-
Notifications
You must be signed in to change notification settings - Fork 28
/
scarpegen.rb
193 lines (162 loc) · 9.63 KB
/
scarpegen.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# frozen_string_literal: true
require "erb"
class ScarpeGenerator
def initialize
@filename = ""
@shoes_styles = ""
end
def generate
show_welcome_message
get_filename
choice = get_choice
if choice == "1"
class_template_choice = get_class_template_choice
get_shoes_styles if class_template_choice != "1"
end
generate_files(choice, class_template_choice)
end
private
def show_welcome_message
welcome_line1 = "Welcome to SCARPE! Let's build something cool!"
puts " \e[1;32m"
puts " ╔ SCARPE ────────────────────────────────────────────────────────╗"
puts " ║ ║"
puts " ║ #{welcome_line1} ║"
puts " ║ ║"
puts " ╚────────────────────────────────────────────────────────────────╝"
puts " \e[0m\n"
end
def get_filename
print "\n\e[33mEnter the filename: \e[0m"
@filename = gets.chomp
end
def get_choice
loop do
puts " \e[32m\e[1m╭──────────────────────────────────────────────────────────────────────────────────╮"
puts " │ Do you want to generate: │"
puts " │ │"
puts " │ 1. Class │"
puts " │ 2. Module │"
puts " │ │"
print " ╰──────────────────────────────────────────────────────────────────────────────────╯\e[0m\n\n\e[33mEnter your choice:\e[0m "
choice = gets.chomp
if choice == "1" || choice == "2"
return choice
else
puts "\e[31m
╭─ error ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ Invalid choice. Please enter 1 or 2. │
│ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
\e[0m"
end
end
end
def get_class_template_choice
loop do
puts " \e[32m\e[1m╭──────────────────────────────────────────────────────────────────────────────────╮"
puts " │ Which type of class file template do you want to generate? │"
puts " │ │"
puts " │ 1. Basic class template │"
puts " │ 2. Class template with basic event bind (button) |"
puts " │ 3. Class template with shapes (e.g., star) |"
puts " │ │"
puts " ╰──────────────────────────────────────────────────────────────────────────────────╯\e[0m\n\n\e[33mEnter your choice:\e[0m "
choice = gets.chomp
if ["1", "2", "3"].include?(choice)
return choice
else
puts "\e[31m
╭─ error ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ Invalid choice. Please enter 1, 2, or 3. │
│ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
\e[0m"
end
end
end
def get_shoes_styles
print "\n\e[33mDo you want to enter Shoes styles? (y/n):\e[0m "
response = gets.chomp.downcase
@shoes_styles = response == "y" || response == "yes" ? get_properties_input : ":dummy"
end
def get_properties_input
print "\e[33mEnter the Shoes styles:(enter like :width,:height)\e[0m "
gets.chomp
end
def generate_files(choice, class_template_choice)
case choice
when "1"
generate_class_file(class_template_choice)
when "2"
generate_module_file
else
puts "\e[31mInvalid choice. Exiting...\e[0m"
exit
end
generate_webview_file
generate_example_file
add_require_relative_to_drawables_file
puts "\n\e[1;32mYayyyy! Files generated successfully!\e[0m\n"
end
def add_require_relative_to_drawables_file
drawables_file_path = "lacci/lib/shoes/drawables.rb"
filename = @filename.downcase
require_line = "require \"shoes/drawables/#{filename}\""
File.open(drawables_file_path, "a") do |file|
file.puts require_line
end
puts "Added require_relative to drawables.rb file"
end
def generate_webview_file
webview_template = File.read("templates/webview_template.erb")
webview_content = ERB.new(webview_template).result(binding_with_argument)
File.write("lib/scarpe/wv/#{@filename}.rb", webview_content)
puts "generated webview file"
add_require_relative_to_wv_file
end
def add_require_relative_to_wv_file
wv_file_path = "lib/scarpe/wv.rb"
filename = @filename.downcase
require_line = "require_relative \"wv/#{filename}\""
File.open(wv_file_path, "a") do |file|
file.puts require_line
end
puts "Added require_relative to wv.rb file"
end
def generate_example_file
example_template = File.read("templates/example_template.erb")
example_content = ERB.new(example_template).result(binding_with_argument)
File.write("examples/#{@filename}.rb", example_content)
end
def generate_class_file(class_template_choice)
case class_template_choice
when "1"
class_template_file = "templates/basic_class_template.erb"
when "2"
class_template_file = "templates/class_template_with_event_bind.erb"
when "3"
class_template_file = "templates/class_template_with_shapes.erb"
end
class_template = File.read(class_template_file)
class_content = ERB.new(class_template).result(binding_with_argument(class_template_choice))
File.write("lacci/lib/shoes/drawables/#{@filename}.rb", class_content)
end
def generate_module_file
module_template = File.read("templates/module_template.erb")
module_content = ERB.new(module_template).result(binding_with_argument(""))
File.write("lib/scarpe/#{@filename}.rb", module_content)
end
def binding_with_argument(class_template_choice = nil)
capitalized_argument = @filename.capitalize
binding.dup.tap do |b|
b.local_variable_set(:argument, capitalized_argument)
b.local_variable_set(:shoes_styles, @shoes_styles)
b.local_variable_set(:class_template_choice, class_template_choice) unless class_template_choice.nil?
end
end
end
generator = ScarpeGenerator.new
generator.generate