Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import rake tasks from a gem when using Sinatra?

I'm trying to add some basic rake tasks to the orientdb gem that will allow me to create the database, create database migrations, and migrate the database - similar to rails migrations.

When I had the rake tasks locally, I got the db:settings, db:create, and db:create_migration to work, but after I put them in the gem I can't figure out how to access them from the Sinatra application when using "rake".

I have a feeling I am either a) not organizing the files in the gem properly and/or b) not calling things properly from the Sinatra app.

The current state of my forked repository is at https://github.com/ricaurte/orientdb-jruby

I put the file for the tasks in lib/orientdb/tasks/database.rake => https://github.com/ricaurte/orientdb-jruby/blob/master/lib/orientdb/tasks/database.rake

Sinatra App Rakefile

APPLICATION_ROOT = File.expand_path('..', __FILE__)

require 'rake'
require 'orientdb'
#import "orientdb/tasks/database.rake"

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

config/environment

require "rubygems"
require "bundler"
Bundler.setup

require 'sinatra'
require "orientdb"

ruby: jruby 1.6.0 (ruby 1.9.2 patchlevel 136) (2011-03-24 5f5278c) (OpenJDK 64-Bit Server VM 1.6.0_20) [linux-amd64-java]

like image 250
ricaurte Avatar asked Mar 28 '11 11:03

ricaurte


1 Answers

I figured out how to do it after 10+ hours of trying to figure it out and I had to patch Rake in the process. I submitted this patch: https://github.com/jimweirich/rake/pull/28

I also wrote up a blog entry that contains the patched code: http://www.justinidea.com/2011/03/proposed-modification-to-rakes-discovery-of-tasks.html

[UPDATE]

I also found another way that doesn't require a rake patch, go figure...all I had to do was create a tasks.rb and require it inside the rakefile, but to make this work I had to make it look like this:

require 'rake'
require 'bundler'
Bundler.setup
require 'orientdb'
require 'orientdb/tasks'

I still think the rake patch is pretty cool though. :)

[UPDATE 2]

In the mean time until the pull request gets accepted by the rake team, I created a gem called alltasks that will load all of the rake tasks that the gems in your Gemfile and their dependencies contain.

https://github.com/ricaurte/alltasks

like image 159
ricaurte Avatar answered Oct 15 '22 19:10

ricaurte