Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make Rake tasks run under my Sinantra app/environment?

I'm using Sinatra, and I wanted to set up some of the convenience rake tasks that Rails has, specifically rake db:seed.

My first pass was this:

namespace :db do
  desc 'Load the seed data from db/seeds.rb'
  task :seed do
    seed_file = File.join(File.dirname(__FILE__), 'db', 'seeds.rb')
    system("racksh < #{seed_file}")
  end
end

racksh is a gem that mimics Rails' console. So I was just feeding the code in the seed file directly into it. It works, but it's obviously not ideal. What I'd like to do is create an environment task that allows commands to be run under the Sinanta app/environment, like so:

task :environment do
  # what goes here?
end

task :seed => :environment do
  seed_file = File.join(File.dirname(__FILE__), 'db', 'seeds.rb')
  load(seed_file) if File.exist?(seed_file)
end

But what I can't figure out is how to set up the environment so the rake tasks can run under it. Any help would be much appreciated.

like image 724
Brandon Weiss Avatar asked Sep 11 '10 18:09

Brandon Weiss


People also ask

How to run rails Rake task?

How to run rake? To run a rake task, just call the rake command with the name of your task. Don't forget to include your namespaces when you have them.

What is Rake tasks?

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.

What is Rakefile in Ruby?

Rake is a tool you can use with Ruby projects. It allows you to use ruby code to define "tasks" that can be run in the command line. Rake can be downloaded and included in ruby projects as a ruby gem. Once installed, you define tasks in a file named "Rakefile" that you add to your project.

What is a Rake in rails?

Rake is Ruby Make, a standalone Ruby utility that replaces the Unix utility 'make', and uses a 'Rakefile' and . rake files to build up a list of tasks. In Rails, Rake is used for common administration tasks, especially sophisticated ones that build off of each other.


1 Answers

I've set up a Rakefile for Sinatra using a kind of Rails-like environment:

task :environment do
  require File.expand_path(File.join(*%w[ config environment ]), File.dirname(__FILE__))
end

You then have something in config/environment.rb that contains what you need to start up your app properly. It might be something like:

require "rubygems"
require "bundler"
Bundler.setup

require 'sinatra'

Putting this set-up in a separate file avoids cluttering your Rakefile and can be used to launch your Sinatra app through config.ru if you use that:

require File.expand_path(File.join(*%w[ config environment ]), File.dirname(__FILE__))

run Sinatra::Application
like image 121
tadman Avatar answered Oct 19 '22 14:10

tadman