11
12
13
14
15
16
17
18
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/source_win_bat.rb', line 11
def main(argv)
if argv.length < 4 || argv[3].chomp.empty?
STDERR.puts "Usage: sw windows_cmd_or_batch [options_for_the_cmd]\n\nInternal Ruby command Usage:\n \#{File.basename(__FILE__)} env_out macro_out cwd_out windows_cmd_or_batch [options_for_the_cmd]\n EOS\n exit\n end\n\n unless [:cygwin, :msys, :wsl].include? UnixCompatEnv.compat_env\n raise \"You're in an unsupported UNIX compatible environment\"\n end\n\n env = prepare_env_vars\n env_tmp_file_in = mk_tmpname(\".env\")\n macro_tmp_file_in = mk_tmpname(\".doskey\")\n cwd_tmp_file_in = mk_tmpname(\".cwd\")\n win_cmd = argv[3..-1].map {|v| \"\#{v}\"}.join(\" \")\n win_cmd += \" & call set SW_EXITSTATUS=%^ERRORLEVEL% \"\n win_cmd = concat_envdump(win_cmd, env_tmp_file_in)\n win_cmd = concat_macrodump(win_cmd, macro_tmp_file_in)\n win_cmd = concat_cwddump(win_cmd, cwd_tmp_file_in)\n win_cmd += \" & call exit %^SW_EXITSTATUS%\"\n # puts win_cmd\n Signal.trap(:INT, \"SIG_IGN\")\n if UnixCompatEnv.compat_env == :wsl\n # * Skip winpty, assuming the system's WSL supports ConPTY\n # * Use an absolute path since EC overwrites PATH with Windows-style PATH in WSL\n pid = Process.spawn(env,\n UnixCompatEnv.to_compat_path('C:\\\\Windows\\\\System32\\\\cmd.exe'),\n '/C', win_cmd, :in => 0, :out => 1, :err => 2)\n elsif !STDOUT.isatty\n pid = Process.spawn(env, 'cmd.exe', '/C', win_cmd, :in => 0, :out => 1, :err => 2)\n else\n pid = Process.spawn(env, 'winpty', '--', 'cmd.exe', '/C', win_cmd, :in => 0, :out => 1, :err => 2)\n end\n Signal.trap(:INT) do\n Process.signal(\"-KILL\", pid)\n end\n status = nil\n loop do\n _, status = Process.wait2(pid)\n break if status.exited?\n end\n \n begin\n codepage = detect_ansi_codepage\n conv_setenv_stmts(env_tmp_file_in, argv[0], :bash, codepage)\n conv_doskey_stmts(macro_tmp_file_in, argv[1], :bash, codepage)\n gen_chdir_cmds(cwd_tmp_file_in, argv[2], :bash, codepage)\n [env_tmp_file_in, macro_tmp_file_in, cwd_tmp_file_in].each do |f|\n File.delete f\n end\n rescue Errno::ENOENT\n # ignore\n end\n\n exit(status.exitstatus)\nend\n"
|