Vinova tuyển lập trình viên Mobile & Web ở Hà Nội, lương $300-1000

Article: Liên kết Subversion với Ruby 1164

Ruby căn bản
tnd.myopenid.com 17
Updated over 5 years ago
Subversion (SVN) trong thời gian gần đây đã trở thành một công cụ khá phổ biến thay thế cho CVS. SVN có các liên kết cho phép lập trình bằng các ngôn ngữ script như Perl, Python và Ruby để điều khiển SVN. Bài viết này đưa ra một số ví dụ về cách sử dụng Ruby liên kết với SVN.

Cài đặt thư viện libsvn-ruby

Để sử dụng được liên kết SVN-Ruby thì trước tiên phải có thư viện libsvn-ruby. Nếu bạn sử dụng Debian hay Ubuntu thì dùng lệnh sau để cài đặt:

    #apt-get install libsvn-ruby1.8
Để kiểm tra xem việc cài đặt đã diễn ra tốt đẹp hay không:

    $irb
    irb(main):001:0> require 'svn/core' 
    => true
    irb(main):002:0>

Sử dụng thư viện

Xem code ví dụ sau:

  1 require "fileutils" 
  2 require "svn/core" 
  3 require "svn/client" 
  4 require "svn/wc" 
  5 require "svn/repos" 
  6
  7 class TestSvn
  8   def initialize
  9     setup_basic
 10   end
 11
 12   def setup_basic
 13     @author = ENV["USER"] || "sample-user" 
 14     @repos_path = File.join("test", "repos")
 15     @full_repos_path = File.expand_path(@repos_path)
 16     @repos_uri = "file://#{@full_repos_path}" 
 17     @wc_path = File.join("test", "wc")
 18     @full_wc_path = File.expand_path(@wc_path)
 19     @config_path = File.join("test", "config")
 20   end
 21
 22   def setup_repository(path=@repos_path, config={}, fs_config={})
 23     FileUtils.rm_rf(path)
 24     FileUtils.mkdir_p(File.dirname(path))
 25     Svn::Repos.create(path, config, fs_config)
 26     @repos = Svn::Repos.open(@repos_path)
 27     @fs = @repos.fs
 28   end
 29
 30   def make_context(log)
 31     ctx = Svn::Client::Context.new
 32     ctx.set_log_msg_func do |items|
 33       [true, log]
 34     end
 35     ctx.add_username_prompt_provider(0) do |cred, realm, username, may_save|
 36       cred.username = @author
 37       cred.may_save = false
 38     end
 39     setup_auth_baton(ctx.auth_baton)
 40     ctx
 41   end
 42
 43   def setup_auth_baton(auth_baton)
 44     auth_baton[Svn::Core::AUTH_PARAM_CONFIG_DIR] = @config_path
 45     auth_baton[Svn::Core::AUTH_PARAM_DEFAULT_USERNAME] = @author
 46   end
 47
 48   def setup_config
 49     FileUtils.rm_rf(@config_path)
 50     Svn::Core::Config.ensure(@config_path)
 51   end
 52
 53   def setup_wc
 54     FileUtils.rm_rf(@wc_path)
 55     ctx = make_context("sample log")
 56     ctx.checkout(@repos_uri, @wc_path)
 57     filetest_path =  File.join(@wc_path, "filetest")
 58     FileUtils.touch(filetest_path)
 59     puts "add #{filetest_path}" 
 60     ctx.add(filetest_path, false)
 61     puts("set property: prop1:val1")
 62     ctx.propset("prop1","val1", filetest_path, false, false)
 63     puts "commit..." 
 64     ctx.commit(@wc_path)
 65   end
 66 end
 67
 68 svn = TestSvn.new
 69 svn.setup_repository
 70 svn.setup_config
 71 svn.setup_wc

Dòng từ 68 khởi tạo các giá trị đường dẫn cho repository (repos), wc (working copy) và config.

Dòng 69 khởi tạo một repository và gọi đến hàm setup_repository và hàm cần gọi là dòng 26 Svn::Repos.create(path, config, fs_config) sẽ tạo ra một repository tại path.

Dòng 70 sẽ tạo ra cấu trúc của config và gọi đến hàm Svn::Core::Config.ensure(@config_path) tại dòng số 50.

Dòng số 71 sẽ checkout để tạo ra 1 cái working-copy. Trước khi gọi đến lệnh checkout thì ta phải tạo ra 1 context như trong hàm make_context(log).

Tài liệu tham khảo

Gói tài liệu libsvn-doc có đủ thông tin về API sử dụng cho libsvn. Xem mã nguồn của gói libsvn-ruby trong /usr/lib/ruby/1.8/svn

Comments

You must login to be able to comment

Uploaded files

No file uploaded yet

You must login to be able to upload

Nhà tài trợ:

Mọi người đều tự do viết bài, sửa bài của người khác, và bình luận ở trang web này. Bạn muốn chủ động tạo bài mới để chia sẻ kinh nghiệm với mọi người? Xin click link ở dưới.

Create new content