Sha256: 0a632e4612418f7582438b5f02e0e1c9ff2e4204369b7104ff65cc016b2e4911

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true
require_relative 'indexes_generator'
require_relative '../xcassets/xcassets'
require_relative 'scale_pathname'

module KCommercial
  module Resources
    class ImagesIndexesGenerator < IndexesGenerator

      def write_files
        xcassets = XCAssets::XCAssets.new('Images')
        asset_objects.each do |asset|
          rows = asset.trait_rows
          row_keys = rows.map {|row| row.resource}.uniq
          row_keys.each do |image_path|
            name = image_path.filename_without_scale
            imageset = XCAssets::ImageSet.new(name)
            xcassets.add_imageset(imageset)
            scale_paths = ['1x', '2x', '3x'].map { |scale|
              scale_path = image_path.parent.join("#{name}@#{scale}#{image_path.extname}")
              scale_path = image_path.parent.join("#{name}@#{scale.upcase}#{image_path.extname}") unless scale_path.exist?
              unless scale_path.exist?
                scale_path = nil
              end
              scale_path
            }
            if scale_paths.compact.count == 0
              imageset.scale2_image.resource = image_path
            else
              if scale_paths[0]
                imageset.scale1_image.resource = scale_paths[0]
              end
              if scale_paths[1]
                imageset.scale2_image.resource = scale_paths[1]
              end
              if scale_paths[2]
                imageset.scale3_image.resource = scale_paths[2]
              end
            end
          end
        end
        root_path.mkpath unless root_path.exist?
        xcassets.save(root_path)

      end

      def index_resources
        hash = super
        asset_objects.each do |asset|
          hash[asset.name] = asset.trait_rows.map do |row|
            {
              :traits => row.traits,
              :resource => row.resource.filename_without_scale
            }
          end
        end
        hash
      end

      def indexes_filename
        'images'
      end

      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
KCommercialPipeline-0.2.5.1 lib/KCommercialPipeline/core/resource/bundle/images_generator.rb