Ubuntu下创建简单的ROR工程环境

Ruby on Rails 专栏收录该内容
8 篇文章 0 订阅

之前由于实际工作的需要,忙于在ubuntu下创建一个初始化工程环境,终于有机会把脚本写出来了,贴出来保存一下,也分享给有需要的人看看.

1.基础依赖环境


    Ubuntu OS (12.04是个不错的选择)

    rvm (参考文官安装环境, https://rvm.io/)

curl -L get.rvm.io | bash -s stable
source ~/.rvm/scripts/'rvm'
rvm requirements

    ruby-1.9.3-p125 (ruby193的语言要求更严格,值得试一下的)

rvm install 1.9.3-p125
    mysql 5.5
sudo apt-get install mysql-server mysql-client
    rubygem 1.8+ (193已带有)
    rails 3.2+ (3.2.1)
gem install rails -v 3.2.1
    rake 0.9.2.2
gem install rake -v 0.9.2.2

    bundler 1.0.22+

gem install bundler
    mysql2 0.3.11
    sqlite3

2.创建一个新的工程

rails new $project_name -f -d mysql -T --skip-bundle
可以先设置一个工程的名字:  project_name=xxxxx

3.添加工程对rvm的支持

cd $project_name
touch .rvmrc
echo "rvm use 1.9.3-p125@his --create --install" > .rvmrc
rvm use 1.9.3-p125@his --create --install

4.配置Gemfile并加入需要的插件

#cd $project_name
echo '# source "https://rubygems.org"
source "http://ruby.taobao.org/"

gem "rails", "3.2.1"

# Bundle edge Rails instead:
# gem "rails", :git => "git://github.com/rails/rails.git"

gem "mysql2", "0.3.11"
gem "sqlite3", "1.3.5"

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem "sass-rails",   "~> 3.2.3"
  gem "coffee-rails", "~> 3.2.1"

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  gem "execjs", "~> 1.3.0"
  gem "therubyracer"

  gem "uglifier", ">= 1.0.3"
  gem "compass-rails", "~> 1.0.1"
  gem "bootstrap-sass", "~> 2.0.2"          # compass plugin for boostrap
  #gem "bootstrap-rails"


end
# gem "haml", "~> 3.1.4"
gem "haml-rails", "~> 0.3.4"

gem "jquery-rails"

# To use ActiveModel has_secure_password
# gem "bcrypt-ruby", "~> 3.0.0"

# To use Jbuilder templates for JSON
# gem "jbuilder"

# Use unicorn as the web server
# gem "unicorn"

# Deploy with Capistrano
gem "capistrano"

# To use debugger
# gem "ruby-debug19", :require => "ruby-debug"

gem "jquery-rails"

gem "devise", "~> 2.0.4"
gem "oauth", "~> 0.4.5"
gem "oauth2"
gem "cancan"
#gem "userstamp"                        # right now not support well for rails 3.2
#gem "globalize3"

gem "simple-navigation"                 # Menu generator
gem "paperclip"                         # File and image upload
# gem "tiny_mce"                          # Rich text editor, multiple language
gem "tinymce-rails", "~> 3.4.9"
# gem "ckeditor"                        # Rich text editor
gem "simple_form"
gem "will_paginate", "~> 3.0.pre2"

#gem "rmagick", "~> 2.13.1"             # Interface between Ruby and ImageMagick.
#gem "rtesseract", "~> 0.0.8"           # Ruby library for working with the Tesseract OCR

gem "sunspot_rails", "~> 2.0.0.pre.111215"
gem "sunspot_solr", "~> 2.0.0.pre.111215"
gem "tlsmail", "~> 0.0.1"               # enables pop or smtp via ssl/tls

group :development, :test do
  #gem "delorean"                       # Travel in time with Ruby by mocking Time.now
  gem "simplecov", "~> 0.6.1"           # instead of rcov, Code coverage tool for Ruby.
  gem "yard", "~> 0.7.5"
  gem "hamlify", "~> 1.0.0"             # HTML to HAML conversion tool http://rubygems.org/gems/hamlify
    gem "hpricot", "~> 0.8.4"
    gem "ruby_parser", "~> 2.3.1"
end

group :test do
  gem "rspec-rails"
  gem "shoulda"
  gem "cucumber-rails"
  gem "cucumber-websteps"
  gem "capybara"
  gem "factory_girl_rails"
  gem "database_cleaner"
  gem "launchy"
  #gem "parallel_tests"                 # Speedup Test::Unit + RSpec + Cucumber
  # by running parallel on multiple CPUs (or cores).
end' > Gemfile

bundle install

5.为工程创建一个专属于的mysql账号

echo "grant all privileges on *.* to 'his'@'localhost' identified by 'his123';"|mysql -uroot -p$mysql_pwd
可以预先设置好mysql当前root的密码,以保证创建成功:  mysql_pwd=xxxx

6.配置工程数据库

#cd $project_name
sed -i 's/username\: root/username\: his/g' config/database.yml
sed -i 's/password\:/password\: his123/g' config/database.yml

7.初始化数据库

#cd $project_name
bundle exec rake db:create


好了,可以安装一些特殊的插件上去了.....


8.安装devise插件支持(登录插件)

bundle exec rails g devise:install
# 会首先以下提示
#  ===============================================================================
#
#  Some setup you must do manually if you haven't yet:
#    1. config/environments/development.rb:
#        config.action_mailer.default_url_options = { :host => 'localhost:3000' }
#        In production, :host should be set to the actual host of your application.
#    2. Ensure you have defined root_url to *something* in your config/routes.rb.
#         root :to => "home#index"
#    3. Ensure you have flash messages in app/views/layouts/application.html.erb.
#         <p class="notice"><%= notice %></p>
#         <p class="alert"><%= alert %></p>
#    4. If you are deploying Rails 3.1 on Heroku, you may want to set:
#         config.assets.initialize_on_precompile = false
#
#       On config/application.rb forcing your application to not access the DB
#       or load models when precompiling your assets.
#  ===============================================================================

8.1 重新修改主页的支持

mv public\index.html public\index.html.bak
bundle exec rails g controller home index
sed -i 's/# root/root/g' config/routes.rb
sed -i 's/welcome#index/home#index/g' config/routes.rb

8.2 添加本机邮箱主机

sed -i 's/^end/\n  config\.action_mailer\.default_url_options = \{ :host => "localhost:3000" \}\n\nend/g' config/environments/development.rb
#要是有域名的话,可以用上域名,如果只是本地试一下,用localhost即可.

8.3 安装devise的模型及视图

bundle exec rails g devise user
bundle exec rails g devise:views users

8.4 将erb文件转换成haml文件

touch tohaml.rb
echo 'class ToHaml
  def initialize(path)
    @path = path
  end
  
  def convert!
    Dir["#{@path}/**/*.erb"].each do |file|
      `html2haml -rx #{file} #{file.gsub(/\.erb$/, '.haml')}`
    end
  end
end

path = File.join(File.dirname(__FILE__), 'app', 'views')
ToHaml.new(path).convert!' > tohaml.rb 

ruby tohaml.rb
# 注意,如果运行tohaml.rb不成功,则要考虑安装一下haml,即:
# gem install haml

rm -rf app/views/**/**/*.erb

8.5 让devise的注册支持邮箱确认

sed -i 's/# t.string   :confirmation_token/t.string   :confirmation_token/g' db/migrate/*_devise_create_users.rb
sed -i 's/# t.datetime :confirmed_at/t.datetime :confirmed_at/g' db/migrate/*_devise_create_users.rb
sed -i 's/# t.datetime :confirmation_sent_at/t.datetime :confirmation_sent_at/g' db/migrate/*_devise_create_users.rb
sed -i 's/# t.string   :unconfirmed_email/t.string   :unconfirmed_email/g' db/migrate/*_devise_create_users.rb
  
sed -i 's/t.string :email/t.string :username\nt.string :email/g' db/migrate/*_devise_create_users.rb
sed -i 's/:validatable/:validatable, :confirmable/g' app/models/user.rb
sed -i 's/attr_accessible/attr_accessible :username,/g' app/models/user.rb
  
# 要在development.rb配置邮箱才行, 以下是用gmail来配置,它需要tls/ssl的邮箱登录方式,163的配置相对简单,不细说.
sed -i 's/^end/\n\
  require "tlsmail"\
  Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)\
  ActionMailer::Base.delivery_method = :smtp\
  ActionMailer::Base.perform_deliveries = true\
  ActionMailer::Base.smtp_settings = {\
      :address => "smtp.gmail.com",\
      :port => 587,\
      :domain => "mails.google.com",\
      :user_name => "<your_mail>@gmail.com",\
      :password => "<password>",\
      :authentication => "plain",\
      :enable_starttls_auto => true}\nend/g' config/environments/development.rb

9.安装sunspot的支持

bundle exec rails g sunspot_rails:install

10.安装rspec的支持

bundle exec rails g rspec:install

11.安装cucumber的支持

bundle exec rails g cucumber:install

11.1 配置一下cucumber的web_step

# referrence from https://www.relishapp.com/kucaahbe/cucumber-websteps/docs
touch features/support/paths.rb
#vim features/support/paths.rb 
echo '  
#########################################################
# features/support/paths.rb
module NavigationHelpers
  # Maps a name to a path. Used by the
  #
  #   When /^I go to (.+)$/ do |page_name|
  #
  # step definition
  #
  def path_to(page_name)
    case page_name

      when /^the home\s?page$/
        "/"

      when /the sign up page/
        "/users/sign_up"

      when /the sign in page/
        "/users/sign_in"


      # Add more mappings here.
      # Here is an example that pulls values out of the Regexp:
      #
      #   when /^(.*)''s profile page$/i
      #     user_profile_path(User.find_by_login($1))

      else
        begin
          page_name =~ /^the (.*) page$/
          path_components = $1.split(/\s+/)
          self.send(path_components.push("path").join("_").to_sym)
        rescue NoMethodError, ArgumentError
          raise "Can''t find mapping from \"#{page_name}\" to a path.\n" +
                    "Now, go and add a mapping in #{__FILE__}"
        end
    end
  end
end

World(NavigationHelpers)
#########################################################'> features/support/paths.rb 

touch features/support/selectors.rb
#vim features/support/selectors.rb 
echo ' 
#########################################################
# features/support/selectors.rb
module HtmlSelectorsHelpers
  # Maps a name to a selector. Used primarily by the
  #
  #   When /^(.+) within (.+)$/ do |step, scope|
  #
  # step definitions
  #
  def selector_for(locator)
    case locator

    when "the page"
      "html > body"

    # Add more mappings here.
    # Here is an example that pulls values out of the Regexp:
    #
    #  when /^the (notice|error|info) flash$/
    #    ".flash.#{$1}"

    # You can also return an array to use a different selector
    # type, like:
    #
    #  when /the header/
    #    [:xpath, "//header"]

    # This allows you to provide a quoted selector as the scope
    # for "within" steps as was previously the default for the
    # web steps:
    when /^"(.+)"$/
      $1

    else
      raise "Can''t find mapping from \"#{locator}\" to a selector.\n" +
        "Now, go and add a mapping in #{__FILE__}"
    end
  end
end

World(HtmlSelectorsHelpers)
#########################################################' > features/support/selectors.rb

#vim features/step_definitions/web_steps.rb
echo '
#########################################################    
# features/step_definitions/web_steps.rb
require "cucumber/websteps"
#########################################################' > features/step_definitions/web_steps.rb

12.安装simple_navigation导航栏或面包屑

bundle exec rails g navigation_config

13.配置i18n支持

一些基本的多语言的yml文件可以从相关的网站下载:  https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
可以拷贝到config/locales/defaults
配置config/application.rb支持多语言yml:

sed -i 's/# config.i18n.load_path/config.i18n.load_path/g' config/application.rb
sed -i "s/'locales',/'locales', '**',/g" config/application.rb
sed -i 's/# config.i18n.default_locale = :de/config.i18n.default_locale = :en/g' config/application.rb

创建其他多语言的配置文件夹,用于工程工发过程的各个类型的多语言配置

#  make model support i18n, need gem globalize3
#  category of locales file
#      - locales
#        - defaults
#        - model
#        - views
#          -users
#          -others
#  * controller should use flash[:notice] support
#  * model
#  * helper,viewer use the "t('.word')"
mkdir config/locales/defaults
mkdir config/models
mkdir config/views
mkdir config/views/users

14.配置工程支持compass & bootstrap

# 安装compass到rails工程
bundle exec compass init rails --syntax sass
# 详细可以参考: http://compass-style.org/install/

# 配置bootstrap-sass集成到compass
echo "require 'bootstrap-sass'" >> config/compass.rb

# 新建一个样式sass专门引入bootstrap的配置,用@import方式
touch app/assets/stylesheets/app_bootstrap.css.sass
echo '@import "bootstrap"
@import "bootstrap-responsive"' > app/assets/stylesheets/app_bootstrap.css.sass

# 配置js脚本引入bootstrap的脚本
sed -i 's/= require_tree/=# require_tree/g' app/assets/javascripts/application.js
echo '// Loads all Bootstrap javascripts
//= require bootstrap

// Or peek any of them yourself
//#= require bootstrap-transition
//#= require bootstrap-alert
//#= require bootstrap-modal
//#= require bootstrap-dropdown
//#= require bootstrap-scrollspy
//#= require bootstrap-tab
//#= require bootstrap-tooltip
//#= require bootstrap-popover
//#= require bootstrap-button
//#= require bootstrap-collapse
//#= require bootstrap-carousel
//#= require bootstrap-typeahead' >> app/assets/javascripts/application.js

15.安装simple_form的支持

rails g simple_form:install --bootstrap

主要插件基本装完,可以启动服务器了....

rake db:migrate

rails s

Bootstrap是网格的样式效果,支持html5及mobile, 除了它之外,像blueprint也是相类似的,而blueprint是compass支持的模式,不过没有较好的js效果.

关于sass与scss,这些都是compass框架可以进行解析的文件格式,两者实现的效果完全没有任何区别,只是两种不一样的代码模式,在工程中完全可以穿插使用,全部由compass解析.
另外还有一种是less模式,是bootstrap完全支持的,不过不能由compass解析,需要其他插件解析支持.
可以参考: 
 
bootstrap的插件很多个,bootstrap-sass只是其中一个,以目前的知识知道还可以实现,其他没有测试过.

关于一些linux命令的使用

sed

sed -i 's/old_string/new_strig/g' 文件路径
# old_string即文件中原来的路径, 可以包括""号文本,或要包括单引号文本,则需要加上单绰号,如下例子: 
# e.g1: sed -i 's/helloword/hello"world"/g' my_file.txt# e.g2: sed -i 's/helloword/hello''world''/g' my_file.txt# e.g3: sed -i "s/helloword/hello'world'/g" my_file.txt# 不过要是使用双引号的话,则有些变量以及正则式特殊字符回失效.# 参数-i表示替换原文件,立即生效, 不能用-e,否则无法生效

echo

# 打印变量
echo $variable
# 输出字符串到文件,并替换整个文件
echo "context" > file
# 输出字符串到文件,并追加到文件尾处
echo "new line" > file
# 输出整个文本块到文件
echo 'begin text, line 1
line 2
line 3
line "2232344"
line 5
line end' >file
set variable

new_var=1
echo $new_var
# output: 1
# $new_var then can be use in shell.






  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论
请先登录 后发表评论~
©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页

打赏作者

aegoose

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值