How can I tell if Rails code is being run via rake or script/generate?

ruby-on-rails

Solution

It's as simple as that:

if $rails_rake_task
  puts 'Guess what, I`m running from Rake'
else
  puts 'No; this is not a Rake task'
end

Rails 4+

Instead of `$rails_rake_task`, use:

File.basename($0) == 'rake'

Problem

I've got a plugin that is a bit heavy-weight. (Bullet, configured with Growl notifications.) I'd like to not enable it if I'm just running a rake task or a generator, since it's not useful in those situations. Is there any way to tell if that's the case?

Original source

Related problems