Class: Nova::Task::Gen1X

Inherits:
Nova::Task
  • Object
show all
Defined in:
lib/tasks/gen_1x_from_2x.rb

Instance Method Summary collapse

Instance Method Details

#invoke(params = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tasks/gen_1x_from_2x.rb', line 10

def invoke(params={})
  Dir["#{Nova.src_root}/**/*.imageset"].each do |path|
    name  = File.basename path, '.*'
    index = JSON.load File.open("#{path}/Contents.json")
    file2x = nil
    file1x = nil
    index['images'].delete_if do |image|
        image['filename'].nil?
    end
    index['images'].each do |image|
      filename = image['filename']
      case image['scale']
      when '2x'
        file2x = "#{path}/#{filename}"
      when '1x'
        file1x = "#{path}/#{filename}"
      end
    end
    if file2x
      puts "Gen 1x: #{name}"
      image2x = ImageList.new file2x
      image1x = image2x.minify
      if file1x
        image1x.write file1x
      else
        filename1x = "#{name}.png"
        file1x = "#{path}/#{filename1x}"
        image1x.write file1x
        index['images'] << { "idiom" => "universal","scale" => "1x", "filename" => filename1x }
        File.open("#{path}/Contents.json", 'w').write JSON.generate(index)
      end
    end
  end
end