-
Notifications
You must be signed in to change notification settings - Fork 10
/
ios-assets.rb
148 lines (121 loc) · 4.14 KB
/
ios-assets.rb
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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env ruby
#
# https://github.com/SteveKChiu/ios-res-tool
#
# Copyright 2015, Steve K. Chiu <[email protected]>
#
# What it does is to scan iOS resources and generate R+assets.swift for
# .xcassets catalog
#
# The MIT License (http://www.opensource.org/licenses/mit-license.php)
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
require 'fileutils'
require 'pathname'
require 'optparse'
ARGV << '--help' if ARGV.empty?
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: ios-assets.rb [options]"
opts.on("--import=IOS_RES_DIR", "import from iOS resources") do |v|
options[:res] = v
end
opts.on_tail("--help", "Show this message") do
puts opts
exit
end
end.parse!
res_path = Pathname.new(File.expand_path(options[:res])) if options[:res]
unless res_path
puts "Error! resource directory not specified"
exit
end
unless res_path.exist?
puts "Error! resource directory not found: #{res_path}"
exit
end
imageset_keys = {}
spriteatlas_keys = {}
dataset_keys = {}
Pathname.glob(res_path + '*.xcassets/').each { |assets_path|
Pathname.glob(assets_path + "**/*.imageset/").each { |imageset_path|
key = imageset_path.basename.to_s.gsub(/^(.*)\.imageset$/, '\1')
imageset_keys[key] = true
}
Pathname.glob(assets_path + "**/*.spriteatlas/").each { |spriteatlas_path|
key = spriteatlas_path.basename.to_s.gsub(/^(.*)\.spriteatlas$/, '\1')
spriteatlas_keys[key] = true
}
Pathname.glob(assets_path + "**/*.dataset/").each { |dataset_path|
key = dataset_path.basename.to_s.gsub(/^(.*)\.dataset$/, '\1')
dataset_keys[key] = true
}
}
imageset_keys = imageset_keys.keys.sort
spriteatlas_keys = spriteatlas_keys.keys.sort
dataset_keys = dataset_keys.keys.sort
swift_path = res_path + "R+assets.swift"
swift_path.delete if swift_path.exist?
File.open(swift_path, 'wb') { |f|
f.write "// THIS FILE IS GENERATED BY TOOL, PLEASE DO NOT EDIT!\n\n"
f.write "import UIKit\n"
if not spriteatlas_keys.empty?
f.write "import SpriteKit\n"
end
f.write "\n"
f.write "extension R {\n\n"
if not imageset_keys.empty?
f.write " enum image : String {\n"
imageset_keys.each { |key|
f.write " case #{key}\n"
}
f.write " }\n\n"
end
if not spriteatlas_keys.empty?
f.write " enum atlas : String {\n"
spriteatlas_keys.each { |key|
f.write " case #{key}\n"
}
f.write " }\n\n"
end
if not dataset_keys.empty?
f.write " enum data : String {\n"
dataset_keys.each { |key|
f.write " case #{key}\n"
}
f.write " }\n\n"
end
f.write "}\n\n"
if not imageset_keys.empty? or not spriteatlas_keys.empty? or not dataset_keys.empty?
f.write "postfix operator ^\n\n"
end
if not imageset_keys.empty?
f.write "postfix func ^ (key: R.image) -> UIImage {\n"
f.write " return UIImage(named: key.rawValue)!\n"
f.write "}\n\n"
puts "#{imageset_keys.size} imageset found"
end
if not spriteatlas_keys.empty?
f.write "postfix func ^ (key: R.atlas) -> SKTextureAtlas {\n"
f.write " return SKTextureAtlas(named: key.rawValue)!\n"
f.write "}\n\n"
puts "#{spriteatlas_keys.size} spriteatlas found"
end
}