Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Writing your own apps generator

Yi-Ting Cheng
December 07, 2012

Writing your own apps generator

creating bootstrappers

Yi-Ting Cheng

December 07, 2012
Tweet

More Decks by Yi-Ting Cheng

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

  3. Bootstrappers
    12年12月7⽇日星期五

    View Slide

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

    View Slide

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

    View Slide

  6. 12年12月7⽇日星期五

    View Slide

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

    View Slide

  8. Gemfile
    12年12月7⽇日星期五

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide