Module: PantographCore::Helper

Defined in:
pantograph_core/lib/pantograph_core/helper.rb

Class Method Summary collapse

Class Method Details

.backticks(command, print: true) ⇒ Object

Runs a given command using backticks (‘) and prints them out using the UI.command method



283
284
285
286
287
288
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 283

def self.backticks(command, print: true)
  UI.command(command) if print
  result = `#{command}`
  UI.command_output(result) if print
  return result
end

.buildlog_pathObject

Logs base directory



121
122
123
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 121

def self.buildlog_path
  return ENV["PANT_BUILDLOG_PATH"] || "~/Library/Logs"
end

.bundler?boolean

Returns true if executing with bundler (like ‘bundle exec pantograph [action]’).

Returns:

  • (boolean)

    true if executing with bundler (like ‘bundle exec pantograph [action]’)



29
30
31
32
33
34
35
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 29

def self.bundler?
  # Bundler environment variable
  ['BUNDLE_BIN_PATH', 'BUNDLE_GEMFILE'].each do |current|
    return true if ENV.key?(current)
  end
  return false
end

.ci?boolean

Returns true if building in a known CI environment.

Returns:

  • (boolean)

    true if building in a known CI environment



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 68

def self.ci?
  # Check for Jenkins, Travis CI, ... environment variables
  ci_vars = [
    'JENKINS_HOME',
    'JENKINS_URL',
    'TRAVIS',
    'CIRCLECI',
    'CI',
    'APPCENTER_BUILD_ID',
    'TEAMCITY_VERSION',
    'GO_PIPELINE_NAME',
    'bamboo_buildKey',
    'GITLAB_CI', 'XCS',
    'TF_BUILD',
    'GITHUB_ACTION',
    'GITHUB_ACTIONS'
  ]

  ci_vars.any? { |var| ENV.key?(var) }
end

.colors_disabled?Boolean

Do we want to disable the colored output?

Returns:



111
112
113
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 111

def self.colors_disabled?
  PantographCore::Env.truthy?("PANTOGRAPH_DISABLE_COLORS")
end

.contained_pantograph?Boolean

Do we run from a bundled pantograph, which contains Ruby and OpenSSL? Usually this means the pantograph directory is ~/.pantograph/bin/ We set this value via the environment variable ‘PANTOGRAPH_SELF_CONTAINED`

Returns:



40
41
42
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 40

def self.contained_pantograph?
  ENV["PANTOGRAPH_SELF_CONTAINED"].to_s == "true" && !self.homebrew?
end

.executable?(cmd_path) ⇒ Boolean

checks if a given path is an executable file

Returns:



364
365
366
367
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 364

def self.executable?(cmd_path)
  # no executable files on Windows, so existing is enough there
  cmd_path && !File.directory?(cmd_path) && (File.executable?(cmd_path) || (self.windows? && File.exist?(cmd_path)))
end

.gem_path(gem_name) ⇒ Object

DEPRECATED: Use the ‘ROOT` constant from the appropriate tool module instead e.g. File.join(Sigh::ROOT, ’lib’, ‘assets’, ‘resign.sh’)

Path to the installed gem to load resources (e.g. resign.sh)



405
406
407
408
409
410
411
412
413
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 405

def self.gem_path(gem_name)
  UI.deprecated('`Helper.gem_path` is deprecated. Use the `ROOT` constant from the appropriate tool module instead.')

  if !Helper.test? && Gem::Specification.find_all_by_name(gem_name).any?
    return Gem::Specification.find_by_name(gem_name).gem_dir
  else
    return './'
  end
end

.hide_loading_indicatorObject



354
355
356
357
358
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 354

def self.hide_loading_indicator
  if self.should_show_loading_indicator? && @require_pantograph_spinner
    @require_pantograph_spinner.success
  end
end

.homebrew?Boolean

returns true if pantograph was installed via Homebrew

Returns:



45
46
47
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 45

def self.homebrew?
  ENV["PANTOGRAPH_INSTALLED_VIA_HOMEBREW"].to_s == "true"
end

.is_ci?Boolean

Returns:



389
390
391
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 389

def self.is_ci?
  ci?
end

