tags:

views:

70

answers:

3

I have a bunch of rake building tasks.

They each have unique input / output properties, but the majority of the properties I set on the tasks are the same each time. Currently I'm doing that via simple repetition like this:

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

In my usual programming headspace I'd expect to be able to break out the population of the recurring task properties to a re-usable function.

Is there a rake analogy for this? Some way I can have a single function where the shared properties are set on any task? Something equivalent to:

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

Thanks.

======

In reply to SR:

Thanks Stephen,

I'm obviously missing something - I've got:

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

I've tried defining the function immediately after the task (there's no namespace: in this file) and right at the end of the file after the last task, and I'm getting "undefined method `populate_test_task' for main:Object" - which seems to me like it's not finding the function.

What am I missing?

A: 

You can call regular methods defined lower down the rake file, e.g.

namespace :build do

    desc 'builds ABC module'
    task :abc do
        build_mod('abc')
    end

    desc 'builds DEF module'
    taks :def do
        build_mod('def')
    end
end

def build_mod(module_name)
    # stuff to build the module
end

then call it with rake build:abc and rake build:def

sr

stephenr
Thanks Stephen,I'm obviously missing something - I've got:desc 'Compile run the test harness'unit :test do |t| populate_test_task(t)enddef populate_test_task(t) t.source_path << "support" t.prepended_args = '+configname=air -define+=CONFIG::LocalDebug,true'endI've tried defining the function immediately after the task (there's no namespace: in this file) and right at the end of the file after the last task, and I'm getting "undefined method `populate_test_task' for main:Object" - which seems to me like it's not finding the function.What am I missing?
Stray
Ug. Comments formatting is so annoying. Added at the bottom of my Q instead.
Stray
Hi Stray, I'm not familiar with the unit :test do |t| line - where's that from?
stephenr
These tasks are from Project Sprouts (http://projectsprouts.org)
Luke Bayes
A: 

Sorry about the confusion here....

This problem is a side effect of how Project Sprouts creates Rake tasks. Because some parameters in the block need to modify the prerequisites of the task, some of our tasks evaluate their associated block as they are defined.

It's for this reason that your configure function has to be defined before the tasks that use it.

Luke Bayes
A: 

Awesome team work!

Thanks to both, answer was to break out the population of the task properties to a function, but to define that function above the block using it (for project sprouts reasons).

Like this:

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  
Stray