Skip to content

Commit

Permalink
add component generator
Browse files Browse the repository at this point in the history
  • Loading branch information
tyabe committed Sep 7, 2013
1 parent 4325460 commit 396b36c
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 2 deletions.
2 changes: 1 addition & 1 deletion padrino-gen/lib/padrino-gen.rb
Expand Up @@ -74,7 +74,7 @@ def load_components!
##
# We add our generators to Padrino::Generators
#
Padrino::Generators.load_paths << Dir[File.dirname(__FILE__) + '/padrino-gen/generators/{project,app,mailer,controller,model,migration,plugin}.rb']
Padrino::Generators.load_paths << Dir[File.dirname(__FILE__) + '/padrino-gen/generators/{project,app,mailer,controller,model,migration,plugin,component}.rb']

##
# We add our tasks to padrino-core
Expand Down
66 changes: 66 additions & 0 deletions padrino-gen/lib/padrino-gen/generators/component.rb
@@ -0,0 +1,66 @@
module Padrino
module Generators

##
# Responsible for add components within a Padrino project.
#
class Component < Thor::Group

# Add this generator to our padrino-gen
Padrino::Generators.add_generator(:component, self)

# Define the source template root
def self.source_root; File.expand_path(File.dirname(__FILE__)); end
# Defines the banner for this CLI generator
def self.banner; "padrino-gen component [options]"; end

# Include related modules
include Thor::Actions
include Padrino::Generators::Actions
include Padrino::Generators::Components::Actions

desc "Description:\n\n\tpadrino-gen component add components into a Padrino project"

class_option :root, :desc => 'The root destination', :aliases => '-r', :default => '.', :type => :string
class_option :adapter, :desc => 'SQL adapter for ORM (sqlite, mysql, mysql2, mysql-gem, postgres)', :aliases => '-a', :default => 'sqlite', :type => :string

# Definitions for the available customizable components
defines_component_options :default => false

# For each component, retrieve a valid choice and then execute the associated generator
def setup_components
self.destination_root = options[:root]
if in_app_root?
@_components = options.dup.slice(*self.class.component_types)
if @_components.values.delete_if(&:blank?).empty?
self.class.start(["-h"])
say
say "Current Selected Components:"
list = []
self.class.component_types.each do |comp|
list << [comp, fetch_component_choice(comp)]
end
print_table(list, :indent => 2)
exit
end

self.class.component_types.each do |comp|
next if @_components[comp].blank?

choice = @_components[comp] = resolve_valid_choice(comp)
existing = fetch_component_choice(comp)
if existing != 'none' && existing != choice
next unless yes?("Switch #{comp} to '#{choice}' from '#{existing}' ?[yes/no]:")
end
@project_name = fetch_component_choice(:namespace)
execute_component_setup(comp, choice)
store_component_choice(comp, choice)
end
else
say 'You are not at the root of a Padrino application! (config/boot.rb not found)'
end
end

end # Component
end # Generators
end # Padrino
85 changes: 85 additions & 0 deletions padrino-gen/test/test_component_generator.rb
@@ -0,0 +1,85 @@
require File.expand_path(File.dirname(__FILE__) + '/helper')

describe "ComponentGenerator" do
def setup
@apptmp = "#{Dir.tmpdir}/padrino-tests/#{UUID.new.generate}"
`mkdir -p #{@apptmp}`
end

def teardown
`rm -rf #{@apptmp}`
end

context 'the controller generator' do
should "fail outside app root" do
out, err = capture_io { generate(:component, 'demo', "-r=#{@apptmp}") }
assert_match(/not at the root/, out)
end
end


context "add components" do
should "properly generate default" do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}") }
out, err = capture_io { generate(:component, '--orm=activerecord', "-r=#{@apptmp}/sample_project") }
assert_match(/applying.*?activerecord.*?orm/, out)
assert_match_in_file(/gem 'activerecord', '>= 3.1', :require => 'active_record'/, "#{@apptmp}/sample_project/Gemfile")
assert_match_in_file(/gem 'sqlite3'/, "#{@apptmp}/sample_project/Gemfile")
assert_no_match(/Switch renderer to/, out)
components_chosen = YAML.load_file("#{@apptmp}/sample_project/.components")
assert_equal 'activerecord', components_chosen[:orm]
end

should "properly generate with adapter" do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}") }
out, err = capture_io { generate(:component, '--orm=sequel', '--adapter=postgres', "-r=#{@apptmp}/sample_project") }
assert_match_in_file(/gem 'pg'/, "#{@apptmp}/sample_project/Gemfile")
assert_no_match(/Switch renderer to/, out)
components_chosen = YAML.load_file("#{@apptmp}/sample_project/.components")
assert_equal 'sequel', components_chosen[:orm]
end
end


context "specified of same the component" do
should "does not change" do
capture_io { generate(:project, 'sample_project', '--script=jquery', "--root=#{@apptmp}") }
components_chosen = YAML.load_file("#{@apptmp}/sample_project/.components")
assert_equal 'jquery', components_chosen[:script]
out, err = capture_io { generate(:component, '--script=jquery', "-r=#{@apptmp}/sample_project") }
assert_match(/applying.*?jquery.*?script/, out)
assert_no_match(/Switch renderer to/, out)
components_chosen = YAML.load_file("#{@apptmp}/sample_project/.components")
assert_equal 'jquery', components_chosen[:script]
end
end


context "component changes" do
should "when allow changes, will be applied" do
capture_io { generate(:project, 'sample_project', '-renderer=slim', "--root=#{@apptmp}") }
components_chosen = YAML.load_file("#{@apptmp}/sample_project/.components")
assert_equal 'slim', components_chosen[:renderer]
$stdin.stub(:gets, 'yes') do
out, err = capture_io { generate(:component, '--renderer=haml', "-r=#{@apptmp}/sample_project") }
assert_match(/applying.*?haml.*?renderer/, out)
assert_match(/Switch renderer to/, out)
components_chosen = YAML.load_file("#{@apptmp}/sample_project/.components")
assert_equal 'haml', components_chosen[:renderer]
end
end
should "when deny changes, will not be applied" do
capture_io { generate(:project, 'sample_project', '-renderer=slim', "--root=#{@apptmp}") }
components_chosen = YAML.load_file("#{@apptmp}/sample_project/.components")
assert_equal 'slim', components_chosen[:renderer]
$stdin.stub(:gets, 'no') do
out, err = capture_io { generate(:component, '--renderer=haml', "-r=#{@apptmp}/sample_project") }
assert_no_match(/applying.*?haml.*?renderer/, out)
assert_match(/Switch renderer to/, out)
components_chosen = YAML.load_file("#{@apptmp}/sample_project/.components")
assert_equal 'slim', components_chosen[:renderer]
end
end
end

end
2 changes: 1 addition & 1 deletion padrino-gen/test/test_generator.rb
Expand Up @@ -4,7 +4,7 @@

context "the generator" do
should "have default generators" do
%w{controller mailer migration model app plugin}.each do |gen|
%w{controller mailer migration model app plugin component}.each do |gen|
assert Padrino::Generators.mappings.has_key?(gen.to_sym)
assert_equal "Padrino::Generators::#{gen.camelize}", Padrino::Generators.mappings[gen.to_sym].name
assert Padrino::Generators.mappings[gen.to_sym].respond_to?(:start)
Expand Down

0 comments on commit 396b36c

Please sign in to comment.