-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-xcode-imageset
executable file
·48 lines (43 loc) · 1.29 KB
/
generate-xcode-imageset
1
2
3
4
5
6
7
8
9
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
44
45
46
47
48
#!/usr/bin/env ruby -w
require 'json'
def main
while dir = ARGV.shift
unless File.directory? dir
puts "[warn] Skipping non-directory: #{dir}"
next
end
all_images = Dir[File.join(dir, '*@[23]x.png')].map { |f| File.basename f }
image3x = File.basename Dir[File.join(dir, '*@3x.png')].first
image2x = File.basename Dir[File.join(dir, '*@2x.png')].first
image1x = (all_images - [image2x, image3x]).first
unless [2, 3].include?(all_images.size) && image2x && image3x
puts "[warn] Skipping #{dir} because 2x and 3x images are required. 1x image is optional. Max 3 images."
next
end
images = [image1x, image2x, image3x]
scale = 0
imageset_images = images
.map { |filename|
scale += 1
filename && {
idiom: 'universal',
filename: filename,
scale: "#{scale}x"
}
}
.compact
imageset = {
images: imageset_images,
info: {
version: 1,
author: 'generate-xcode-imageset'
}
}
json = JSON.pretty_generate(imageset)
path = File.join(dir, 'Contents.json')
File.write(path, json)
images_written = images.compact
puts "* Wrote #{images_written.size} images to #{path}: #{images_written.join(', ')}"
end
end
main if $0 == __FILE__