Search code examples
rubyrake

How to use FileLists as rake dependencies


I'm using rake to help compile Coffeescript for a Chrome extension I'm writing.

My Rakefile looks like this:

COFFEE = FileList['src/*.coffee']
JS = COFFEE.ext 'js'

directory 'extension'

rule '.js' => ['.coffee', 'extension'] do |t|
  `coffee -c -o extension #{t.source}`
end

desc "Build the extension in the 'extension' directory"
task :build => ['extension', JS] do
  cp File.join('src', 'manifest.json'), 'extension'
end

When I only have one .coffee file in my src directory, there's no problem. But as soon as I have more than one .coffee files it errors:

$ rake build
> rake aborted!
> Don't know how to build task 'src/app.js src/background.js'
>
> Tasks: TOP => build
> (See full trace by running task with --trace)

Is it possible to specify a FileList as a dependency? How else would I tell rake that I want all my Coffeescript files compiled durring the build task?


Solution

  • Rake’s dependency list is an Array of task names. When you use a FileList as one of its elements, you nest arrays – effectively, this:

    task :build => ['extension', ['src/app.js', 'src/background.js']] do
    

    Rake just uses the String representation of all passed dependency Array elements, which is why it complains about being unable to build a 'src/app.js src/background.js' task (note how this is one string).

    Splatting your FileList (or flattening the dependency Array) will solve the issue, i.e.:

    task :build => ['extension', *JS] do
    

    or

    task :build => ['extension', JS].flatten do