Class: Acbaker::AssetPack
- Inherits:
-
Object
- Object
- Acbaker::AssetPack
- Defined in:
- lib/acbaker/asset_pack.rb
Instance Attribute Summary collapse
-
#images ⇒ Object
readonly
Returns the value of attribute images.
-
#processors ⇒ Object
readonly
Returns the value of attribute processors.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #defaults ⇒ Object
-
#initialize(type, options = {}) ⇒ AssetPack
constructor
A new instance of AssetPack.
- #process(source_image_file, target_directory, &block) ⇒ Object
Constructor Details
#initialize(type, options = {}) ⇒ AssetPack
Returns a new instance of AssetPack.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/acbaker/asset_pack.rb', line 8 def initialize(type, = {}) @type = type @processors = [] if [:json] @json_data = [:json] else @json_file = File.join(File.dirname(File.(__FILE__)), "config", "#{type}.json") @json_data = JSON.parse(File.open(@json_file).read) end @images = @json_data['images'] @options = self.defaults.merge() end |
Instance Attribute Details
#images ⇒ Object (readonly)
Returns the value of attribute images.
6 7 8 |
# File 'lib/acbaker/asset_pack.rb', line 6 def images @images end |
#processors ⇒ Object (readonly)
Returns the value of attribute processors.
6 7 8 |
# File 'lib/acbaker/asset_pack.rb', line 6 def processors @processors end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/acbaker/asset_pack.rb', line 6 def type @type end |
Instance Method Details
#defaults ⇒ Object
21 22 23 |
# File 'lib/acbaker/asset_pack.rb', line 21 def defaults { json: false, gravity: 'Center', strategy: 'Cover' } end |
#process(source_image_file, target_directory, &block) ⇒ Object
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/acbaker/asset_pack.rb', line 25 def process(source_image_file, target_directory, &block) # Define variables json_output_file = File.join(target_directory, "Contents.json") # Get processors if @json_data['processors']&.length @json_data['processors'].each do |processor_spec| @processors.push(Object.const_get(processor_spec['type']).new(self, processor_spec['config'])) end else @processors = [Object.const_get("Acbaker::Processors::#{@options[:strategy]}").new(self)] end # Loop through images @json_data['images'].each_with_index.map do |image_spec, index| image_size_present = image_spec['size'] image = Magick::ImageList.new(source_image_file) image_spec['size'] = "#{image.columns}x#{image.rows}" unless image_size_present # Get size scale = image_spec['scale'].gsub('x', '').to_i if image_size_present (width_str, height_str) = image_spec['size'].split('x') width = width_str.to_i * scale height = height_str.to_i * scale else width = image.columns height = image.rows end # 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 # process image @processors.each do |processor| image = processor.run(image, image_spec, width, height) end # Generate filename if image_spec["filename"] filename = image_spec["filename"] else 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'] case image_spec['subtype'] when '736h' filename_array.push('retina-hd-55') when '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 end # save image image.write("#{target_directory}/#{filename}") # Trigger Callback proc block&.call("#{target_directory}/#{filename}", index + 1) # Update json data image_spec['filename'] = filename end # Save Contents.json @json_data.delete('processors') File.open(json_output_file, "w") do |f| f.write(JSON.pretty_generate(@json_data)) end true end |