cocoapods-tt 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. checksums.yaml +7 -0
  2. data/lib/cocoapods-tt/command/native/install.rb +56 -0
  3. data/lib/cocoapods-tt/command/native/update.rb +157 -0
  4. data/lib/cocoapods-tt/command/tt/make.rb +92 -0
  5. data/lib/cocoapods-tt/command/tt.rb +115 -0
  6. data/lib/cocoapods-tt/command.rb +1 -0
  7. data/lib/cocoapods-tt/gem_version.rb +3 -0
  8. data/lib/cocoapods-tt/native/command.rb +185 -0
  9. data/lib/cocoapods-tt/native/config.rb +366 -0
  10. data/lib/cocoapods-tt/native/core_overrides.rb +1 -0
  11. data/lib/cocoapods-tt/native/downloader/cache.rb +322 -0
  12. data/lib/cocoapods-tt/native/downloader/request.rb +86 -0
  13. data/lib/cocoapods-tt/native/downloader/response.rb +16 -0
  14. data/lib/cocoapods-tt/native/downloader.rb +192 -0
  15. data/lib/cocoapods-tt/native/executable.rb +247 -0
  16. data/lib/cocoapods-tt/native/external_sources/abstract_external_source.rb +205 -0
  17. data/lib/cocoapods-tt/native/external_sources/downloader_source.rb +30 -0
  18. data/lib/cocoapods-tt/native/external_sources/path_source.rb +55 -0
  19. data/lib/cocoapods-tt/native/external_sources/podspec_source.rb +54 -0
  20. data/lib/cocoapods-tt/native/external_sources.rb +57 -0
  21. data/lib/cocoapods-tt/native/gem_version.rb +5 -0
  22. data/lib/cocoapods-tt/native/generator/acknowledgements/markdown.rb +44 -0
  23. data/lib/cocoapods-tt/native/generator/acknowledgements/plist.rb +94 -0
  24. data/lib/cocoapods-tt/native/generator/acknowledgements.rb +107 -0
  25. data/lib/cocoapods-tt/native/generator/app_target_helper.rb +363 -0
  26. data/lib/cocoapods-tt/native/generator/bridge_support.rb +22 -0
  27. data/lib/cocoapods-tt/native/generator/constant.rb +19 -0
  28. data/lib/cocoapods-tt/native/generator/copy_dsyms_script.rb +56 -0
  29. data/lib/cocoapods-tt/native/generator/copy_resources_script.rb +223 -0
  30. data/lib/cocoapods-tt/native/generator/copy_xcframework_script.rb +227 -0
  31. data/lib/cocoapods-tt/native/generator/dummy_source.rb +31 -0
  32. data/lib/cocoapods-tt/native/generator/embed_frameworks_script.rb +196 -0
  33. data/lib/cocoapods-tt/native/generator/file_list.rb +39 -0
  34. data/lib/cocoapods-tt/native/generator/header.rb +103 -0
  35. data/lib/cocoapods-tt/native/generator/info_plist_file.rb +128 -0
  36. data/lib/cocoapods-tt/native/generator/module_map.rb +99 -0
  37. data/lib/cocoapods-tt/native/generator/prefix_header.rb +60 -0
  38. data/lib/cocoapods-tt/native/generator/script_phase_constants.rb +100 -0
  39. data/lib/cocoapods-tt/native/generator/umbrella_header.rb +46 -0
  40. data/lib/cocoapods-tt/native/hooks_manager.rb +132 -0
  41. data/lib/cocoapods-tt/native/installer/analyzer/analysis_result.rb +87 -0
  42. data/lib/cocoapods-tt/native/installer/analyzer/locking_dependency_analyzer.rb +103 -0
  43. data/lib/cocoapods-tt/native/installer/analyzer/pod_variant.rb +87 -0
  44. data/lib/cocoapods-tt/native/installer/analyzer/pod_variant_set.rb +175 -0
  45. data/lib/cocoapods-tt/native/installer/analyzer/podfile_dependency_cache.rb +55 -0
  46. data/lib/cocoapods-tt/native/installer/analyzer/sandbox_analyzer.rb +268 -0
  47. data/lib/cocoapods-tt/native/installer/analyzer/specs_state.rb +108 -0
  48. data/lib/cocoapods-tt/native/installer/analyzer/target_inspection_result.rb +58 -0
  49. data/lib/cocoapods-tt/native/installer/analyzer/target_inspector.rb +258 -0
  50. data/lib/cocoapods-tt/native/installer/analyzer.rb +1204 -0
  51. data/lib/cocoapods-tt/native/installer/base_install_hooks_context.rb +135 -0
  52. data/lib/cocoapods-tt/native/installer/installation_options.rb +195 -0
  53. data/lib/cocoapods-tt/native/installer/pod_source_installer.rb +224 -0
  54. data/lib/cocoapods-tt/native/installer/pod_source_preparer.rb +77 -0
  55. data/lib/cocoapods-tt/native/installer/podfile_validator.rb +168 -0
  56. data/lib/cocoapods-tt/native/installer/post_install_hooks_context.rb +9 -0
  57. data/lib/cocoapods-tt/native/installer/post_integrate_hooks_context.rb +9 -0
  58. data/lib/cocoapods-tt/native/installer/pre_install_hooks_context.rb +51 -0
  59. data/lib/cocoapods-tt/native/installer/pre_integrate_hooks_context.rb +9 -0
  60. data/lib/cocoapods-tt/native/installer/project_cache/project_cache.rb +11 -0
  61. data/lib/cocoapods-tt/native/installer/project_cache/project_cache_analysis_result.rb +53 -0
  62. data/lib/cocoapods-tt/native/installer/project_cache/project_cache_analyzer.rb +200 -0
  63. data/lib/cocoapods-tt/native/installer/project_cache/project_cache_version.rb +43 -0
  64. data/lib/cocoapods-tt/native/installer/project_cache/project_installation_cache.rb +103 -0
  65. data/lib/cocoapods-tt/native/installer/project_cache/project_metadata_cache.rb +73 -0
  66. data/lib/cocoapods-tt/native/installer/project_cache/target_cache_key.rb +176 -0
  67. data/lib/cocoapods-tt/native/installer/project_cache/target_metadata.rb +74 -0
  68. data/lib/cocoapods-tt/native/installer/sandbox_dir_cleaner.rb +105 -0
  69. data/lib/cocoapods-tt/native/installer/sandbox_header_paths_installer.rb +45 -0
  70. data/lib/cocoapods-tt/native/installer/source_provider_hooks_context.rb +34 -0
  71. data/lib/cocoapods-tt/native/installer/target_uuid_generator.rb +34 -0
  72. data/lib/cocoapods-tt/native/installer/user_project_integrator/target_integrator/xcconfig_integrator.rb +179 -0
  73. data/lib/cocoapods-tt/native/installer/user_project_integrator/target_integrator.rb +815 -0
  74. data/lib/cocoapods-tt/native/installer/user_project_integrator.rb +280 -0
  75. data/lib/cocoapods-tt/native/installer/xcode/multi_pods_project_generator.rb +82 -0
  76. data/lib/cocoapods-tt/native/installer/xcode/pods_project_generator/aggregate_target_dependency_installer.rb +66 -0
  77. data/lib/cocoapods-tt/native/installer/xcode/pods_project_generator/aggregate_target_installer.rb +192 -0
  78. data/lib/cocoapods-tt/native/installer/xcode/pods_project_generator/app_host_installer.rb +154 -0
  79. data/lib/cocoapods-tt/native/installer/xcode/pods_project_generator/file_references_installer.rb +329 -0
  80. data/lib/cocoapods-tt/native/installer/xcode/pods_project_generator/pod_target_dependency_installer.rb +195 -0
  81. data/lib/cocoapods-tt/native/installer/xcode/pods_project_generator/pod_target_installer.rb +1239 -0
  82. data/lib/cocoapods-tt/native/installer/xcode/pods_project_generator/pod_target_integrator.rb +312 -0
  83. data/lib/cocoapods-tt/native/installer/xcode/pods_project_generator/pods_project_writer.rb +90 -0
  84. data/lib/cocoapods-tt/native/installer/xcode/pods_project_generator/project_generator.rb +120 -0
  85. data/lib/cocoapods-tt/native/installer/xcode/pods_project_generator/target_installation_result.rb +140 -0
  86. data/lib/cocoapods-tt/native/installer/xcode/pods_project_generator/target_installer.rb +257 -0
  87. data/lib/cocoapods-tt/native/installer/xcode/pods_project_generator/target_installer_helper.rb +110 -0
  88. data/lib/cocoapods-tt/native/installer/xcode/pods_project_generator.rb +291 -0
  89. data/lib/cocoapods-tt/native/installer/xcode/pods_project_generator_result.rb +54 -0
  90. data/lib/cocoapods-tt/native/installer/xcode/single_pods_project_generator.rb +38 -0
  91. data/lib/cocoapods-tt/native/installer/xcode/target_validator.rb +170 -0
  92. data/lib/cocoapods-tt/native/installer/xcode.rb +11 -0
  93. data/lib/cocoapods-tt/native/installer.rb +1044 -0
  94. data/lib/cocoapods-tt/native/native_target_extension.rb +60 -0
  95. data/lib/cocoapods-tt/native/open-uri.rb +33 -0
  96. data/lib/cocoapods-tt/native/podfile.rb +13 -0
  97. data/lib/cocoapods-tt/native/project.rb +544 -0
  98. data/lib/cocoapods-tt/native/resolver/lazy_specification.rb +88 -0
  99. data/lib/cocoapods-tt/native/resolver/resolver_specification.rb +41 -0
  100. data/lib/cocoapods-tt/native/resolver.rb +600 -0
  101. data/lib/cocoapods-tt/native/sandbox/file_accessor.rb +532 -0
  102. data/lib/cocoapods-tt/native/sandbox/headers_store.rb +163 -0
  103. data/lib/cocoapods-tt/native/sandbox/path_list.rb +242 -0
  104. data/lib/cocoapods-tt/native/sandbox/pod_dir_cleaner.rb +71 -0
  105. data/lib/cocoapods-tt/native/sandbox/podspec_finder.rb +23 -0
  106. data/lib/cocoapods-tt/native/sandbox.rb +470 -0
  107. data/lib/cocoapods-tt/native/sources_manager.rb +221 -0
  108. data/lib/cocoapods-tt/native/target/aggregate_target.rb +558 -0
  109. data/lib/cocoapods-tt/native/target/build_settings.rb +1385 -0
  110. data/lib/cocoapods-tt/native/target/pod_target.rb +1168 -0
  111. data/lib/cocoapods-tt/native/target.rb +378 -0
  112. data/lib/cocoapods-tt/native/user_interface/error_report.rb +204 -0
  113. data/lib/cocoapods-tt/native/user_interface/inspector_reporter.rb +102 -0
  114. data/lib/cocoapods-tt/native/user_interface.rb +463 -0
  115. data/lib/cocoapods-tt/native/validator.rb +1170 -0
  116. data/lib/cocoapods-tt/native/version_metadata.rb +26 -0
  117. data/lib/cocoapods-tt/native/xcode/framework_paths.rb +54 -0
  118. data/lib/cocoapods-tt/native/xcode/linkage_analyzer.rb +22 -0
  119. data/lib/cocoapods-tt/native/xcode/xcframework/xcframework_slice.rb +138 -0
  120. data/lib/cocoapods-tt/native/xcode/xcframework.rb +99 -0
  121. data/lib/cocoapods-tt/native/xcode.rb +7 -0
  122. data/lib/cocoapods-tt.rb +1 -0
  123. data/lib/cocoapods_plugin.rb +17 -0
  124. metadata +193 -0
