---
Churn项目:轻松编写脚本
The Churn Project: Writing Scripts without Fuss
---
subversion安装包下载:
svn-1.4.5-setup.rar
#---
# Excerpted from "Everyday Scripting in Ruby"
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/bmsft for more book information.
#---
# This is the same as churn.v7.rb. It's a different file because
# of the mechanics of book production.
# 处理时间(过去一个月)
def month_before(a_time)
a_time - 28 * 24 * 60 * 60
end
# 处理打印标题信息
def header(an_svn_date)
"Changes since #{an_svn_date}:"
end
# 组成打印主体信息(参数:子项目,修改次数)
def subsystem_line(subsystem_name, change_count)
asterisks = asterisks_for(change_count)
# 注释②
"#{subsystem_name.rjust(14)} #{asterisks} (#{change_count})"
end
# 计算星号
def asterisks_for(an_integer)
'*' * (an_integer / 5.0).round
end
# 计算修改次数
def change_count_for(name, start_date)
extract_change_count_from(svn_log(name, start_date))
end
# 根据文本信息(解析字符串)
def extract_change_count_from(log_text)
lines = log_text.split("\n")
dashed_lines = lines.find_all do | line | # 注释③
line.include?('--------')
end
dashed_lines.length - 1
end
# 使用外部程序获取文本信息
def svn_log(subsystem, start_date)
timespan = "--revision HEAD:{#{start_date}}"
root = "svn://rubyforge.org//var/svn/churn-demo"
`svn log #{timespan} #{root}/#{subsystem}`
end
# 格式化时间
def svn_date(a_time)
a_time.strftime("%Y-%m-%d") # 注释①
end
if $0 == __FILE__
subsystem_names = ['audit', 'fulfillment', 'persistence',
'ui', 'util', 'inventory']
start_date = svn_date(month_before(Time.mktime(2005,9,2,0,0,0,0)))
puts header(start_date)
subsystem_names.each do | name |
puts subsystem_line(name, change_count_for(name, start_date))
end
end
** 运行结果 **
>ruby churn.v7copy.rb
Changes since 2005-08-05:
audit * (5)
fulfillment (2)
persistence * (3)
ui ** (8)
util * (4)
inventory (2)
>Exit code: 0
** **
① Time对象的strftime用于格式化时间..
irb(main):005:0> Time.now.strftime('%m-%d %Y')
=> "10-28 2008"
② rjust用于实现右对齐..
irb(main):007:0> 'right'.rjust(2)
=> "right"
irb(main):008:0> 'right'.rjust(8)
=> " right"
③ find_all: 获取代码块返回为真的所有元素, 然后放入一个新的数组作为返回值..
irb(main):009:0> ['abxc', 'xx', 'b', 'ycx'].find_all do | ele |
irb(main):010:1* ele.include?('x')
irb(main):011:1> end
=> ["abxc", "xx", "ycx"]
require 'test/unit'
require 'churn.v7copy'
class ChurnMyTests < Test::Unit::TestCase
def test_month_before
assert_equal(Time.local(2008,9,30), month_before(Time.local(2008,10,28)))
end
def test_svn_date
assert_equal("2008-10-28",svn_date(Time.now))
end
def test_header
assert_equal("Changes since 2008-10-28:", header(svn_date(Time.mktime(2008,10,28,0,0,0,0))))
end
def test_extract_change_count_from
assert_equal(2,extract_change_count_from("------------------------------------------------------------------------
r2 | marick | 2005-08-07 14:26:21 -0500 (Mon, 07 Aug 2005) | 1 line
added code to handle merger
------------------------------------------------------------------------
r1 | marick | 2005-08-07 14:21:47 -0500 (Mon, 07 Aug 2005) | 1 line
first touches
No commit for revision 0.
------------------------------------------------------------------------"))
end
def test_asterisks_for
assert_equal("*********", asterisks_for(46))
end
end
** 运行结果 **
>ruby churn-mytest-v7copy.rb
Loaded suite churn-mytest-v7copy
Started
.....
Finished in 0.0 seconds.
5 tests, 5 assertions, 0 failures, 0 errors
>Exit code: 0
** **
# Exercises
# 处理时间(过去一个月)
def month_before(a_time)
a_time - 28 * 24 * 60 * 60
end
# 处理打印标题信息
def header(an_svn_start_date, an_svn_today_date)
"Changes between #{an_svn_start_date} and #{an_svn_today_date}:"
end
# 组成打印主体信息(参数:子项目,修改次数)
def subsystem_line(subsystem_name, change_count)
asterisks = asterisks_for(change_count)
change_text = change_text_from(change_count)
unless change_count == 0
return "#{subsystem_name.ljust(14)} #{change_text.ljust(14)} #{asterisks}"
end
return "#{subsystem_name.ljust(14)} #{'-'.ljust(14)} -"
end
# 计算星号
def asterisks_for(an_integer)
if(an_integer < 3)
'*'
else
'*' * (an_integer / 5.0).round
end
end
# 处理修改次数信息
def change_text_from(an_integer)
return "(#{an_integer} changes)"
end
# 计算修改次数
def change_count_for(name, start_date)
extract_change_count_from(svn_log(name, start_date))
end
# 根据文本信息(解析字符串)
def extract_change_count_from(log_text)
lines = log_text.split("\n")
dashed_lines = lines.find_all do | line |
line.include?('--------')
end
dashed_lines.length - 1
end
# 使用外部程序获取文本信息
def svn_log(subsystem, start_date)
timespan = "--revision HEAD:{#{start_date}}"
root = "svn://rubyforge.org//var/svn/churn-demo"
`svn log #{timespan} #{root}/#{subsystem}`
end
# 格式化时间
def svn_date(a_time)
a_time.strftime("%Y-%m-%d")
end
if $0 == __FILE__
subsystem_names = ['audit', 'fulfillment', 'persistence',
'ui', 'util', 'inventory']
start_date = svn_date(month_before(Time.mktime(2005,9,2,0,0,0,0)))
today_date = svn_date(Time.now)
puts header(start_date, today_date)
subsystem_names.each do | name |
puts subsystem_line(name, change_count_for(name, start_date))
end
end
** 运行结果 **
>ruby churn.v7copy.rb
Changes between 2005-08-05 and 2008-10-28:
audit (5 changes) *
fulfillment (2 changes) *
persistence (3 changes) *
ui (8 changes) **
util (4 changes) *
inventory (2 changes) *
>Exit code: 0
** **