Skip to content

Instantly share code, notes, and snippets.

@stas
Created November 22, 2012 15:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stas/4131823 to your computer and use it in GitHub Desktop.
Save stas/4131823 to your computer and use it in GitHub Desktop.
Rails Engine: rake task and generator to DRY `spec/dummy`
require 'rails/generators'
require 'rails/generators/rails/plugin_new/plugin_new_generator'
module MyEngine
class DummyGenerator < Rails::Generators::PluginNewGenerator
def self.default_source_root
Rails::Generators::PluginNewGenerator.default_source_root
end
def do_nothing
end
alias :create_root :do_nothing
alias :create_root_files :do_nothing
alias :create_app_files :do_nothing
alias :create_config_files :do_nothing
alias :create_lib_files :do_nothing
alias :create_public_stylesheets_files :do_nothing
alias :create_javascript_files :do_nothing
alias :create_script_files :do_nothing
alias :update_gemfile :do_nothing
alias :create_test_files :do_nothing
alias :finish_template :do_nothing
end
end
#!/usr/bin/env rake
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec => :dummy_app) do |t|
t.pattern = File.expand_path('../spec/**/*_spec.rb', __FILE__)
end
task :default => :spec
desc 'Generates a dummy app for testing'
task :dummy_app => [:setup, :migrate]
task :setup do
require 'rails'
require 'my_engine'
require 'my_engine/generators/dummy_generator'
dummy = File.expand_path('../spec/dummy', __FILE__)
sh "rm -rf #{dummy}"
MyEngine::DummyGenerator.start(
%W(. --quiet --force --skip-bundle --old-style-hash --dummy-path=#{dummy})
)
end
task :migrate do
rakefile = File.expand_path('../spec/dummy/Rakefile', __FILE__)
sh("rake -f #{rakefile} my_engine:install:migrations")
sh("rake -f #{rakefile} db:create db:migrate db:test:prepare")
end
@etdsoft
Copy link

etdsoft commented Mar 13, 2013

Thanks for this, it is amazing.

@stas not sure if I'm trying to get too fancy, but I'd like to remove the dependency of hard-coding the engine name in the :migrate task. I'm working on an open source framework for security testing that is extendable using plugins.

I've removed the hard-coding dependency in the :setup task by creating my DummyGenerator in the core of the framework and using it from my plugins. You can see an example here Rakefile#L20-31:

task :setup do
  require 'rails'
  require 'dradis_core'
  require 'dradis/generators/dummy/dummy_generator'

  dummy = File.expand_path('../spec/dummy', __FILE__)

  sh "rm -rf #{dummy}"
  Dradis::DummyGenerator.start(
    %W(. --quiet --force --skip-bundle --dummy-path=#{dummy})
  )
end

task :migrate do
  rakefile = File.expand_path('../spec/dummy/Rakefile', __FILE__)

  # dradis_core migrations
  sh("rake -f #{rakefile} dradis:install:migrations")

  # this gem's migrations
  if Dir[ File.expand_path('../db/migrate/*', __FILE__) ].empty?
    puts "Skipping :migrate as this plugin doesn't define any migrations"
  else
    # TODO: hard-coded plugin name ahead!
    sh("rake -f #{rakefile} dradis_html_export:install:migrations")
  end
  sh("rake -f #{rakefile} db:create db:migrate db:test:prepare")
end

This is the Rake file in one of the plugins and you can see the hard-coded dradis_html_export reference next to the bottom. Ideally I'd like to have my plugins:

require 'dradis/tasks/plugins'

And manage from the framework classes the :dummy_app, :setup and :migrate tasks. My first idea has been to pass the engine name as a variable, something like:

Dradis::Core::DummyAppTask.new('dradis_html_export')

But this isn't very neat. I assume that there should be some way for the core library to learn about the engine name where it is being called from. At the end of the day, this is done by the DummyGenerator, it correctly adds the following to the dummy app's core/applicarion.rb:

Bundler.require
require "dradis-html_export"

I'm still trying to figure this one out, but I'm not as familiar with the rails internals as I should be and was wondering if you may have some thoughts on it.

Thanks!

@stas
Copy link
Author

stas commented May 20, 2013

@gottfrois
Copy link

Cool gem, definitely plan on using it, Thx ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment