-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ForwardingAppImplementation.cs
104 lines (85 loc) · 3.17 KB
/
ForwardingAppImplementation.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#if NET
using System.Diagnostics;
namespace Microsoft.DotNet.Cli.Utils
{
/// <summary>
/// A class which encapsulates logic needed to forward arguments from the current process to another process
/// invoked with the dotnet.exe host.
/// </summary>
internal class ForwardingAppImplementation
{
private readonly string _forwardApplicationPath;
private readonly IEnumerable<string> _argsToForward;
private readonly string _depsFile;
private readonly string _runtimeConfig;
private readonly string _additionalProbingPath;
private Dictionary<string, string> _environmentVariables;
private readonly string[] _allArgs;
public ForwardingAppImplementation(
string forwardApplicationPath,
IEnumerable<string> argsToForward,
string depsFile = null,
string runtimeConfig = null,
string additionalProbingPath = null,
Dictionary<string, string> environmentVariables = null)
{
_forwardApplicationPath = forwardApplicationPath;
_argsToForward = argsToForward;
_depsFile = depsFile;
_runtimeConfig = runtimeConfig;
_additionalProbingPath = additionalProbingPath;
_environmentVariables = environmentVariables ?? new Dictionary<string, string>();
var allArgs = new List<string>();
allArgs.Add("exec");
if (_depsFile != null)
{
allArgs.Add("--depsfile");
allArgs.Add(_depsFile);
}
if (_runtimeConfig != null)
{
allArgs.Add("--runtimeconfig");
allArgs.Add(_runtimeConfig);
}
if (_additionalProbingPath != null)
{
allArgs.Add("--additionalprobingpath");
allArgs.Add(_additionalProbingPath);
}
allArgs.Add(_forwardApplicationPath);
allArgs.AddRange(_argsToForward);
_allArgs = allArgs.ToArray();
}
public int Execute()
{
return GetProcessStartInfo().Execute();
}
public ProcessStartInfo GetProcessStartInfo()
{
var processInfo = new ProcessStartInfo
{
FileName = GetHostExeName(),
Arguments = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(_allArgs),
UseShellExecute = false
};
foreach (var entry in _environmentVariables)
{
processInfo.Environment[entry.Key] = entry.Value;
}
return processInfo;
}
public ForwardingAppImplementation WithEnvironmentVariable(string name, string value)
{
_environmentVariables.Add(name, value);
return this;
}
private string GetHostExeName()
{
// Should instead make this a full path to dotnet
return Environment.ProcessPath;
}
}
}
#endif