我有一个任务,我正在尝试将其重构为一个外部模块,以便以后可以将其与该项目分开并将其用于其他项目。
当我尝试运行我的任务时,我收到一个错误:
Buildr aborted!
NoMethodError : undefined method `path_to' for nil:NilClass
本质上,似乎project.task
从未调用该块的代码。
代码如下。一些代码来自编译任务的工作代码,所以我知道这些位可能是正确的。其他部分来自构建器的记录示例,根据今天早些时候的经验,可以用一粒盐(或有时是整个湖)来获取。不过,我无法弄清楚我做错了什么。
module MacAppBundle
include Buildr::Extension
class MacAppBundleTask < Rake::Task
attr_accessor :app_name
def initialize(*args)
super
enhance do |task|
#TODO: @project is always nil here because associate_with is never called
app = @project.path_to("target/#{app_name}.app")
if File.exists?(app)
FileUtils.rm_rf(app)
end
Dir.mkdir(app)
#... omitting copying the rest of the stuff into the bundle ...
end
end
protected
def associate_with(project)
@project = project
end
end
before_define do |project|
mac_app_bundle = MacAppBundleTask.define_task('mac_app_bundle')
project.task 'mac_app_bundle' do |task|
#TODO: This code never executes. Why?
mac_app_bundle.send :associate_with, project
project.local_task('mac_app_bundle')
end
end
after_define do |project|
#TODO: This bit is definitely questionable because I can't find any documentation
# or working examples of similar code.
task('mac_app_bundle' => project.package(:jar))
end
def mac_app_bundle
task('mac_app_bundle')
end
end
class Buildr::Project
include MacAppBundle
end
#TODO: Find a place to move this. Seems weird to have to call it in global scope.
Project.local_task('mac_app_bundle') do |name|
puts "Creating Mac OS X app bundle for #{name}"
end