Module: Squared::Common::Shell
- Included in:
- Workspace::Project::Support::OptionPartition
- Defined in:
- lib/squared/common/shell.rb
Class Method Summary collapse
- .basic_option(flag, val, **kwargs) ⇒ Object
- .fill_option(val, **kwargs) ⇒ Object
- .line_width(lines) ⇒ Object
- .quote_option(flag, val, **kwargs) ⇒ Object
- .shell_bin(name, env: true) ⇒ Object
- .shell_escape(val, quote: false, option: false, force: false, double: false, override: false) ⇒ Object
- .shell_option(flag, val = nil, sep: '=', escape: true, quote: true, force: true, double: false, merge: false, switch: nil, override: false) ⇒ Object
- .shell_parse(val, escape: false, force: true, **kwargs) ⇒ Object
- .shell_quote(val, option: true, force: true, double: false, preserve: true, pass: false, override: false) ⇒ Object
- .shell_split(val, join: nil, **kwargs) ⇒ Object
Class Method Details
.basic_option(flag, val, **kwargs) ⇒ Object
180 181 182 |
# File 'lib/squared/common/shell.rb', line 180 def basic_option(flag, val, **kwargs) shell_option(flag, val, escape: false, force: false, **kwargs) end |
.fill_option(val, **kwargs) ⇒ Object
169 170 171 172 173 174 |
# File 'lib/squared/common/shell.rb', line 169 def fill_option(val, **kwargs) return val unless val.is_a?(::String) return "-#{val}" if val.match?(/\A(?:[a-z]\d*|\d)\z/i) shell_escape(val.start_with?('-') ? val : "--#{val}", **kwargs) end |
.line_width(lines) ⇒ Object
165 166 167 |
# File 'lib/squared/common/shell.rb', line 165 def line_width(lines) [lines.empty? ? 80 : [lines.max_by(&:size).size, 80].max, Rake.application.terminal_width].min end |
.quote_option(flag, val, **kwargs) ⇒ Object
176 177 178 |
# File 'lib/squared/common/shell.rb', line 176 def quote_option(flag, val, **kwargs) shell_option(flag, val, escape: false, **kwargs) end |
.shell_bin(name, env: true) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/squared/common/shell.rb', line 157 def shell_bin(name, env: true) require_relative 'base' key = name.to_s.upcase key = File.basename(key, '.*') if Rake::Win32.windows? shell_quote((env && ENV["PATH_#{key}"]) || PATH[key] || PATH[key.to_sym] || PATH[key = key.downcase] || PATH[key.to_sym] || name, option: false, force: false, double: true) end |
.shell_escape(val, quote: false, option: false, force: false, double: false, override: false) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/squared/common/shell.rb', line 19 def shell_escape(val, quote: false, option: false, force: false, double: false, override: false) val = val.to_s if (r = /\A(--?)([^=\s]+)((=|\s+)(["'])?(?(5)(.*)\5|(.*)))?\z/m.match(val)) if (data = r[2].match(QUOTE_VALUE)) double = data[1] == '"' override = true elsif !r[3] || r[6] return val end ch = r[7] opt = if ch.start_with?('"', "'") "#{ch}#{ch[0]}" elsif ch.end_with?('"', "'") "#{ch[-1]}#{ch}" else return val unless ch.match?(/\s/) ch end r[1] + (data ? data[2] : r[2]) + r[4] + shell_quote(opt, force: force, double: double, override: override) elsif option && val =~ /\A(-{0,2}[^=\s-][^=\s]*)=(.+)\z/m return val if $2.match?(QUOTE_VALUE) "#{$1}=%s" % if $2.include?(' ') shell_quote($2, option: false) elsif Rake::Win32.windows? $2 else Shellwords.escape($2) end elsif Rake::Win32.windows? quote ? shell_quote(val, force: force, double: double) : val else val.empty? ? '' : Shellwords.escape(val) end end |
.shell_option(flag, val = nil, sep: '=', escape: true, quote: true, force: true, double: false, merge: false, switch: nil, override: false) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/squared/common/shell.rb', line 77 def shell_option(flag, val = nil, sep: '=', escape: true, quote: true, force: true, double: false, merge: false, switch: nil, override: false) flag = flag.to_s if flag =~ QUOTE_VALUE double = $1 == '"' flag = $2 escape = false override = true end sep = unless flag.empty? if flag[0] == '-' if flag[1] == '-' sep else merge ? '' : ' ' end elsif switch pre = switch unless flag.start_with?(switch) merge ? '' : sep elsif flag.size == 1 pre = '-' merge ? '' : ' ' else pre = '--' sep end end "#{pre}#{flag}#{unless val.nil? "#{sep}#{if escape shell_escape(val, quote: quote, double: double, override: override) elsif quote shell_quote(val, option: false, force: force, double: double, override: override) else val end}" end}" end |
.shell_parse(val, escape: false, force: true, **kwargs) ⇒ Object
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 152 153 154 155 |
# File 'lib/squared/common/shell.rb', line 122 def shell_parse(val, escape: false, force: true, **kwargs) a = [] b = [] c = [] d = [] target = [a, b] j = -1 val.shellsplit.each_with_index do |opt, i| if opt == '--' target = [c, d] elsif opt =~ /\A--?[^=]+(=|\z)/ j = $1 == '=' ? -1 : i target[0] << [opt] elsif j >= 0 target[0][j] << opt else target[1] << shell_quote(opt, option: false, force: force) end end ret = [[a, b], [], [c, d]].flat_map do |e, f| next '--' unless e e.flat_map do |item| if item.size == 1 fill_option(item.first) else flag = item.shift item.map { |s| shell_option(flag, s, escape: escape, force: force, **kwargs) } end end.concat(f) end ret.pop if ret.last == '--' ret end |
.shell_quote(val, option: true, force: true, double: false, preserve: true, pass: false, override: false) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/squared/common/shell.rb', line 56 def shell_quote(val, option: true, force: true, double: false, preserve: true, pass: false, override: false) val = val.to_s return val if (!force && !val.include?(' ')) || val.empty? if option pat = /\A(?:-[^=\s-](?:=|\s+)?|(--)?[^=\s-][^=\s]*(?(1)(?:=|\s+)|=))(["']).+\2\z/m return val if val.match?(pat) end if val =~ QUOTE_VALUE return val if pass || ($1 == '"' && Rake::Win32.windows? && val.match?(/(?:[#{File::SEPARATOR} ]|\\")/o)) base = $2 unless preserve end q = -> { (base || val).gsub("'\\\\''", "'") } if double || Rake::Win32.windows? || (ARG[:QUOTE] == '"' && !override) "\"#{q.call.gsub(/(?<!\\)"/, '\\"')}\"" else "'#{q.call.gsub("'", "'\\\\''")}'" end end |
.shell_split(val, join: nil, **kwargs) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/squared/common/shell.rb', line 115 def shell_split(val, join: nil, **kwargs) ret = val.shellsplit.map { |opt| shell_escape(opt, option: true, double: true, **kwargs) } return ret unless join ret.join(join.is_a?(::String) ? join : ' ') end |