Rails and Rake : .rakeTasks? - ruby-on-rails

What is .rakeTasks file for ?
Rails version : 3.2.1

The .rakeTasks file is created by the IntelliJ IDEA Ruby plugin (and possibly by JetBrains' Ruby-specific RubyMine IDE as well, I'm not sure). As the comments in the file itself state:
<!--This file was automatically generated by Ruby plugin.
You are allowed to:
1. Remove rake task
2. Add existing rake tasks
To add existing rake tasks automatically delete this file and reload the project.
-->
It provides the list of Rake tasks that appears when you select Tools / Run Rake Task...:
Unless you need to customize that list for some reason, you probably never need to edit the file. Personally I exclude it from version control in my .gitignore, along with the .ipr, .iml, and .iws files. (I might check it in if I was working in a homogeneous IDEA shop, but I'm not.)

This file are auto-require and define you project specific task accessible by rake.

Related

Ignore certain files in the asset pipeline when performing assets:precompile

I have a templates directory in my assets folder on my Rails setup which hosts my templated-HTML files. I've implemented a partial system where I can load text snippets into each template so long as it has a .erb file attached to it. The partials are prefixed with an underscore, so /assets/templates/_form.html is a partial which is included inside of /assets/templates/edit.html. This works, but there is an issue when I run assets:precompile.
The assets:precompile command will precompile the _form.html files as well (since they're located inside of the assets/templates folder) and I don't want this to happen since they can't be precompiled (since any erb variables are missing and so on).
Is there any way I can inform the rake assets:precompile command to ignore certain files (in this case files that have an underscore infront of them)? Does someone know how to do this? Or can someone point me to the github repository which has the source for the rake assets:precompile command?
In Rails 3.2.8 you can find the source for the rake task definition here.
What you're asking has been brought up at least once in Sprockets issues tracker
https://github.com/sstephenson/sprockets/pull/86
joshpeek added a new stub directive in Sprockets 2.2 which looks like a promising solution to 'ignoring' certain assets from the pipeline.
Rails 3.2 doesn't support a version of Sprockets anywhere near 2.2 and there doesn't seem to be any intention on doing so before Rails 4 from what I've read (though I'd love to be wrong about that). Here's at least one closed issue (no action taken) requesting an upgrade to 2.2.
https://github.com/rails/rails/pull/5984
For your situation though, it seems to me that you might be using app/assets incorrectly. I can't offer an alternative solution as I don't know enough about your setup/usage to say definitively, but as you said, files with .erb in app/assets are going to be processed by Sprockets.

Why isn't my config object available in my other rake tasks?

My file layout is like:
RakeFile
Tasks/*.rake
In my main RakeFile I have:
config = # load from yaml
Now in my other rake files (in the tasks folder), if I make reference to config it says the method or variable doesn't exist.
Why isn't it reachable in my *.rake files? How can I fix this?
BTW, where are the built-in rails rake files, I want to see how they created their rake tasks for running migrations, dropping the db, seeding etc.
In a Rails project, your custom tasks should be in the lib/tasks directory to get pulled in automatically.
The built-in tasks can be found in railties/lib/rails/tasks for a number of them, and some others are spread out in places like activerecord/lib/active_record/railties. You'll have to look around to find them all, or perhaps use the find command.

Install barista/coffeescript on Rails 2.3

I am trying to get barista up and running in a Rails 2.3 application (that may not be moved to a new version of rails for the time beeing..). I switched the app to bundle so I added the following gems to my Gemspec:
gem "barista"
gem "json"
Then executed bundle install which run through. Now as far as I understand to "compile" the coffeescript there is a rake task that comes with barista. But it doesn't seem to be installed properly so I can use it with rake. I.e. when I execute rake -T there is no barista:brew
I saw a pending pull request on git hub suggesting to add require 'barista/tasks' but that only resulted in rake not finding it. So what am I doing wrong or more general how do I get up and running with barista on Rails 2.3.x?
It has been some time ago since I used Barista and I have it not in use in any project, so I cannot verify it.
But I remember that one advantage of Barista is, that it waits serving a request until a modified CoffeeScript file is recompiled. This ensures that the browser doesn't request an outdated file.
So there is no need to compile the CoffeeScript files with a Rake task.
CoffeeScript itself comes also with a watch function, that compiles CoffeeScripts when a change is detected:
coffee -w /path/to/scripts
The reason why I stopped using Barista is simply that I discovered Guard. So I wrote guard-coffeescript to compile my CoffeeScripts in the same moment I save the file.
Guard-coffeescript has some advantages over Barista and CoffeeScript:
Fast and low CPU consumption because it relies on file system modification events.
Can be configured in many ways, e.g. multiple source folders and output folders.
Immediate feedback when an error occurs, even with system notifications like Growl.
Note that Rails 2 support for Barista is, according to Barista's README, "untested" (it was originally built for Rails 3 only), so there may be compatibility issues. Also note that you need either therubyracer gem, or the node binary on your system's PATH (or any of the other JS runtimes supported by ExecJS).
Try this:
Add a file named foo.coffee to the folder app/coffeescripts with the contents
alert 'Hello, Barista!'
Now add <%= javascript_include_tag "foo" %> to an ERB file and load that page.
You should get the alert, just as you would if the compiled foo.js were in public/javascripts.
I've successfully integrated barista and rails 2.3.14. In development, when I ask for a js file, the coffeescript file is found and compiled on the fly.
I also successfully ran the barista:brew rake task and the js files were generated.
I did notice that for production, unless I include an ExecJS compatible compiler, I need to precompile my js files before a push, which might be another +1 for the guard solution by #netzpirat.
For reference - I'm using Barista 1.3.0 and coffee-script 2.2. Not sure how that affects things, but thought it was noteworthy.
Also, I added a line to load the barista tasks in my Rakefile:
# in my Rakefile
load "barista/tasks/barista.rake"

What steps are followed after you say "rake install"?

I'd like to install a plugin but I'm afraid it's going to install a lot of unnecessary stuff. I'd like to look at whatever file rake takes it installation instructions from and remove anything unnecessary.
I believe this is the Rakefile. But I'm not sure what happens when rake looks at the rakefile - does it execute the entire rakefile or only parts of the rakefile that are designated as being relevant to this "install" procedure?
a rake file is a collection of tasks, when you call rake with an argument (in this case install) that's the task that get's executed. (It's similar to ant if you come from Java)
So, no, rake does not execute the whole rakefile when you call "rake + task" but only the task chosen. Note that tasks can have dependencies (eg the "test" task may depend on other previous tasks, like creating some folders and stuff for the tests to run).
Lastly, the rake user guide as pointed by other users is useful, but I recommend a more enjoyable read here -> Ruby on Rails Rake tutorial.
Rake is set up much like Make in that a Rakefile consists of targets and dependencies. This is different from a regular ruby script in that Rake starts at the target you ask for and recursively executes its dependencies before executing the target itself.
So, install might look like this:
task :install => :stage do
# stuff to do
end
Here, your target is the install task, and it depends on some other task called stage.
To execute install, Rake must first execute the dependencies of stage (if it has any), then stage, then finally it executes install. So no, you don't execute the whole file, just enough of it to safely execute the target you asked for.
Rake also supports file targets:
file 'foo.html' => 'bar.xml' do |t|
# Build foo.html from bar.xml, however that is done
end
If you know Make, this looks familiar. Rake first checks whether bar.xml depends on anything, and if so, it executes that. Then, if bar.xml is newer than foo.html, then Rake executes this file task. If foo.html is newer, then Rake assumes that it has aleady been built and skips it.
For more, the Rake User Guide is a good place to start if you want to learn what Rake does.
Why would a plugin install something "unnecessary"?
Assuming your fears are justified, though, could you not install the plugin, do your investigation and then, if not satisfied, revert to the pre-installed version using your source control system? If you're not using source control, this might be the perfect excuse to start...

How do I find the source file for a rake task?

I know you can view all possible rake tasks by typing
rake -T
But I need to know what exactly a task does. From the output, how can I find a source file that actually has the task? For example, I'm trying to find the source for the db:schema:dump task.
I know this is an old question, but in any case:
rake -W
This was introduced in rake 0.9.0.
http://rake.rubyforge.org/doc/release_notes/rake-0_9_0_rdoc.html
Support for the –where (-W) flag for showing where a task is defined.
Despite what others have said, you can programmatically get the source location of rake tasks in a rails application. To do this, just run something like the following in your code or from a console:
# load all the tasks associated with the rails app
Rails.application.load_tasks
# get the source locations of actions called by a task
task_name = 'db:schema:load' # fully scoped task name
Rake.application[task_name].actions.map(&:source_location)
This will return the source locations of any code that gets executed for this task. You can also use #prerequisites instead of #source_location to get a list of prerequisite task names (e.g. 'environment', etc).
You can also list all tasks loaded using:
Rake.application.tasks
UPDATE: See Magne's good answer below. For versions of rake >= 0.9.0 you can use rake -W to show the source location of your rake tasks.
There is no programmatic way to do this unfortunately. Rake tasks can be loaded either from rails itself, lib/tasks, or from any plugin with a tasks directory.
This should nab most everything not within Rails itself:
find . -name "*.rake" | xargs grep "whatever"
As for db:schema:dump, here's the source:
desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
task :dump => :environment do
require 'active_record/schema_dumper'
File.open(ENV['SCHEMA'] || "#{RAILS_ROOT}/db/schema.rb", "w") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
end
It can be found on line 242 of lib/tasks/database.rake in the rails 2.2.2 gem. If you've got a different version of Rails, just search for "namespace :schema".
You probably actually want the source of the ActiveRecord::SchemaDumper, but I think you should have no trouble figuring out where that is. :-)
For most rake tasks in Rails, look in the Rails gem directory, in lib/tasks.
If you've vendored Rails into your app directory structure then look in vendor/rails/railties/lib/tasks instead
Either way, db:schema:dump is in databases.rake.

Resources