Search code examples
rakerake-task

Rake file task with 100s of files


I'm just now learning about file tasks in Rake. I have 100s of javascript source files, that get concatenated to 10 or so files. I see this kind of mapping can be done intelligently with file tasks. It appears you need to specify every file.

Is there an efficient way to write a file task that recursively finds all of these javascript files and compare them to the concatenated files on the right side? The concatenation part is a tricky step and is being done by a custom build tool at the moment until I can get that in ruby.

For example,

/a.js
/c.js            -----------> base.js
/geo/b.js
/geo/c.js        -----------> geo.js
/mod/d.js
/mod/e.js        -----------> mod.js

Solution

  • file 'base.js' => Dir['*.js'] do |t|
      concatenate t.prerequisites, t.name
    end
    
    file 'geo.js' => Dir['geo/*.js'] do |t|
      concatenate t.prerequisites, t.name
    end
    

    and so on, you will have to implement the concatenate method yourself, obviously (in my example the first argument is the list of files and the second is the destination file, e.g. geo.js). If all created files are named after directories you can do something like this:

    %w(geo mod xyz abc).each do |module|
      file "#{module}.js" => Dir["#{module}/*.js"] do |t|
        concatenate t.prerequisites, t.name
      end
    end
    

    If the directories can be globbed somehow you could make it even more dynamic by replacing the list of directory names with Dir[...] too.