Commit 1a1c5adb authored by Gabriel Mazetto's avatar Gabriel Mazetto

Rewrite gemojione.rake into tanuki_emoji.rake

parent 76931e40
# frozen_string_literal: true # frozen_string_literal: true
namespace :gemojione do namespace :tanuki_emoji do
desc 'Generates Emoji SHA256 digests' desc 'Generates Emoji SHA256 digests'
task aliases: ['yarn:check', 'environment'] do task aliases: %w[environment] do
require 'json'
aliases = {} aliases = {}
index_file = File.join(Rails.root, 'fixtures', 'emojis', 'index.json') TanukiEmoji.index.all.each do |emoji|
index = Gitlab::Json.parse(File.read(index_file)) emoji.aliases.each do |emoji_alias|
aliases[TanukiEmoji::Character.format_name(emoji_alias)] = emoji.name
index.each_pair do |key, data|
data['aliases'].each do |a|
a.tr!(':', '')
aliases[a] = key
end end
end end
out = File.join(Rails.root, 'fixtures', 'emojis', 'aliases.json') aliases_json_file = File.join(Rails.root, 'fixtures', 'emojis', 'aliases.json')
File.open(out, 'w') do |handle| File.open(aliases_json_file, 'w') do |handle|
handle.write(Gitlab::Json.pretty_generate(aliases, indent: ' ', space: '', space_before: '')) handle.write(Gitlab::Json.pretty_generate(aliases, indent: ' ', space: '', space_before: ''))
end end
end end
...@@ -32,51 +25,41 @@ namespace :gemojione do ...@@ -32,51 +25,41 @@ namespace :gemojione do
# We don't have `node_modules` available in built versions of GitLab # We don't have `node_modules` available in built versions of GitLab
FileUtils.cp_r(Rails.root.join('node_modules', 'emoji-unicode-version', 'emoji-unicode-version-map.json'), File.join(Rails.root, 'fixtures', 'emojis')) FileUtils.cp_r(Rails.root.join('node_modules', 'emoji-unicode-version', 'emoji-unicode-version-map.json'), File.join(Rails.root, 'fixtures', 'emojis'))
dir = Gemojione.images_path digest_emoji_map = {}
resultant_emoji_map = {} emojis_map = {}
resultant_emoji_map_new = {}
Gitlab::Emoji.emojis.each do |name, emoji_hash| TanukiEmoji.index.all.each do |emoji|
# Ignore aliases emoji_path = File.join(TanukiEmoji.images_path, emoji.image_name)
unless Gitlab::Emoji.emojis_aliases.key?(name)
fpath = File.join(dir, "#{emoji_hash['unicode']}.png")
hash_digest = Digest::SHA256.file(fpath).hexdigest
category = emoji_hash['category'] digest_entry = {
if name == 'gay_pride_flag' category: emoji.category,
category = 'flags' moji: emoji.codepoints,
end description: emoji.description,
unicodeVersion: Gitlab::Emoji.emoji_unicode_version(emoji.name),
entry = { digest: Digest::SHA256.file(emoji_path).hexdigest
category: category,
moji: emoji_hash['moji'],
description: emoji_hash['description'],
unicodeVersion: Gitlab::Emoji.emoji_unicode_version(name),
digest: hash_digest
} }
resultant_emoji_map[name] = entry digest_emoji_map[emoji.name] = digest_entry
# Our new map is only characters to make the json substantially smaller # Our new map is only characters to make the json substantially smaller
new_entry = { emoji_entry = {
c: category, c: emoji.category,
e: emoji_hash['moji'], e: emoji.codepoints,
d: emoji_hash['description'], d: emoji.description,
u: Gitlab::Emoji.emoji_unicode_version(name) u: Gitlab::Emoji.emoji_unicode_version(emoji.name)
} }
resultant_emoji_map_new[name] = new_entry emojis_map[emoji.name] = emoji_entry
end
end end
out = File.join(Rails.root, 'fixtures', 'emojis', 'digests.json') digests_json = File.join(Rails.root, 'fixtures', 'emojis', 'digests.json')
File.open(out, 'w') do |handle| File.open(digests_json, 'w') do |handle|
handle.write(Gitlab::Json.pretty_generate(resultant_emoji_map)) handle.write(Gitlab::Json.pretty_generate(digest_emoji_map))
end end
out_new = File.join(Rails.root, 'public', '-', 'emojis', '1', 'emojis.json') emojis_json = File.join(Rails.root, 'public', '-', 'emojis', '1', 'emojis.json')
File.open(out_new, 'w') do |handle| File.open(emojis_json, 'w') do |handle|
handle.write(Gitlab::Json.pretty_generate(resultant_emoji_map_new)) handle.write(Gitlab::Json.pretty_generate(emojis_map))
end end
end end
...@@ -104,23 +87,16 @@ namespace :gemojione do ...@@ -104,23 +87,16 @@ namespace :gemojione do
SPRITESHEET_WIDTH = 860 SPRITESHEET_WIDTH = 860
SPRITESHEET_HEIGHT = 840 SPRITESHEET_HEIGHT = 840
# Set up a map to rename image files # Re-create the assets folder and copy emojis renaming them to use name instead of unicode hex
emoji_unicode_string_to_name_map = {}
Gitlab::Emoji.emojis.each do |name, emoji_hash|
# Ignore aliases
unless Gitlab::Emoji.emojis_aliases.key?(name)
emoji_unicode_string_to_name_map[emoji_hash['unicode']] = name
end
end
# Copy the Gemojione assets to the temporary folder for renaming
emoji_dir = "app/assets/images/emoji" emoji_dir = "app/assets/images/emoji"
FileUtils.rm_rf(emoji_dir) FileUtils.rm_rf(emoji_dir)
FileUtils.mkdir_p(emoji_dir, mode: 0700) FileUtils.mkdir_p(emoji_dir, mode: 0700)
FileUtils.cp_r(File.join(Gemojione.images_path, '.'), emoji_dir)
Dir[File.join(emoji_dir, "**/*.png")].each do |png| TanukiEmoji.index.all.each do |emoji|
image_path = png source = File.join(TanukiEmoji.images_path, emoji.image_name)
rename_to_named_emoji_image!(emoji_unicode_string_to_name_map, image_path) destination = File.join(emoji_dir, TanukiEmoji.name, '.png')
FileUtils.cp(source, destination)
end end
Dir.mktmpdir do |tmpdir| Dir.mktmpdir do |tmpdir|
...@@ -206,7 +182,7 @@ namespace :gemojione do ...@@ -206,7 +182,7 @@ namespace :gemojione do
return if defined?(SpriteFactory) && defined?(Magick) return if defined?(SpriteFactory) && defined?(Magick)
puts <<-MSG.strip_heredoc puts <<-MSG.strip_heredoc
This task is disabled by default and should only be run when the Gemojione This task is disabled by default and should only be run when the TanukiEmoji
gem is updated with new Emojis. gem is updated with new Emojis.
To enable this task, *temporarily* add the following lines to Gemfile and To enable this task, *temporarily* add the following lines to Gemfile and
...@@ -226,20 +202,4 @@ namespace :gemojione do ...@@ -226,20 +202,4 @@ namespace :gemojione do
image.write(image_path) { self.quality = 100 } image.write(image_path) { self.quality = 100 }
image.destroy! image.destroy!
end end
EMOJI_IMAGE_PATH_RE = /(.*?)(([0-9a-f]-?)+)\.png$/i.freeze
def rename_to_named_emoji_image!(emoji_unicode_string_to_name_map, image_path)
# Rename file from unicode to emoji name
matches = EMOJI_IMAGE_PATH_RE.match(image_path)
preceding_path = matches[1]
unicode_string = matches[2]
name = emoji_unicode_string_to_name_map[unicode_string]
if name
new_png_path = File.join(preceding_path, "#{name}.png")
FileUtils.mv(image_path, new_png_path)
new_png_path
else
puts "Warning: emoji_unicode_string_to_name_map missing entry for #{unicode_string}. Full path: #{image_path}"
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment