-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipa2adhoc.rb
executable file
·358 lines (280 loc) · 9.23 KB
/
ipa2adhoc.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env ruby
# coding: UTF-8
=begin
= AdHocビルドのipaファイルからiOSデバイスに直接インストールするための plist と HTML を生成する
Authors:: GNUE(鵺)
Version:: 1.2.4 2012-06-09 gnue
Copyright:: Copyright (C) gnue, 2011-2012. All rights reserved.
License:: MIT ライセンスに準拠
AdHocビルドのipaファイルからiOSデバイスに直接インストールするための plist と HTML を生成する
== 使い方
HTMLの生成
$ ipa2adhoc.rb [-o OUTPUT] baseURL FILES…
$ ipa2adhoc.rb [-o OUTPUT] -f CONFIG [FILES…]
config.json と template.html の生成
$ ipa2adhoc.rb -g
== 設定ファイル
{
"baseURL": "http://foo.com/bar/", // ベースURL(省略時は第1引数で指定)
"template": "template.html", // テンプレートファイル(省略可)
"files": ["foo.ipa", "bar.ipa"] // ipaファイルのリスト(省略可)
}
== 注意
* ipaファイルのファイル名に空白が入っているとインストールできません
== 動作環境
* 以下のライブラリが必要です(gem でインストールできます)
* zipruby
* CFPropertyList
* json
* PNG を変換する pngcrush は iOS SDK に含まれています
== TODO
* CGI対応
== 開発履歴
* 1.2.4 2012-06-09
* ruby 1.9系において CFPropertyList::List.new でバイナリデータを読込もうとするとエラーになる問題に対応
* コマンド引数で * を指定したときに .ipa ファイル以外のファイルやディレクトリが対象になってしまいエラーになる問題に対応
* コマンド引数の解析を変更
* -v オプションでバージョン表示するようにした
* 1.2.3 2012-04-04
* 入力ファイルと出力ディレクトリが同じときエラーになってしまう問題を修正
* 出力ディレクトリの自動作成で途中のディレクトリも作成するようにした
* 1.2.2 2012-04-04
* xcode-select で pngcrush の実行ファイルを探すようにした
* 設定ファイルで template を指定しないとエラーになってしまうバグを修正
* 1.2.1 2011-09-02
* 出力ディレクトリを指定できるようにした
* 1.2 2011-09-02
* 設定ファイル(JSON)を指定できるようにした
* テンプレートを指定できるようにした(設定ファイルで指定)
* 1.1.1 2011-09-02
* アイコンがみつからなくてもエラーにならないようにした
* Info.plist に CFBundleIconFile がない場合は CFBundleIconFiles の最初の要素を使うようにした
* 1.1 2011-06-02
* バイナリplist の読み書きに CFPropertyList を使用するようにした
* RubyCocoa を使わないので Mac OS X 以外の環境でも使用できるようになりました
* Ruby 1.9 に対応
* pngcrush がない場合は iOS用の PNG をそのまま使用するようにした
* 1.0.1 2011-03-10
* Mac/PCでもアイコンが見れるように最適化前の PNG を保存するようにした
* 引数が足りないときに Usage を表示するようにした
* 1.0 2011-02-02
* とりあえず作ってみた
=end
Version = '1.2.4'
require 'rubygems'
require 'zipruby'
require 'cfpropertylist'
require 'optparse'
require 'tempfile'
require 'uri'
require 'pathname'
require 'stringio'
class IPA
XCODE_SELECT = '/usr/bin/xcode-select'
if (File.executable?(XCODE_SELECT))
TOOLS = `#{XCODE_SELECT} -print-path`.chomp+'/Platforms/iPhoneOS.platform/Developer/usr/bin'
else
TOOLS = '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin'
end
def initialize(path, baseURL)
@path = path
@baseURL = baseURL
@appInfo = appInfo(path)
end
def appInfo(path)
Zip::Archive.open(path) do |archives|
archives.each do |a|
if /^Payload\/.+.app\/Info\.plist$/ =~ a.name
plist = CFPropertyList::List.new({:data => a.read})
return CFPropertyList.native_types(plist.value)
end
end
end
end
def adhocPlist(appInfo = @appInfo)
# adHock用plistの生成
asset = {
'kind' => 'software-package',
'url' => @baseURL + URI.escape(File.basename(@path)),
}
metadata = {
'bundle-identifier' => appInfo['CFBundleIdentifier'],
'bundle-version' => appInfo['CFBundleVersion'],
'kind' => 'software',
'title' => appInfo['CFBundleDisplayName'],
}
return {'items' => [{'assets' => [asset], 'metadata' => metadata}]}
end
def writeAdhocPlist(saveName = @path)
# adHock用plistの保存
name = File.basename(saveName, '.*')
path = "#{name}.plist"
plist = CFPropertyList::List.new
plist.value = CFPropertyList.guess(adhocPlist)
plist.save(path, CFPropertyList::List::FORMAT_BINARY)
path
end
def writeIconFile(saveName = @path, appInfo = @appInfo)
# adHock用plistの保存
iconFile = appInfo['CFBundleIconFile']
iconFile = appInfo['CFBundleIconFiles'].first if ! iconFile
name = File.basename(saveName, '.*')
path = "#{name}.png"
iconFile = 'Icon.png' unless iconFile
Zip::Archive.open(@path) do |archives|
archives.each do |a|
if /^Payload\/.+.app\/#{iconFile}$/ =~ a.name
icon = a.read
pngcrush = "#{TOOLS}/pngcrush"
if File.executable?(pngcrush)
# 最適化前の PNG に戻す
temp = Tempfile.new(path)
temp.write(icon)
temp.close
system(pngcrush, '-revert-iphone-optimizations', '-q', temp.path, path)
else
File.new(path, 'w').write(icon)
end
return path
end
end
end
nil
end
def ganerate
adhoc = {}
adhoc['appName'] = @appInfo['CFBundleDisplayName']
adhoc['plistFile'] = writeAdhocPlist
adhoc['iconFile'] = writeIconFile
adhoc
end
end
def adHocHTML(baseURL, adhocs, templateFile = nil)
# adHock用HTMLの生成
html = File.read(templateFile) if templateFile
html = DATA.read.lstrip if ! html
html.gsub(/(<!-- adhoc begin -->)((.|\n)+)(<!-- adhoc end -->)/) {
st = $1
templ = $2
ed = $4
s = adhocs.map { |adhoc|
e = templ.clone
e.gsub!(/__ADHOCK_URL__/, baseURL + URI.escape(File.basename(adhoc['plistFile'])))
e.gsub!(/__APPICON__/, baseURL + URI.escape(File.basename(adhoc['iconFile']))) if adhoc['iconFile']
e.gsub!(/__APPNAME__/, adhoc['appName'])
}
st + s.join("\n") + ed
}
end
if __FILE__ == $0
# baseURL の末尾を必ず / にしておく
def directoryURL(url)
url.gsub(/\/$/, '') + '/'
end
def validFile(path, type)
abort "ERR: #{type} file '#{path}' not exits" if path && ! File.exists?(path)
end
def read_json(path)
validFile(path, 'config')
begin
require 'json'
data = File.read(path)
JSON.parse(data)
rescue Exception => e
abort e.to_s
end
end
def ganarate_templates(configFile = 'config.json', templateFile = 'template.html')
open(templateFile, 'w') { |f|
f.write(DATA.read.lstrip)
}
open(configFile, 'w') { |f|
f.write <<-EOS
{
"baseURL": "",
"template": "#{templateFile}",
"files": []
}
EOS
}
exit
end
# コマンド引数の解析
config = {}
OptionParser.new { |opts|
opts.banner = "Usage: #{opts.program_name} [-o OUTPUT] baseURL FILES…\n" +
" #{opts.program_name} [-o OUTPUT] -f CONFIG [FILES…]"
opts.on('-o OUTPUT', 'oputput directory') { |v| config['output'] = v }
opts.on('-f CONFIG', 'use json config file') { |v| config.merge!(read_json(v)) }
opts.on('-g', 'genarete config and template file') { ganarate_templates }
opts.on('-h', '--help') { abort opts.help }
opts.parse!(ARGV)
# baseURL
config['baseURL'] = ARGV.shift if ! config['baseURL']
abort opts.help if ! config['baseURL']
}
# template
validFile(config['template'], 'template')
templateFile = Pathname.new(config['template']).realpath if config['template']
# files
config['files'] = [] if ! config['files']
config['files'] += ARGV.reject { |x| x !~ /\.ipa$/ }
# output
if config['output'] then
require 'fileutils'
output_dir = File.expand_path(config['output'])
FileUtils.mkpath(output_dir)
config['files'].each do |f|
dir = File.expand_path(File.dirname(f))
FileUtils.cp(f, output_dir) unless dir == output_dir
end
Dir.chdir(output_dir)
end
# 初期化
adhocs = []
# baseURL の末尾を必ず / にしておく
baseURL = directoryURL(config['baseURL'])
# adHock用のファイル生成
config['files'].each do |f|
ipa = IPA.new(f, baseURL)
adhocs << ipa.ganerate
end
# Webページの生成
File.open("index.html", 'w') { |f|
f.print adHocHTML(baseURL, adhocs, templateFile && templateFile.to_s)
}
end
__END__
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" id="viewport" content="width=320, user-scalable=no, minimum-scale=1, maximum-scale=1" />
<title>AdHoc</title>
<style>
body {
font-family: "Helvetica";
padding: 10px;
}
ul.adhoc {
list-style-type: none;
}
.adhoc img {
vertical-align: middle;
width: 57px;
height: 57px;
margin: 4px;
}
</style>
</head>
<body>
<ul class="adhoc">
<!-- adhoc begin -->
<li>
<img src="__APPICON__"/>
__APPNAME__
<a href="itms-services://?action=download-manifest&url=__ADHOCK_URL__">Install</a>
</li>
<!-- adhoc end -->
</ul>
</body>
</html>