This repository has been archived by the owner on Jun 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build
executable file
·189 lines (150 loc) · 4.24 KB
/
build
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
#!/usr/bin/env ruby
# 3024 Builder (based upon https://github.com/chriskempson/base16-builder)
require "securerandom"
require "yaml"
require "erb"
require "base64"
class Theme
BASE_PATH = File.dirname(__FILE__)
attr_accessor :template
def initialize
@scheme_dir = File.join(BASE_PATH, "schemes")
@template_dir = File.join(BASE_PATH, "templates")
@output_dir = File.join(BASE_PATH, "output")
end
def build(scheme_file)
if scheme_file then
build_single_scheme(scheme_file)
else
build_all_schemes
end
end
def build_all_schemes
scheme_files = read_scheme_dir
scheme_files.each do |scheme_file|
build_single_scheme(scheme_file)
end
end
def build_single_scheme(scheme_file)
puts scheme_file
scheme_data = read_scheme_file(scheme_file)
populate_template_variables(scheme_data)
create_output_files
end
def read_scheme_file(scheme_file)
YAML.load_file(scheme_file)
rescue StandardError
abort(read_error_message(scheme_file))
end
def read_scheme_dir
Dir.glob(File.join(@scheme_dir, '**', '*.yml'))
rescue StandardError
abort(read_error_message(scheme_dir))
end
def read_template_dir
Dir.glob(File.join(@template_dir, '**', '*.erb'))
rescue StandardError
abort(read_error_message(@template_dir))
end
def read_template_file(template_file)
File.read(template_file)
rescue StandardError
abort(read_error_message(template_file))
end
def populate_template_variables(scheme_data)
# Define ERB vars
@scheme = scheme_data["scheme"]
@author = scheme_data["author"]
@slug = slug(scheme_data["scheme"])
# Define ERB color vars
@base = {}
[
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B",
"0C", "0D", "0E", "0F"
].each do |key|
hex = scheme_data["base" + key];
@base[key] = {
"hex" => hex,
"hexbgr" => to_hex_bgr(hex),
"dhex" => to_dhex(hex),
"rgb" => to_rgb(hex),
"srgb" => to_srgb(hex)
}
end
end
def create_output_files
# Read each
read_template_dir.each do |template_file|
puts " - " + template_file # Show which file we are parsing
# Grab the results of the parsed ERB file
contents = parse_template_file(template_file)
write_output_file(template_file, contents)
end
end
def parse_template_file(template_file)
# Define ERB vars
@uuid = SecureRandom.uuid
template_contents = read_template_file(template_file)
parsed = ERB.new(template_contents)
return parsed.result(binding)
end
def write_output_file(template_file, contents)
dir_name = File.basename(File.dirname(template_file))
file_name = File.basename(template_file, ".erb")
scheme_name = slug(@scheme)
output_dir = File.join(@output_dir, dir_name)
make_dir(output_dir)
# If the filename starts with a dash, we use the dash to separate
delimiter = file_name.starts_with?("-") ? "" : "."
output_filename = File.join(output_dir,
"#{scheme_name}#{delimiter}#{file_name}")
output_file = File.open(output_filename, "w")
output_file.write(contents)
output_file.close
end
def make_dir(name)
if FileTest::directory?(name)
return
end
Dir::mkdir(name)
end
def slug(string)
string.downcase.strip.gsub(' ', '.').gsub(/[^\w-]/, '')
end
def to_hex_bgr(hex)
hex.scan(/../).reverse.join
end
def to_dhex(hex)
hex.split(//).map {|char| char + char}.join
end
def to_rgb(hex)
hex.scan(/../).map {|color| color.to_i(16)}
end
def to_srgb(hex)
hex.scan(/../).map {|color| color.to_i(16).to_f / 255 }
end
def split_by_slash(hex)
hex.scan(/.{1,2}/).join('/')
end
def read_error_message(file)
"Error reading #{file}"
end
end
# Provide starts_with? method borrowed from Rails
class String
def starts_with?(prefix)
prefix = prefix.to_s
self[0, prefix.length] == prefix
end
end
help_message = <<-EOF
3024 Builder v0.1
https://github.com/chriskempson/base16-builder
usage: ./build build all schemes
EOF
case ARGV[0]
when "-h", "--help"
puts help_message
else
Theme.new.build(ARGV[0])
end