-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSdks.cs
140 lines (120 loc) · 3.96 KB
/
Sdks.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.IO;
using Newtonsoft.Json;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
namespace dotnet_manage
{
public static class Sdks
{
#region Sdks
#region Helpers
static List<Sdk> _Items = null;
static List<Sdk> GetItems()
{
var result = new List<Sdk>();
try
{
foreach (var line in Helpers.GetLinesFromProcess("dotnet", "--list-sdks"))
{
var items = line.Split('[');
if (items.Length == 2)
{
result.Add(new Sdk
{
Version = items[0].Trim(),
Directory = System.IO.Path.Combine(items[1].Replace("]", "").Trim(), items[0].Trim())
});
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
return result;
}
#endregion
public static List<Sdk> Items { get => _Items ?? (_Items = GetItems()); }
public static List<string> VersionItems
{
get => (from item in Items select new string(item.Version)).ToList();
}
public static Sdk GetSdkByVersion(string version)
=> Items.FirstOrDefault(item => item.Version == version);
public static Sdk Last => Items.Last();
public static Sdk Current
{
get
{
try
{
if (GlobalJsonExist())
{
var version = GlobalJsonRead();
if (!string.IsNullOrEmpty(version))
{
var currentSdk = Items.FirstOrDefault(sdk => sdk.Version == version);
return new Sdk
{
Version = version,
Directory = currentSdk == null ? "No sdk directory found" : currentSdk.Directory
};
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
return Last;
}
}
#endregion
#region Global.Json
public static string GlobalJsonFilename = "global.json";
public static bool GlobalJsonExist() => File.Exists(GlobalJsonFilename);
public static void GlobalJsonRemove()
{
if (GlobalJsonExist())
File.Delete(GlobalJsonFilename);
}
public static bool GlobalJsonWrite(string version)
{
try
{
File.WriteAllText(GlobalJsonFilename,
JsonConvert.SerializeObject(
new GlobalJson(version), Formatting.Indented));
return true;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return false;
}
}
public static string GlobalJsonRead()
{
try
{
return GlobalJsonExist() ? JsonConvert.DeserializeObject<GlobalJson>(File.ReadAllText(GlobalJsonFilename)).Sdk.Version : null;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return null;
}
}
#endregion
#region Actions
public static void SdkDirectoryOpenWithBrowserWindow(Sdk sdk)
=> Helpers.DirectoryOpenWithBrowserWindow(sdk.Directory);
public static void SdkDirectoryOpenWithShellWindow(Sdk sdk)
=> Helpers.DirectoryOpenWithShellWindow(sdk.Directory);
#endregion
}
}