2014-06-08 3 views
0

나는 나중에 외부 프로젝트를 분리하고 다른 프로젝트에 사용할 수 있도록 외부 모듈로 리팩터링하려고한다. 외부에서 정의 된 사용자 정의 buildr 태스크는 어떻게 구현합니까?

내 작업을 실행하려고

, 나는 오류가 발생 :

Buildr aborted! 
NoMethodError : undefined method `path_to' for nil:NilClass 

은 본질적으로는 호출되지 않습니다되어 project.task 블록의 코드처럼 보인다.

코드는 다음과 같습니다. 일부 코드는 컴파일 작업의 작업 코드에서 가져온 것입니다. 따라서 비트가 맞을 수도 있습니다. 다른 부분은 buildr에 대한 문서화 된 예제에서 왔는데, 오늘의 경험으로 인해 곡물 (때로는 호수 전체)로 소금을 채취 할 수 있습니다. 나는 내가 잘못한 것을 알아낼 수 없다.

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 

답변

0

올바른 방법으로 나타납니다

before_define do |project| 
    mac_app_bundle = MacAppBundleTask.define_task('mac_app_bundle') 
    mac_app_bundle.send :associate_with, project 
    project.task('mac_app_bundle') 
    end