0

I'm using Rails 5.1. How do I get my initialization file (located at config/my_init.rb) to recognize when it is being run in the context of server startup versus when it is run in the context of a rake task? I thought the below would do it ...

puts "iniitializing ... #{(!Rails.env.test?) } defined; #{!defined?(::Rake)}"
if(!Rails.env.test?) && !defined?(::Rake)

And when I'm running a rake task, the " !defined?(::Rake)" correctly returns false ...

localhost:myproject davea$ rake db:seed
iniitializing ... true defined; false

but when I start up my server, that clause is still returning false

localhost:myproject davea$ rails s -b 127.0.0.1
=> Booting Puma
=> Rails 5.1.5 application starting in development
=> Run `rails server -h` for more startup options
iniitializing ... true defined; false

How can I make a distinction between teh two?

Dave
  • 15,639
  • 133
  • 442
  • 830
  • Maybe you could define a constant in your Rakefile and check if it is defined before executing the required block of code. Constants defined in Rakefile will not be available in the Rails env. – Sinstein Apr 19 '18 at 05:52

3 Answers3

1

You can check of $0 or $PROGRAM_NAME which is an alias

if $PROGRAM_NAME.end_with?("rake")
  puts "I am running under rake"
elsif   $PROGRAM_NAME.end_with?("rails")
  puts "I am running under rails"
end

Running it

$ rake db:seed
I am running under rake

$ rails s -b 127.0.0.1
=> Booting Puma
=> Rails 5.2.0 application starting in development 
=> Run `rails server -h` for more startup options
I am running under rails
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
0

The short explanation is: Rails core team decided to have consistency by enabling rails command to support everything that rake does.

By running rails --help, in Rails 5, you can to see all the commands that is supported by rails, a long list of options.

You can still choose to use rake to run those commands, as it you can do in Rails 4. This is because Rails community has introduced Rake Proxy instead of completely moving the command options from rake to rails.

Internally, by running rails routes, Rails checks if routes is something that rails natively supports or not. In this case routes is not natively supported by rails, so Rails delegates the execution to Rake via Rake Proxy.

A detailed explanation

EDIT

You can check if the extension is .rake using File.extname

# config/initializers/my_init.rb
Rails.application.config.after_initialize do
    # tweak this as required...
    if File.extname($0) =='.rake'
      #Doing some stuff
    end
end
mmsilviu
  • 1,211
  • 15
  • 25
  • I am unclear by reading the link or your explanation what changes I need to make in my file to accommodate the behavior I want. Please provide a code example if you have one. – Dave Apr 13 '18 at 19:48
  • From my understanding, it's not possible to do like that. Because the rake files have the extension `.rake`, you can check if the given file is `.rake'. Please see the update answer – mmsilviu Apr 14 '18 at 05:32
  • Thanks. I changed my check to be "if(!Rails.env.test?) && File.extname($0) !='.rake'" and then I ran "rake db:migrate" but my script still ran even though I didn't want it to. – Dave Apr 14 '18 at 22:44
0

$0 holds the current ruby program being run. So this check should work for you:

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

$0 is a globally defined variable in Ruby - Ruby Doc for Global Variables

Contains the name of the script being executed. May be assignable.

Sinstein
  • 887
  • 11
  • 42