AnApprentice
AnApprentice

Reputation: 111050

how to build a rake task

I'd like to learn how to build a rake task for my rails 3 app that does two things.

  1. Pushes assets to the CDN
  2. Deploys to heroku

Commands for the above steps:

 rake cache:s3
 heroku jammit:deploy --app #{app}

Here's what I have /lib/tasks/deployer.rake

task :deployit do
  puts '=== Storing assets on s3 ==='
  run "rake cache:s3"
  puts '=== Deploying to Heroku ==='
  run "heroku jammit:deploy --app #{app}"
end

def run(cmd)
  shell cmd
  if $?.exitstatus == 0
    display "[OK]"
  else
    display "[FAIL]"
  end
end

But that errors with 'undefined method `shell' for main:Object'

Suggestions on how to make this work? Should this be a task or something else?

Thanks

Upvotes: 0

Views: 470

Answers (2)

suweller
suweller

Reputation: 514

The first task is another rake task, which will run if it is a dependency of the :deployit task. Your current code would loads rake twice.

If you use system like Arun suggested you would get:

task :deployit => 'cache:s3' do
  puts '=== Deploying to Heroku ==='
  system "heroku jammit:deploy --app #{app}"
end

Upvotes: 2

Arun Kumar Arjunan
Arun Kumar Arjunan

Reputation: 6857

Use system command instead. For more info: http://blog.jayfields.com/2006/06/ruby-kernel-system-exec-and-x.html

Upvotes: 2

Related Questions