如何为我独立的Ruby脚本设置Rails环境?

时间:2021-08-10 12:19:19

I have a Ruby script in my Rails app that I use to load some data from Twitter.

我在我的Rails应用程序中有一个Ruby脚本,用于从Twitter加载一些数据。

In the future I will make it an automatic background process, but for now I run it manually like:

将来我会把它变成一个自动后台进程,但是现在我手动运行它:

ruby /lib/twitter/twitterLoad.rb

In order to use the Rails model classes and such, I have the following as the top line of the script:

为了使用Rails模型类等,我将以下作为脚本的第一行:

require "#{File.dirname(__FILE__)}/../../config/environment.rb"

By default, the development environment is used. But, I'd like to be able to choose the production environment at some point.

默认情况下,使用开发环境。但是,我希望能够在某个时候选择生产环境。

Update #1: The RAILS_ENV constant is getting set in the environment.rb file. So, I was able to put ENV['RAILS_ENV'] = 'production' at the very top (before the environment.rb) line and solve my problem somewhat. So, my new question is, can do pass in env vars through the command line?

更新#1:RAILS_ENV常量在environment.rb文件中设置。所以,我能够将ENV ['RAILS_ENV'] ='生产'放在最顶层(环境.rb之前)并解决我的问题。那么,我的新问题是,可以通过命令行传入env变量吗?

7 个解决方案

#1


22  

If you're going to be using the rails environment, your best bet would be to make this a rake script. To do this, put a twitter.rake file into lib/tasks and begin and end it like this:

如果您要使用rails环境,最好的办法是将其作为rake脚本。为此,将twitter.rake文件放入lib / tasks中,然后以这样的方式开始和结束:

task(:twitter_load => :environment) do
  # your code goes here
end

That way, you're doing it by "following conventions" and it doesn't have that 'orrible smell associated with it.

这样,你就是通过“遵循惯例”来做到这一点,并且它没有那种与之相关的难闻气味。

#2


9  

I currently use the following method, and I know the environment doesn't have the rb extension, it's not needed. You can also set it before running it to overwrite the ENV["RAILS_ENV"].

我目前使用以下方法,我知道环境没有rb扩展名,不需要它。您也可以在运行它之前设置它以覆盖ENV [“RAILS_ENV”]。

#!/usr/bin/env ruby

# Set your environment here.
ENV["RAILS_ENV"] ||= "production"

require File.dirname(__FILE__) + "/../../config/environment"

puts "Rails was loaded!"

Then to change the environment, just run it with:

然后要更改环境,只需运行它:

rb /lib/tasks/file.rb RAILS_ENV=development

#3


5  

Don't forget script/runner.

不要忘记脚本/跑步者。

Set your environment variable from the command line and

从命令行设置环境变量

ruby script/runner your_script_here.rb

#4


1  

You can also do

你也可以这样做

script/console development < path/to/your/script.rb

脚本/控制台开发

Admiteddly cumbersome -and will spit out lots of irb garbage after evaluating each and every line of your script- but works for quickies and you dont have to remember that damned require line.

在评估你的剧本的每一行之后,他们会非常麻烦 - 并会吐出大量的垃圾垃圾 - 但是为了快速工作,你不必记住该死的需求线。

And don't forget that maybe the most elegant way to extend your app with scripts that do useful things is writing Rake tasks!

并且不要忘记,使用可以执行有用操作的脚本扩展应用程序的最优雅方法是编写Rake任务!

#5


1  

The accepted answer to use rake is well-taken, but you may still want to manually set the environment for testing utility classes.

使用rake的公认答案很明确,但您可能仍希望手动设置测试实用程序类的环境。

Here's what I use to set up the test environment for utility classes in /lib. For these I tend to use the Ruby convention of making my class file execute its tests when it gets run from the command line. This way I can do TDD outside of Rails' web-centric test harnesses, but still use the class within rake without affecting the environment that it sets.

这是我用来为/ lib中的实用程序类设置测试环境的方法。对于这些我倾向于使用Ruby约定,使我的类文件在从命令行运行时执行其测试。这样我可以在Rails的以网络为中心的测试工具之外进行TDD,但仍然使用rake中的类而不影响它设置的环境。

This goes at the top:

这是在顶部:

if (__FILE__ == $0)
  ENV['RAILS_ENV'] ||= 'test'
  require File.join(File.dirname(__FILE__),'../config/environment.rb')
end

and this goes at the bottom:

这是在底部:

if (__FILE__ == $0)
  require 'test/unit/ui/console/testrunner'
  Test::Unit::UI::Console::TestRunner.run(MyClassTest)
end

#6


0  

add the line: RAILS_ENV = '<your environment of choice>'

添加以下行:RAILS_ENV =' <您选择的环境>

http://wiki.rubyonrails.org/rails/pages/Environments#script

pantulis: It's cool that that works, but for quickies I just use RAILS_ENV = '<your environment of choice>' ruby path/to/script.rb. This is how you set environment variables in the console.

pantulis:这很酷,但是对于快速我只需使用RAILS_ENV =' <您选择的环境> 'ruby path / to / script.rb。这是您在控制台中设置环境变量的方法。

#7


