Thursday, December 21, 2006

Ruby: rake commit

update: Paul Gross created a GitHub project where these tasks live and can be improved. Check it out at http://github.com/pgr0ss/rake_commit_tasks/tree/master

I wrote in the past about rake pc, the task that I run before checking in. The rake pc task is good, but it stops one step short: actually checking in.

The first implementation of this task used a task with parameters; however, the final implementation uses STDIN.gets and a data file.

The rake commit task also allows you specify a pair if you are working on multiple pairing stations.

desc "Run to check in"
task :commit => :pc do
commit_pair = retrieve_saved_data "pair"
commit_message = retrieve_saved_data "message"
command = %[svn ci -m "#{commit_pair.chomp} - #{commit_message.chomp}"]
puts command
puts %x[#{command}]
end

def retrieve_saved_data attribute
data_path = File.expand_path(File.dirname(__FILE__) + "/#{attribute}.data")
`touch #{data_path}` unless File.exist? data_path
saved_data = File.read(data_path)

puts "last #{attribute}: " + saved_data unless saved_data.chomp.empty?
print "#{attribute}: "

input = STDIN.gets
while (saved_data.chomp.empty? && (input.chomp.empty?))
print "#{attribute}: "
input = STDIN.gets
end
if input.chomp.any?
File.open(data_path, "w") { |file| file << input }
else
puts "using: " + saved_data.chomp
end
input.chomp.any? ? input : saved_data
end

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.