Sha256: 502e23ce467885d819149c03306883662aabd7069d9ba4797a99f7a553557229

Contents?: true

Size: 1.92 KB

Versions: 16

Compression:

Stored size: 1.92 KB

Contents

WIN32 = RUBY_PLATFORM == "i386-mswin32"
CYGWIN = RUBY_PLATFORM == "i386-cygwin"
WINDOWS = WIN32 || CYGWIN

require 'fileutils'

def with_working_dir(dir)
  # Can't use Dir.chdir{ block } - will fail with multithreaded code.
  # http://www.ruby-doc.org/core/classes/Dir.html#M000790
  #
  prev = Dir.pwd
  begin
    dir = File.expand_path(dir)
    FileUtils.mkdir_p(dir)
    Dir.chdir(dir)
    yield
  ensure
    Dir.chdir(prev)
  end
end

# Utility for converting between win32 and cygwin paths. Does nothing on *nix.
module RSCM
  module PathConverter
    def filepath_to_nativepath(path, escaped)
      return nil if path.nil?
      path = File.expand_path(path)
      if(WIN32)
        path.gsub(/\//, "\\")
      elsif(CYGWIN)
        cmd = "cygpath --windows #{path}"
        Better.popen(cmd) do |io|
          cygpath = io.read.chomp
          escaped ? cygpath.gsub(/\\/, "\\\\\\\\") : cygpath
        end
      else
        path
      end
    end

    def filepath_to_nativeurl(path)
      return nil if path.nil?
      if(WINDOWS)
        urlpath = filepath_to_nativepath(path, false).gsub(/\\/, "/")
        "file:///#{urlpath}"
      else
        "file://#{File.expand_path(path)}"
      end
    end

    def nativepath_to_filepath(path)
      return nil if path.nil?
      if(WIN32)
        path.gsub(/\//, "\\")
      elsif(CYGWIN)
        path = path.gsub(/\\/, "/")
        cmd = "cygpath --unix #{path}"
        Better.popen(cmd) do |io|
          io.read.chomp
        end
      else
        path
      end
    end
    
    def ensure_trailing_slash(url)
      return nil if url.nil?
      if(url && url[-1..-1] != "/")
        "#{url}/"
      else
        url
      end
    end

    module_function :filepath_to_nativepath
    module_function :filepath_to_nativeurl
    module_function :nativepath_to_filepath
    module_function :ensure_trailing_slash
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rscm-0.3.4 lib/rscm/path_converter.rb
rscm-0.3.15 lib/rscm/path_converter.rb
rscm-0.3.2 lib/rscm/path_converter.rb
rscm-0.3.5 lib/rscm/path_converter.rb
rscm-0.3.13 lib/rscm/path_converter.rb
rscm-0.3.12 lib/rscm/path_converter.rb
rscm-0.3.3 lib/rscm/path_converter.rb
rscm-0.3.14 lib/rscm/path_converter.rb
rscm-0.3.1 lib/rscm/path_converter.rb
rscm-0.3.10 lib/rscm/path_converter.rb
rscm-0.3.11 lib/rscm/path_converter.rb
rscm-0.3.16 lib/rscm/path_converter.rb
rscm-0.3.9 lib/rscm/path_converter.rb
rscm-0.3.6 lib/rscm/path_converter.rb
rscm-0.3.8 lib/rscm/path_converter.rb
rscm-0.3.7 lib/rscm/path_converter.rb