class Motion::Project::Assets

Constants

ANDROID_ICONS
ANDROID_SPLASHES
BASE_ANDROID_ICON_SIZE
BASE_IOS_ICON_SIZE
BASE_SPLASH_SIZE
IOS_ICONS
IOS_SPLASHES
VERSION

Public Class Methods

new(config) click to toggle source
# File lib/motion/project/assets.rb, line 74
def initialize(config)
  @config = config
  @images = []
  @icons = Icons.new(@config, platform)
  @image_optim = '/Applications/ImageOptim.app/Contents/MacOS/ImageOptim'
  
  if ios?
    @icons << IOS_ICONS
    @splashes = IOS_SPLASHES
  end

  if android?
    @config.icon = 'icon.png'
    @icons << ANDROID_ICONS
    @splashes = ANDROID_SPLASHES
  end

  @source_icon = "./src_images/icon.png"
  @source_splash = "./src_images/splash.png"
  @output_dir = @config.resources_dirs.first
end

Public Instance Methods

generate!() click to toggle source
# File lib/motion/project/assets.rb, line 124
def generate!
  validate_source_icon
  validate_source_splash
  validate_output_dir
  generate_icons
  generate_splashes
  optimize_images
end
icons() click to toggle source
# File lib/motion/project/assets.rb, line 108
def icons
  @icons
end
icons=(icons) click to toggle source
# File lib/motion/project/assets.rb, line 112
def icons=(icons)
  @icons = Icons.new(@config, platform)
  if ios?
    @config.icons = []
  end
  @icons << icons
end
output_dir=(output_dir) click to toggle source
# File lib/motion/project/assets.rb, line 104
def output_dir=(output_dir)
  @output_dir = output_dir
end
source_icon=(source_icon) click to toggle source
# File lib/motion/project/assets.rb, line 96
def source_icon=(source_icon)
  @source_icon = source_icon
end
source_splash=(source_splash) click to toggle source
# File lib/motion/project/assets.rb, line 100
def source_splash=(source_splash)
  @source_splash = source_splash
end
splashes() click to toggle source
# File lib/motion/project/assets.rb, line 120
def splashes
  @splashes
end

Protected Instance Methods

android?() click to toggle source
# File lib/motion/project/assets.rb, line 213
def android?
  platform == :android
end
crop_image(source, path, dimensions) click to toggle source
# File lib/motion/project/assets.rb, line 180
def crop_image(source, path, dimensions)
  image = MiniMagick::Image.open(source)
  result = image.combine_options do |cmd|
    cmd.gravity(:center)
    cmd.crop("#{dimensions}!+0+0")
  end
  result.format("png")
  result.write(path)
end
generate_icons() click to toggle source
# File lib/motion/project/assets.rb, line 153
def generate_icons
  App.info "[info]", "Generating icons..."
  @icons.each do |icon|
    path = File.join(@output_dir, icon.name)
    FileUtils.mkdir_p(File.dirname(path))
    resize_image(@source_icon, path, icon.dimensions)
    @images << path
    App.info "-", icon.name
  end
end
generate_splashes() click to toggle source
# File lib/motion/project/assets.rb, line 141
def generate_splashes
  App.info "[info]", "Generating splashes..."
  @splashes.each do |splash|
    parts = splash.split('|')
    path = File.join(@output_dir, parts[0])
    FileUtils.mkdir_p(File.dirname(path))
    crop_image(@source_splash, path, parts[1])
    @images << path
    App.info "-", parts[0]
  end
end
ios?() click to toggle source
# File lib/motion/project/assets.rb, line 217
def ios?
  platform == :ios
end
optimize_images() click to toggle source
# File lib/motion/project/assets.rb, line 164
def optimize_images
  if File.exist?(@image_optim)
    App.info "[info]", "Optimizing images..."
    system("#{@image_optim} #{@images.join(' ')}")
  else
    App.info "[warning]", "motion-assets uses ImageOptim to optimize your images, please install it : https://imageoptim.com"
  end
end
platform() click to toggle source
# File lib/motion/project/assets.rb, line 221
def platform
  Motion::Project::App.template
end
resize_image(source, path, dimensions) click to toggle source
# File lib/motion/project/assets.rb, line 173
def resize_image(source, path, dimensions)
  image = MiniMagick::Image.open(source)
  image.resize(dimensions)
  image.format("png")
  image.write(path)
end
validate_output_dir() click to toggle source
# File lib/motion/project/assets.rb, line 135
def validate_output_dir
  unless File.exist?(@output_dir)
    App.fail "Output directory : #{@output_dir} doesn't exist, please create it."
  end
end
validate_source_icon() click to toggle source
# File lib/motion/project/assets.rb, line 190
def validate_source_icon
  unless File.exist?(@source_icon)
    App.fail "You have to provide a valid base icon in your rakefile : app.assets.source_icon = './some/path/image.png"
  end
  image = MiniMagick::Image.open(@source_icon)
  if ios? && image.dimensions != BASE_IOS_ICON_SIZE
    App.info "[warning]", "Your source icon image dimensions #{image.dimensions} is different from recommended dimensions : #{BASE_IOS_ICON_SIZE}"
  end
  if android? && image.dimensions != BASE_ANDROID_ICON_SIZE
    App.info "[warning]", "Your source icon image dimensions #{image.dimensions} is different from recommended dimensions : #{BASE_ANDROID_ICON_SIZE}"
  end
end
validate_source_splash() click to toggle source
# File lib/motion/project/assets.rb, line 203
def validate_source_splash
  unless File.exist?(@source_splash)
    App.fail "You have to provide a valid base splash in your rakefile : app.assets.source_splash = './some/path/image.png"
  end
  image = MiniMagick::Image.open(@source_splash)
  if image.dimensions != BASE_SPLASH_SIZE
    App.info "[warning]", "Your source splash image dimensions #{image.dimensions} is different from recommended dimensions : #{BASE_SPLASH_SIZE}"
  end
end