6 Replies - 1657 Views - Last Post: 28 January 2013 - 02:32 PM Rate Topic: -----

#1 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 529
  • Joined: 17-September 08

Can I use a FileList as a dependency to a rake task?

Posted 25 January 2013 - 02:13 PM

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?

Is This A Good Question/Topic? 0
  • +

Replies To: Can I use a FileList as a dependency to a rake task?

#2 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Can I use a FileList as a dependency to a rake task?

Posted 25 January 2013 - 03:24 PM

Unless I'm mistaken, line 11 of your Rakefile is malformed.

I'm used to tasks being defined as...
task :build do



Then you can define a default task by:
task :default => :build



But you might wanna consult the build task in this link to re-write your code. http://andrewberls.c...script-projects


files = join_filenames(
  FILENAMES.map { |file| "#{file}.coffee" },
  SRC_PATH
)

 
# Compile everything

`coffee -b --output #{BUILD_PATH} --compile #{files}`




edit:

Also, this seems to just work, though I'm not very Rakefile savvy right now so it may have oddities.

SRC = FileList['src/*.coffee']
OBJ = SRC.ext('coffee')

directory 'extension'

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

file "hello" => OBJ do
  sh "coffee -c -o extension #{OBJ}"
end

task :default => ['hello']


This post has been edited by NotarySojac: 25 January 2013 - 03:39 PM

Was This Post Helpful? 1
  • +
  • -

#3 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 529
  • Joined: 17-September 08

Re: Can I use a FileList as a dependency to a rake task?

Posted 26 January 2013 - 12:11 AM

Line 11 shows how rake defines task dependencies. This is basically telling rake, before you do the task I've set out for you, ensure these other tasks have taken place. See the rake tutorial on Dream.In.Code.

I know I can loop over my FileList and use that to compile the Coffeescript. Also, I could loop over the FileList and add each file as a separate dependency to the build task. I don't like either of these because frankly they feel bloated compared to the rest of rake's DSL. Rake has a way to specify lists of files, and ways of defining tasks for kinds of files, and a nice way to specify dependencies, but apparently no way to combine all three of those things together.

What I've come up with for now is to explicitly concat the FileList onto the dependency array, which just feels like the shortest way of setting each file individually as a dependency to the build task.

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'].concat(JS) do
   cp File.join('src', 'manifest.json'), 'extension'
end



I'm going to continue seeing if there's a shorter way to do this, but if nothing turns up I'll turn it into a feature request on rake.
Was This Post Helpful? 1
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 3,031
  • Joined: 21-June 11

Re: Can I use a FileList as a dependency to a rake task?

Posted 26 January 2013 - 09:03 AM

View Post5thWall, on 26 January 2013 - 08:11 AM, said:

What I've come up with for now is to explicitly concat the FileList onto the dependency array, which just feels like the shortest way of setting each file individually as a dependency to the build task.
[...]
I'm going to continue seeing if there's a shorter way to do this, but if nothing turns up I'll turn it into a feature request on rake.


You can use ['extension', *JS].

['extension'] + JS would work, too.

This post has been edited by sepp2k: 26 January 2013 - 09:05 AM

Was This Post Helpful? 2
  • +
  • -

#5 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Can I use a FileList as a dependency to a rake task?

Posted 27 January 2013 - 11:11 AM

Thanks for the link 5thwall. I went through a very brief tut yesterday out of curiosity and realized what you meant by the thread title and last couple lines. I had no idea how handy Rakefiles were outside of the ruby world, it's actually really impressive.

At the risk of showing my green further, I have another note on your rake file, line 6.

rule '.js' => ['.coffee', 'extension'] do |t|



That rule seems to imply that '*.coffee' files and 'extension' files are used to generate '.js' files, right? So maybe it should be just:

rule '.js' => ['.coffee'] do |t|



As that the directory 'extension' line handles the directory dependency issue.

This post has been edited by NotarySojac: 27 January 2013 - 11:13 AM

Was This Post Helpful? 1
  • +
  • -

#6 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 529
  • Joined: 17-September 08

Re: Can I use a FileList as a dependency to a rake task?

Posted 27 January 2013 - 05:42 PM

directory, file, rule, and task are all ways of defining tasks in rake. So the extension directory is only created when that task is explicitly called, either directly or as a dependency to some other task.

So the rule line says to run a .js task, ensure that there is a .coffee file with the same name, and the extensions directory exists. The directory needs to exist because I tell the Coffeescript compiler to use it as an output directory. Say if I had a fresh clone of the repository, and just wanted to see the Javascript output of one of my files. I could run rake src/app.js and it would first ensure there existed src/app.coffee and an extension directory.

Granted I usually just run rake build, which already has that directory as a dependency, but this will help if I ever refactor my extension directory (say I want separate directories for CSS, Javascript, Images, etc.). If you want to see the file context for this, you can check out the git repo.
Was This Post Helpful? 0
  • +
  • -

#7 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Can I use a FileList as a dependency to a rake task?

Posted 28 January 2013 - 02:32 PM

aha, that makes sense now. Cool project, thanks for sharing it. I've been meaning to build a chrome/firefox extension one of these days but haven't gotten around to it, but it's good that it's a mostly straightforward file structure.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1