Module: UnixCompatEnv

Defined in:
lib/unixcompatenv.rb

Constant Summary collapse

@@compat_env =
nil
@@compat_root =
nil
@@win_root =
nil
@@win_tmp =
nil

Class Method Summary collapse

Class Method Details

.compat_envObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/unixcompatenv.rb', line 29

def self.compat_env
  @@compat_env ||=
    case RUBY_PLATFORM
    when /msys/
      :msys
    when /linux/
      :wsl
    when /cygwin/
      :cygwin
    else
      :win
    end
end

.compat_root_in_winObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/unixcompatenv.rb', line 43

def self.compat_root_in_win
  return @@compat_root if @@compat_root || compat_env == :wsl  # @@compat_root should be nil for :wsl
  case compat_env
  when :msys, :cygwin
    path = `cygpath -w /`.chomp
    if !path.end_with?("\\")
      path += "\\"
    end
    @@compat_root = path
  when :wsl
    @@compat_root = nil
  end
  @@compat_root
end

.detect_installed_compat_envsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/unixcompatenv.rb', line 10

def self.detect_installed_compat_envs
  res = {}

  default_paths = {
    wsl: "/c/Windows/System32/bash.exe",
    msys: "/c/tools/msys64/usr/bin/bash.exe",
    cygwin: "/c/tools/cygwin/bin/bash.exe"
  }

  default_paths.each do |env, path|
    path = win_root_in_compat[0..-2] + path
    if File.exists?(path)
      res[env] = path
    end
  end

  res
end

.to_compat_path(path) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/unixcompatenv.rb', line 96

def self.to_compat_path(path)
  if !compat_root_in_win.nil? && path.start_with?(compat_root_in_win)
    path = path[compat_root_in_win.length - 1 .. -1]
  end
  if /^[a-zA-Z]:/ =~ path
    drive = path[0]
    path = win_root_in_compat + drive.downcase + (path[2..-1] || "")
  end
  path.gsub('\\', '/')
end

.to_win_path(path) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/unixcompatenv.rb', line 81

def self.to_win_path(path)
  path = Pathname.new(path).cleanpath.to_s
  raise "Abs path is expected: #{path}" if path[0] != "/"

  if path.start_with?(win_root_in_compat)
    drive = path[win_root_in_compat.length]
    "#{drive.upcase}:\\" + (path[(win_root_in_compat.length + 2)..-1] || '').gsub('/', '\\')
  elsif compat_env == :wsl
    raise "A WSL path which cannot be accessed from Windows: #{path}"
  else
    # [0...-1] trims trailing '/'
    compat_root_in_win[0...-1] + path.gsub('/', '\\')
  end
end

.win_root_in_compatObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/unixcompatenv.rb', line 58

def self.win_root_in_compat
  return @@win_root if @@win_root
  case compat_env
  when :msys, :cygwin
    root = `cygpath -u c:/`.chomp
  when :wsl
    root = `wslpath c:/`.chomp
  end
  raise "unexpected win root path" unless root.end_with?("c/")
  @@win_root = root[0...-2]
end

.win_tmp_in_compatObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/unixcompatenv.rb', line 70

def self.win_tmp_in_compat
  return @@win_tmp if @@win_tmp
  case compat_env
  when :wsl, :cygwin
    @@win_tmp = to_compat_path(`cmd.exe /C "echo %TEMP%"`.chomp) + '/'
  when :msys
    @@win_tmp = to_compat_path(ENV['temp'])
  end
  @@win_tmp
end