Obromios
Obromios

Reputation: 16373

Run rake task in another directory

I have a ruby on rails api_app and and a test_app that exercises the api_app. While development mode I want to run, from the api_app, a rake task called match:reset that is in the test_app. I am trying to do this with a ruby file that is in the api_app

#!/usr/bin/env ruby
require 'tty-command'
cmd = TTY::Command.new
Dir.chdir('../test_app') do
   cmd.run 'run rake match:reset'
end

When I do this I get the following error

 Your Ruby version is 2.4.4, but your Gemfile specified 2.4.1 (Bundler::RubyVersionMismatch)

I have tried adding cmd.run 'rvm use 2.4.1' but this does not work. How do set the correct environment so this does work?

Upvotes: 1

Views: 1474

Answers (2)

Obromios
Obromios

Reputation: 16373

I could not get this working with a ruby command file. I think the reason for this is that the file is run with ruby from the api_ppp directory so you cannot change to another ruby. What I ended up doing is using a bash file:

#!/bin/bash
cd ../test_app
source $HOME/.rvm/scripts/rvm
rvm use 2.4.4@test_app_2.4.4
BUNDLE_GEMFILE=../test_lab/Gemfile bundle exec rake -f ../test_app/Rakefile match:reset

I am happy to consider an answer that uses a ruby command file, as long as the answer has been tested.

Upvotes: 1

This solution is not tested, based on the documentation I have made changes to the script.

require 'tty-command'
require 'rake'

cmd = TTY::Command.new
cmd.run("rvm use 2.4.1")
Dir.chdir("../test_app") do
  cmd.run(:rake, 'match:reset', env: {rails_env: :development})
end

Upvotes: 0

Related Questions