-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.cs
executable file
·161 lines (149 loc) · 4.13 KB
/
Utilities.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
using System;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Reflection;
namespace VSCodeDebug
{
public class Utilities
{
private const string OSASCRIPT = "/usr/bin/osascript"; // osascript is the AppleScript interpreter on OS X
private const string LINUX_TERM = "/usr/bin/gnome-terminal"; //private const string LINUX_TERM = "/usr/bin/x-terminal-emulator";
private const string OSX_BIN_DIR = "/usr/local/bin";
private static readonly Regex VARIABLE = new Regex(@"\{(\w+)\}");
/*
* Is this Windows?
*/
public static bool IsWindows()
{
return true;
/*
PlatformID pid = Environment.OSVersion.Platform;
return ! (pid == PlatformID.Unix || pid == PlatformID.MacOSX);
*/
}
/*
* Is this OS X?
*/
public static bool IsOSX()
{
return File.Exists(OSASCRIPT); // mono has no better way to determine whether this is OS X
}
/*
* Is this Linux?
*/
public static bool IsLinux()
{
return File.Exists(LINUX_TERM); // mono has no better way to determine whether this is Linux
}
/*
* On OS X make sure that /usr/local/bin is on the PATH
*/
public static void FixPathOnOSX()
{
if (Utilities.IsOSX()) {
var path = System.Environment.GetEnvironmentVariable("PATH");
if (!path.Split(':').Contains(OSX_BIN_DIR)) {
path += ":" + OSX_BIN_DIR;
System.Environment.SetEnvironmentVariable("PATH", path);
}
}
}
/*
* Resolve hostname, dotted-quad notation for IPv4, or colon-hexadecimal notation for IPv6 to IPAddress.
* Returns null on failure.
*/
/* public static IPAddress ResolveIPAddress(string addressString)
{
try {
IPAddress ipaddress = null;
if (IPAddress.TryParse(addressString, out ipaddress)) {
return ipaddress;
}
#if DNXCORE50
IPHostEntry entry = Dns.GetHostEntryAsync(addressString).Result;
#else
IPHostEntry entry = Dns.GetHostEntry(addressString);
#endif
if (entry != null && entry.AddressList != null && entry.AddressList.Length > 0) {
if (entry.AddressList.Length == 1) {
return entry.AddressList[0];
}
foreach (IPAddress address in entry.AddressList) {
if (address.AddressFamily == AddressFamily.InterNetwork) {
return address;
}
}
}
}
catch (Exception) {
// fall through
}
return null;
}*/
/*
* Find a free socket port.
*/
/*
public static int FindFreePort(int fallback)
{
TcpListener l = null;
try {
l = new TcpListener(IPAddress.Loopback, 0);
l.Start();
return ((IPEndPoint)l.LocalEndpoint).Port;
}
catch (Exception) {
// ignore
}
finally {
l.Stop();
}
return fallback;
}
*/
public static string ExpandVariables(string format, dynamic variables, bool underscoredOnly = true)
{
if (variables == null) {
variables = new { };
}
Type type = variables.GetType();
return VARIABLE.Replace(format, match => {
string name = match.Groups[1].Value;
if (!underscoredOnly || name.StartsWith("_")) {
PropertyInfo property = type.GetProperty(name);
if (property != null) {
object value = property.GetValue(variables, null);
return value.ToString();
}
return '{' + name + ": not found}";
}
return match.Groups[0].Value;
});
}
/**
* converts the given absPath into a path that is relative to the given dirPath.
*/
public static string MakeRelativePath(string dirPath, string absPath)
{
if (!dirPath.EndsWith("/")) {
dirPath += "/";
}
if (absPath.StartsWith(dirPath)) {
return absPath.Replace(dirPath, "");
}
return absPath;
/*
Uri uri1 = new Uri(path);
Uri uri2 = new Uri(dir_path);
return uri2.MakeRelativeUri(uri1).ToString();
*/
}
}
}