LoginSignup
6
6

More than 5 years have passed since last update.

spring-commands-anyでRailsプロジェクトのマイタスクも高速化する

Posted at

rails, rake, rspec等に加えて自作のコマンド(bin/scriptディレクトリのスクリプト)もSpringで高速化できるようにspring-commands-anyというGemを作りました。

GemといいつつSpringと連携する部分のコード自体はとても少ないので、コマンドに直接書いてもいいかも。

以下、実際に試してみます。

spring-commands-anyを追加

Gemfile
group :development do
  gem 'spring-commands-any'
end

bundle installすると自作のコマンド(hogehoge)もhelpで出てくるようになります。

./bins/spring help

Commands for your application:

  rails           Run a rails command. The following sub commands will use spring: console, runner, generate, destroy.
  rake            Runs the rake command
  rspec           Runs the rspec command
  hogehoge        Runs the tdnet command

spring binstubを実行

bundle exec spring binstub hogehoge

bin/hogehogeにbin/springをloadするスクリプトが挿入されます。このまま使うとbundle execでタスクを実行した際にもSpringが起動してしまうので、回避コードを追加します。

bin/hogehoge
begin
#  load File.expand_path("../spring", __FILE__)
  load File.expand_path("../spring", __FILE__) unless defined?(Bundler)
rescue LoadError
end

コマンドを実行する

Springが起動している場合は一旦止めてください。次の起動時にspring-commands-anyがrequireされます。

./bin/spring stop

time ./bin/hogehoge download 2014/06/29 2014/06/29

real    0m1.927s
user    0m0.110s
sys 0m0.050s

time ./bin/hogehoge download 2014/06/29 2014/06/29

real    0m0.491s
user    0m0.110s
sys 0m0.048s

./bin/spring status

53743 spring server | test-app | started 2 mins ago      
53744 spring app    | test-app | started 2 mins ago | development mode

Springが起動して高速化されていますね。デフォルトのenvironmentはdevelopmentですが、-eや--environment=で指定できます。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6