Module: U3dCore::Helper

Defined in:
lib/u3d_core/helper.rb

Constant Summary collapse

DEFAULT_LINUX_PATH =
File.join(ENV['HOME'], '.u3d').freeze
DEFAULT_MAC_PATH =
File.join(ENV['HOME'], 'Library', 'Application Support', 'u3d').freeze
DEFAULT_WINDOWS_PATH =
File.join(ENV['HOME'], 'AppData', 'Local', 'u3d').freeze

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



50
51
52
53
54
55
# File 'lib/u3d_core/helper.rb', line 50

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

.bundler?boolean

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

Returns:

  • (boolean)

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



68
69
70
71
72
73
74
# File 'lib/u3d_core/helper.rb', line 68

def self.bundler?
  # Bundler environment variable
  %w[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



77
78
79
80
81
82
83
# File 'lib/u3d_core/helper.rb', line 77

def self.ci?
  # Check for Jenkins, Travis CI, ... environment variables
  %w[JENKINS_HOME JENKINS_URL TRAVIS CIRCLECI CI TEAMCITY_VERSION GO_PIPELINE_NAME bamboo_buildKey GITLAB_CI XCS].each do |current|
    return true if ENV.key?(current)
  end
  return false
end

.colors_disabled?Boolean

Do we want to disable the colored output?

Returns:

  • (Boolean)


137
138
139
# File 'lib/u3d_core/helper.rb', line 137

def self.colors_disabled?
  ENV["U3D_DISABLE_COLORS"]
end

.data_pathObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/u3d_core/helper.rb', line 33

def self.data_path
  case operating_system
  when :linux
    DEFAULT_LINUX_PATH
  when :mac
    DEFAULT_MAC_PATH
  when :win
    DEFAULT_WINDOWS_PATH
  end
end

.iterm?Boolean

Does the user use iTerm?

Returns:

  • (Boolean)


147
148
149
# File 'lib/u3d_core/helper.rb', line 147

def self.iterm?
  !ENV["ITERM_SESSION_ID"].nil?
end

.linux?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/u3d_core/helper.rb', line 90

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

.mac?Boolean

Is the currently running computer a Mac?

Returns:

  • (Boolean)


104
105
106
# File 'lib/u3d_core/helper.rb', line 104

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

.mac_stock_terminal?Boolean

Does the user use the Mac stock terminal

Returns:

  • (Boolean)


142
143
144
# File 'lib/u3d_core/helper.rb', line 142

def self.mac_stock_terminal?
  !ENV["TERM_PROGRAM_VERSION"].nil?
end

.operating_systemObject

the current operating system



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/u3d_core/helper.rb', line 114

def self.operating_system
  # rubocop:disable GuardClause
  if linux?
    return :linux
  elsif mac?
    return :mac
  elsif windows?
    return :win
  else
    raise 'Could not assume what OS you\'re running, please specify it as much as possible'
  end
  # rubocop:enable GuardClause
end

.operating_systemsObject

the valid operating systems



109
110
111
# File 'lib/u3d_core/helper.rb', line 109

def self.operating_systems
  %i[linux mac win]
end

.strip_ansi_colors(str) ⇒ Object

removes ANSI colors from string



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

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 'lib/u3d_core/helper.rb', line 58

def self.test?
  defined? SpecHelper
end

.ubuntu_on_windows?Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
# File 'lib/u3d_core/helper.rb', line 94

def self.ubuntu_on_windows?
  # taken from: https://github.com/Microsoft/BashOnWindows/issues/423#issuecomment-221627364
  proc_version = '/proc/version'
  return false unless File.exist? proc_version
  File.open(proc_version, 'r') do |f|
    return !(/Microsof|WSL/ =~ f.read).nil?
  end
end

.win_32?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/u3d_core/helper.rb', line 132

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

.win_64?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/u3d_core/helper.rb', line 128

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

.windows?Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/u3d_core/helper.rb', line 85

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

.windows_path(path) ⇒ Object



44
45
46
# File 'lib/u3d_core/helper.rb', line 44

def self.windows_path(path)
  path.gsub(%r{\/(\d)}, '/\\\\\1').tr('/', '\\')
end