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



52
53
54
55
56
57
# File 'lib/u3d_core/helper.rb', line 52

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]’)



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

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



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

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)


140
141
142
# File 'lib/u3d_core/helper.rb', line 140

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

.data_pathObject



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

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)


150
151
152
# File 'lib/u3d_core/helper.rb', line 150

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

.linux?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/u3d_core/helper.rb', line 92

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

.mac?Boolean

Is the currently running computer a Mac?

Returns:

  • (Boolean)


107
108
109
# File 'lib/u3d_core/helper.rb', line 107

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

.mac_stock_terminal?Boolean

Does the user use the Mac stock terminal

Returns:

  • (Boolean)


145
146
147
# File 'lib/u3d_core/helper.rb', line 145

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

.operating_systemObject

the current operating system



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/u3d_core/helper.rb', line 117

def self.operating_system
  # rubocop:disable Style/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 Style/GuardClause
end

.operating_systemsObject

the valid operating systems



112
113
114
# File 'lib/u3d_core/helper.rb', line 112

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

.strip_ansi_colors(str) ⇒ Object

removes ANSI colors from string



65
66
67
# File 'lib/u3d_core/helper.rb', line 65

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



60
61
62
# File 'lib/u3d_core/helper.rb', line 60

def self.test?
  defined? SpecHelper
end

.ubuntu_on_windows?Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
# File 'lib/u3d_core/helper.rb', line 96

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)


135
136
137
# File 'lib/u3d_core/helper.rb', line 135

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

.win_64?Boolean

Returns:

  • (Boolean)


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

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

.windows?Boolean

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/u3d_core/helper.rb', line 87

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



46
47
48
# File 'lib/u3d_core/helper.rb', line 46

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