PageRenderTime 55ms CodeModel.GetById 46ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Rake/teamcity.rb

http://github.com/agross/netopenspace
Ruby | 66 lines | 54 code | 12 blank | 0 comment | 5 complexity | ef3111c83b4b119a276313d83da22668 MD5 | raw file
 1require 'rexml/document'
 2include REXML
 3
 4module TeamCity
 5	def teamcity_progress(task)
 6		teamcity_service_message 'progressStart', task
 7		yield if block_given?
 8		teamcity_service_message 'progressFinish', task
 9	end
10	
11	def teamcity_service_message(type = '', message = '')
12		puts "##teamcity[#{type} '#{message.encode}']" if ENV['TEAMCITY_PROJECT_NAME']
13	end
14	
15	def self.add_statistic(key = '', value = '')
16		puts "##teamcity[buildStatisticValue key='#{key.to_s.encode}' value='#{value.to_s.encode}']" if ENV['TEAMCITY_PROJECT_NAME']
17	end
18	
19	def self.import_data(type = '', path = '')
20		puts "##teamcity[importData type='#{type.to_s.encode}' path='#{path.to_s.encode}']" if ENV['TEAMCITY_PROJECT_NAME']
21	end
22	
23	def self.append_build_status_text(text = '')
24		return if not ENV['TEAMCITY_PROJECT_NAME']
25		
26		teamcity_info = 'teamcity-info.xml'
27
28		if File.exists?(teamcity_info)
29			doc = Document.new File.read(teamcity_info) 
30		else
31			doc = Document.new ''
32		end
33		
34		XPath.first(doc, "//").add_element('build') if not XPath.first(doc, "//build")
35		XPath.first(doc, "//build").add_element('statusInfo') if not XPath.first(doc, "//build/statusInfo")
36		XPath.first(doc, "//build/statusInfo").add_element('text', {'action' => 'append'}).text = text
37	
38		out = ''
39		doc.write out
40		File.open(teamcity_info, 'w') do |f|
41			f.write(out)
42		end
43	end
44end
45
46class String
47	def encode
48		self \
49			.gsub(/\|/, "||") \
50			.gsub(/'/, "|'") \
51			.gsub(/\n/, "|n") \
52			.gsub(/\r/, "|r") \
53			.gsub(/\]/, "|]")
54	end
55end
56
57class Rake::Task
58	include TeamCity
59	old_execute = self.instance_method(:execute)
60	
61	define_method(:execute) do |args|
62		teamcity_progress("Executing #{name} rake task") do
63			old_execute.bind(self).call(args)
64		end
65	end
66end