update_emoji.cr 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #
  2. # Copyright (c) 2023 supercell
  3. #
  4. # SPDX-License-Identifier: BSD-3-Clause
  5. #
  6. require "http/client"
  7. require "json"
  8. # update_github_emojis.dart now generates the emoji list using the GitHub API
  9. # to retrieve the emoji list. It uses this emoji source as a source to keep
  10. # binary compatibility with the Unicode sequences for each emoji found here.
  11. EMOJIS_JSON_RAW_URL = "https://raw.githubusercontent.com/muan/emojilib/v2.4.0/emojis.json"
  12. EMOJIS_FILE_PATH = "src/luce/legacy_emojis.cr"
  13. response = HTTP::Client.get(URI.parse EMOJIS_JSON_RAW_URL)
  14. json = Hash(String, Hash(String, JSON::Any?)).from_json response.body
  15. emoji_content = String::Builder.new
  16. emoji_content << <<-EOS
  17. # GENERATED FILE. DO NOT EDIT.
  18. #
  19. # This file was generated from emojilib's data file:
  20. # #{EMOJIS_JSON_RAW_URL}
  21. # at #{Time.utc} by the script, tools/update_emojis.cr.
  22. \n
  23. EOS
  24. emoji_content.puts "module Luce"
  25. emoji_content.puts " class LegacyEmojis"
  26. emoji_content.puts
  27. emoji_content.puts " # Returns the emoji for *name*"
  28. emoji_content.puts " def self.[](name : String) : String"
  29. emoji_content.puts " @@hash[name]"
  30. emoji_content.puts " end"
  31. emoji_content.puts
  32. emoji_content.puts " # Returns the emoji for *name*, or `nil` if no emoji exists."
  33. emoji_content.puts " def self.[]?(name : String) : String?"
  34. emoji_content.puts " @@hash.fetch(name, nil)"
  35. emoji_content.puts " end"
  36. emoji_content.puts
  37. emoji_content.puts " def self.each(& : Tuple(String, String) -> ) : Nil"
  38. emoji_content.puts " @@hash.each { |key| yield key }"
  39. emoji_content.puts " end"
  40. emoji_content.puts
  41. emoji_content.puts " def self.includes?(key : String) : Bool"
  42. emoji_content.puts " @@hash.includes?(key)"
  43. emoji_content.puts " end"
  44. emoji_content.puts
  45. emoji_content.puts " @@hash : Hash(String, String) = {"
  46. emoji_count = 0
  47. ignored = [] of String
  48. # Dump in sorted order now to facilitate comparison with new GitHub emoji
  49. sorted_keys = json.keys.sort!
  50. sorted_keys.each do |name|
  51. info = json[name]
  52. if info.has_key?("char")
  53. emoji_content.puts " \"#{name}\" => \"#{info["char"]}\","
  54. emoji_count += 1
  55. else
  56. ignored << name
  57. end
  58. end
  59. emoji_content.puts " }" # end of @@hash definition
  60. emoji_content.puts " end" # class Emojis
  61. emoji_content.puts "end" # module Luce
  62. File.write(EMOJIS_FILE_PATH, emoji_content.to_s)
  63. puts "WARNING: This updates only the LEGACY emoji - to update the active\n" +
  64. "emoji recognized by luce, execute `update_github_emojis.cr`.\n"
  65. puts "Wrote data to #{EMOJIS_FILE_PATH} for #{emoji_count} emoji, " +
  66. "ignoring #{ignored.size}: #{ignored.join(", ")}."
  67. Process.run("crystal", ["tool", "format", EMOJIS_FILE_PATH])