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

来源:互联网 发布:linux修改root密码脚本 编辑:程序博客网 时间:2024/04/28 14:28

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

1.基础依赖环境


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

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

curl -L get.rvm.io | bash -s stablesource ~/.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_nametouch .rvmrcecho "rvm use 1.9.3-p125@his --create --install" > .rvmrcrvm use 1.9.3-p125@his --create --install

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

#cd $project_nameecho '# 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 Capistranogem "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 generatorgem "paperclip"                         # File and image upload# gem "tiny_mce"                          # Rich text editor, multiple languagegem "tinymce-rails", "~> 3.4.9"# gem "ckeditor"                        # Rich text editorgem "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 OCRgem "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/tlsgroup :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"endgroup :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' > Gemfilebundle 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_namesed -i 's/username\: root/username\: his/g' config/database.ymlsed -i 's/password\:/password\: his123/g' config/database.yml

7.初始化数据库

#cd $project_namebundle 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.bakbundle exec rails g controller home indexsed -i 's/# root/root/g' config/routes.rbsed -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 userbundle exec rails g devise:views users

8.4 将erb文件转换成haml文件

touch tohaml.rbecho 'class ToHaml  def initialize(path)    @path = path  end    def convert!    Dir["#{@path}/**/*.erb"].each do |file|      `html2haml -rx #{file} #{file.gsub(/\.erb$/, '.haml')}`    end  endendpath = File.join(File.dirname(__FILE__), 'app', 'views')ToHaml.new(path).convert!' > tohaml.rb ruby tohaml.rb# 注意,如果运行tohaml.rb不成功,则要考虑安装一下haml,即:# gem install hamlrm -rf app/views/**/**/*.erb

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

sed -i 's/# t.string   :confirmation_token/t.string   :confirmation_token/g' db/migrate/*_devise_create_users.rbsed -i 's/# t.datetime :confirmed_at/t.datetime :confirmed_at/g' db/migrate/*_devise_create_users.rbsed -i 's/# t.datetime :confirmation_sent_at/t.datetime :confirmation_sent_at/g' db/migrate/*_devise_create_users.rbsed -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.rbsed -i 's/:validatable/:validatable, :confirmable/g' app/models/user.rbsed -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/docstouch features/support/paths.rb#vim features/support/paths.rb echo '  ########################################################## features/support/paths.rbmodule 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  endendWorld(NavigationHelpers)#########################################################'> features/support/paths.rb touch features/support/selectors.rb#vim features/support/selectors.rb echo ' ########################################################## features/support/selectors.rbmodule 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  endendWorld(HtmlSelectorsHelpers)#########################################################' > features/support/selectors.rb#vim features/step_definitions/web_steps.rbecho '#########################################################    # features/step_definitions/web_steps.rbrequire "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.rbsed -i "s/'locales',/'locales', '**',/g" config/application.rbsed -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/defaultsmkdir config/modelsmkdir config/viewsmkdir config/views/users

14.配置工程支持compass & bootstrap

# 安装compass到rails工程bundle exec compass init rails --syntax sass# 详细可以参考: http://compass-style.org/install/# 配置bootstrap-sass集成到compassecho "require 'bootstrap-sass'" >> config/compass.rb# 新建一个样式sass专门引入bootstrap的配置,用@import方式touch app/assets/stylesheets/app_bootstrap.css.sassecho '@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.jsecho '// 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:migraterails s

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

关于sass与scss,这些都是compass框架可以进行解析的文件格式,两者实现的效果完全没有任何区别,只是两种不一样的代码模式,在工程中完全可以穿插使用,全部由compass解析.
另外还有一种是less模式,是bootstrap完全支持的,不过不能由compass解析,需要其他插件解析支持.
可以参考: 
    http://stackoverflow.com/questions/7127677/compass-vs-twitter-bootstrap
 
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 1line 2line 3line "2232344"line 5line end' >file
set variable

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




	
				
		
原创粉丝点击