Skip to content

Commit

Permalink
Barebone cramp application generator.
Browse files Browse the repository at this point in the history
  Example:

  $ cramp new appname
  • Loading branch information
lifo committed Jul 31, 2011
1 parent 312160a commit 49977b1
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
== 0.14 Asgard (Unreleased)

* Barebone application generator using Thor.

Example:

$ cramp new liveblog

== 0.13 Facelift (31 July, 2011)

* Added use_fiber_pool for wrapping every on_start/on_finish/periodic_timer callbacks within a Fiber. Default fiber pool size is 100.
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ PATH
activesupport (~> 3.0.9)
eventmachine (~> 0.12.10)
rack (~> 1.2.1)
thor (~> 0.14.6)

GEM
remote: http://rubygems.org/
Expand Down Expand Up @@ -48,6 +49,7 @@ GEM
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
thor (0.14.6)
tzinfo (0.3.29)
unicorn (3.4.0)
kgio (~> 2.2)
Expand Down
17 changes: 17 additions & 0 deletions bin/cramp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env ruby

require 'cramp'
require 'cramp/generators/application'

if ['--version', '-v'].include?(ARGV.first)
puts "Cramp #{Cramp::VERSION}"
exit(0)
end

if ARGV.first != "new"
ARGV[0] = "--help"
else
ARGV.shift
end

Cramp::Generators::Application.start
6 changes: 5 additions & 1 deletion cramp.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ Gem::Specification.new do |s|
s.add_dependency('activesupport', '~> 3.0.9')
s.add_dependency('rack', '~> 1.2.1')
s.add_dependency('eventmachine', '~> 0.12.10')
s.add_dependency('thor', '~> 0.14.6')

s.files = Dir['README', 'MIT-LICENSE', 'lib/**/*']
s.files = Dir['README', 'MIT-LICENSE', 'lib/**/*', 'bin/**/*']
s.has_rdoc = false

s.require_path = 'lib'

s.bindir = 'bin'
s.executables = ['cramp']
end
83 changes: 83 additions & 0 deletions lib/cramp/generators/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require 'thor/group'
require 'active_support/core_ext/string/strip'
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/object/blank'

module Cramp
module Generators

class Application < Thor::Group
include Thor::Actions

argument :application_path, :type => :string

def initialize(*args)
raise Thor::Error, "No application name supplied. Please run: cramp --help" if args[0].blank?

super
end

def self.source_root
@_source_root ||= File.join(File.dirname(__FILE__), "templates/application")
end

def self.banner
"cramp new #{self.arguments.map(&:usage).join(' ')} [options]"
end

def create_root
self.destination_root = File.expand_path(application_path, destination_root)
valid_const?

empty_directory '.'
FileUtils.cd(destination_root)
end

def create_root_files
template 'config.ru'
template 'Gemfile'
template 'application.rb'
end

def create_config
empty_directory "config"

inside "config" do
template "routes.rb"
end
end

def create_home_action
empty_directory "app/actions"

inside "app/actions" do
template "base_action.rb"
template "home_action.rb"
end
end

protected

def app_name
@app_name ||= File.basename(destination_root)
end

def app_const
@app_const ||= "#{app_const_base}::Application"
end

def app_const_base
@app_const_base ||= app_name.gsub(/\W/, '_').squeeze('_').camelize
end

def valid_const?
if app_const =~ /^\d/
raise Thor::Error, "Invalid application name #{app_name}. Please give a name which does not start with numbers."
elsif Object.const_defined?(app_const_base)
raise Thor::Error, "Invalid application name #{app_name}, constant #{app_const_base} is already in use. Please choose another application name."
end
end
end

end
end
16 changes: 16 additions & 0 deletions lib/cramp/generators/templates/application/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
source :rubygems

gem 'cramp'

# Async webserver for running a cramp application
gem 'thin'

# Rack based routing
gem 'http_router'

# For async Active Record models
# gem 'mysql2'
# gem 'activerecord'

# Using Fibers + async callbacks to emulate synchronous programming
# gem 'em-synchrony'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class BaseAction < Cramp::Action
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class HomeAction < BaseAction
def start
render "Hello World!"
finish
end
end
20 changes: 20 additions & 0 deletions lib/cramp/generators/templates/application/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "rubygems"
require "bundler"

Bundler.setup(:default)

require 'cramp'
require 'http_router'

require './app/actions/base_action'
require './app/actions/home_action'

module <%= app_const_base %>
class Application

def self.routes
@_routes ||= eval(File.read('./config/routes.rb'))
end

end
end
4 changes: 4 additions & 0 deletions lib/cramp/generators/templates/application/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require './application'

# bundle exec thin --max-persistent-conns 1024 -V -R config.ru start
run <%= app_const %>.routes
3 changes: 3 additions & 0 deletions lib/cramp/generators/templates/application/config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
HttpRouter.new do
add('/').to(HomeAction)
end

0 comments on commit 49977b1

Please sign in to comment.