Module: ReactNativeUtil::Util

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

Overview

Module with utility methods

Instance Method Summary collapse

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)



59
60
61
62
63
64
# File 'lib/react_native_util/util.rb', line 59

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



43
44
45
46
47
48
49
50
51
52
# File 'lib/react_native_util/util.rb', line 43

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, "#{command.shelljoin}: #{$?}" 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



71
72
73
74
75
76
# File 'lib/react_native_util/util.rb', line 71

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

#have_command?(command) ⇒ Boolean

Determine if a specific command is available.

Parameters:

  • command (#to_s)

    A command to check for

Returns:

  • (Boolean)

    true if found, false otherwise



100
101
102
103
104
# File 'lib/react_native_util/util.rb', line 100

def have_command?(command)
  # May be shell-dependent, OS-dependent
  # Kernel#system does not raise Errno::ENOENT when running under the Bundler
  !`which #{command}`.empty?
end

#log(message) ⇒ Object

Wrapper for STDOUT.log

Parameters:

  • message (#to_s)

    message to log



92
93
94
# File 'lib/react_native_util/util.rb', line 92

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



86
87
88
# File 'lib/react_native_util/util.rb', line 86

def mac?
  platform.mac?
end

#platformObject

TTY::Platform

Object with platform information



79
80
81
# File 'lib/react_native_util/util.rb', line 79

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

#run_command_with_spinner!(*command, log: nil, chdir: nil) ⇒ Object

Executes a command with no output to the terminal. A spinner is displayed instead. Output may be directed to a file.

Parameters:

  • command

    Variadic command to be executed

  • log (String, Symbol, nil) (defaults to: nil)

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

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

    Directory in which to execute the command

Raises:

  • ExecutionError on failure



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

def run_command_with_spinner!(*command, log: nil, chdir: nil)
  STDOUT.flush
  STDERR.flush
  spinner = TTY::Spinner.new "[:spinner] #{command.shelljoin}", format: :flip
  spinner.auto_spin
  start_time = Time.now
  execute(*command, log: nil, output: log, chdir: chdir)
  elapsed = Time.now - start_time
  spinner.success "success in #{format('%.1f', elapsed)} s"
rescue ExecutionError
  elapsed = Time.now - start_time
  spinner.error "failure in #{format('%.1f', elapsed)} s"
  STDOUT.log "See #{log} for details." if log && log.kind_of?(String)
  raise
end

#validate_commands!(commands) ⇒ Object

Validate one or more commands. If the specified command is not available in the PATH (via which), a ConversionError is raised noting the package to be installed (from Homebrew, e.g.).

When package names to be installed differ from command names, a Hash may be used. For example:

validate_commands! [:yarn, 'react-native' => 'react-native-cli']

Parameters:

  • commands (Array, Hash, #to_s)

    one or more commands to be validated.

Raises:

  • ConversionError if any command not found



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/react_native_util/util.rb', line 116

def validate_commands!(commands)
  errors = []

  case commands
  when Array
    # Validate each command in the array, accumulating error messages
    # if necessary.
    commands.each do |c|
      begin
        validate_commands! c
      rescue ConversionError => e
        errors += e.message.split("\n")
      end
    end
  when Hash
    # Each key represents a command to check. The value is the package to
    # install if missing.
    commands.each do |key, value|
      next if have_command?(key)

      errors << "#{key} command not found. Please install #{value} to continue."
    end
  else
    # commands is a single command to be validated. The package name is the
    # same. Usually a symbol or a string, but only has to respond to #to_s.
    errors << "#{commands} command not found. Please install #{commands} to continue." unless have_command? commands
  end

  return if errors.empty?

  raise ConversionError, errors.join("\n")
end