BigW Consortium Gitlab

gemojione.rake 4.97 KB
Newer Older
1
namespace :gemojione do
2 3 4 5 6
  desc 'Generates Emoji SHA256 digests'
  task digests: :environment do
    require 'digest/sha2'
    require 'json'

7
    dir = Gemojione.images_path
8 9 10
    digests = []
    aliases = Hash.new { |hash, key| hash[key] = [] }
    aliases_path = File.join(Rails.root, 'fixtures', 'emojis', 'aliases.json')
11

12 13 14 15
    JSON.parse(File.read(aliases_path)).each do |alias_name, real_name|
      aliases[real_name] << alias_name
    end

16
    Gitlab::AwardEmoji.emojis.map do |name, emoji_hash|
17 18 19
      fpath = File.join(dir, "#{emoji_hash['unicode']}.png")
      digest = Digest::SHA256.file(fpath).hexdigest

20 21 22 23 24
      digests << { name: name, unicode: emoji_hash['unicode'], digest: digest }

      aliases[name].each do |alias_name|
        digests << { name: alias_name, unicode: emoji_hash['unicode'], digest: digest }
      end
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    end

    out = File.join(Rails.root, 'fixtures', 'emojis', 'digests.json')

    File.open(out, 'w') do |handle|
      handle.write(JSON.pretty_generate(digests))
    end
  end

  # This task will generate a standard and Retina sprite of all of the current
  # Gemojione Emojis, with the accompanying SCSS map.
  #
  # It will not appear in `rake -T` output, and the dependent gems are not
  # included in the Gemfile by default, because this task will only be needed
  # occasionally, such as when new Emojis are added to Gemojione.
40
  task sprite: :environment do
41 42 43 44 45 46 47
    begin
      require 'sprite_factory'
      require 'rmagick'
    rescue LoadError
      # noop
    end

48 49 50 51 52
    check_requirements!

    SIZE   = 20
    RETINA = SIZE * 2

53 54 55 56 57
    # Update these values to the width and height of the spritesheet when
    # new emoji are added.
    SPRITESHEET_WIDTH = 860
    SPRITESHEET_HEIGHT = 840

58 59
    Dir.mktmpdir do |tmpdir|
      # Copy the Gemojione assets to the temporary folder for resizing
60
      FileUtils.cp_r(Gemojione.images_path, tmpdir)
61 62 63 64 65 66 67 68 69 70 71

      Dir.chdir(tmpdir) do
        Dir["**/*.png"].each do |png|
          resize!(File.join(tmpdir, png), SIZE)
        end
      end

      style_path = Rails.root.join(*%w(app assets stylesheets pages emojis.scss))

      # Combine the resized assets into a packed sprite and re-generate the SCSS
      SpriteFactory.cssurl = "image-url('$IMAGE')"
72
      SpriteFactory.run!(File.join(tmpdir, 'png'), {
73 74 75 76 77 78 79 80 81 82 83 84 85
        output_style: style_path,
        output_image: "app/assets/images/emoji.png",
        selector:     '.emoji-',
        style:        :scss,
        nocomments:   true,
        pngcrush:     true,
        layout:       :packed
      })

      # SpriteFactory's SCSS is a bit too verbose for our purposes here, so
      # let's simplify it
      system(%Q(sed -i '' "s/width: #{SIZE}px; height: #{SIZE}px; background: image-url('emoji.png')/background-position:/" #{style_path}))
      system(%Q(sed -i '' "s/ no-repeat//" #{style_path}))
86
      system(%Q(sed -i '' "s/ 0px/ 0/" #{style_path}))
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

      # Append a generic rule that applies to all Emojis
      File.open(style_path, 'a') do |f|
        f.puts
        f.puts <<-CSS.strip_heredoc
        .emoji-icon {
          background-image: image-url('emoji.png');
          background-repeat: no-repeat;
          height: #{SIZE}px;
          width: #{SIZE}px;

          @media only screen and (-webkit-min-device-pixel-ratio: 2),
                 only screen and (min--moz-device-pixel-ratio: 2),
                 only screen and (-o-min-device-pixel-ratio: 2/1),
                 only screen and (min-device-pixel-ratio: 2),
                 only screen and (min-resolution: 192dpi),
                 only screen and (min-resolution: 2dppx) {
            background-image: image-url('emoji@2x.png');
105
            background-size: #{SPRITESHEET_WIDTH}px #{SPRITESHEET_HEIGHT}px;
106 107 108 109 110 111 112 113 114
          }
        }
        CSS
      end
    end

    # Now do it again but for Retina
    Dir.mktmpdir do |tmpdir|
      # Copy the Gemojione assets to the temporary folder for resizing
115
      FileUtils.cp_r(Gemojione.images_path, tmpdir)
116 117 118 119 120 121 122 123

      Dir.chdir(tmpdir) do
        Dir["**/*.png"].each do |png|
          resize!(File.join(tmpdir, png), RETINA)
        end
      end

      # Combine the resized assets into a packed sprite and re-generate the SCSS
124
      SpriteFactory.run!(File.join(tmpdir), {
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
        output_image: "app/assets/images/emoji@2x.png",
        style:        false,
        nocomments:   true,
        pngcrush:     true,
        layout:       :packed
      })
    end
  end

  def check_requirements!
    return if defined?(SpriteFactory) && defined?(Magick)

    puts <<-MSG.strip_heredoc
      This task is disabled by default and should only be run when the Gemojione
      gem is updated with new Emojis.

      To enable this task, *temporarily* add the following lines to Gemfile and
      re-bundle:

      gem 'sprite-factory'
      gem 'rmagick'
    MSG

    exit 1
  end

  def resize!(image_path, size)
    # Resize the image in-place, save it, and free the object
    image = Magick::Image.read(image_path).first
    image.resize!(size, size)
    image.write(image_path) { self.quality = 100 }
    image.destroy!
  end
end