【问题标题】:Avoiding duplication in setting properties on the task in Rake tasks避免在 Rake 任务中设置任务属性的重复
【发布时间】:2010-05-31 17:38:31
【问题描述】:

我有一堆构建耙子的任务。

它们每个都有唯一的输入/输出属性,但我在任务上设置的大多数属性每次都是相同的。目前我正在通过这样的简单重复来做到这一点:

task :buildThisModule => "bin/modules/thisModule.swf"

mxmlc "bin/modules/thisModule.swf" do |t|
    t.input = "src/project/modules/ThisModule.as"
    t.prop1 = value1
    t.prop2 = value2 ... (And many more property=value sets that are the same in each task)
end


task :buildThatModule => "bin/modules/thatModule.swf"

mxmlc "bin/modules/thatModule.swf" do |t|
    t.input = "src/project/modules/ThatModule.as"
    t.prop1 = value1
    t.prop2 = value2 ... (And many more property=value sets that are the same in each task)
end

在我通常的编程空间中,我希望能够将重复任务属性的数量分解为可重用的函数。

这有一个耙类比吗?某种方式我可以有一个单一的功能,在任何任务上设置共享属性?相当于:

task :buildThisModule => "bin/modules/thisModule.swf"

mxmlc "bin/modules/thisModule.swf" do |t|
    addCommonTaskParameters(t)
    t.input = "src/project/modules/ThisModule.as"
end


task :buildThatModule => "bin/modules/thatModule.swf"

mxmlc "bin/modules/thatModule.swf" do |t|
    addCommonTaskParameters(t)
    t.input = "src/project/modules/ThatModule.as"
end

谢谢。

======

回复 SR:

谢谢斯蒂芬,

我显然遗漏了一些东西 - 我有:

desc 'Compile run the test harness'
unit :test do |t|
  populate_test_task(t)
end

def populate_test_task(t)
  t.source_path << "support"
  t.prepended_args       = '+configname=air -define+=CONFIG::LocalDebug,true'
end

我尝试在任务之后立即定义函数(在此文件中没有命名空间:),并在最后一个任务之后的文件末尾,我得到“未定义的方法 `populate_test_task' for main:对象”——在我看来,它没有找到函数。

我错过了什么?

【问题讨论】:

    标签: ruby rake


    【解决方案1】:

    您可以调用定义在 rake 文件下方的常规方法,例如

    命名空间:构建做 desc '构建 ABC 模块' 任务:abc 做 build_mod('abc') 结尾 desc '构建 DEF 模块' 任务 :def 做 build_mod('def') 结尾 结尾 def build_mod(module_name) # 构建模块的东西 结尾

    然后用 rake build:abc 和 rake build:def 调用它

    sr

    【讨论】:

    • 谢谢斯蒂芬,我显然遗漏了一些东西 - 我有: desc '编译运行测试工具' 单元:test do |t| populate_test_task(t) end def populate_test_task(t) t.source_path
    • 丑。评论格式太烦人了。而是添加到我的 Q 底部。
    • 嗨Stray,我不熟悉这个单元:test do |t|行 - 这是从哪里来的?
    【解决方案2】:

    对不起,这里的混乱......

    这个问题是Project Sprouts 如何创建 Rake 任务的副作用。因为块中的某些参数需要修改任务的先决条件,所以我们的一些任务会在定义时评估它们关联的块。

    因此,您的配置函数必须在使用它的任务之前定义。

    【讨论】:

      【解决方案3】:

      很棒的团队合作!

      多亏了这两者,答案是将任务属性的总体分解为一个函数,但是在使用它的块上方定义该函数(出于项目新芽的原因)。

      像这样:

      def populate_test_task(t)
        t.source_path << "support"
        t.prepended_args       = '+configname=air -define+=CONFIG::LocalDebug,true'
      end
      
      desc 'Compile run the test harness'
      unit :test do |t|
        populate_test_task(t)
      end  
      

      【讨论】:

        猜你喜欢
        • 2016-01-20
        • 1970-01-01
        • 2020-07-19
        • 2019-12-22
        • 1970-01-01
        • 2017-06-10
        • 1970-01-01
        • 1970-01-01
        • 2013-02-14
        相关资源
        最近更新 更多