forked from lifo/cramp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Barebone cramp application generator.
Example: $ cramp new appname
- Loading branch information
Showing
11 changed files
with
166 additions
and
1 deletion.
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
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,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 |
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,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 |
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,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' |
2 changes: 2 additions & 0 deletions
2
lib/cramp/generators/templates/application/app/actions/base_action.rb
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,2 @@ | ||
class BaseAction < Cramp::Action | ||
end |
6 changes: 6 additions & 0 deletions
6
lib/cramp/generators/templates/application/app/actions/home_action.rb
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,6 @@ | ||
class HomeAction < BaseAction | ||
def start | ||
render "Hello World!" | ||
finish | ||
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
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 |
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,4 @@ | ||
require './application' | ||
|
||
# bundle exec thin --max-persistent-conns 1024 -V -R config.ru start | ||
run <%= app_const %>.routes |
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,3 @@ | ||
HttpRouter.new do | ||
add('/').to(HomeAction) | ||
end |