Sha256: dbeacf61b58c51d3504c2c1a5c00da35ea6ad450d66f5e119f136a1217919376

Contents?: true

Size: 932 Bytes

Versions: 3

Compression:

Stored size: 932 Bytes

Contents

require 'pathname'
require 'rbconfig'
require 'fileutils'

module RepoManager
  module ActionHelper

    def shell_quote(string)
      return "" if string.nil? or string.empty?
      if windows?
        %{"#{string}"}
      else
        string.split("'").map{|m| "'#{m}'" }.join("\\'")
      end
    end

    def windows?
      RbConfig::CONFIG['host_os'] =~ /mswin|mingw/i
    end

    # @return[String] the relative path from the CWD
    def relative_path(path)
      return unless path

      path = Pathname.new(File.expand_path(path, FileUtils.pwd))
      cwd = Pathname.new(FileUtils.pwd)

      if windows?
        # c:/home D:/path/here will faile with ArgumentError: different prefix
        return path.to_s if path.to_s.capitalize[0] != cwd.to_s.capitalize[0]
      end

      path = path.relative_path_from(cwd)
      path = "./#{path}" unless path.absolute? || path.to_s.match(/^\./)
      path.to_s
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
repo_manager-0.7.3 lib/repo_manager/actions/action_helper.rb
repo_manager-0.7.2 lib/repo_manager/actions/action_helper.rb
repo_manager-0.7.1 lib/repo_manager/actions/action_helper.rb