0  

require 'bundler'
require 'bundler/setup'

ENV['RAILS_ENV'] ||= 'development'

RAILS_ROOT = Pathname.new(File.expand_path('~/path/to/root'))

puts "Loading Rails Environment from #{RAILS_ROOT}..."
require RAILS_ROOT.join('config/environment').to_s

This works but only if the Gemfile of your script contains all the dependencies of your rails app. You might be able to load one Gemfile from another, e.g just eval it, to overcome this copy/paste.

这可以工作,但前提是脚本的Gemfile包含rails应用程序的所有依赖项。您可能能够从另一个Gemfile加载一个Gemfile,例如只是eval它,以克服此复制/粘贴。

#1


22  

If you're going to be using the rails environment, your best bet would be to make this a rake script. To do this, put a twitter.rake file into lib/tasks and begin and end it like this:

如果您要使用rails环境,最好的办法是将其作为rake脚本。为此,将twitter.rake文件放入lib / tasks中,然后以这样的方式开始和结束:

task(:twitter_load => :environment) do
  # your code goes here
end

That way, you're doing it by "following conventions" and it doesn't have that 'orrible smell associated with it.

这样,你就是通过“遵循惯例”来做到这一点,并且它没有那种与之相关的难闻气味。

#2


9  

I currently use the following method, and I know the environment doesn't have the rb extension, it's not needed. You can also set it before running it to overwrite the ENV["RAILS_ENV"].

我目前使用以下方法,我知道环境没有rb扩展名,不需要它。您也可以在运行它之前设置它以覆盖ENV [“RAILS_ENV”]。

#!/usr/bin/env ruby

# Set your environment here.
ENV["RAILS_ENV"] ||= "production"

require File.dirname(__FILE__) + "/../../config/environment"

puts "Rails was loaded!"

Then to change the environment, just run it with:

然后要更改环境,只需运行它:

rb /lib/tasks/file.rb RAILS_ENV=development

#3


5  

Don't forget script/runner.

不要忘记脚本/跑步者。

Set your environment variable from the command line and

从命令行设置环境变量

ruby script/runner your_script_here.rb

#4


1  

You can also do

你也可以这样做

script/console development < path/to/your/script.rb

脚本/控制台开发

Admiteddly cumbersome -and will spit out lots of irb garbage after evaluating each and every line of your script- but works for quickies and you dont have to remember that damned require line.

在评估你的剧本的每一行之后,他们会非常麻烦 - 并会吐出大量的垃圾垃圾 - 但是为了快速工作,你不必记住该死的需求线。

And don't forget that maybe the most elegant way to extend your app with scripts that do useful things is writing Rake tasks!

并且不要忘记,使用可以执行有用操作的脚本扩展应用程序的最优雅方法是编写Rake任务!

#5


1  

The accepted answer to use rake is well-taken, but you may still want to manually set the environment for testing utility classes.

使用rake的公认答案很明确,但您可能仍希望手动设置测试实用程序类的环境。

Here's what I use to set up the test environment for utility classes in /lib. For these I tend to use the Ruby convention of making my class file execute its tests when it gets run from the command line. This way I can do TDD outside of Rails' web-centric test harnesses, but still use the class within rake without affecting the environment that it sets.

这是我用来为/ lib中的实用程序类设置测试环境的方法。对于这些我倾向于使用Ruby约定,使我的类文件在从命令行运行时执行其测试。这样我可以在Rails的以网络为中心的测试工具之外进行TDD,但仍然使用rake中的类而不影响它设置的环境。

This goes at the top:

这是在顶部:

if (__FILE__ == $0)
  ENV['RAILS_ENV'] ||= 'test'
  require File.join(File.dirname(__FILE__),'../config/environment.rb')
end

and this goes at the bottom:

这是在底部:

if (__FILE__ == $0)
  require 'test/unit/ui/console/testrunner'
  Test::Unit::UI::Console::TestRunner.run(MyClassTest)
end

#6


0  

add the line: RAILS_ENV = '<your environment of choice>'

添加以下行:RAILS_ENV =' <您选择的环境>

http://wiki.rubyonrails.org/rails/pages/Environments#script

pantulis: It's cool that that works, but for quickies I just use RAILS_ENV = '<your environment of choice>' ruby path/to/script.rb. This is how you set environment variables in the console.

pantulis:这很酷,但是对于快速我只需使用RAILS_ENV =' <您选择的环境> 'ruby path / to / script.rb。这是您在控制台中设置环境变量的方法。

#7


0  

require 'bundler'
require 'bundler/setup'

ENV['RAILS_ENV'] ||= 'development'

RAILS_ROOT = Pathname.new(File.expand_path('~/path/to/root'))

puts "Loading Rails Environment from #{RAILS_ROOT}..."
require RAILS_ROOT.join('config/environment').to_s

This works but only if the Gemfile of your script contains all the dependencies of your rails app. You might be able to load one Gemfile from another, e.g just eval it, to overcome this copy/paste.

这可以工作,但前提是脚本的Gemfile包含rails应用程序的所有依赖项。您可能能够从另一个Gemfile加载一个Gemfile,例如只是eval它,以克服此复制/粘贴。