.is_mac?Boolean

Returns:



393
394
395
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 393

def self.is_mac?
  self.mac?
end

.is_test?Boolean

Use Helper.test?, Helper.ci?, Helper.mac? or Helper.windows? instead (legacy calls)

Returns:



385
386
387
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 385

def self.is_test?
  self.test?
end

.is_windows?Boolean

Returns:



397
398
399
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 397

def self.is_windows?
  self.windows?
end

.itms_pathObject

Returns the full path to the iTMSTransporter executable.

Returns:

  • the full path to the iTMSTransporter executable



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 209

def self.itms_path
  return ENV["PANTOGRAPH_ITUNES_TRANSPORTER_PATH"] if PantographCore::Env.truthy?("PANTOGRAPH_ITUNES_TRANSPORTER_PATH")

  if self.mac?
    # First check for manually install iTMSTransporter
    user_local_itms_path = "/usr/local/itms"
    return user_local_itms_path if File.exist?(user_local_itms_path)

    # Then check for iTMSTransporter in the Xcode path
    [
      "../Applications/Application Loader.app/Contents/MacOS/itms",
      "../Applications/Application Loader.app/Contents/itms",
      "../SharedFrameworks/ContentDeliveryServices.framework/Versions/A/itms" # For Xcode 11
    ].each do |path|
      result = File.expand_path(File.join(self.xcode_path, path))
      return result if File.exist?(result)
    end
    UI.user_error!("Could not find transporter at #{self.xcode_path}. Please make sure you set the correct path to your Xcode installation.")
  elsif self.windows?
    [
      "C:/Program Files (x86)/itms"
    ].each do |path|
      return path if File.exist?(path)
    end
    UI.user_error!("Could not find transporter at usual locations. Please use environment variable `PANTOGRAPH_ITUNES_TRANSPORTER_PATH` to specify your installation path.")
  else
    # not Mac or Windows
    return ''
  end
end

.json_file?(filename) ⇒ Boolean

checks if given file is a valid json file base taken from: stackoverflow.com/a/26235831/1945875

Returns:



371
372
373
374
375
376
377
378
379
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 371

def self.json_file?(filename)
  return false unless File.exist?(filename)
  begin
    JSON.parse(File.read(filename))
    return true
  rescue JSON::ParserError
    return false
  end
end

.keychain_path(keychain_name) ⇒ Object

keychain



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 243

def self.keychain_path(keychain_name)
  # Existing code expects that a keychain name will be expanded into a default path to Library/Keychains
  # in the user's home directory. However, this will not allow the user to pass an absolute path
  # for the keychain value
  #
  # So, if the passed value can't be resolved as a file in Library/Keychains, just use it as-is
  # as the keychain path.
  #
  # We need to expand each path because File.exist? won't handle directories including ~ properly
  #
  # We also try to append `-db` at the end of the file path, as with Sierra the default Keychain name
  # has changed for some users: https://github.com/urbanquakers/pantograph/issues/5649

  # Remove the ".keychain" at the end of the keychain name
  name = keychain_name.sub(/\.keychain$/, "")

  possible_locations = [
    File.join(Dir.home, 'Library', 'Keychains', name),
    name
  ].map { |path| File.expand_path(path) }

  # Transforms ["thing"] to ["thing-db", "thing.keychain-db", "thing", "thing.keychain"]
  keychain_paths = []
  possible_locations.each do |location|
    keychain_paths << "#{location}-db"
    keychain_paths << "#{location}.keychain-db"
    keychain_paths << location
    keychain_paths << "#{location}.keychain"
  end

  keychain_path = keychain_paths.find { |path| File.file?(path) }
  UI.user_error!("Could not locate the provided keychain. Tried:\n\t#{keychain_paths.join("\n\t")}") unless keychain_path
  keychain_path
end

.linux?Boolean

Returns:



101
102
103
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 101

def self.linux?
  (/linux/ =~ RUBY_PLATFORM) != nil
end

.logObject

