class Resper::Parser

Public Class Methods

new(type, input, width, height, output, format) click to toggle source
# File lib/resper.rb, line 32
def initialize type, input, width, height, output, format
  @type = type
  @input = input
  @width = width
  @height = height
  @output = output
  @format = format
end

Public Instance Methods

parse_resources!() click to toggle source
# File lib/resper.rb, line 41
def parse_resources!
  if @type == :all || @type == :android
          @@ANDROID_SCALES.each do |scale|
            file = for_android w: scale[:s]*@width, h: scale[:s]*@height,
                               d: scale[:d], o: "drawable-#{scale[:o]}"
            reduce_size file
          end
  end

  if @type == :all || @type == :ios
          @@IOS_UNIVERSAL_SCALES.each do |scale|
            file = for_ios scale[:s], scale[:o], scale[:d]
            reduce_size file
          end
          generate_universal_contents 
  end
  if @type == :all || @type == :react
          @@IOS_UNIVERSAL_SCALES.each do |scale|
            file = for_react scale[:s], scale[:o], scale[:d]
            reduce_size file
          end
  end

end

Private Instance Methods

android_image_name() click to toggle source
# File lib/resper.rb, line 139
def android_image_name
                    name = File.basename(@input, ".*")
  "#{name.underscore}.png"
end
android_output_path() click to toggle source
# File lib/resper.rb, line 135
def android_output_path
  @output[0]
end
convert(width:, height:, density:, to:, name: file_output = File.join(to, " click to toggle source
# File lib/resper.rb, line 106
def convert width:, height:, density:, to:, name:
  file_output = File.join(to, "#{name}")
  FileUtils.mkdir_p to
  `convert -size '#{width}x#{height}' -verbose -background none #{@format}:#{@input} #{file_output}`
  file_output
end
for_android(w:, h:, d:, o: out = File.join(android_output_path, o)) click to toggle source
# File lib/resper.rb, line 68
def for_android w:, h:, d:, o:
  out = File.join(android_output_path, o)
  convert width: w, height: h, density: d, to: out, name: android_image_name
end
for_ios(factor, postfix, density) click to toggle source
# File lib/resper.rb, line 73
def for_ios factor, postfix, density
  convert width: @width*factor, height: @height*factor, 
                                                    density: density, to: ios_output_path, name: ios_image_name(postfix)
end
for_react(factor, postfix, density) click to toggle source
# File lib/resper.rb, line 78
def for_react factor, postfix, density
  path = @output[2]
  name = "#{react_image_name_without_ext}#{postfix}.png"
  name = "#{react_image_name_without_ext}.png" if factor == 1
   
  convert width: @width*factor, height: @height*factor, density: density, to: path, name: name
end
generate_universal_contents() click to toggle source
# File lib/resper.rb, line 86
def generate_universal_contents
  images = @@IOS_UNIVERSAL_SCALES.map do |scale|
    {
      idiom: 'universal',
                                    filename: ios_image_name(scale[:o]),
      scale: "#{scale[:s].to_i}x"
    }
  end
  meta = {
    images: images,
    info: {
      version: 1,
                                    author: 'xcode'
                            }
  }
  File.open(File.join(ios_output_path, 'Contents.json'),'w') do |f|
    f.write(JSON.pretty_generate(meta))
  end
end
ios_image_name(postfix) click to toggle source
# File lib/resper.rb, line 121
def ios_image_name postfix
  "#{ios_image_name_without_ext}#{postfix}.png"
end
ios_image_name_without_ext() click to toggle source
# File lib/resper.rb, line 125
def ios_image_name_without_ext
  name = File.basename(@input, ".*").underscore
  name.split('_').map{|e| e.capitalize}.join
end
ios_output_path() click to toggle source
# File lib/resper.rb, line 117
def ios_output_path
  File.join(@output[1], "#{ios_image_name_without_ext}.imageset")
end
react_image_name_without_ext() click to toggle source
# File lib/resper.rb, line 130
def react_image_name_without_ext
  name = File.basename(@input, ".*").underscore
  name.gsub "_", "-"
end
reduce_size(png_path) click to toggle source
# File lib/resper.rb, line 113
def reduce_size png_path
  system('pngquant', "#{png_path}", '--ext=.png', '--force')
end