Upload
yc-ling
View
5.090
Download
6
Embed Size (px)
Citation preview
相依性管理以 Ruby 開發為例
大貓 (@miaout17)
About Me
• 大貓 (YC Ling, @miaout17)• 2004 年因為奇妙的機緣開始學習 Ruby
– RPG Maker XP– Rails? 能吃嗎 ?
About Me
• 大貓 (YC Ling, @miaout17)• 業餘 Ruby 遊戲開發者 (2004~2007)• C++/Lua/ActionScript 遊戲開發者
(2007~2010)• Java/Ruby Web 開發者 (2010~now)• 業餘 Functional Programming 愛好者• Github: https://github.com/miaout17
這場分享• 不是來傳 Ruby 教的• 沒什麼 Ruby 程式碼,不會 Ruby 也能聽懂
• 分享我對「高階語言相依性管理」的想法– Ruby 開發流程很方便– 自 Ruby 開發流程抽取與語言無關的概念
Top 10 Programmer Excuses
Google: 2011 COSCUP Lightning Talk 嵌入式開發的故事
Top 10 Programmer WTF
相依性!
高階語言開發的相依性
Hardware
OS
Native Code / Libraries
Virtual Machine/Interpreter/Compiler
Libraries
Applications
Abstraction!
Write once, debug run anywhere
How can we make it more smooth?
High-Level Languages Developmentin 2012 should…
1. 容易安裝、切換執行環境2. 容易安裝函式庫3. 確保所有開發者、部署環境使用相同版本
的函式庫4. 使用版本控制系統,程式化地管理相依性5. 函式庫應使用一致的版號規則描述相容性
“Getting Started” Scenario
Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
Run the program
安裝 Ruby Rubies
• Not always up-to-date• Always not up-to-date• Multiple Rubies: MRI 1.8.7,
1.9.2, 1.9.3, REE, JRuby, Rubinius, IronRuby, MacRuby, …
Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
$ apt-get install ruby $ yum –y install ruby
RVM (Ruby Version Manager)
安裝 RVM Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
See Also: PerlBrew, PythonBrew, PHPBrew…
$ curl -L get.rvm.io | bash -s stable
$ rvm install 1.8.7$ rvm install 1.9.3-p125$ rvm install jruby
安裝 Rubies
切換不同 Rubies$ rvm use 1.8.7$ rvm use 1.9.3 --default
.rvmrc提醒您使用正確的 Ruby 環境
安裝環境Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
$ rvm install 1.9.3
下載專案源始碼Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
$ git clone git://github.com/rails/rails.git
Library Management in Ruby
• 主流工具:– RubyGems: 管理 Ruby 套件– Bundler: 管理 Application 與 Gem
的相依性
Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
RubyGems
安裝 gem 及其相依的 gems
Yes. A lot of languages can do this. See Also: pip, cpan, pear, onion, cabal, npm, maven, ivy…
$ gem install rails$ gem install rails –v=2.3.5
更新 gems
$ gem update
發佈 gems
$ gem push foo-1.0.0.gem
Gemspec
Gem 相依性
s.add_dependency ’devise', ‘1.5.0'
固定版本
s.add_dependency ’devise', ‘>= 1.5.0'
最小版本
版本範圍s.add_dependency ’devise', ‘>= 1.5’, ‘< 2.0’
Just a shortcut?
s.add_dependency ’devise', ‘~> 1.5’ # ‘>= 1.5’, ‘< 2.0’s.add_dependency ’devise', ‘~> 1.5.1’ # ‘>=1.5.1’, ‘< 1.6’
“Twiddle Wakka”
LibraryFoobar
Devise1.5.0
Devise1.6.0
Devise2.0.0
Broken!
Semantic Versioning• Authored by Tom Preston-Werner, inventor of
Gravatars and cofounder of GitHub• 一致的版號規則 : Major.Minor.Patch• Rails 3.1.10
– Major version: 3– Minor version: 1– Patch version: 10
Semantic Versioning
• Major version change– 不向下相容– Ex. Rails 2.3.7 -> 3.0.0
• Minor version change– 向下相容,增加新功能– Ex. Rails 3.0.10 -> 3.1.0
• Patch version change– 向下相容,只修正 bug– Ex. Rails 3.1.0 -> 3.1.1
Semantic Versioning• 例外 : Major version 0 為早期開發
– 0.0.1, 0.0.2, 0.9.4 什麼都可能不相容• 更多細節請見 http://semver.org/
– Pre-release– Tag name– Semantic versioning v2.0.0-rc1
• See also: Apache APR Version Numbering
Semantic Versioning
• Semantic Version + Twiddle Wakka– 使用一致的版號規則描述相容性– 簡潔地指定「相容的版本區間」
• Encouraged by the Ruby community– http://robots.thoughtbot.com/post/2508037841/twiddle-wakka– http://www.slideshare.net/copiousfreetime/gemology– 許多專案 Follow 此規則
s.add_dependency ’devise', ‘~> 1.5’ # ‘>= 1.5’, ‘< 2.0’s.add_dependency ’devise', ‘~> 1.5.0’ # ‘>=1.5.0’, ‘< 1.6’
為什麼需要 Bundler?
Rails2.3.7
ProjectWahaha
ProjectAlala
Rails3.0.0
Devise2.0.0
Sqlite31.3.3
Installed Gems
Broken!
Execute withBundler
JSON1.2.1
Execute withBundler
Gem v.s. Bundler
• Gem– 安裝 / 反安裝 / 發佈 gem– Gemspec 管理 gem 與 gem 的相依性
• Bundler– 管理 application 與 gem 的相依性
• 幫你把 application 需要的 gem 一次全部裝好• 確保所有開發者及部署使用相同版本的 gem
– 題外話: bundler 也能拿來開發 gem
Bundler
See Also: gradle, maven, ivy, sbt, virtualenv, carton
在 Gemfile 中設定相依性
Bundlerbundle install
執行 `bundle install`: 1. 計算並安裝所需要的 gem2. 產生 “ Gemfile.lock”
Gemfile.lock
BundlerEnsure you are using right gems
require "rubygems"require "bundler/setup"
$ bunlde exec ruby your_app.rb
在 Shell 中執行:
或是在程式啟始點:
建議:所有的 Ruby 專案都可以使用 Bundler
Bundler with git
$ vim Gemfile$ bundle install # or bundle update$ git add Gemfile Gemfile.lock; git commit
修改 Gemfile
$ bundle update$ git add Gemfile.lock; git commit
更新 Gems
安裝相依之函式庫Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
$ bundle install
執行單元測試…使用正確版本的函式庫
Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
$ bundle exec rake test
Wrapping up
Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
$ rvm install 1.9.3-p125
$ git clone git://github.com/rails/rails.git
$ cd rails$ bundle install
$ bundle exec rake test
結論
High-Level Languages Developmentin 2012 should…
1. 容易安裝、切換執行環境2. 容易安裝函式庫3. 確保所有開發者、部署環境使用相同版本
的函式庫4. 使用版本控制系統,程式化地管理相依性5. 函式庫應使用一致的版號規則描述相容性
這些概念與語言無關
PHPBrew, Onion
https://github.com/c9s/phpbrewBrew & manage PHP versions in pure PHP at HOMEhttps://github.com/c9s/OnionOnion, The fast approach to build/bundle PEAR packages for PHP.
Carton
Carton is a new command to manage Perl module dependencies for your applications. Like gem bundler for Ruby, Carton solves the issue of "upgrading CPAN modules broke my code" by allowing you to lock and freeze dependencies. Carton makes it easy to manage and reproduce module installations on any environments.
不要走開,接下來是 iHower 大大精彩的分享「那些 Functional Programming 教我的事」
Any Question?