Permalink
Browse files

Generator for a spare padrino gem

This generator generates a spare Padrino application that works both as a standalone application and
as a mountable Gem in other probjects.
  • Loading branch information...
skade committed Jan 12, 2013
1 parent 5456f30 commit 2975efa8a6f395b9075b1a7e939fff70412d8e91
@@ -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,gem}.rb']
##
# We add our tasks to padrino-core
@@ -0,0 +1,118 @@
require 'padrino-core/version'
require 'securerandom' unless defined?(SecureRandom)
module Padrino
module Generators
##
# Responsible for generating a new standalone Padrino app that can be packaged as a gem.
#
class Gem < Thor::Group
# Add this generator to our padrino-gen
Padrino::Generators.add_generator(:gem, 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 gem [name] [options]"; end
# Include related modules
include Thor::Actions
include Padrino::Generators::Actions
include Padrino::Generators::Runner
include Padrino::Generators::Components::Actions
desc "Description:\n\n\tpadrino-gen gem generates a new standalone Padrino app that can be packages as a gem"
argument :name, :desc => 'The name of your gem'
class_option :app, :desc => 'The application name', :aliases => '-n', :default => nil, :type => :string
class_option :bundle, :desc => 'Run bundle install', :aliases => '-b', :default => false, :type => :boolean
class_option :root, :desc => 'The root destination', :aliases => '-r', :default => '.', :type => :string
class_option :dev, :desc => 'Use padrino from a git checkout', :default => false, :type => :boolean
class_option :tiny, :desc => 'Generate tiny app skeleton', :aliases => '-i', :default => false, :type => :boolean
class_option :template, :desc => 'Generate project from template', :aliases => '-p', :default => nil, :type => :string
# Definitions for the available customizable components
# component_option :orm, 'database engine', :aliases => '-d', :choices => [:activerecord, :mini_record, :datamapper, :mongomapper, :mongoid, :sequel, :couchrest, :ohm, :mongomatic, :ripple], :default => :none
component_option :test, 'testing framework', :aliases => '-t', :choices => [:rspec, :shoulda, :cucumber, :bacon, :testspec, :riot, :minitest], :default => :none
component_option :mock, 'mocking library', :aliases => '-m', :choices => [:mocha, :rr], :default => :none
# component_option :script, 'javascript library', :aliases => '-s', :choices => [:jquery, :prototype, :rightjs, :mootools, :extcore, :dojo], :default => :none
# component_option :renderer, 'template engine', :aliases => '-e', :choices => [:haml, :erb, :liquid, :slim], :default => :haml
# component_option :stylesheet, 'stylesheet engine', :aliases => '-c', :choices => [:less, :sass, :compass, :scss], :default => :none
# Show help if no argv given
require_arguments!
# Copies over the Padrino base application App
#
# @api private
def setup_project
valid_constant?(options[:app] || name)
@app_name = (options[:app] || name).gsub(/\W/, '_').underscore.camelize + "::App"
self.destination_root = File.join(options[:root], name)
git_user_name = `git config user.name`.chomp
git_user_email = `git config user.email`.chomp
@author = git_user_name.empty? ? "TODO: Write your name" : git_user_name,
@email = git_user_email.empty? ? "TODO: Write your email address" : git_user_email
if in_app_root?
say 'You are in the root of a Padrino application! (config/boot.rb found)', :red
say 'Refusing to generate a new gem inside an existing application', :red
else
if options[:template] # Run the template to create project
execute_runner(:template, options[:template])
else # generate project without template
directory('project/', destination_root)
store_component_config('.components')
app_skeleton('app', options[:tiny])
template 'templates/gem/Gemfile.tt', destination_root('Gemfile')
template 'templates/Rakefile.tt', destination_root('Rakefile')
template 'templates/gem/gemspec.tt', destination_root(name + '.gemspec')
empty_directory destination_root('lib')
empty_directory destination_root("lib/#{name}")
template 'templates/gem/lib/libname.tt', destination_root("lib/#{name}.rb")
template 'templates/gem/lib/libname/version.tt', destination_root("lib/#{name}/version.rb")
end
end
end
# For each component, retrieve a valid choice and then execute the associated generator
#
# @api private
def setup_components
return if options[:template]
@_components = options.dup.slice(*self.class.component_types)
self.class.component_types.each do |comp|
choice = @_components[comp] = resolve_valid_choice(comp)
execute_component_setup(comp, choice)
end
store_component_config('.components')
end
# Bundle all required components using bundler and Gemfile
#
# @api private
def bundle_dependencies
if options[:bundle]
run_bundler
end
end
# Finish message
#
# @api private
def finish_message
say
say '=' * 65, :green
say "#{name} is ready for development!", :green
say '=' * 65, :green
say "$ cd #{options[:root]}/#{name}"
say "$ bundle" unless options[:bundle]
say "="*65, :green
say
end
end # Project
end # Generators
end # Padrino
@@ -0,0 +1,25 @@
source :rubygems
gemspec
# Server requirements
# gem 'thin' # or mongrel
# gem 'trinidad', :platform => 'jruby'
# Project requirements
gem 'rake'
# Component requirements
# Test requirements
# Use Padrino as specified in gemspec
gem 'padrino'
# Or Padrino Edge
# gem 'padrino', :git => 'git://github.com/padrino/padrino-framework.git'
# Or Individual Gems
<% unless options.dev? %># <% end %>%w(core gen helpers cache mailer admin).each do |g|
<% unless options.dev? %># <% end %> gem 'padrino-' + g, <% if options.dev? %>:path => '<%= Padrino::Generators::DEV_PATH %>/padrino-' + g<% else %>'<%= Padrino.version %>'<% end %>
<% unless options.dev? %># <% end %>end
@@ -0,0 +1,19 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/<%=name%>/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = [<%=@author.inspect%>]
gem.email = [<%=@email.inspect%>]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = ""
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = <%=name.inspect%>
gem.require_paths = ["lib"]
gem.version = <%=name.camelcase%>::VERSION
gem.add_dependency "padrino-core"
end
@@ -0,0 +1,2 @@
module Foo
end
@@ -0,0 +1,3 @@
module <%=name.camelcase%>
VERSION = '0.0.1'
end

0 comments on commit 2975efa

Please sign in to comment.