Module: ReactNativeUtil::Util

Included in:
CLI, Converter
Defined in:
lib/react_native_util/util.rb

Overview

Module with utility methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#platformObject (readonly)

TTY::Platform

Object with platform information



9
10
11
# File 'lib/react_native_util/util.rb', line 9

def platform
  @platform
end

Instance Method Details

#boolean_env_var?(var, default_value: false) ⇒ Boolean

Return a Boolean value associated with an environment variable.

Parameters:

  • var (#to_s)

    The name of an environment variable

  • default_value (true, false) (defaults to: false)

    Returned if the environment variable is not set

Returns:

  • (Boolean)

    true if the value of the environment variable begins with y or t (case-insensitive)



36
37
38
39
40
41
# File 'lib/react_native_util/util.rb', line 36

def boolean_env_var?(var, default_value: false)
  value = ENV[var.to_s]
  return default_value if value.nil?

  /^(y|t)/i.match? value
end

#execute(*command, chdir: nil, output: STDOUT, log: STDOUT) ⇒ Object

Execute the specified command. If output is non-nil, generate a log at that location. Main log (open) is log.

Parameters:

  • command

    Variadic command to be executed

  • chdir (String, nil) (defaults to: nil)

    Directory in which to execute the command

  • output (String, Symbol, IO) (defaults to: STDOUT)

    Output for command (path, IO or a symbol such as :close)

  • log (IO, nil) (defaults to: STDOUT)

    Open IO for main log (nil to suppress logging command to main log)

Returns:

  • nil

Raises:

  • ExecutionError If the command fails



20
21
22
23
24
25
26
27
28
29
# File 'lib/react_native_util/util.rb', line 20

def execute(*command, chdir: nil, output: STDOUT, log: STDOUT)
  log.log_command command unless log.nil?

  options = chdir.nil? ? {} : { chdir: chdir }
  system(*command, options.merge(%i[err out] => output))

  raise ExecutionError unless $?.success?

  nil
end

#float_env_var(var, default_value: 0) ⇒ Float

Return a Float value associated with an environment variable.

Parameters:

  • var (#to_s)

    The name of an environment variable

  • default_value (#to_f) (defaults to: 0)

    Returned if the environment variable is not set

Returns:

  • (Float)

    the numeric value of the environment variable or the default_value



48
49
50
51
52
53
# File 'lib/react_native_util/util.rb', line 48

def float_env_var(var, default_value: 0)
  value = ENV[var.to_s]
  return default_value.to_f if value.nil?

  value.to_f
end

#log(message) ⇒ Object

Wrapper for STDOUT.log

Parameters:

  • message (#to_s)

    message to log



65
66
67
# File 'lib/react_native_util/util.rb', line 65

def log(message)
  STDOUT.log message
end

#mac?Boolean

Convenience method to determine if running on a Mac.

Returns:

  • (Boolean)

    true if running on a Mac

  • false otherwise



58
59
60
61
# File 'lib/react_native_util/util.rb', line 58

def mac?
  @platform ||= TTY::Platform.new
  @platform.mac?
end