Class: Dev::Common
Overview
Class contains several common useful development methods
Instance Method Summary collapse
-
#ask(message, default = nil) ⇒ Object
Asks for user input using the given message and returns it If a default was specified and the user doesn’t give any input, the default will be returned.
-
#run_command(command, stdin: $stdin, stdout: $stdout, stderr: $stderr, env: ENV, capture: false) ⇒ Object
Runs a command in a subshell.
-
#running_codebuild? ⇒ Boolean
Checks if CODEBUILD_INITIATOR or INITIATOR env variable are set If they are not set, it assumes it is not running in codebuild and return false Otherwise it returns true.
-
#strip_non_json(str) ⇒ Object
Remove all leading non left-curly-brace characters Remove all trailing non right-curly-brace characters.
-
#tokenize(str) ⇒ Object
This method breaks up a string by spaces, however if it finds quoted strings in it, it attempts to preserve those as a single element e.g.
-
#version_greater_than(required_version, actual_version) ⇒ Object
Takes two versions and attempts to compare them Returns true if the actual_version is greater than the required version (false otherwise).
-
#with_confirmation(message, default = 'y', color_message: true) ⇒ Object
Wraps a block of code in a y/n question.
Instance Method Details
#ask(message, default = nil) ⇒ Object
Asks for user input using the given message and returns it If a default was specified and the user doesn’t give any input, the default will be returned
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/firespring_dev_commands/common.rb', line 63 def ask(, default = nil) msg = " #{message}" msg << " [#{default}]" if default msg << ': ' print msg answer = $stdin.gets.to_s.strip return default if default && answer == '' answer end |
#run_command(command, stdin: $stdin, stdout: $stdout, stderr: $stderr, env: ENV, capture: false) ⇒ Object
Runs a command in a subshell. By default, the subshell is connected to the stdin/stdout/stderr of the current program By default, the current environment is passed to the subshell You can capture the output of the command by setting capture to true
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/firespring_dev_commands/common.rb', line 10 def run_command(command, stdin: $stdin, stdout: $stdout, stderr: $stderr, env: ENV, capture: false) command = Array(command) output = nil # If capture was specified, write stdout to a pipe so we can return it stdoutread, stdout = ::IO.pipe if capture # Spawn a subprocess to run the command pid = ::Process.spawn(env, *command, in: stdin, out: stdout, err: stderr) # Wait for the subprocess to finish and capture the result _, result = ::Process.wait2(pid) # If capture was specified, close the write pipe, read the output from the read pipe, close the read pipe, and return the output if capture stdout.close output = stdoutread.readlines.join stdoutread.close end # If the exitstatus was non-zero, exit with an error unless result.exitstatus.zero? puts output if capture LOG.error "#{result.exitstatus} exit status while running [ #{command.join(' ')} ]\n".red exit result.exitstatus end output end |
#running_codebuild? ⇒ Boolean
Checks if CODEBUILD_INITIATOR or INITIATOR env variable are set If they are not set, it assumes it is not running in codebuild and return false Otherwise it returns true
86 87 88 89 90 |
# File 'lib/firespring_dev_commands/common.rb', line 86 def running_codebuild? return false if ENV['CODEBUILD_INITIATOR'].to_s.strip.empty? && ENV['INITIATOR'].to_s.strip.empty? true end |
#strip_non_json(str) ⇒ Object
Remove all leading non left-curly-brace characters Remove all trailing non right-curly-brace characters
94 95 96 |
# File 'lib/firespring_dev_commands/common.rb', line 94 def strip_non_json(str) str.sub(/\A[^{]*{/m, '{').sub(/}[^}]*\z/m, '}') end |
#tokenize(str) ⇒ Object
This method breaks up a string by spaces, however if it finds quoted strings in it, it attempts to preserve those as a single element e.g. “foo ‘bin baz’ bar” => [foo, ‘bin baz’, bar]
77 78 79 80 81 |
# File 'lib/firespring_dev_commands/common.rb', line 77 def tokenize(str) str.split(/\s(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/) .reject(&:empty?) .map { |s| s.gsub(/(^ +)|( +$)|(^["']+)|(["']+$)/, '') } end |
#version_greater_than(required_version, actual_version) ⇒ Object
Takes two versions and attempts to compare them Returns true if the actual_version is greater than the required version (false otherwise)
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/firespring_dev_commands/common.rb', line 100 def version_greater_than(required_version, actual_version) required_version = required_version.to_s.split('.') actual_version = actual_version.to_s.split('.') required_version.each_with_index do |required, index| required = required.to_i actual = actual_version[index].to_i return true if actual > required next if actual == required return false end end |
#with_confirmation(message, default = 'y', color_message: true) ⇒ Object
Wraps a block of code in a y/n question. If the user answers ‘y’ then the block is executed. If the user answers ‘n’ then the block is skipped.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/firespring_dev_commands/common.rb', line 43 def with_confirmation(, default = 'y', color_message: true) = "\n #{message}? " = .light_green if print print '('.light_green << 'y'.light_yellow << '/'.light_green << 'n'.light_yellow << ') '.light_green answer = default answer = $stdin.gets unless ENV['NON_INTERACTIVE'] == 'true' unless answer.strip.casecmp('y').zero? puts "\n Cancelled.\n".light_yellow exit 1 end puts yield end |