PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/travis/build/script.rb

https://gitlab.com/mayakarya/travis-build
Ruby | 229 lines | 194 code | 34 blank | 1 comment | 7 complexity | f474daa153b6c400583778f56ecafd56 MD5 | raw file
  1. require 'core_ext/hash/deep_merge'
  2. require 'core_ext/hash/deep_symbolize_keys'
  3. require 'core_ext/object/false'
  4. require 'erb'
  5. require 'rbconfig'
  6. require 'travis/build/addons'
  7. require 'travis/build/appliances'
  8. require 'travis/build/git'
  9. require 'travis/build/helpers'
  10. require 'travis/build/stages'
  11. require 'travis/build/script/android'
  12. require 'travis/build/script/c'
  13. require 'travis/build/script/clojure'
  14. require 'travis/build/script/cpp'
  15. require 'travis/build/script/crystal'
  16. require 'travis/build/script/csharp'
  17. require 'travis/build/script/d'
  18. require 'travis/build/script/dart'
  19. require 'travis/build/script/erlang'
  20. require 'travis/build/script/elixir'
  21. require 'travis/build/script/go'
  22. require 'travis/build/script/groovy'
  23. require 'travis/build/script/generic'
  24. require 'travis/build/script/haskell'
  25. require 'travis/build/script/haxe'
  26. require 'travis/build/script/julia'
  27. require 'travis/build/script/nix'
  28. require 'travis/build/script/node_js'
  29. require 'travis/build/script/objective_c'
  30. require 'travis/build/script/perl'
  31. require 'travis/build/script/perl6'
  32. require 'travis/build/script/php'
  33. require 'travis/build/script/pure_java'
  34. require 'travis/build/script/python'
  35. require 'travis/build/script/r'
  36. require 'travis/build/script/ruby'
  37. require 'travis/build/script/rust'
  38. require 'travis/build/script/scala'
  39. require 'travis/build/script/smalltalk'
  40. require 'travis/build/script/shared/directory_cache'
  41. module Travis
  42. module Build
  43. class Script
  44. TEMPLATES_PATH = File.expand_path('../templates', __FILE__)
  45. DEFAULTS = {}
  46. class << self
  47. def defaults
  48. Git::DEFAULTS.merge(self::DEFAULTS)
  49. end
  50. end
  51. include Module.new { Stages::STAGES.each_slice(2).map(&:last).flatten.each { |stage| define_method(stage) {} } }
  52. include Appliances, DirectoryCache, Deprecation, Template
  53. attr_reader :sh, :data, :options, :validator, :addons, :stages
  54. attr_accessor :setup_cache_has_run_for
  55. def initialize(data)
  56. @data = Data.new({ config: self.class.defaults }.deep_merge(data.deep_symbolize_keys))
  57. @options = {}
  58. @sh = Shell::Builder.new
  59. @addons = Addons.new(self, sh, self.data, config)
  60. @stages = Stages.new(self, sh, config)
  61. @setup_cache_has_run_for = {}
  62. end
  63. def compile(ignore_taint = false)
  64. Shell.generate(sexp, ignore_taint)
  65. end
  66. def sexp
  67. run
  68. sh.to_sexp
  69. end
  70. def cache_slug_keys
  71. plain_env_vars = Array((config[:env] || []).dup).delete_if {|env| env.start_with? 'SECURE '}
  72. [
  73. 'cache',
  74. config[:os],
  75. config[:dist],
  76. config[:osx_image],
  77. OpenSSL::Digest::SHA256.hexdigest(plain_env_vars.sort.join('='))
  78. ]
  79. end
  80. def cache_slug
  81. cache_slug_keys.compact.join('-')
  82. end
  83. def archive_url_for(bucket, version, lang = self.class.name.split('::').last.downcase, ext = 'bz2')
  84. sh.if "$(uname) = 'Linux'" do
  85. sh.raw "travis_host_os=$(lsb_release -is | tr 'A-Z' 'a-z')"
  86. sh.raw "travis_rel_version=$(lsb_release -rs)"
  87. end
  88. sh.elif "$(uname) = 'Darwin'" do
  89. sh.raw "travis_host_os=osx"
  90. sh.raw "travis_rel=$(sw_vers -productVersion)"
  91. sh.raw "travis_rel_version=${travis_rel%*.*}"
  92. end
  93. "archive_url=https://s3.amazonaws.com/#{bucket}/binaries/${travis_host_os}/${travis_rel_version}/$(uname -m)/#{lang}-#{version}.tar.#{ext}"
  94. end
  95. def debug_build_via_api?
  96. ! data.debug_options.empty?
  97. end
  98. private
  99. def config
  100. data.config
  101. end
  102. def debug
  103. if debug_build_via_api?
  104. sh.echo "Debug build initiated by #{data.debug_options[:created_by]}", ansi: :yellow
  105. if debug_quiet?
  106. sh.raw "travis_debug --quiet"
  107. else
  108. sh.raw "travis_debug"
  109. end
  110. sh.echo
  111. sh.echo "All remaining steps, including caching and deploy, will be skipped.", ansi: :yellow
  112. end
  113. end
  114. def run
  115. stages.run if apply :validate
  116. sh.raw template('footer.sh')
  117. # apply :deprecations
  118. end
  119. def header
  120. sh.raw template('header.sh', build_dir: BUILD_DIR), pos: 0
  121. end
  122. def configure
  123. apply :show_system_info
  124. apply :update_glibc
  125. apply :clean_up_path
  126. apply :fix_resolv_conf
  127. apply :fix_etc_hosts
  128. apply :no_ipv6_localhost
  129. apply :fix_etc_mavenrc
  130. apply :etc_hosts_pinning
  131. apply :fix_wwdr_certificate
  132. apply :put_localhost_first
  133. apply :home_paths
  134. apply :disable_initramfs
  135. apply :disable_ssh_roaming
  136. apply :debug_tools
  137. apply :npm_registry
  138. end
  139. def checkout
  140. apply :checkout
  141. end
  142. def export
  143. apply :env
  144. end
  145. def prepare
  146. apply :services
  147. apply :fix_ps4 # TODO if this is to fix an rvm issue (as the specs say) then should this go to Rvm instead?
  148. end
  149. def disable_sudo
  150. apply :disable_sudo
  151. end
  152. def reset_state
  153. if debug_build_via_api?
  154. raise "Debug payload does not contain 'previous_state' value." unless previous_state = data.debug_options[:previous_state]
  155. sh.echo
  156. sh.echo "This is a debug build. The build result is reset to its previous value, \\\"#{previous_state}\\\".", ansi: :yellow
  157. case previous_state
  158. when "passed"
  159. sh.export 'TRAVIS_TEST_RESULT', '0', echo: false
  160. when "failed"
  161. sh.export 'TRAVIS_TEST_RESULT', '1', echo: false
  162. when "errored"
  163. sh.raw 'travis_terminate 2'
  164. end
  165. end
  166. end
  167. def config_env_vars
  168. @config_env_vars ||= Build::Env::Config.new(data, config)
  169. Array(@config_env_vars.data[:env])
  170. end
  171. def host_os
  172. case RbConfig::CONFIG["host_os"]
  173. when /^(?i:linux)/
  174. '$(lsb_release -is | tr "A-Z" "a-z")'
  175. when /^(?i:darwin)/
  176. 'osx'
  177. end
  178. end
  179. def rel_version
  180. case RbConfig::CONFIG["host_os"]
  181. when /^(?i:linux)/
  182. '$(lsb_release -rs)'
  183. when /^(?i:darwin)/
  184. '${$(sw_vers -productVersion)%*.*}'
  185. end
  186. end
  187. def debug_quiet?
  188. debug_build_via_api? && data.debug_options[:quiet]
  189. end
  190. def debug_enabled?
  191. ENV['TRAVIS_ENABLE_DEBUG_TOOLS'] == '1'
  192. end
  193. end
  194. end
  195. end