forked from EvilBeaver/OneScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleProcess.cs
More file actions
131 lines (111 loc) · 4.78 KB
/
ConsoleProcess.cs
File metadata and controls
131 lines (111 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Newtonsoft.Json.Linq;
using Serilog;
namespace VSCode.DebugAdapter
{
internal class ConsoleProcess : DebugeeProcess
{
public ConsoleProcess(PathHandlingStrategy pathHandling) : base(pathHandling)
{
}
public string RuntimeExecutable { get; set; }
public string WorkingDirectory { get; set; }
public string StartupScript { get; set; }
public string ScriptArguments { get; set; }
public string RuntimeArguments { get; set; }
public IDictionary<string, string> Environment { get; set; } = new Dictionary<string, string>();
protected override void InitInternal(JObject args)
{
var options = args.ToObject<ConsoleLaunchOptions>();
if (options.Program == null)
{
throw new InvalidDebugeeOptionsException(1001, "Property 'program' is missing or empty.");
}
// validate argument 'cwd'
var workingDirectory = options.Cwd;
if (workingDirectory != null)
{
workingDirectory = workingDirectory.Trim();
if (workingDirectory.Length == 0)
{
throw new InvalidDebugeeOptionsException(3003, "Property 'cwd' is empty.");
}
workingDirectory = ConvertClientPathToDebugger(workingDirectory);
if (!Directory.Exists(workingDirectory))
{
throw new InvalidDebugeeOptionsException(3004, $"Working directory '{workingDirectory}' does not exist.");
}
}
else
{
workingDirectory = Path.GetDirectoryName(options.Program);
}
// Кодировка DAP
SetEncoding(options.OutputEncoding);
WorkingDirectory = workingDirectory;
Log.Information("Working directory for debuggee is {WorkingDirectory}", WorkingDirectory);
var programPath = Path.Combine(workingDirectory ?? Directory.GetCurrentDirectory(), options.Program);
if (!File.Exists(programPath))
{
throw new InvalidDebugeeOptionsException(1002, $"Script '{programPath}' does not exist.");
}
// validate argument 'runtimeExecutable'
var runtimeExecutable = options.RuntimeExecutable;
if (runtimeExecutable != null)
{
runtimeExecutable = runtimeExecutable.Trim();
if (runtimeExecutable.Length == 0)
{
throw new InvalidDebugeeOptionsException(3005, "Property 'runtimeExecutable' is empty.");
}
runtimeExecutable = ConvertClientPathToDebugger(runtimeExecutable);
if (!File.Exists(runtimeExecutable))
{
throw new InvalidDebugeeOptionsException(3006, $"Runtime executable '{runtimeExecutable}' does not exist.");
}
}
else
{
runtimeExecutable = "oscript.exe";
}
RuntimeExecutable = runtimeExecutable;
RuntimeArguments = Utilities.ConcatArguments(options.RuntimeArgs);
StartupScript = options.Program;
ScriptArguments = Utilities.ConcatArguments(options.Args);
DebugPort = options.DebugPort;
Environment = options.Env;
WaitOnStart = options.WaitOnStart ?? true;
}
protected override Process CreateProcess()
{
var dbgArgs = new List<string>();
if (DebugPort != 0)
{
dbgArgs.Add($"-port={DebugPort}");
}
if (!WaitOnStart)
{
dbgArgs.Add("-noWait");
}
var debugArguments = string.Join(" ", dbgArgs);
var process = new Process();
var psi = process.StartInfo;
psi.FileName = RuntimeExecutable;
psi.UseShellExecute = false;
psi.Arguments = $"{RuntimeArguments} -debug {debugArguments} \"{StartupScript}\" {ScriptArguments}";
psi.WorkingDirectory = WorkingDirectory;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
LoadEnvironment(psi, Environment);
return process;
}
}
}