Writing your own apps generator

19e786a2a74377ff6e052d87fd8d1fa8?s=47 Yi-Ting Cheng
December 07, 2012

Writing your own apps generator

creating bootstrappers

19e786a2a74377ff6e052d87fd8d1fa8?s=128

Yi-Ting Cheng

December 07, 2012
Tweet

Transcript

  1. 2.

    Me • http://blog.xdite.net • Rails Developer & Entrepreneur • ROCO

    ( http://rocodev.com) • Facebook World Hack : World Grand Prize • using bootstrappers (prototype) 12年12月7⽇日星期五
  2. 7.

    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⽇日星期五
  3. 16.

    #!/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⽇日星期五
  4. 17.

    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⽇日星期五
  5. 18.

    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⽇日星期五
  6. 19.

    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⽇日星期五