Class: Rake::CocoaApplication

Inherits:
CocoaTask
  • Object
show all
Defined in:
lib/rake/cocoa.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from CocoaTask

#generate_compilation_tasks

Constructor Details

#initialize {|_self| ... } ⇒ CocoaApplication

Returns a new instance of CocoaApplication.

Yields:

  • (_self)

Yield Parameters:



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
# File 'lib/rake/cocoa.rb', line 127

def initialize
  @name = :app
  @application = "RubyObjC"
  @identifier = 'com.rubyobjc.app'
  @icon_file = ''
  @creator_code = '????'
  @info = nil
  @resources = []

  @arch = "-arch i386 -arch ppc"  # build universal apps by default
  @cflags = "-g -Wall"

  @frameworks = %w{Cocoa}
  @libs = %w{objc rubyobjc}
  @lib_dirs = []
  @ldflags = ""
   
  if @gcc_objects != []
    if File.exist? "/System/Library/Frameworks/Ruby.framework"
      @frameworks << "Ruby"
    else
      @libs << "ruby-static"
      if File.exist? "/usr/local/lib/libruby-static.a"
        @lib_dirs << "/usr/local/lib"
      elsif File.exist? "/opt/local/lib/libruby-static.a"
        @lib_dirs << "/opt/local/lib" # macports
        @arch = "" # macports are never universal
      else
        raise "can't find Ruby.framework or libruby-static.a"
      end
    end
  end

  yield self if block_given?
  
  @app_dir         = "#{@application}.app"
  @contents_dir    = "#{@app_dir}/Contents"
  @executable_dir  = "#{@contents_dir}/MacOS"
  @resource_dir    = "#{@contents_dir}/Resources"
  @localized_dir   = "#{@contents_dir}/Resources/English.lproj"
  FileList[@app_dir, @contents_dir, @executable_dir, @resource_dir, @localized_dir].each {|d| directory d}

  task :default => :app

  desc "Build the application."
  task :app => [:executable, :resources, :info_plist, :pkginfo]

  desc "Run the application."
  task :run => :app do
    sh "open '#{@application}.app'"
  end

  desc "Debug the application by running it from the console; log messages will be displayed in the terminal."
  task :debug => :app do
    sh "'#{@executable_dir}/#{@application}'"
  end

  desc "Debug the application with gdb."
  task :gdb => :app do
    sh "gdb '#{@executable_dir}/#{@application}'"
  end

  desc "Build a disk image for distributing the application."
  task :dmg => :app do
    sh "rm -rf '#{@application}.dmg' dmg"
    sh "mkdir dmg; cp -r '#{@application}.app' dmg"
    sh "hdiutil create -srcdir dmg '#{@application}.dmg' -volname '#{@application}'"
    sh "rm -rf dmg"
  end

  generate_compilation_tasks

  @ruby_files = File.exist?("ruby") ? FileList['ruby/*.rb'] : FileList['*.rb'] 
  @nu_files = File.exist?("nu") ? FileList['nu/*.nu'] : FileList['*.nu'] 
  
  desc "Create the executable (subtask of app)."
  task :executable  => [:generated, @executable_dir, "#{@executable_dir}/#{@application}"]
  if @gcc_objects == []
    file "#{@executable_dir}/#{@application}" => BASEAPP do |t|
      sh "cp #{BASEAPP} '#{t.name}'"
      sh "chmod +x '#{t.name}'"
    end
  else
    file "#{@executable_dir}/#{@application}" => @gcc_objects do |t|
      sh "#{@cc} #{@gcc_objects} #{@cflags} #{@arch} #{@ldflags} -o '#{t.name}'"
    end
  end

  desc "Copy files to the application's Resources directory (subtask of app)."
  task :resources   => [@resource_dir, :infoplist_strings, :mainmenu]
  [@ruby_files, @nu_files, @icon_files, @resources].each{|list| list.each {|f|
    task :resources => "#{@resource_dir}/#{f.split("/")[-1]}"
    file "#{@resource_dir}/#{f.split("/")[-1]}" => f do |t|
      cp_r f, t.name
    end
  }}

  @nib_files.each {|f|
    g = f.split('/')[-1]
    task :resources => "#{@localized_dir}/#{g}"
    file "#{@localized_dir}/#{g}" => f do |t|
      cp_r "English.lproj/#{g}", t.name
    end
  }

  desc "Copy the default MainMenu.nib file, if necessary."
  task :mainmenu => @localized_dir do |t|
    cp_r LIBDIR+"/resources/English.lproj/MainMenu.nib", @localized_dir+"/MainMenu.nib" unless File.exist? "English.lproj/MainMenu.nib"
  end

  desc "Copy the default InfoPlist.strings file, if necessary."
  task :infoplist_strings => @localized_dir do |t|
    cp_r LIBDIR+"/resources/English.lproj/InfoPlist.strings", @localized_dir+"/InfoPlist.strings"
  end

  if File.exist?("lib")
    task :resources => "#{@resource_dir}/lib"
    file "#{@resource_dir}/lib" => "lib" do |t|
      sh "rm -rf #{t.name}"
      cp_r "lib", t.name
    end
  end

  desc "Create the Info.plist file (subtask of app)."
  task :info_plist  => [@contents_dir, "#{@contents_dir}/Info.plist"]
  file "#{@contents_dir}/Info.plist" do |t|
    info = {
      :CFBundleDevelopmentRegion => "English",
      :CFBundleExecutable => @application,
      :CFBundleIconFile => @icon_file,
      :CFBundleIdentifier => @identifier,
      :CFBundleInfoDictionaryVersion => "6.0",
      :CFBundleName => @application,
      :CFBundlePackageType => "APPL",
      :CFBundleSignature => @creator_code,
      :CFBundleVersion => "1.0",
      :NSMainNibFile => "MainMenu",
      :NSPrincipalClass => "NSApplication"
    }
    info = info.merge(@info) if @info
    plist = PList.generate(info)
    File.open(t.name, "w") {|f| f.write plist}
  end

  desc "Create the PkgInfo file (subtask of app)."
  task :pkginfo     => [@contents_dir, "#{@contents_dir}/PkgInfo"]
  file "#{@contents_dir}/PkgInfo" do |t|
    sh "echo -n 'APPL#{@creator_code}' > '#{t.name}'"
  end

  CLEAN.include("**/*.o")                             # all object files
  CLOBBER.include("*.lproj/*~.nib")			              # backup nib files
  CLOBBER.include("#{@app_dir}")                      # the application
  CLOBBER.include(".gdb_history")                     # support files

  if @gcc_objects != []
    desc "Create a bundle containing all objc sources; bundles can be loaded for testing and debugging in Ruby."
    task :bundle => [:generated, "#{@application.downcase}.bundle"]
    file "#{@application.downcase}.bundle" => @gcc_objects do |t|
      sh "#{@cc} #{@gcc_objects} #{@cflags} #{@arch} #{@ldflags} -o '#{@application.downcase}.bundle' -bundle"
    end
  end
