[OSDC12]相依性管理 - 以Ruby開發為例
-
Upload
yc-ling -
Category
Technology
-
view
5.090 -
download
6
Transcript of [OSDC12]相依性管理 - 以Ruby開發為例
![Page 1: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/1.jpg)
相依性管理以 Ruby 開發為例
大貓 (@miaout17)
![Page 2: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/2.jpg)
About Me
• 大貓 (YC Ling, @miaout17)• 2004 年因為奇妙的機緣開始學習 Ruby
– RPG Maker XP– Rails? 能吃嗎 ?
![Page 3: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/3.jpg)
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
![Page 4: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/4.jpg)
這場分享• 不是來傳 Ruby 教的• 沒什麼 Ruby 程式碼,不會 Ruby 也能聽懂
• 分享我對「高階語言相依性管理」的想法– Ruby 開發流程很方便– 自 Ruby 開發流程抽取與語言無關的概念
![Page 5: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/5.jpg)
Top 10 Programmer Excuses
Google: 2011 COSCUP Lightning Talk 嵌入式開發的故事
![Page 6: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/6.jpg)
Top 10 Programmer WTF
![Page 7: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/7.jpg)
相依性!
![Page 8: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/8.jpg)
高階語言開發的相依性
Hardware
OS
Native Code / Libraries
Virtual Machine/Interpreter/Compiler
Libraries
Applications
Abstraction!
Write once, debug run anywhere
How can we make it more smooth?
![Page 9: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/9.jpg)
High-Level Languages Developmentin 2012 should…
1. 容易安裝、切換執行環境2. 容易安裝函式庫3. 確保所有開發者、部署環境使用相同版本
的函式庫4. 使用版本控制系統,程式化地管理相依性5. 函式庫應使用一致的版號規則描述相容性
![Page 10: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/10.jpg)
“Getting Started” Scenario
Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
Run the program
![Page 11: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/11.jpg)
安裝 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
![Page 12: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/12.jpg)
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
![Page 13: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/13.jpg)
.rvmrc提醒您使用正確的 Ruby 環境
![Page 14: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/14.jpg)
安裝環境Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
$ rvm install 1.9.3
![Page 15: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/15.jpg)
下載專案源始碼Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
$ git clone git://github.com/rails/rails.git
![Page 16: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/16.jpg)
Library Management in Ruby
• 主流工具:– RubyGems: 管理 Ruby 套件– Bundler: 管理 Application 與 Gem
的相依性
Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
![Page 17: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/17.jpg)
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
![Page 18: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/18.jpg)
Gemspec
![Page 19: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/19.jpg)
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!
![Page 20: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/20.jpg)
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
![Page 21: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/21.jpg)
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
![Page 22: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/22.jpg)
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
![Page 23: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/23.jpg)
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’
![Page 24: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/24.jpg)
為什麼需要 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
![Page 25: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/25.jpg)
Gem v.s. Bundler
• Gem– 安裝 / 反安裝 / 發佈 gem– Gemspec 管理 gem 與 gem 的相依性
• Bundler– 管理 application 與 gem 的相依性
• 幫你把 application 需要的 gem 一次全部裝好• 確保所有開發者及部署使用相同版本的 gem
– 題外話: bundler 也能拿來開發 gem
![Page 26: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/26.jpg)
Bundler
See Also: gradle, maven, ivy, sbt, virtualenv, carton
在 Gemfile 中設定相依性
![Page 27: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/27.jpg)
Bundlerbundle install
執行 `bundle install`: 1. 計算並安裝所需要的 gem2. 產生 “ Gemfile.lock”
![Page 28: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/28.jpg)
Gemfile.lock
![Page 29: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/29.jpg)
BundlerEnsure you are using right gems
require "rubygems"require "bundler/setup"
$ bunlde exec ruby your_app.rb
在 Shell 中執行:
或是在程式啟始點:
建議:所有的 Ruby 專案都可以使用 Bundler
![Page 30: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/30.jpg)
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
![Page 31: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/31.jpg)
安裝相依之函式庫Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
$ bundle install
![Page 32: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/32.jpg)
執行單元測試…使用正確版本的函式庫
Install environment
Clone the project
Install projectdependencies
Execute unit test
TDD
$ bundle exec rake test
![Page 33: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/33.jpg)
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
![Page 34: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/34.jpg)
結論
![Page 35: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/35.jpg)
High-Level Languages Developmentin 2012 should…
1. 容易安裝、切換執行環境2. 容易安裝函式庫3. 確保所有開發者、部署環境使用相同版本
的函式庫4. 使用版本控制系統,程式化地管理相依性5. 函式庫應使用一致的版號規則描述相容性
![Page 36: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/36.jpg)
這些概念與語言無關
![Page 37: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/37.jpg)
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.
![Page 38: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/38.jpg)
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.
![Page 39: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/39.jpg)
![Page 40: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/40.jpg)
![Page 41: [OSDC12]相依性管理 - 以Ruby開發為例](https://reader036.fdocuments.net/reader036/viewer/2022062300/5555a7d1d8b42afe5d8b45ca/html5/thumbnails/41.jpg)
不要走開,接下來是 iHower 大大精彩的分享「那些 Functional Programming 教我的事」
Any Question?