Created
January 30, 2011 09:06
-
-
Save melborne/802707 to your computer and use it in GitHub Desktop.
Scaffolding Sinatra Basic Template
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
#!/usr/bin/env ruby | |
#-*-encoding: utf-8-*- | |
# | |
require "pathname" | |
class EaseSinatra | |
module Builder | |
MAIN_DIR = Pathname.pwd | |
BASE_NAME = "app" # MAIN_DIR.split[1] | |
MAIN_NAME = "#{BASE_NAME}.rb" | |
JS_DIR = 'public/javascripts' | |
def create(opts={}) | |
opts = {:coffee => true, :gem => true}.update(opts) | |
create_main_file(MAIN_NAME) | |
create_view_files(:layout, :index, :style) | |
if opts[:js] | |
create_js_file "#{BASE_NAME}.js" | |
else | |
create_coffee_file "#{BASE_NAME}.coffee" | |
end | |
create_model_file "model.rb" if opts[:model] | |
create_env_files if opts[:gem] | |
nil | |
rescue | |
STDERR.puts "something go wrong..." | |
end | |
private | |
def create_main_file(main_name) | |
main = MAIN_DIR + main_name | |
write(main, MAIN_TP) | |
end | |
def create_view_files(*files) | |
view = MAIN_DIR + 'views' | |
files.each do |fname| | |
ext = fname == :style ? "scss" : "haml" | |
file = view + "#{fname.to_s}.#{ext}" | |
write(file, VIEW_TP(fname)) | |
end | |
end | |
def create_js_file(base_name='app.js') | |
fname = MAIN_DIR + JS_DIR + base_name | |
write(fname, JS_TP) | |
end | |
def create_coffee_file(base_name='app.coffee', path='views') | |
fname = MAIN_DIR + path + base_name | |
write(fname, COFFEE_TP) | |
end | |
def create_env_files | |
files = { "config.ru" => CONF_TP, | |
"Gemfile" => GEM_TP, | |
".gitignore" => GITIGNORE_TP, | |
".rvmrc" => RVMRC_TP } | |
files.each do |name, template| | |
fname = MAIN_DIR + name | |
write(fname, template) | |
end | |
end | |
def create_model_file(fname) | |
model = MAIN_DIR + 'model' + fname | |
write(model, MODEL_TP) | |
end | |
def write(fname, template) | |
unless fname.exist? | |
fname.dirname.mkpath | |
open(fname, "w") do |f| | |
f.puts template | |
puts "created: #{fname}" | |
end | |
else | |
STDERR.puts "exist: #{fname}" | |
end | |
end | |
def VIEW_TP(fname) | |
case fname | |
when :layout then LAYOUT_TP | |
when :index then INDEX_TP | |
when :style then STYLE_TP | |
end | |
end | |
MAIN_TP, LAYOUT_TP, INDEX_TP, STYLE_TP, JS_TP, COFFEE_TP, CONF_TP, GEM_TP, MODEL_TP, GITIGNORE_TP, RVMRC_TP =<<MAIN, <<LAYOUT, <<INDEX, <<STYLE, <<JS, <<COFFEE, <<CONF, <<GEM, <<MODEL, <<GITIGNORE, <<RVMRC | |
require 'sinatra' | |
require 'haml' | |
require 'sass' | |
configure do | |
APP_TITLE = "#{BASE_NAME}" | |
end | |
get '/' do | |
haml :index | |
end | |
before do | |
end | |
helpers do | |
end | |
get '/javascripts/app.js' do | |
coffee :app | |
end | |
get '/style.css' do | |
scss :style | |
end | |
MAIN | |
!!! 5 | |
%html | |
%head | |
%meta{:charset => 'utf-8'} | |
%title= APP_TITLE | |
/%link{:rel => 'stylesheet', :href => 'http://www.w3.org/StyleSheets/Core/Chocolate'} | |
%link{:rel => 'stylesheet', :href => '/style.css'} | |
%script{:src => "https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"} | |
/%script{:src => "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"} | |
%script{:src => "/javascripts/#{BASE_NAME}.js"} | |
%body | |
= yield | |
LAYOUT | |
%header | |
#main | |
%h2 Welcome aboard! | |
%footer | |
INDEX | |
$font_color: #D0FFD0; | |
$bg_color: #325F82; | |
$canvas_color: #FFFFFF; | |
@mixin rounded($topl:8px, $topr:8px, $btmr:8px, $btml:8px) { | |
border-radius: $topl $topr $btmr $btml; | |
-moz-border-radius: $topl $topr $btmr $btml; | |
} | |
* { | |
margin: 0; | |
padding: 0; | |
font-family: Trebuchet ms, Verdana, Myriad Web, Syntax, sans-serif | |
} | |
body { | |
color: $font_color; | |
background-color: $bg_color; | |
width: 1000px; | |
margin: 60px auto; | |
} | |
header { | |
display:block; | |
} | |
#main { | |
canvas { | |
border: thin solid #444; | |
background-color: $canvas_color; | |
@include rounded(); | |
} | |
} | |
footer { | |
display: block; | |
height: 30px; | |
text-align: center; | |
margin-top: 20px; | |
a { | |
text-decoration: none; | |
color: $font_color; | |
&:visited { | |
color: $font_color; | |
} | |
} | |
} | |
STYLE | |
$(document).ready(function(){ | |
}) | |
JS | |
$-> | |
COFFEE | |
require 'bundler' | |
Bundler.require | |
$LOAD_PATH << File.expand_path(File.dirname(__FILE__)) | |
require '#{BASE_NAME}' | |
run Sinatra::Application | |
CONF | |
source :rubygems | |
gem 'sinatra' | |
gem 'haml' | |
gem 'coffee-script' | |
gem 'therubyracer-heroku', '0.8.1.pre3' | |
GEM | |
require "dm-core" | |
require "dm-migrations" | |
require "dm-timestamps" | |
require "dm-validations" | |
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/model/#{BASE_NAME}.db") | |
class Post | |
include DataMapper::Resource | |
property :id, Serial | |
property :title, String | |
property :body, Text | |
has n, :comments | |
end | |
class Comment | |
include DataMapper::Resource | |
property :id, Serial | |
property :body, Text | |
property :created_at, DateTime | |
belongs_to :user | |
end | |
MODEL | |
.DS_Store | |
*.log | |
.sass-cache | |
tmp/restart.txt | |
.rvmrc | |
GITIGNORE | |
rvm 1.9.2 | |
RVMRC | |
end | |
extend Builder | |
end | |
if __FILE__ == $0 | |
require "trollop" | |
opts = Trollop::options do | |
opt :model, "use Model file with DataMapper based" | |
opt :coffee, "use CoffeeScript file", :default => true | |
opt :js, "use JavaScript file" | |
opt :gem, "use Gemfile", :default => true | |
end | |
EaseSinatra.create(opts) | |
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
require "trollop" | |
require_relative "ease_sinatra" | |
def es(*args) | |
opts = Trollop::options do | |
opt :model, "use Model file with DataMapper based" | |
opt :coffee, "use CoffeeScript file", :default => true | |
opt :js, "use JavaScript file" | |
opt :gem, "use Gemfile", :default => true | |
end | |
EaseSinatra.create(opts) | |
end | |
es ARGV | |
__END__ | |
ex. | |
es -mj # => use :model & :js options |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment