-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.rb
executable file
·82 lines (76 loc) · 1.91 KB
/
init.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
#!/usr/bin/env ruby
require 'liquid'
require 'yaml'
require 'fileutils'
require 'find'
# Open the properties file
properties_file = File.open "properties.yaml"
properties_yaml = properties_file.read
puts properties_yaml
puts
# Ask the user if the properties are correct
puts "Are these properties correct?"
reply = gets
if reply[0] != "y" then
puts "Aborting. Edit properties.yaml to make changes."
exit
end
# Load the properties
properties = YAML::load(properties_yaml)
# Create Liquid filters
module NameFilter
def as_path(input)
input
.tr('.', '/')
.tr('-', '')
end
def as_java_class(input)
input
.gsub(/^[a-z]|[^a-zA-Z0-9]+[a-z]/) { |a| a.upcase }
.gsub(/[^a-zA-Z0-9]/, '')
end
end
Liquid::Template.register_filter(NameFilter)
# process the files
ignore = [ ".git" ]
Find.find(".") do |path|
if ignore.include? File.basename(path) then
Find.prune
elsif FileTest.directory?(path)
next
else
name = CGI::unescape(path)
name = Liquid::Template.parse(name).render properties
# Make the directory for the file
dir = File.dirname(name)
if !File.exists? dir then
puts "Creating #{dir}"
FileUtils.mkdir_p dir
end
if name =~ /\.liquid$/ then
name = name.sub(/\.liquid$/, '')
puts "Processing #{name}..."
# read the file
template = File.open(path, "r").read
# process the file
rendering = Liquid::Template.parse(template).render properties
# write the file
File.open(name, "w").write(rendering)
# Delete the template file
File.delete path
elsif name == path then
puts "Copying #{name}..."
# do nothing - already in place
else
puts "Copying #{name}..."
FileUtils.mv(path, name)
end
end
end
puts "Successful. Cleaning up..."
files_to_delete = [ "init.rb", "README" ]
files_to_delete.each do |file|
puts "Removing #{file}..."
File.delete file
end
puts "Done!"