@@ -0,0 +1,86 @@
1
+ require 'digest'
2
+
3
+ module Pod
4
+ module Downloader
5
+ # This class represents a download request for a given Pod.
6
+ #
7
+ class Request
8
+ # @return [Specification,Nil] The specification for the pod whose download
9
+ # is being requested.
10
+ #
11
+ attr_reader :spec
12
+
13
+ # @return [Boolean] Whether this download request is for a released pod.
14
+ #
15
+ attr_reader :released_pod
16
+ alias_method :released_pod?, :released_pod
17
+
18
+ # @return [String] The name of the pod whose dowload is being requested.
19
+ #
20
+ attr_reader :name
21
+
22
+ # @return [Hash<Symbol, String>] The download parameters for this request.
23
+ #
24
+ attr_reader :params
25
+
26
+ # Initialize a new instance
27
+ #
28
+ # @param [Specification,Nil] spec
29
+ # see {#spec}
30
+ #
31
+ # @param [Boolean] released
32
+ # see {#released_pod}
33
+ #
34
+ # @param [String,Nil] name
35
+ # see {#name}
36
+ #
37
+ # @param [Hash<Symbol,String>,Nil] params
38
+ # see {#params}
39
+ #
40
+ def initialize(spec: nil, released: false, name: nil, params: false)
41
+ @released_pod = released
42
+ @spec = spec
43
+ @params = spec ? (spec.source && spec.source.dup) : params
44
+ @name = spec ? spec.name : name
45
+
46
+ validate!
47
+ end
48
+
49
+ # @param [String] name
50
+ # the name of the pod being downloaded.
51
+ #
52
+ # @param [Hash<#to_s, #to_s>] params
53
+ # the download parameters of the pod being downloaded.
54
+ #
55
+ # @param [Specification] spec
56
+ # the specification of the pod being downloaded.
57
+ #
58
+ # @return [String] The slug used to store the files resulting from this
59
+ # download request.
60
+ #
61
+ def slug(name: self.name, params: self.params, spec: self.spec)
62
+ checksum = spec && spec.checksum && '-' << spec.checksum[0, 5]
63
+ if released_pod?
64
+ "Release/#{name}/#{spec.version}#{checksum}"
65
+ else
66
+ opts = params.to_a.sort_by(&:first).map { |k, v| "#{k}=#{v}" }.join('-')
67
+ digest = Digest::MD5.hexdigest(opts)
68
+ "External/#{name}/#{digest}#{checksum}"
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ # Validates that the given request is well-formed.
75
+ #
76
+ # @return [Void]
77
+ #
78
+ def validate!
79
+ raise ArgumentError, 'Requires a name' unless name
80
+ raise ArgumentError, 'Must give a spec for a released download request' if released_pod? && !spec
81
+ raise ArgumentError, 'Requires a version if released' if released_pod? && !spec.version
82
+ raise ArgumentError, 'Requires params' unless params
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,16 @@
1
+ module Pod
2
+ module Downloader
3
+ # A response to a download request.
4
+ #
5
+ # @attr [Pathname] location
6
+ # the location where this downloaded pod is stored on disk.
7
+ #
8
+ # @attr [Specification] spec
9
+ # the specification that describes this downloaded pod.
10
+ #
11
+ # @attr [Hash<Symbol, String>] checkout_options
12
+ # the downloader parameters necessary to recreate this exact download.
13
+ #
14
+ Response = Struct.new(:location, :spec, :checkout_options)
15
+ end
16
+ end
@@ -0,0 +1,192 @@
1
+ require 'cocoapods-downloader'
2
+ require 'claide/informative_error'
3
+ require 'fileutils'
4
+ require 'tmpdir'
5
+
6
+ module Pod
7
+ module Downloader
8
+ require 'cocoapods/downloader/cache'
9
+ require 'cocoapods/downloader/request'
10
+ require 'cocoapods/downloader/response'
11
+
12
+ # Downloads a pod from the given `request` to the given `target` location.
13
+ #
14
+ # @return [Response] The download response for this download.
15
+ #
16
+ # @param [Request] request
17
+ # the request that describes this pod download.
18
+ #
19
+ # @param [Pathname,Nil] target
20
+ # the location to which this pod should be downloaded. If `nil`,
21
+ # then the pod will only be cached.
22
+ #
23
+ # @param [Boolean] can_cache
24
+ # whether caching is allowed.
25
+ #
26
+ # @param [Pathname,Nil] cache_path
27
+ # the path used to cache pod downloads.
28
+ #
29
+ def self.download(
30
+ request,
31
+ target,
32
+ can_cache: true,
33
+ cache_path: Config.instance.cache_root + 'Pods'
34
+ )
35
+ can_cache &&= !Config.instance.skip_download_cache
36
+
37
+ request = preprocess_request(request)
38
+
39
+ if can_cache
40
+ raise ArgumentError, 'Must provide a `cache_path` when caching.' unless cache_path
41
+ cache = Cache.new(cache_path)
42
+ result = cache.download_pod(request)
43
+ else
44
+ raise ArgumentError, 'Must provide a `target` when caching is disabled.' unless target
45
+
46
+ require 'cocoapods/installer/pod_source_preparer'
47
+ result, = download_request(request, target)
48
+ Installer::PodSourcePreparer.new(result.spec, result.location).prepare!
49
+ end
50
+
51
+ if target && result.location && target != result.location
52
+ UI.message "Copying #{request.name} from `#{result.location}` to #{UI.path target}", '> ' do
53
+ Cache.read_lock(result.location) do
54
+ FileUtils.rm_rf target
55
+ FileUtils.cp_r(result.location, target)
56
+ end
57
+ end
58
+ end
59
+ result
60
+ end
61
+
62
+ # Performs the download from the given `request` to the given `target` location.
63
+ #
64
+ # @return [Response, Hash<String,Specification>]
65
+ # The download response for this download, and the specifications
66
+ # for this download grouped by name.
67
+ #
68
+ # @param [Request] request
69
+ # the request that describes this pod download.
70
+ #
71
+ # @param [Pathname,Nil] target
72
+ # the location to which this pod should be downloaded. If `nil`,
73
+ # then the pod will only be cached.
74
+ #
75
+ def self.download_request(request, target)
76
+ result = Response.new
77
+ result.checkout_options = download_source(target, request.params)
78
+ result.location = target
79
+
80
+ if request.released_pod?
81
+ result.spec = request.spec
82
+ podspecs = { request.name => request.spec }
83
+ else
84
+ podspecs = Sandbox::PodspecFinder.new(target).podspecs
85
+ podspecs[request.name] = request.spec if request.spec
86
+ podspecs.each do |name, spec|
87
+ if request.name == name
88
+ result.spec = spec
89
+ end
90
+ end
91
+ end
92
+
93
+ [result, podspecs]
94
+ end
95
+
96
+ private
97
+
98
+ # Downloads a pod with the given `params` to `target`.
99
+ #
100
+ # @param [Pathname] target
101
+ #
102
+ # @param [Hash<Symbol,String>] params
103
+ #
104
+ # @return [Hash] The checkout options required to re-download this exact
105
+ # same source.
106
+ #
107
+ def self.download_source(target, params)
108
+ FileUtils.rm_rf(target)
109
+ downloader = Downloader.for_target(target, params)
110
+ downloader.download
111
+ target.mkpath
112
+
113
+ if downloader.options_specific?
114
+ params
115
+ else
116
+ downloader.checkout_options
117
+ end
118
+ end
119
+
120
+ # Return a new request after preprocessing by the downloader
121
+ #
122
+ # @param [Request] request
123
+ # the request that needs preprocessing
124
+ #
125
+ # @return [Request] the preprocessed request
126
+ #
127
+ def self.preprocess_request(request)
128
+ Request.new(
129
+ :spec => request.spec,
130
+ :released => request.released_pod?,
131
+ :name => request.name,
132
+ :params => Downloader.preprocess_options(request.params))
133
+ end
134
+
135
+ public
136
+
137
+ class DownloaderError; include CLAide::InformativeError; end
138
+
139
+ class Base
140
+ override_api do
141
+ def execute_command(executable, command, raise_on_failure = false)
142
+ Executable.execute_command(executable, command, raise_on_failure)
143
+ rescue CLAide::InformativeError => e
144
+ raise DownloaderError, e.message
145
+ end
146
+
147
+ # Indicates that an action will be performed. The action is passed as a
148
+ # block.
149
+ #
150
+ # @param [String] message
151
+ # The message associated with the action.
152
+ #
153
+ # @yield The action, this block is always executed.
154
+ #
155
+ # @return [void]
156
+ #
157
+ def ui_action(message)
158
+ UI.section(" > #{message}", '', 1) do
159
+ yield
160
+ end
161
+ end
162
+
163
+ # Indicates that a minor action will be performed. The action is passed
164
+ # as a block.
165
+ #
166
+ # @param [String] message
167
+ # The message associated with the action.
168
+ #
169
+ # @yield The action, this block is always executed.
170
+ #
171
+ # @return [void]
172
+ #
173
+ def ui_sub_action(message)
174
+ UI.section(" > #{message}", '', 2) do
175
+ yield
176
+ end
177
+ end
178
+
179
+ # Prints an UI message.
180
+ #
181
+ # @param [String] message
182
+ # The message associated with the action.
183
+ #
184
+ # @return [void]
185
+ #
186
+ def ui_message(message)
187
+ UI.puts message
188
+ end
189
+ end
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,247 @@
1
+ module Pod
2
+ # Module which provides support for running executables.
3
+ #
4
+ # In a class it can be used as:
5
+ #
6
+ # extend Executable
7
+ # executable :git
8
+ #
9
+ # This will create two methods `git` and `git!` both accept a command but
10
+ # the later will raise on non successful executions. The methods return the
11
+ # output of the command.
12
+ #
13
+ module Executable
14
+ # Creates the methods for the executable with the given name.
15
+ #
16
+ # @param [Symbol] name
17
+ # the name of the executable.
18
+ #
19
+ # @return [void]
20
+ #
21
+ def executable(name)
22
+ define_method(name) do |*command|
23
+ Executable.execute_command(name, Array(command).flatten, false)
24
+ end
25
+
26
+ define_method(name.to_s + '!') do |*command|
27
+ Executable.execute_command(name, Array(command).flatten, true)
28
+ end
29
+ end
30
+
31
+ # Executes the given command displaying it if in verbose mode.
32
+ #
33
+ # @param [String] executable
34
+ # The binary to use.
35
+ #
36
+ # @param [Array<#to_s>] command
37
+ # The command to send to the binary.
38
+ #
39
+ # @param [Bool] raise_on_failure
40
+ # Whether it should raise if the command fails.
41
+ #
42
+ # @raise If the executable could not be located.
43
+ #
44
+ # @raise If the command fails and the `raise_on_failure` is set to true.
45
+ #
46
+ # @return [String] the output of the command (STDOUT and STDERR).
47
+ #
48
+ def self.execute_command(executable, command, raise_on_failure = true)
49
+ bin = which!(executable)
50
+
51
+ command = command.map(&:to_s)
52
+ if File.basename(bin) == 'tar.exe'
53
+ # Tar on Windows needs --force-local
54
+ command.push('--force-local')
55
+ end
56
+ full_command = "#{bin} #{command.join(' ')}"
57
+
58
+ if Config.instance.verbose?
59
+ UI.message("$ #{full_command}")
60
+ stdout = Indenter.new(STDOUT)
61
+ stderr = Indenter.new(STDERR)
62
+ else
63
+ stdout = Indenter.new
64
+ stderr = Indenter.new
65
+ end
66
+
67
+ status = popen3(bin, command, stdout, stderr)
68
+ stdout = stdout.join
69
+ stderr = stderr.join
70
+ output = stdout + stderr
71
+ unless status.success?
72
+ if raise_on_failure
73
+ raise Informative, "#{full_command}\n\n#{output}"
74
+ else
75
+ UI.message("[!] Failed: #{full_command}".red)
76
+ end
77
+ end
78
+
79
+ output
80
+ end
81
+
82
+ # Returns the absolute path to the binary with the given name on the current
83
+ # `PATH`, or `nil` if none is found.
84
+ #
85
+ # @param [String] program
86
+ # The name of the program being searched for.
87
+ #
88
+ # @return [String,Nil] The absolute path to the given program, or `nil` if
89
+ # it wasn't found in the current `PATH`.
90
+ #
91
+ def self.which(program)
92
+ program = program.to_s
93
+ paths = ENV.fetch('PATH') { '' }.split(File::PATH_SEPARATOR)
94
+ paths.unshift('./')
95
+ paths.uniq!
96
+ paths.each do |path|
97
+ bin = File.expand_path(program, path)
98
+ if Gem.win_platform?
99
+ bin += '.exe'
100
+ end
101
+ if File.file?(bin) && File.executable?(bin)
102
+ return bin
103
+ end
104
+ end
105
+ nil
106
+ end
107
+
108
+ # Returns the absolute path to the binary with the given name on the current
109
+ # `PATH`, or raises if none is found.
110
+ #
111
+ # @param [String] program
112
+ # The name of the program being searched for.
113
+ #
114
+ # @return [String] The absolute path to the given program.
115
+ #
116
+ def self.which!(program)
117
+ which(program).tap do |bin|
118
+ raise Informative, "Unable to locate the executable `#{program}`" unless bin
119
+ end
120
+ end
121
+
122
+ # Runs the given command, capturing the desired output.
123
+ #
124
+ # @param [String] executable
125
+ # The binary to use.
126
+ #
127
+ # @param [Array<#to_s>] command
128
+ # The command to send to the binary.
129
+ #
130
+ # @param [Symbol] capture
131
+ # Whether it should raise if the command fails.
132
+ #
133
+ # @param [Hash] env
134
+ # Environment variables to be set for the command.
135
+ #
136
+ # @raise If the executable could not be located.
137
+ #
138
+ # @return [(String, Process::Status)]
139
+ # The desired captured output from the command, and the status from
140
+ # running the command.
141
+ #
142
+ def self.capture_command(executable, command, capture: :merge, env: {}, **kwargs)
143
+ bin = which!(executable)
144
+
145
+ require 'open3'
146
+ command = command.map(&:to_s)
147
+ case capture
148
+ when :merge then Open3.capture2e(env, [bin, bin], *command, **kwargs)
149
+ when :both then Open3.capture3(env, [bin, bin], *command, **kwargs)
150
+ when :out then Open3.capture3(env, [bin, bin], *command, **kwargs).values_at(0, -1)
151
+ when :err then Open3.capture3(env, [bin, bin], *command, **kwargs).drop(1)
152
+ when :none then Open3.capture3(env, [bin, bin], *command, **kwargs).last
153
+ end
154
+ end
155
+
156
+ # (see Executable.capture_command)
157
+ #
158
+ # @raise If running the command fails
159
+ #
160
+ def self.capture_command!(executable, command, **kwargs)
161
+ capture_command(executable, command, **kwargs).tap do |result|
162
+ result = Array(result)
163
+ status = result.last
164
+ unless status.success?
165
+ output = result[0..-2].join
166
+ raise Informative, "#{executable} #{command.join(' ')}\n\n#{output}".strip
167
+ end
168
+ end
169
+ end
170
+
171
+ private
172
+
173
+ def self.popen3(bin, command, stdout, stderr)
174
+ require 'open3'
175
+ Open3.popen3(bin, *command) do |i, o, e, t|
176
+ reader(o, stdout)
177
+ reader(e, stderr)
178
+ i.close
179
+
180
+ status = t.value
181
+
182
+ o.flush
183
+ e.flush
184
+ sleep(0.01)
185
+
186
+ status
187
+ end
188
+ end
189
+
190
+ def self.reader(input, output)
191
+ Thread.new do
192
+ buf = ''
193
+ begin
194
+ loop do
195
+ buf << input.readpartial(4096)
196
+ loop do
197
+ string, separator, buf = buf.partition(/[\r\n]/)
198
+ if separator.empty?
199
+ buf = string
200
+ break
201
+ end
202
+ output << (string << separator)
203
+ end
204
+ end
205
+ rescue EOFError, IOError
206
+ output << (buf << $/) unless buf.empty?
207
+ end
208
+ end
209
+ end
210
+
211
+ #-------------------------------------------------------------------------#
212
+
213
+ # Helper class that allows to write to an {IO} instance taking into account
214
+ # the UI indentation level.
215
+ #
216
+ class Indenter < ::Array
217
+ # @return [Fixnum] The indentation level of the UI.
218
+ #
219
+ attr_reader :indent
220
+
221
+ # @return [IO] the {IO} to which the output should be printed.
222
+ #
223
+ attr_reader :io
224
+
225
+ # Init a new Indenter
226
+ #
227
+ # @param [IO] io @see io
228
+ #
229
+ def initialize(io = nil)
230
+ @io = io
231
+ @indent = ' ' * UI.indentation_level
232
+ end
233
+
234
+ # Stores a portion of the output and prints it to the {IO} instance.
235
+ #
236
+ # @param [String] value
237
+ # the output to print.
238
+ #
239
+ # @return [void]
240
+ #
241
+ def <<(value)
242
+ super
243
+ io << "#{indent}#{value}" if io
244
+ end
245
+ end
246
+ end
247
+ end