Slide 1

Slide 1 text

Writing your own apps generator creating bootstrappers 12年12月7⽇日星期五

Slide 2

Slide 2 text

Me • http://blog.xdite.net • Rails Developer & Entrepreneur • ROCO ( http://rocodev.com) • Facebook World Hack : World Grand Prize • using bootstrappers (prototype) 12年12月7⽇日星期五

Slide 3

Slide 3 text

Bootstrappers 12年12月7⽇日星期五

Slide 4

Slide 4 text

Apps Generator with Bootstrap Layout 12年12月7⽇日星期五

Slide 5

Slide 5 text

`bootstrappers PROJECT_NAME` 12年12月7⽇日星期五

Slide 6

Slide 6 text

12年12月7⽇日星期五

Slide 7

Slide 7 text

Goal • Setting up project’s layout • Setting up project’s basic function • Setting up project’s basic development tool • Setting up project’s deployment config • Setting up my personal best practices 12年12月7⽇日星期五

Slide 8

Slide 8 text

Gemfile 12年12月7⽇日星期五

Slide 9

Slide 9 text

layouts/application.html.erb 12年12月7⽇日星期五

Slide 10

Slide 10 text

config/config.yml 12年12月7⽇日星期五

Slide 11

Slide 11 text

application.css.scss 12年12月7⽇日星期五

Slide 12

Slide 12 text

config/initializers/ 12年12月7⽇日星期五

Slide 13

Slide 13 text

https://github.com/xdite/bootstrappers • TODO.md • CHANGELOG.md 12年12月7⽇日星期五

Slide 14

Slide 14 text

How to write your own generators 12年12月7⽇日星期五

Slide 15

Slide 15 text

https://gist.github.com/4222145 12年12月7⽇日星期五

Slide 16

Slide 16 text

#!/usr/bin/env ruby require File.expand_path(File.join('..', 'lib', 'bootstrapers', 'generators', 'app_generator'), File.dirname(__FILE__ require File.expand_path(File.join('..', 'lib', 'bootstrapers', 'app_builder'), File.dirname(__FILE__)) templates_root = File.expand_path(File.join("..", "templates"), File.dirname(__FILE__)) # template file directory Bootstrapers::AppGenerator.source_root templates_root Bootstrapers::AppGenerator.source_paths << Rails::Generators::AppGenerator.source_root << templates_root Bootstrapers::AppGenerator.start # these three lines can make itself become executable generator bin/bootstrappers 12年12月7⽇日星期五

Slide 17

Slide 17 text

default options module Bootstrappers class AppGenerator < Rails::Generators::AppGenerator class_option :database, :type => :string, :aliases => '-d', :default => 'mysql', :desc => "Preconfigure for selected database (options: #{DATABASES.join('/')})" # ..... def finish_template invoke :bootstrappers_customization super end end end 12年12月7⽇日星期五

Slide 18

Slide 18 text

module Bootstrappers class AppBuilder < Rails::AppBuilder include Bootstrappers::Actions def remove_public_index remove_file 'public/index.html' end def create_partials_directory empty_directory 'app/views/application' empty_directory 'app/views/pages' empty_directory 'app/views/common' end def use_mysql_config_template template 'mysql_database.yml.erb', 'config/database.yml',:force => true template 'mysql_database.yml.erb', 'config/database.yml.example', :force => true # ..... end def add_custom_gems additions_path = find_in_source_paths 'Gemfile_additions' new_gems = File.open(additions_path).read inject_into_file 'Gemfile', "\n#{new_gems}", :after => /gem 'jquery-rails'/ end def setup_stylesheets copy_file 'app/assets/stylesheets/application.css', 'app/assets/stylesheets/application.css.scss' remove_file 'app/assets/stylesheets/application.css' concat_file 'import_scss_styles', 'app/assets/stylesheets/application.css.scss' end end end Thor 12年12月7⽇日星期五

Slide 19

Slide 19 text

module Bootstrappers class AppBuilder < Rails::AppBuilder # .... def use_mysql_config_template template 'mysql_database.yml.erb', 'config/database.yml',:force => true template 'mysql_database.yml.erb', 'config/database.yml.example', :force => true db_user_name = ask("What is your local database user name? [root]") db_password = ask("What is your local database password? ['']") replace_in_file 'config/database.yml', 'username: root', "username: #{db_user_name}" if db_user_name.presen replace_in_file 'config/database.yml', 'password: ""', "password: '#{db_password}'" if db_password.present? end end end Thor Setting up database force config/database.yml create config/database.yml.example What is your local database user name? [root] What is your local database password? [''] 123456 12年12月7⽇日星期五

Slide 20

Slide 20 text

thanks for listening https://github.com/xdite/bootstrappers 12年12月7⽇日星期五