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
-
#boolean_env_var?(var, default_value: false) ⇒ Boolean
Return a Boolean value associated with an environment variable.
-
#execute(*command, chdir: nil, output: STDOUT, log: STDOUT) ⇒ Object
Execute the specified command.
-
#float_env_var(var, default_value: 0) ⇒ Float
Return a Float value associated with an environment variable.
-
#have_command?(command) ⇒ Boolean
Determine if a specific command is available.
-
#log(message) ⇒ Object
Wrapper for STDOUT.log.
-
#mac? ⇒ Boolean
Convenience method to determine if running on a Mac.
-
#platform ⇒ Object
- TTY::Platform
-
Object with platform information.
-
#run_command_with_spinner!(*command, log: nil, chdir: nil) ⇒ Object
Executes a command with no output to the terminal.
-
#validate_commands!(commands) ⇒ Object
Validate one or more commands.
Instance Method Details
#boolean_env_var?(var, default_value: false) ⇒ Boolean
Return a Boolean value associated with an environment variable.
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.
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? = chdir.nil? ? {} : { chdir: chdir } system(*command, .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.
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.
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
92 93 94 |
# File 'lib/react_native_util/util.rb', line 92 def log() STDOUT.log end |
#mac? ⇒ Boolean
Convenience method to determine if running on a Mac.
86 87 88 |
# File 'lib/react_native_util/util.rb', line 86 def mac? platform.mac? end |
#platform ⇒ Object
- 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.
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']
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..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 |