This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Muxer.cs
91 lines (75 loc) · 2.44 KB
/
Muxer.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
using System;
using System.IO;
using System.Reflection;
namespace Microsoft.DotNet.Cli.Utils
{
public class Muxer
{
public static readonly string MuxerName = "dotnet";
private static readonly string s_muxerFileName = MuxerName + Constants.ExeSuffix;
private string _muxerPath;
internal string SharedFxVersion
{
get
{
var depsFile = new FileInfo(GetDataFromAppDomain("FX_DEPS_FILE"));
return depsFile.Directory.Name;
}
}
public string MuxerPath
{
get
{
if (_muxerPath == null)
{
throw new InvalidOperationException(LocalizableStrings.UnableToLocateDotnetMultiplexer);
}
return _muxerPath;
}
}
public Muxer()
{
if (!TryResolveMuxerFromParentDirectories())
{
TryResolverMuxerFromPath();
}
}
public static string GetDataFromAppDomain(string propertyName)
{
var appDomainType = typeof(object).GetTypeInfo().Assembly?.GetType("System.AppDomain");
var currentDomain = appDomainType?.GetProperty("CurrentDomain")?.GetValue(null);
var deps = appDomainType?.GetMethod("GetData")?.Invoke(currentDomain, new[] { propertyName });
return deps as string;
}
private bool TryResolveMuxerFromParentDirectories()
{
var fxDepsFile = GetDataFromAppDomain("FX_DEPS_FILE");
if (string.IsNullOrEmpty(fxDepsFile))
{
return false;
}
var muxerDir = new FileInfo(fxDepsFile).Directory?.Parent?.Parent?.Parent;
if (muxerDir == null)
{
return false;
}
var muxerCandidate = Path.Combine(muxerDir.FullName, s_muxerFileName);
if (!File.Exists(muxerCandidate))
{
return false;
}
_muxerPath = muxerCandidate;
return true;
}
private bool TryResolverMuxerFromPath()
{
var muxerPath = Env.GetCommandPath(MuxerName, Constants.ExeSuffix);
if (muxerPath == null || !File.Exists(muxerPath))
{
return false;
}
_muxerPath = muxerPath;
return true;
}
}
}