/tools/Rake/teamcity.rb

http://github.com/agross/netopenspace · Ruby · 66 lines · 54 code · 12 blank · 0 comment · 5 complexity · ef3111c83b4b119a276313d83da22668 MD5 · raw file

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