build_task.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env ruby
  2. #
  3. # Copyright (c) 2015-present, Parse, LLC.
  4. # All rights reserved.
  5. #
  6. # This source code is licensed under the BSD-style license found in the
  7. # LICENSE file in the root directory of this source tree. An additional grant
  8. # of patent rights can be found in the PATENTS file in the same directory.
  9. #
  10. module XCTask
  11. # This class defines all possible build formatters for BuildTask.
  12. class BuildFormatter
  13. XCODEBUILD = 'xcodebuild'
  14. XCPRETTY = 'xcpretty'
  15. XCTOOL = 'xctool'
  16. def self.verify(formatter)
  17. if formatter &&
  18. formatter != XCPRETTY &&
  19. formatter != XCODEBUILD &&
  20. formatter != XCTOOL
  21. fail "Unknown formatter used. Available formatters: 'xcodebuild', 'xcpretty', 'xctool'."
  22. end
  23. end
  24. end
  25. # This class defines all possible build actions for BuildTask.
  26. class BuildAction
  27. BUILD = 'build'
  28. CLEAN = 'clean'
  29. TEST = 'test'
  30. def self.verify(action)
  31. if action.nil? ||
  32. (action != BUILD &&
  33. action != CLEAN &&
  34. action != TEST)
  35. fail "Unknown build action used. Available actions: 'build', 'clean', 'test'."
  36. end
  37. end
  38. end
  39. # This class adds ability to easily configure a xcodebuild task and execute it.
  40. class BuildTask
  41. attr_accessor :directory
  42. attr_accessor :workspace
  43. attr_accessor :project
  44. attr_accessor :scheme
  45. attr_accessor :sdk
  46. attr_accessor :configuration
  47. attr_accessor :destinations
  48. attr_accessor :additional_options
  49. attr_accessor :actions
  50. attr_accessor :formatter
  51. attr_accessor :reports_enabled
  52. def initialize
  53. @directory = '.'
  54. @destinations = []
  55. @additional_options = {}
  56. yield self if block_given?
  57. end
  58. def execute
  59. verify
  60. prepare_build
  61. build
  62. end
  63. private
  64. def verify
  65. BuildFormatter.verify(@formatter)
  66. @actions.each do |action|
  67. BuildAction.verify(action)
  68. end
  69. end
  70. def prepare_build
  71. Dir.chdir(@directory) unless @directory.nil?
  72. end
  73. def build
  74. system(build_command_string(@formatter))
  75. end
  76. def build_command_string(formatter)
  77. command_string = nil
  78. case formatter
  79. when BuildFormatter::XCODEBUILD
  80. command_string = 'xcodebuild ' + build_options_string
  81. when BuildFormatter::XCPRETTY
  82. command_string = build_command_string('xcodebuild') + ' | xcpretty -c'
  83. if @actions.include? BuildAction::TEST
  84. command_string += " --report junit --output build/reports/#{@sdk}.xml"
  85. end
  86. command_string += ' ; exit ${PIPESTATUS[0]}'
  87. when BuildFormatter::XCTOOL
  88. command_string = 'xctool ' + build_options_string
  89. else
  90. command_string = build_command_string(BuildFormatter::XCODEBUILD)
  91. end
  92. command_string
  93. end
  94. def build_options_string
  95. opts = []
  96. opts << "-workspace #{@workspace}" if @workspace
  97. opts << "-project #{@project}" if @project
  98. opts << "-scheme #{@scheme}" if @scheme
  99. opts << "-sdk #{@sdk}" if @sdk
  100. opts << "-configuration #{@configuration}" if @configuration
  101. @destinations.each do |d|
  102. opts << "-destination #{d}"
  103. end
  104. opts << @actions.compact.join(' ') if @actions
  105. @additional_options.each do |key, value|
  106. opts << "#{key}=#{value}"
  107. end
  108. opts.compact.join(' ')
  109. end
  110. end
  111. end