11

I have the following Rake task:

namespace :foreman do
  task :dev do
    `foreman start -f Procfile.dev`
  end
end

desc "Run Foreman using Procfile.dev"
task :foreman => 'foreman:dev'

The forman command works fine from the shell, however when I run rake foreman I get the following error:

/Users/me/.gem/ruby/2.0.0/gems/bundler-1.5.2/lib/bundler/rubygems_integration.rb:240:in `block in replace_gem': foreman is not part of the bundle. Add it to Gemfile. (Gem::LoadError)
    from /Users/me/.gem/ruby/2.0.0/bin/foreman:22:in `<main>'

Forman specifically states:

Ruby users should take care not to install foreman in their project's Gemfile

So how can I get this task to run?

Undistraction
  • 42,754
  • 56
  • 195
  • 331

4 Answers4

3

If you must make it work via rake, try changing the shell-out via backtick to use a hard-coded path to the system-wide foreman binary

`/global/path/to/foreman start -f Procfile.dev`

You just need to use 'which' or 'locate' or a similar tool to determine the path that works outside your bundler context. If you are using rbenv, then this might be sufficient :

$ rbenv which rake
/home/name/.rbenv/versions/1.9.3-p448/bin/rake

I hope that helps you move forward.

SciPhi
  • 2,585
  • 1
  • 18
  • 19
2

Not sure if this will work, but you could export the environment variables associated with your shell explicitly and then make a call to foreman. FWIW, I don't think this is recommended, and would suggest using a bash script as @dax proposes.

Steps

  1. Get the $PATH and other environment variables from your shell

    printenv >> shell.env
    
  2. Get the environment variables from the rails environment

    namespace :foreman_test do
      task :dev do
        `printenv >> rails.env`
      end
    end
    
  3. Compare the two and find out the changed environment variables, and set them up in your rake task in the system call

    namespace :foreman do
      task :dev do
        `export PATH=/original/path:/value && GEM_DIR=/some/folder && foreman start -f Procfile.dev`
      end
    end
    
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • Thanks for your answer. Please can you explain why running it from the rake task is failing? Given that the task doesn't rely on `:environment` why exactly is it going through the bundle? – Undistraction Dec 10 '14 at 10:58
  • @Pedr One explanation is that you initialized your repo with `--binstubs`, and so when you execute `rake taskname`, you are actually using `bin/rake`, and your `GEMFILE` is picked by default. Anyway, did my solution work? – Anshul Goyal Dec 10 '14 at 11:25
  • I don't use binstubs. I use a `be` alias instead. I've tried running `rake`, `be rake` and `bin/rake`. All result in the same error I described above. I can't test at the moment, but I'll try as soon as I can. – Undistraction Dec 10 '14 at 11:49
  • @Pedr if you run it as `be rake`, you are already invoking the bundler, which means the Gemfile for the project is going to be anyway referred. Since you mention using `bin/rake`, I think it's safe to assume that `rake` will also refer to `bin/rake`. So in either case, your Gemfile is being referred. Since a system call will initiate a new sub process, it will be safe to export the variables/ reset them within the subprocess. – Anshul Goyal Dec 10 '14 at 12:56
1

if it has to be a rake task, try this (from this answer):

namespace :foreman do
  task :dev do
    sh "foreman start -f Procfile.dev"
  end
end

if it doesn't have to be a rake task, I have a simple bash script to start for a specific project that works well:

#!/bin/bash

export PROJECT_DIR=`pwd`
export PORT=$1

source "$HOME/.rvm/scripts/rvm"

unset BUNDLE_GEMFILE
unset BUNDLE_BIN_PATH
unset RUBYOPT
unset GEM_HOME
unset GEM_PATH

(cd <project full path> && exec foreman start -p $PORT)
Community
  • 1
  • 1
dax
  • 10,779
  • 8
  • 51
  • 86
  • Thanks, think I'd prefer to keep everything in tasks (have a lot already). Unfortunately using `sh` or `system` in the task gets me the same error. – Undistraction Nov 28 '14 at 13:35
  • maybe that warning is to not install in the project gemfile - is it installed/have you tried installing it in your global gemfile? – dax Nov 28 '14 at 13:40
  • It's definitely available to the system. I can run `foreman start -f Procfile.dev` without `bundle exec` and it runs fine. – Undistraction Nov 28 '14 at 13:43
0

A little way down the line and here is how I do it suing chruby:

namespace :foreman do
  task :dev do
    # 1. Load .bash_profile to ensure chruby is available
    # 2. Switch the ruby version to the one defined in .ruby-version
    # 3. Start foreman
    system %(
      source ~/.bash_profile
      chruby "$(cat .ruby-version)"
      foreman start --procfile Procfile.dev
    )
  end
end

desc "Run Foreman using Procfile.dev"
task :foreman => 'foreman:dev'
Undistraction
  • 42,754
  • 56
  • 195
  • 331