Permalink
Browse files

most commands mostly in place - needs more testing

  • Loading branch information...
1 parent 3245721 commit f4ba69e0e426a47cd4545ac6bbf91eb07e6f6277 @kristianmandrup committed May 25, 2011
Showing with 228 additions and 73 deletions.
  1. +1 −1 Rakefile
  2. +54 −48 lib/dummy/app.rb
  3. +1 −1 lib/dummy/export.rb
  4. +121 −0 lib/dummy/generate.rb
  5. +32 −9 lib/dummy/install.rb
  6. +18 −10 lib/dummy/sandbox.rb
  7. +1 −4 lib/dummy/update.rb
View
@@ -22,7 +22,7 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |s|
s.name = "multiengine"
- s.version = "0.5.0"
+ s.version = "0.5.1"
s.summary = "Creates a Rails 3 engine configuration with multiple dummy apps for testing"
s.email = "kmandrup@gmail.com"
s.homepage = "http://github.com/kristianmandrup/multiapp-engine"
View
@@ -9,15 +9,16 @@
require "sugar-high/file"
require 'fileutils'
-require 'dummy/export'
-require 'dummy/import'
-require 'dummy/sandbox'
-require 'dummy/update'
-require 'dummy/install'
-require 'dummy/release'
-require 'dummy/generate'
module Dummy
+ autoload :Export, 'dummy/export'
+ autoload :Import, 'dummy/import'
+ autoload :Update , 'dummy/update'
+ autoload :Sandbox , 'dummy/sandbox'
+ autoload :Install, 'dummy/install'
+ autoload :Release, 'dummy/release'
+ autoload :Generate, 'dummy/generate'
+
class App < Thor::Group
include Thor::Actions
check_unknown_options!
@@ -26,63 +27,65 @@ def self.source_root
@_source_root ||= File.expand_path('../templates', __FILE__)
end
- argument :app_command, :type => :string, :default => 'sandbox',
- :desc => "Dummy App command: sandbox, export, import, gems, generate, update, release"
+ argument :app_command, :type => :string, :default => 'sandbox',
+ :desc => "Dummy App command: sandbox, export, import, gems, generate, update, release"
- class_option :sandbox, :type => :string, :default => "~/rails-dummies", :aliases => "-s",
- :desc => "Where to sandbox rails dummy apps"
+ class_option :sandbox, :type => :string, :default => "~/rails-dummies", :aliases => "-s",
+ :desc => "Where to sandbox rails dummy apps"
- class_option :orms, :type => :array, :default => [], :aliases => "-o",
- :desc => "Only effect dummy apps matching these orms"
+ class_option :orms, :type => :array, :default => [], :aliases => "-o",
+ :desc => "Only effect dummy apps matching these orms"
- class_option :gems, :type => :array, :default => [], :aliases => "-g",
- :desc => "Gems to insert in dummy apps"
+ class_option :gems, :type => :array, :default => [], :aliases => "-g",
+ :desc => "Gems to insert in dummy apps"
- class_option :github, :type => :string, :default => true, :aliases => "-h",
- :desc => "Release: github account."
+ class_option :github, :type => :string, :default => true, :aliases => "-h",
+ :desc => "Release: github account."
- class_option :bundle, :type => :boolean, :default => true, :aliases => "-b",
- :desc => "Export: run bundle after export"
+ class_option :bundle, :type => :boolean, :default => true, :aliases => "-b",
+ :desc => "Export: run bundle after export"
- class_option :command, :type => :string, :aliases => "-c",
- :desc => "Sandbox: run command in sandbox"
+ class_option :command, :type => :string, :aliases => "-c",
+ :desc => "Sandbox: run command in sandbox"
desc "Executes a command on one or more dummy apps"
- def invoke_rails_app_generators
+ def invoke_command
if !has_dummy_apps_dir?
say "dummy must be run from the Rails application root", :red
- exit(0)
+ return
end
-
+
if !valid_command?
say "The command #{app_command} was not recognized, please use one of: #{valid_commands.join(', ')}", :red
- exit(0)
+ return
end
-
- invoke command_class, app_args
+
+ say "invoking #{app_command_class} #{app_args}", :green
+
+ invoke app_command_class, app_args
end
protected
def valid_commands
[:sandbox, :export, :import, :gems, :generate, :update, :install, :release]
end
-
+
def valid_command?
valid_commands.include? command_sym
end
-
+
def command_sym
app_command.downcase.to_sym
- end
-
+ end
+
def app_args
args = case command_sym
when :sandbox
"--command #{command}"
when :export, :import
- "--sandbox #{sandbox} --bundle #{bundle}"
+ "--bundle #{bundle}"
when :generate
"--command #{command}"
when :update
@@ -91,42 +94,45 @@ def app_args
when :release
"--github #{github}"
end
- args << "--orms #{orms}"
+ args = [args]
+ args << "--sandbox #{sandbox}" if !sandbox.empty?
+ args << "--orms #{orms}" if !orms.empty?
+ args
end
-
- def self.class_options
+
+ def self.command_options
[:sandbox, :gems, :command, :github, :orms, :bundle]
end
-
- class_options.each do |clsopt|
+
+ command_options.each do |clsopt|
class_eval %{
def #{clsopt}
options[:#{clsopt}]
end
}
end
-
- def command_class
- "DummyApp::#{command_class}".constantize
+
+ def app_command_class
+ "Dummy::#{app_command_class_name}".constantize
end
-
- def command_class
- command.camelize
+
+ def app_command_class_name
+ app_command.camelize
end
-
+
def dummy_apps_dir
File.join(app_test_path, 'dummy-apps')
end
-
+
def has_dummy_apps_dir?
File.directory? dummy_apps_dir
end
-
+
def app_test_path
return 'test' if File.directory?('test')
return 'spec' if File.directory?('spec')
say "You must have a /spec or /test directory in the root of your project", :red
- exit(0)
- end
+ raise "Missing /spec or /test dir in project"
+ end
end
end
View
@@ -42,7 +42,7 @@ def self.source_root
def check!
if !has_dummy_apps_dir?
say "dummy must be run from the Rails application root", :red
- exit(0)
+ return
end
end
View
@@ -0,0 +1,121 @@
+require "thor/group"
+require "active_support"
+require "active_support/version"
+require "active_support/core_ext/string"
+
+require "rails/generators"
+require "rails/generators/rails/app/app_generator"
+
+require "sugar-high/file"
+require 'fileutils'
+
+# The problem is, that the rails command doesn't work right within a directory with its own Gemfile.
+# You need to first export the app to a sandbox, then run any bundle or rails commands:
+
+# - $ bundle ...
+# - $ rails new ...
+# - $ rails g ...
+
+# This functionality should be integrated into _export_ and _import_ commands of the *dummy* executable (and DummyApp generator).
+# @dummy export cancan_active_record ~/rails-dummies [--bundle]@
+
+module Dummy
+ autoload :Sandbox , 'dummy/sandbox'
+
+ class Generate < Thor::Group
+ include Thor::Actions
+ check_unknown_options!
+
+ def self.source_root
+ @_source_root ||= File.expand_path('../templates', __FILE__)
+ end
+
+ argument :apps, :type => :array, :default => [],
+ :desc => "Dummy apps to export"
+
+ class_option :sandbox, :type => :string, :default => "~/rails-dummies", :aliases => "-s",
+ :desc => "Where to sandbox rails dummy apps"
+
+ class_option :orms, :type => :array, :default => [], :aliases => "-o",
+ :desc => "Orms to match on dummy apps"
+
+ class_option :command, :type => :string, :aliases => "-c",
+ :desc => "Command to run on each dummy app"
+
+ desc "Run a command on a dummy app in the sandbox"
+
+ def set_root
+ self.destination_root = File.expand_path(destination_root)
+ end
+
+ def exec_generate
+ if !has_dummy_apps_dir?
+ say "dummy must be run from the Rails application root", :red
+ return
+ end
+
+ invoke sandbox_generator, sandbox_generate_args
+ end
+
+ protected
+
+ def sandbox_generate_args
+ args = [matching_dummy_apps, "-c #{command}"]
+ args << "--sandbox #{sandbox}" if !sandbox.empty?
+ args
+ end
+
+ def sandbox_generator
+ Dummy::Sandbox
+ end
+
+ def matching_dummy_apps
+ dummy_apps.select {|app| matches_any_orm?(app, orms) }
+ end
+
+ def matches_any_orm? app, orms
+ orms.any? {|orm| app =~ /#{orm}$/ }
+ end
+
+ def dummy_apps
+ FileList.new "dummy-*"
+ end
+
+ def self.class_options
+ [:sandbox, :orms, :command]
+ end
+
+ def sandbox_location
+ @sandbox_location ||= sandbox || '~/rails-dummies'
+ end
+
+ class_options.each do |clsopt|
+ class_eval %{
+ def #{clsopt}
+ options[:#{clsopt}]
+ end
+ }
+ end
+
+ def dummy_apps_dir
+ File.join(destination_root, dummy_apps_dir_relative)
+ end
+
+ def dummy_apps_dir_relative
+ File.join(app_test_path, 'dummy-apps')
+ end
+
+ def has_dummy_apps_dir?
+ File.directory? dummy_apps_dir
+ end
+
+ def app_test_path
+ return 'test' if File.directory?('test')
+ return 'spec' if File.directory?('spec')
+ say "You must have a /spec or /test directory in the root of your project", :red
+ raise "No test or spec dir found in project"
+ end
+ end
+end
+
+
View
@@ -50,22 +50,46 @@ def set_root
end
def sandbox_exec
- matching_dummy_apps.each do |dummy_app|
- export_app dummy_app
- insert_gems
- bundle_install
+ export_apps
+
+ matching_dummy_apps.each do |app|
+ FileUtils.cd sandbox_app_dir(app)
+ insert_gems_for app
+ bundle_install app
+ end
+
import_app dummy_app
end
end
protected
- def insert_gems
- say "Inserting gems into Gemfile: #{gems}"
+ def export_apps
+ invoke Dummy::Export, command_args
+ end
+
+ def command_args
+ args = [matching_dummy_apps]
+ args << "--sandbox #{sandbox}" if !sandbox.empty?
+ args
+ end
+
+ def import_apps
+ invoke Dummy::Import, command_args
+ end
+
+ def insert_gems_for app
+ say "Inserting gems into Gemfile: #{gems} in #{app}"
+ append_to_file gemfile, gems.maps{|gm| "gem '#{gm}'" }.join("\n")
+ end
+
+ def bundle_install app
+ say "bundle install for #{app}"
+ exec 'bundle'
end
- def bundle_install
- exec 'bundle' if bundle?
+ def sandbox_app_dir app
+ File.join(sandbox_location, app)
end
def exec command
@@ -100,7 +124,6 @@ def #{clsopt}
end
}
end
- alias_method :bundle?, :bundle
def
Oops, something went wrong.

0 comments on commit f4ba69e

Please sign in to comment.