From 03281ae4614fa7d9b711535765b3fbd221dce6bc Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Sat, 13 Jul 2013 22:05:56 +0800 Subject: [PATCH] App init script added. --- bin/nyara | 6 ++-- lib/nyara/command.rb | 5 +++ lib/nyara/templates/Gemfile | 4 ++- .../app/controllers/application_controller.rb | 3 ++ .../app/controllers/welcome_controller.rb | 5 +++ .../app/views/layouts/application.erb | 10 ++++++ .../templates/app/views/welcome/index.erb | 1 + lib/nyara/templates/config/boot.rb | 36 +++++++++++++++++++ lib/nyara/templates/config/development.rb | 7 ++++ lib/nyara/templates/config/production.rb | 7 ++++ lib/nyara/templates/config/test.rb | 7 ++++ 11 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 lib/nyara/templates/app/controllers/application_controller.rb create mode 100644 lib/nyara/templates/app/controllers/welcome_controller.rb create mode 100644 lib/nyara/templates/app/views/layouts/application.erb create mode 100644 lib/nyara/templates/app/views/welcome/index.erb diff --git a/bin/nyara b/bin/nyara index acf940f..666d38f 100755 --- a/bin/nyara +++ b/bin/nyara @@ -6,12 +6,12 @@ task_name = args.shift || 'help' case task_name -when 'help' - Nyara::Command.help -when '-h' +when 'help', '-h' Nyara::Command.help when '-v' Nyara::Command.version when 'new' Nyara::Command.new_project(*args) +when 's','server' + Nyara::Command.run_server(*args) end diff --git a/lib/nyara/command.rb b/lib/nyara/command.rb index ce8a42e..5711d0a 100644 --- a/lib/nyara/command.rb +++ b/lib/nyara/command.rb @@ -62,6 +62,11 @@ def new_project(name = nil,*args) puts "Enjoy!" end + + def run_server(*args) + args ||= [] + `ruby config/boot.rb` + end private def render_template(fname, opts = {}) diff --git a/lib/nyara/templates/Gemfile b/lib/nyara/templates/Gemfile index 41a8e7c..e51e8aa 100644 --- a/lib/nyara/templates/Gemfile +++ b/lib/nyara/templates/Gemfile @@ -1,4 +1,6 @@ source 'https://rubygems.org' -gem 'nyara', '<%= nyara_version %>' +gem 'tilt' +gem 'erubis' +gem 'nyara', '<%= Nyara::VERSION %>' gem 'mongoid', '3.1.4' diff --git a/lib/nyara/templates/app/controllers/application_controller.rb b/lib/nyara/templates/app/controllers/application_controller.rb new file mode 100644 index 0000000..d7484fb --- /dev/null +++ b/lib/nyara/templates/app/controllers/application_controller.rb @@ -0,0 +1,3 @@ +class ApplicationController < Nyara::Controller + set_default_layout 'layouts/application' +end diff --git a/lib/nyara/templates/app/controllers/welcome_controller.rb b/lib/nyara/templates/app/controllers/welcome_controller.rb new file mode 100644 index 0000000..e72cd74 --- /dev/null +++ b/lib/nyara/templates/app/controllers/welcome_controller.rb @@ -0,0 +1,5 @@ +class WelcomeController < ApplicationController + get '/' do + render 'welcome/index' + end +end diff --git a/lib/nyara/templates/app/views/layouts/application.erb b/lib/nyara/templates/app/views/layouts/application.erb new file mode 100644 index 0000000..0aff765 --- /dev/null +++ b/lib/nyara/templates/app/views/layouts/application.erb @@ -0,0 +1,10 @@ + + + + + <%= app_name %> + + + <%%= yield %> + + \ No newline at end of file diff --git a/lib/nyara/templates/app/views/welcome/index.erb b/lib/nyara/templates/app/views/welcome/index.erb new file mode 100644 index 0000000..ef7287d --- /dev/null +++ b/lib/nyara/templates/app/views/welcome/index.erb @@ -0,0 +1 @@ +Welcome to use Nyara. \ No newline at end of file diff --git a/lib/nyara/templates/config/boot.rb b/lib/nyara/templates/config/boot.rb index e69de29..681b6ec 100644 --- a/lib/nyara/templates/config/boot.rb +++ b/lib/nyara/templates/config/boot.rb @@ -0,0 +1,36 @@ +require "nyara" +require "erubis" +require 'bundler' and Bundler.setup(:default) +require 'mongoid' + +NYARA_ENV = (ENV['NYARA_ENV'] || 'development').downcase.strip +app_root = File.join(File.dirname(__FILE__),"../") + +# load controllers, models +Dir.glob(File.join("app/controllers/**/*")).each do |fname| + require_relative File.join("..",fname) +end +Dir.glob(File.join("app/models/**/*")).each do |fname| + require_relative File.join("..",fname) +end + +# Configure Mongoid +Mongoid.load!(File.join(app_root,'config/database.yml'), NYARA_ENV) + +configure do + set :env, NYARA_ENV + set :views, 'app/views' + set :session, :name, '_aaa' + set :session, :secure, true + set :session, :expires, 30 * 60 + + map '/', 'welcome' +end + +# TODO: 此处不用会报 Nyara::SimpleController: no action defined (RuntimeError) +get '/' do + send_string '' +end + +# require_relative NYARA_ENV + diff --git a/lib/nyara/templates/config/development.rb b/lib/nyara/templates/config/development.rb index e69de29..899e7e3 100644 --- a/lib/nyara/templates/config/development.rb +++ b/lib/nyara/templates/config/development.rb @@ -0,0 +1,7 @@ +configure do + set :port, 3000 # set listening port number + set :workers, 4 # set number of worker process + # if not set, will estimate best n by CPU count + + set :logger, true # equivalent to no setting +end diff --git a/lib/nyara/templates/config/production.rb b/lib/nyara/templates/config/production.rb index e69de29..899e7e3 100644 --- a/lib/nyara/templates/config/production.rb +++ b/lib/nyara/templates/config/production.rb @@ -0,0 +1,7 @@ +configure do + set :port, 3000 # set listening port number + set :workers, 4 # set number of worker process + # if not set, will estimate best n by CPU count + + set :logger, true # equivalent to no setting +end diff --git a/lib/nyara/templates/config/test.rb b/lib/nyara/templates/config/test.rb index e69de29..1403547 100644 --- a/lib/nyara/templates/config/test.rb +++ b/lib/nyara/templates/config/test.rb @@ -0,0 +1,7 @@ +configure do + set :port, 3000 # set listening port number + set :workers, 1 # set number of worker process + # if not set, will estimate best n by CPU count + + set :logger, true # equivalent to no setting +end \ No newline at end of file