forked from padrino/padrino-framework
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters