forked from buildkite/emojis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
204 lines (161 loc) · 5.15 KB
/
Rakefile
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
194
195
196
197
198
199
200
201
202
203
204
require 'json'
require 'fileutils'
require 'emoji_regex'
# To run via Docker:
#
# docker run -it --rm -v "$(pwd)":/src ruby bash -c "cd /src && bundle install && rake"
desc "Pick a random emoji"
task :random do
emojis = []
emojis_dir = __dir__
Dir.glob("#{emojis_dir}/img-*.json").each do |catalogue|
emojis.concat JSON.parse(File.read(catalogue)).reverse
end
emoj = emojis[Random.rand(emojis.count)]
puts ":#{emoj['name']}:"
end
desc "Generate a table to be copy and pasted into the readme when emojis are updated"
task :default do
emojis = []
Dir.glob("img-*.json").each do |catalogue|
emojis.concat JSON.parse(File.read(catalogue)).reverse
end
groups = {}
emojis.each do |emoji|
category = emoji['category']
next if emoji['name'] =~ /skin-tone/
raise "No category for emoji: #{emoji.inspect}" if category.nil?
groups[category] ||= []
groups[category] << [
%{<img src="#{emoji['image']}" width="20" height="20" alt="#{emoji['name']}"/>},
[ emoji['name'], emoji['aliases'] ].flatten.compact.uniq.map{|s| "`:#{s}:`" }.join(", ")
]
if emoji['modifiers'] && emoji['modifiers'].length > 0
emoji['modifiers'].each do |modifier|
groups[category] << [
%{<img src="#{modifier['image']}" width="20" height="20" alt="#{emoji['name']}"/>},
"`:#{emoji['name']}::#{modifier['name']}:`"
]
end
end
end
order = ["Buildkite",
"Smileys & People",
"Animals & Nature",
"Food & Drink",
"Activities",
"Travel & Places",
"Objects",
"Symbols",
"Flags"]
order.each do |name|
rows = groups.delete(name)
# Reverse the order of the BK group so the latest emojis get added to the top
rows = rows.reverse if name == "Buildkite"
puts "### #{name}\n\n"
puts "Emoji | Aliases"
puts "----- | -------"
rows.each do |cols|
puts cols.join(" | ")
end
puts "\n"
end
if groups.keys.length > 0
raise "Now all groups were shown: `#{group.keys.inspect}`"
end
end
desc "Check there are no duplicate emoji names"
task :verify do
# These duplicates existed prior to the addition of this check,
# so just print them as a warning
ignored_duplicates = [
'cucumber',
'family',
'man-woman-boy'
]
emoji_names = []
Dir.glob("img-*.json").each do |catalogue_file|
catalogue_data = JSON.parse(File.read(catalogue_file))
catalogue_data.each do |emoji|
emoji_names.concat([ emoji['name'], emoji['aliases'] ].flatten.compact)
end
end
emoji_names.sort!
File.write(
"#{__dir__}/all_emoji_names.txt",
emoji_names.join("\n")
)
warning_emoji_names = []
error_emoji_names = []
emoji_names.group_by { |name| name }.select { |_, list| list.size > 1 }.map do |list|
name = list.first
if ignored_duplicates.include?(name)
warning_emoji_names << ":#{name}: (#{name}) refers to #{list.length} emoji"
else
error_emoji_names << ":#{name}: (#{name}) refers to #{list.length} emoji"
end
end
if warning_emoji_names.any?
puts "WARNING: Ignored duplicate emoji names:\n - #{warning_emoji_names.join("\n - ")}\n"
end
if error_emoji_names.any?
puts "ERROR: Unexpected duplicate emoji names:\n - #{error_emoji_names.join("\n - ")}\n"
exit 1
end
end
def preprocess_emoji_json(parsed)
new = []
parsed.each do |emoji|
next unless emoji["has_img_apple"]
aliases = emoji["short_names"].dup
aliases.delete(emoji["short_name"])
short_name = emoji["short_name"]
unicode_points = emoji["unified"]
category = emoji["category"]
category = "Flags" if short_name =~ /flag-/ && category.nil?
unicode = unicode_points.split('-').map(&:hex).pack('U*')
# Append VS16 to code points which require it for display
if !EmojiRegex::Regex.match?(unicode) && EmojiRegex::Regex.match?("#{unicode}\uFE0F")
unicode_points = "#{unicode_points}-FE0F"
end
modifiers = []
if emoji["skin_variations"] && !emoji["skin_variations"].empty?
emoji["skin_variations"].each_with_index do |(key, skin), i|
modifiers << {
name: "skin-tone-#{i + 2}",
image: "img-apple-64/#{skin['image']}",
unicode: skin["unified"]
}
end
end
new << {
name: short_name,
category: category,
image: "img-apple-64/#{emoji['image']}",
unicode: unicode_points,
aliases: aliases,
modifiers: modifiers
}
end
JSON.pretty_generate(new)
end
desc "Generate and show processed emoji data file"
task :generate do
parsed = JSON.parse(File.read(ENV.fetch("EMOJI_JSON")))
puts preprocess_emoji_json(parsed)
end
desc "Synchronise emoji data and images"
task :sync do
puts 'Synchronising Unicode emoji images...'
FileUtils.cp(
Dir["#{__dir__}/node_modules/emoji-datasource-apple/img/apple/64/*.png"],
"#{__dir__}/img-apple-64"
)
puts 'Synchronising Unicode emoji metadata...'
unicode_emoji_catalogue = JSON.parse(File.read("#{__dir__}/node_modules/emoji-datasource-apple/emoji.json"))
File.write(
"#{__dir__}/img-apple-64.json",
preprocess_emoji_json(unicode_emoji_catalogue)
)
puts 'Done!'
end