Class: Acbaker::AssetPack

Inherits:
Object
  • Object
show all
Defined in:
lib/acbaker/asset_pack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, options = {}) ⇒ AssetPack

Returns a new instance of AssetPack.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/acbaker/asset_pack.rb', line 7

def initialize(type, options={})
  @type = type
  if options[:json]
    @json_data = options[:json]
  else
    @json_file = File.join(File.dirname(File.expand_path(__FILE__)), "config", "#{type}.json")
    @json_data = JSON.parse(File.open(@json_file).read)
  end
  @images = @json_data['images']
  @options = self.defaults().merge(options)
end

Instance Attribute Details

#imagesObject (readonly)

Returns the value of attribute images.



5
6
7
# File 'lib/acbaker/asset_pack.rb', line 5

def images
  @images
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/acbaker/asset_pack.rb', line 5

def type
  @type
end

Instance Method Details

#defaultsObject



19
20
21
# File 'lib/acbaker/asset_pack.rb', line 19

def defaults
  {json: false}
end

#process(source_image_file, target_directory, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/acbaker/asset_pack.rb', line 23

def process(source_image_file, target_directory, &block)
  
  # Define variables
  json_output_file = File.join(target_directory, "Contents.json")
  
  # Loop through images
  @json_data['images'].each_with_index do |image_spec, index|

    # Get size
    (width_str, height_str) = image_spec['size'].split('x')
    scale = image_spec['scale'].gsub('x', '').to_i
    width = width_str.to_i * scale
    height = height_str.to_i * scale
    size_max = [width, height].max
    base_width = width / scale
    base_height = height / scale

    # Get version
    if image_spec['minimum-system-version'].nil?
      version = 'ios56'
    elsif image_spec['minimum-system-version'] == '8.0'
      version = 'ios8'
    elsif image_spec['minimum-system-version'] == '7.0'
      version = 'ios78'
    else
      version = 'ios56'
    end

    # Generate filename
    filename_array = []
    filename_array.push(@type)
    filename_array.push(image_spec['idiom']) if image_spec['idiom']
    filename_array.push(image_spec['orientation']) if image_spec['orientation']
    filename_array.push(version)

    # Add subtype
    if image_spec['subtype']
      if image_spec['subtype'] == '736h'
        filename_array.push('retina-hd-55')
      elsif image_spec['subtype'] == '667h'
        filename_array.push('retina-hd-47')
      else
        filename_array.push(image_spec['subtype'])
      end
    end

    # Add extent
    filename_array.push(image_spec['extent']) if image_spec['extent']

    # Add size
    filename_array.push(image_spec['size'])

    if scale > 1
      filename = "#{filename_array.join('-')}@#{scale}x.png"
    else
      filename = "#{filename_array.join('-')}.png"
    end

    # Generate image
    cmd = "convert #{source_image_file} -resize #{width}x#{height}^ -gravity Center -crop #{width}x#{height}+0+0 +repage #{target_directory}/#{filename}"
    system(cmd)
    
    # Trigger Callback proc
    block.call("#{target_directory}/#{filename}", index+1) if not block.nil?

    # Update json data
    image_spec['filename'] = filename

  end
  
  # Save Contents.json
  File.open(json_output_file,"w") do |f|
    f.write(JSON.pretty_generate(@json_data))
  end
  
  true
end