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.
- #elapsed_from(time) ⇒ Object
-
#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.
63 64 65 66 67 68 |
# File 'lib/react_native_util/util.rb', line 63 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 |
#elapsed_from(time) ⇒ Object
54 55 56 |
# File 'lib/react_native_util/util.rb', line 54 def elapsed_from(time) format('%.1f', Time.now - time) 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.
75 76 77 78 79 80 |
# File 'lib/react_native_util/util.rb', line 75 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.
104 105 106 107 108 |
# File 'lib/react_native_util/util.rb', line 104 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
96 97 98 |
# File 'lib/react_native_util/util.rb', line 96 def log() STDOUT.log end |
#mac? ⇒ Boolean
Convenience method to determine if running on a Mac.
90 91 92 |
# File 'lib/react_native_util/util.rb', line 90 def mac? platform.mac? end |
#platform ⇒ Object
- TTY::Platform
-
Object with platform information
83 84 85 |
# File 'lib/react_native_util/util.rb', line 83 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.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/react_native_util/util.rb', line 17 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) spinner.success "success in #{elapsed_from start_time} s" rescue ExecutionError spinner.error "failure in #{elapsed_from start_time} 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']
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 148 149 150 151 |
# File 'lib/react_native_util/util.rb', line 120 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 |