-
Notifications
You must be signed in to change notification settings - Fork 94
/
creator.rb
45 lines (37 loc) · 1.33 KB
/
creator.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
module Processing
# This class creates blank sketches, with the boilerplate filled in.
class Creator < BaseExporter
# Create a blank sketch, given a path.
def create!(path, args)
usage path
main_file = File.basename(path, ".rb")
# Check to make sure that the main file exists
already_exists = File.exists?(path) || File.exists?("#{File.dirname(path)}/#{main_file.underscore}.rb")
puts "That sketch already exists." and exit if already_exists
# Get the substitutions
@name = main_file.camelize
@file_name = main_file.underscore
@title = main_file.titleize
@width, @height = "500", "500"
# Make the file
dir = File.dirname path
mkdir_p dir
template = File.new("#{RP5_ROOT}/lib/templates/create/blank_sketch.rb.erb")
rendered = render_erb_from_string_with_binding(template.read, binding)
File.open(File.join(dir, "#{@file_name}.rb"), "w") do |file|
file.print rendered
end
end
# Show the help/usage message for create.
def usage(predicate)
unless predicate
puts <<-USAGE
Usage: script/generate <sketch_to_generate> <width> <height>
Width and Height are optional.
Example: script/generate fancy_drawing/app 800 600
USAGE
exit
end
end
end
end