Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Barebone cramp application generator.

  Example:

  $ cramp new appname
  • Loading branch information...
commit 49977b1bed624f27bf448ff570be2ff21e97e44a 1 parent 312160a
@lifo authored
View
8 CHANGELOG
@@ -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.
View
2  Gemfile.lock
@@ -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/
@@ -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)
View
17 bin/cramp
@@ -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
View
6 cramp.gemspec
@@ -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
View
83 lib/cramp/generators/application.rb
@@ -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
View
16 lib/cramp/generators/templates/application/Gemfile
@@ -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'
View
2  lib/cramp/generators/templates/application/app/actions/base_action.rb
@@ -0,0 +1,2 @@
+class BaseAction < Cramp::Action
+end
View
6 lib/cramp/generators/templates/application/app/actions/home_action.rb
@@ -0,0 +1,6 @@
+class HomeAction < BaseAction
+ def start
+ render "Hello World!"
+ finish
+ end
+end
View
20 lib/cramp/generators/templates/application/application.rb
@@ -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
View
4 lib/cramp/generators/templates/application/config.ru
@@ -0,0 +1,4 @@
+require './application'
+
+# bundle exec thin --max-persistent-conns 1024 -V -R config.ru start
+run <%= app_const %>.routes
View
3  lib/cramp/generators/templates/application/config/routes.rb
@@ -0,0 +1,3 @@
+HttpRouter.new do
+ add('/').to(HomeAction)
+end
Please sign in to comment.
Something went wrong with that request. Please try again.