Sha256: 14bc69681fad388accd509a5ef87a67c7e351a7920e051c7da31520362b0246a

Contents?: true

Size: 1.53 KB

Versions: 18

Compression:

Stored size: 1.53 KB

Contents

require_relative '../helper'
require 'shellwords'

# Here be monkey patches

class String
  # CrossplatformShellwords
  def shellescape
    CrossplatformShellwords.shellescape(self)
  end
end

class Array
  def shelljoin
    CrossplatformShellwords.shelljoin(self)
  end
end

# Here be helper

module CrossplatformShellwords
  # handle switching between implementations of shellescape
  def shellescape(str)
    if PantographCore::Helper.windows?
      WindowsShellwords.shellescape(str)
    else
      # using `escape` instead of expected `shellescape` here
      # which corresponds to Shellword's `String.shellescape` implementation
      # https://github.com/ruby/ruby/blob/1cf2bb4b2085758112503e7da7414d1ef52d4f48/lib/shellwords.rb#L216
      Shellwords.escape(str)
    end
  end
  module_function :shellescape

  # make sure local implementation is also used in shelljoin
  def shelljoin(array)
    array.map { |arg| shellescape(arg) }.join(' ')
  end
  module_function :shelljoin
end

# Windows implementation
module WindowsShellwords
  def shellescape(str)
    str = str.to_s

    # An empty argument will be skipped, so return empty quotes.
    # https://github.com/ruby/ruby/blob/a6413848153e6c37f6b0fea64e3e871460732e34/lib/shellwords.rb#L142-L143
    return '""'.dup if str.empty?

    str = str.dup

    # wrap in double quotes if contains space
    if str =~ /\s/
      # double quotes have to be doubled if will be quoted
      str.gsub!('"', '""')
      return '"' + str + '"'
    else
      return str
    end
  end
  module_function :shellescape
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
pantograph-0.1.22 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.21 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.20 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.19 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.17 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.16 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.15 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.14 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.13 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.12 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.10 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.8 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.7 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.6 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.4 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.3 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.1 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb
pantograph-0.1.0 pantograph_core/lib/pantograph_core/core_ext/shellwords.rb