end

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



125
126
127
# File 'lib/rake/cocoa.rb', line 125

def application
  @application
end

#archObject

Returns the value of attribute arch.



125
126
127
# File 'lib/rake/cocoa.rb', line 125

def arch
  @arch
end

#cflagsObject

Returns the value of attribute cflags.



125
126
127
# File 'lib/rake/cocoa.rb', line 125

def cflags
  @cflags
end

#frameworksObject

Returns the value of attribute frameworks.



125
126
127
# File 'lib/rake/cocoa.rb', line 125

def frameworks
  @frameworks
end

#icon_fileObject

Returns the value of attribute icon_file.



125
126
127
# File 'lib/rake/cocoa.rb', line 125

def icon_file
  @icon_file
end

#identifierObject

Returns the value of attribute identifier.



125
126
127
# File 'lib/rake/cocoa.rb', line 125

def identifier
  @identifier
end

#infoObject

Returns the value of attribute info.



125
126
127
# File 'lib/rake/cocoa.rb', line 125

def info
  @info
end

#ldflagsObject

Returns the value of attribute ldflags.



125
126
127
# File 'lib/rake/cocoa.rb', line 125

def ldflags
  @ldflags
end

#resourcesObject

Returns the value of attribute resources.



125
126
127
# File 'lib/rake/cocoa.rb', line 125

def resources
  @resources
end