This method is deprecated, use the ‘UI` class urbanquakers.github.io/pantograph/advanced/#user-input-and-output



417
418
419
420
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 417

def self.log
  UI.deprecated("Helper.log is deprecated. Use `UI` class instead")
  UI.current.log
end

.mac?Boolean

Is the currently running computer a Mac?

Returns:



106
107
108
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 106

def self.mac?
  (/darwin/ =~ RUBY_PLATFORM) != nil
end

.mac_stock_terminal?Boolean

Does the user use the Mac stock terminal

Returns:



116
117
118
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 116

def self.mac_stock_terminal?
  PantographCore::Env.truthy?("TERM_PROGRAM_VERSION")
end

.operating_systemObject



89
90
91
92
93
94
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 89

def self.operating_system
  return "macOS"   if RUBY_PLATFORM.downcase.include?("darwin")
  return "Windows" if RUBY_PLATFORM.downcase.include?("mswin")
  return "Linux"   if RUBY_PLATFORM.downcase.include?("linux")
  return "Unknown"
end

.pantograph_enabled?Boolean

pantograph

Returns:



15
16
17
18
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 15

def self.pantograph_enabled?
  # This is called from the root context on the first start
  @enabled ||= !PantographCore::PantographFolder.path.nil?
end

.pantograph_enabled_folder_pathObject

Checks if pantograph is enabled for this project and returns the folder where the configuration lives



21
22
23
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 21

def self.pantograph_enabled_folder_path
  pantograph_enabled? ? PantographCore::PantographFolder.path : '.'
end

.rubygems?Boolean

returns true if pantograph was installed via RubyGems

Returns:



50
51
52
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 50

def self.rubygems?
  !self.bundler? && !self.contained_pantograph? && !self.homebrew?
end

.sh_enabled?Boolean

Returns true if it is enabled to execute external commands.

Returns:

  • (Boolean)

    true if it is enabled to execute external commands



63
64
65
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 63

def self.sh_enabled?
  !self.test?
end

.should_show_loading_indicator?Boolean

loading indicator

Returns:



335
336
337
338
339
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 335

def self.should_show_loading_indicator?
  return false if PantographCore::Env.truthy?("PANTOGRAPH_DISABLE_ANIMATION")
  return false if Helper.ci?
  return true
end

.show_loading_indicator(text = nil) ⇒ Object

Show/Hide loading indicator



342
343
344
345
346
347
348
349
350
351
352
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 342

def self.show_loading_indicator(text = nil)
  if self.should_show_loading_indicator?
    # we set the default here, instead of at the parameters
    # as we don't want to `UI.message` a rocket that's just there for the loading indicator
    text ||= "🚀"
    @require_pantograph_spinner = TTY::Spinner.new("[:spinner] #{text} ", format: :dots)
    @require_pantograph_spinner.auto_spin
  else
    UI.message(text) if text
  end
end

.strip_ansi_colors(str) ⇒ Object

removes ANSI colors from string



291
292
293
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 291

def self.strip_ansi_colors(str)
  str.gsub(/\e\[([;\d]+)?m/, '')
end

.test?Boolean

Returns true if the currently running program is a unit test.

Returns:

  • (Boolean)

    true if the currently running program is a unit test



58
59
60
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 58

def self.test?
  Object.const_defined?("SpecHelper")
end

.transporter_java_executable_pathObject

iTMSTransporter



182
183
184
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 182

def self.transporter_java_executable_path
  return File.join(self.transporter_java_path, 'bin', 'java')
end

.transporter_java_ext_dirObject



186
187
188
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 186

def self.transporter_java_ext_dir
  return File.join(self.transporter_java_path, 'lib', 'ext')
end

.transporter_java_jar_pathObject



190
191
192
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 190

def self.transporter_java_jar_path
  return File.join(self.itms_path, 'lib', 'itmstransporter-launcher.jar')
end

.transporter_java_pathObject



198
199
200
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 198

def self.transporter_java_path
  return File.join(self.itms_path, 'java')
end

.transporter_pathObject

Returns the full path to the iTMSTransporter executable.

Returns:

  • the full path to the iTMSTransporter executable



203
204
205
206
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 203

def self.transporter_path
  return File.join(self.itms_path, 'bin', 'iTMSTransporter') unless Helper.windows?
  return File.join(self.itms_path, 'iTMSTransporter')
end

.transporter_user_dirObject



194
195
196
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 194

def self.transporter_user_dir
  return File.join(self.itms_path, 'bin')
end

.windows?Boolean

Returns:



96
97
98
99
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 96

def self.windows?
  # taken from: https://stackoverflow.com/a/171011/1945875
  (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
end

.with_env_values(hash, &block) ⇒ Object

Executes the provided block after adjusting the ENV to have the provided keys and values set as defined in hash. After the block completes, restores the ENV to its previous state.



319
320
321
322
323
324
325
326
327
328
329
330
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 319

def self.with_env_values(hash, &block)
  old_vals = ENV.select { |k, v| hash.include?(k) }
  hash.each do |k, v|
    ENV[k] = hash[k]
  end
  yield
ensure
  hash.each do |k, v|
    ENV.delete(k) unless old_vals.include?(k)
    ENV[k] = old_vals[k]
  end
end

.xcode_at_least?(version) ⇒ Boolean

Returns true if Xcode version is higher than 8.3.

Returns:

  • (Boolean)

    true if Xcode version is higher than 8.3



173
174
175
176
177
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 173

def self.xcode_at_least?(version)
  PantographCore::UI.user_error!("Unable to locate Xcode. Please make sure to have Xcode installed on your machine") if xcode_version.nil?
  v = xcode_version
  Gem::Version.new(v) >= Gem::Version.new(version)
end

.xcode_pathObject

Returns the full path to the Xcode developer tools of the currently running system.

Returns:

  • the full path to the Xcode developer tools of the currently running system



130
131
132
133
134
135
136
137
138
139
140
141
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 130

def self.xcode_path
  return "" unless self.mac?

  if self.xcode_server?
    # Xcode server always creates a link here
    xcode_server_xcode_path = "/Library/Developer/XcodeServer/CurrentXcodeSymlink/Contents/Developer"
    UI.verbose("We're running as XcodeServer, setting path to #{xcode_server_xcode_path}")
    return xcode_server_xcode_path
  end

  return `xcode-select -p`.delete("\n") + "/"
end

.xcode_server?Boolean

Returns:



143
144
145
146
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 143

def self.xcode_server?
  # XCS is set by Xcode Server
  return ENV["XCS"].to_i == 1
end

.xcode_versionObject

Returns The version of the currently used Xcode installation (e.g. “7.0”).

Returns:

  • The version of the currently used Xcode installation (e.g. “7.0”)



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 149

def self.xcode_version
  return nil unless self.mac?
  return @xcode_version if @xcode_version && @developer_dir == ENV['DEVELOPER_DIR']

  xcodebuild_path = "#{xcode_path}/usr/bin/xcodebuild"

  xcode_build_installed = File.exist?(xcodebuild_path)
  unless xcode_build_installed
    UI.verbose("Couldn't find xcodebuild at #{xcodebuild_path}, check that it exists")
    return nil
  end

  begin
    output = `DEVELOPER_DIR='' "#{xcodebuild_path}" -version`
    @xcode_version = output.split("\n").first.split(' ')[1]
    @developer_dir = ENV['DEVELOPER_DIR']
  rescue => ex
    UI.error(ex)
    UI.user_error!("Error detecting currently used Xcode installation, please ensure that you have Xcode installed and set it using `sudo xcode-select -s [path]`")
  end
  @xcode_version
end

.zip_directory(path, output_path, contents_only: false, overwrite: false, print: true) ⇒ Object

Zips directory



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'pantograph_core/lib/pantograph_core/helper.rb', line 296

def self.zip_directory(path, output_path, contents_only: false, overwrite: false, print: true)
  if overwrite
    overwrite_command = " && rm -f '#{output_path}'"
  else
    overwrite_command = ""
  end

  if contents_only
    command = "cd '#{path}'#{overwrite_command} && zip -r '#{output_path}' *"
  else
    containing_path = File.expand_path("..", path)
    contents_path = File.basename(path)

    command = "cd '#{containing_path}'#{overwrite_command} && zip -r '#{output_path}' '#{contents_path}'"
  end

  UI.command(command) unless print
  Helper.backticks(command, print: print)
end