/
application_builder.rb
305 lines (253 loc) · 9.54 KB
/
application_builder.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
framework 'Foundation'
require 'fileutils'
module HotCocoa
class ApplicationBuilder
class Configuration
attr_reader :name, :identifier, :version, :icon, :resources, :sources, :info_string, :load, :agent
def initialize(file)
require 'yaml'
yml = YAML.load(File.read(file))
@name = yml["name"]
@identifier = yml["identifier"]
@load = yml["load"]
@version = yml["version"] || "1.0"
@icon = yml["icon"]
@info_string = yml["info_string"]
@sources = yml["sources"] || []
@resources = yml["resources"] || []
@overwrite = yml["overwrite"] == true ? true : false
@agent = yml["agent"] == true ? "1" : "0"
end
def overwrite?
@overwrite
end
def icon_exist?
@icon ? File.exist?(@icon) : false
end
end
ApplicationBundlePackage = "APPL????"
attr_accessor :name, :identifier, :load_file, :sources, :overwrite, :icon, :version, :info_string, :resources, :deploy, :agent
def self.build(config, options={:deploy => false})
if !config.kind_of?(Configuration) || !$LOADED_FEATURES.detect {|f| f.include?("standard_rake_tasks")}
require 'rbconfig'
puts "Your Rakefile needs to be updated. Please copy the Rakefile from:"
puts File.expand_path(File.join(Config::CONFIG['datadir'], "hotcocoa_template", "Rakefile"))
exit
end
builder = new
builder.deploy = options[:deploy] == true ? true : false
builder.name = config.name
builder.identifier = config.identifier
builder.load_file = config.load
builder.icon = config.icon if config.icon_exist?
builder.version = config.version
builder.info_string = config.info_string
builder.overwrite = config.overwrite?
builder.agent = config.agent
config.sources.each do |source|
builder.add_source_path source
end
config.resources.each do |resource|
builder.add_resource_path resource
end
builder.build
end
# Used by the "Embed MacRuby" Xcode target.
def self.deploy(path)
raise "Given path `#{path}' does not exist" unless File.exist?(path)
raise "Given path `#{path}' does not look like an application bundle" unless File.extname(path) == '.app'
deployer = new
Dir.chdir(File.dirname(path)) do
deployer.name = File.basename(path, '.app')
deployer.deploy
end
end
def initialize
@sources = []
@resources = []
end
def build
check_for_bundle_root
build_bundle_structure
write_bundle_files
copy_sources
copy_resources
deploy if deploy?
copy_icon_file if icon
end
def deploy
copy_framework
end
def deploy?
@deploy
end
def overwrite?
@overwrite
end
def add_source_path(source_file_pattern)
Dir.glob(source_file_pattern).each do |source_file|
sources << source_file
end
end
def add_resource_path(resource_file_pattern)
Dir.glob(resource_file_pattern).each do |resource_file|
resources << resource_file
end
end
private
def check_for_bundle_root
if File.exist?(bundle_root) && overwrite?
`rm -rf #{bundle_root}`
end
end
def build_bundle_structure
Dir.mkdir(bundle_root) unless File.exist?(bundle_root)
Dir.mkdir(contents_root) unless File.exist?(contents_root)
Dir.mkdir(frameworks_root) unless File.exist?(frameworks_root)
Dir.mkdir(macos_root) unless File.exist?(macos_root)
Dir.mkdir(resources_root) unless File.exist?(resources_root)
end
def write_bundle_files
write_pkg_info_file
write_info_plist_file
build_executable unless File.exist?(File.join(macos_root, objective_c_executable_file))
write_ruby_main
end
def copy_framework
unless File.exist?(File.join(frameworks_root, 'MacRuby.framework'))
FileUtils.mkdir_p frameworks_root
FileUtils.cp_r macruby_framework_path, frameworks_root
end
`install_name_tool -change #{current_macruby_path}/usr/lib/libmacruby.dylib @executable_path/../Frameworks/MacRuby.framework/Versions/#{current_macruby_version}/usr/lib/libmacruby.dylib '#{macos_root}/#{objective_c_executable_file}'`
end
def copy_sources
FileUtils.cp_r load_file, resources_root unless sources.include?(load_file)
sources.each do |source|
destination = File.join(resources_root, source)
FileUtils.mkdir_p(File.dirname(destination)) unless File.exist?(File.dirname(destination))
FileUtils.cp_r source, destination
end
end
def copy_resources
resources.each do |resource|
destination = File.join(resources_root, resource.split("/")[1..-1].join("/"))
FileUtils.mkdir_p(File.dirname(destination)) unless File.exist?(File.dirname(destination))
FileUtils.cp_r resource, destination
end
end
def copy_icon_file
FileUtils.cp(icon, icon_file) unless File.exist?(icon_file)
end
def write_pkg_info_file
File.open(pkg_info_file, "wb") {|f| f.write ApplicationBundlePackage}
end
def write_info_plist_file
File.open(info_plist_file, "w") do |f|
f.puts %{<?xml version="1.0" encoding="UTF-8"?>}
f.puts %{<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">}
f.puts %{<plist version="1.0">}
f.puts %{<dict>}
f.puts %{ <key>CFBundleDevelopmentRegion</key>}
f.puts %{ <string>English</string>}
f.puts %{ <key>CFBundleIconFile</key>} if icon
f.puts %{ <string>#{name}.icns</string>} if icon
f.puts %{ <key>CFBundleGetInfoString</key>} if info_string
f.puts %{ <string>#{info_string}</string>} if info_string
f.puts %{ <key>CFBundleExecutable</key>}
f.puts %{ <string>#{name.gsub(/ /, '')}</string>}
f.puts %{ <key>CFBundleIdentifier</key>}
f.puts %{ <string>#{identifier}</string>}
f.puts %{ <key>CFBundleInfoDictionaryVersion</key>}
f.puts %{ <string>6.0</string>}
f.puts %{ <key>CFBundleName</key>}
f.puts %{ <string>#{name}</string>}
f.puts %{ <key>CFBundlePackageType</key>}
f.puts %{ <string>APPL</string>}
f.puts %{ <key>CFBundleSignature</key>}
f.puts %{ <string>????</string>}
f.puts %{ <key>CFBundleVersion</key>}
f.puts %{ <string>#{version}</string>}
f.puts %{ <key>NSPrincipalClass</key>}
f.puts %{ <string>NSApplication</string>}
f.puts %{ <key>LSUIElement</key>}
f.puts %{ <string>#{agent}</string>}
f.puts %{</dict>}
f.puts %{</plist>}
end
end
def build_executable
File.open(objective_c_source_file, "wb") do |f|
f.puts %{
#import <MacRuby/MacRuby.h>
int main(int argc, char *argv[])
{
return macruby_main("rb_main.rb", argc, argv);
}
}
end
archs = RUBY_ARCH.include?('ppc') ? '-arch ppc' : '-arch i386 -arch x86_64'
puts `cd "#{macos_root}" && gcc main.m -o #{objective_c_executable_file} #{archs} -framework MacRuby -framework Foundation -fobjc-gc-only`
File.unlink(objective_c_source_file)
end
def write_ruby_main
File.open(main_ruby_source_file, "wb") do |f|
f.puts "$:.map! { |x| x.sub(/^\\/Library\\/Frameworks/, NSBundle.mainBundle.privateFrameworksPath) }" if deploy?
f.puts "$:.unshift NSBundle.mainBundle.resourcePath.fileSystemRepresentation"
f.puts "begin"
f.puts " load '#{load_file}'"
f.puts "rescue Exception => e"
f.puts " STDERR.puts e.message"
f.puts " e.backtrace.each { |bt| STDERR.puts bt }"
f.puts "end"
end
end
def bundle_root
"#{name}.app"
end
def contents_root
File.join(bundle_root, "Contents")
end
def frameworks_root
File.join(contents_root, "Frameworks")
end
def macos_root
File.join(contents_root, "MacOS")
end
def resources_root
File.join(contents_root, "Resources")
end
def bridgesupport_root
File.join(resources_root, "BridgeSupport")
end
def info_plist_file
File.join(contents_root, "Info.plist")
end
def icon_file
File.join(resources_root, "#{name}.icns")
end
def pkg_info_file
File.join(contents_root, "PkgInfo")
end
def objective_c_executable_file
name.gsub(/ /, '')
end
def objective_c_source_file
File.join(macos_root, "main.m")
end
def main_ruby_source_file
File.join(resources_root, "rb_main.rb")
end
def current_macruby_version
NSFileManager.defaultManager.pathContentOfSymbolicLinkAtPath(File.join(macruby_versions_path, "Current"))
end
def current_macruby_path
File.join(macruby_versions_path, current_macruby_version)
end
def macruby_versions_path
File.join(macruby_framework_path, "Versions")
end
def macruby_framework_path
"/Library/Frameworks/MacRuby.framework"
end
end
end