build_task.rb 3.3 KB

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