discover netsed vsconfig and parse · microsoft/VSConfigFinder@14cf845 (original) (raw)
`@@ -6,20 +6,13 @@
`
6
6
`namespace VSConfigFinder
`
7
7
`{
`
8
8
`using System.Diagnostics.CodeAnalysis;
`
``
9
`+
using System.Text;
`
``
10
`+
using System.Text.Json;
`
9
11
``
10
``
`-
internal class Utilities
`
``
12
`+
public class Utilities
`
11
13
`{
`
12
``
`-
private static readonly string[] AcceptedOutputOptions = new[] { "config", "commandline" };
`
13
``
-
14
``
`-
public static void ValidateOutputParameter([NotNull] string s, string paramName)
`
15
``
`-
{
`
16
``
`-
ValidateIsNotNullOrEmpty(s, paramName);
`
17
``
-
18
``
`-
if (!AcceptedOutputOptions.Contains(s))
`
19
``
`-
{
`
20
``
`-
throw new ArgumentException($"The --output parameter accepts only two options: {string.Join(",", AcceptedOutputOptions)}.");
`
21
``
`-
}
`
22
``
`-
}
`
``
14
`+
private static readonly string ConfigExtension = ".vsconfig";
`
``
15
`+
private static readonly string Add = "--add";
`
23
16
``
24
17
`public static void ValidateIsNotNullOrEmpty([NotNull] string s, string paramName)
`
25
18
`{
`
`@@ -42,5 +35,64 @@ public static void IsNotEmpty([NotNull] string s, string paramName)
`
42
35
`throw new ArgumentException("The string is empty.", paramName);
`
43
36
`}
`
44
37
`}
`
``
38
+
``
39
`+
public static void CreateOutput(VSConfig finalConfig, CommandLineOptions options)
`
``
40
`+
{
`
``
41
`+
if (options.CreateFile)
`
``
42
`+
{
`
``
43
`+
// Create a file
`
``
44
`+
var serializerOptions = new JsonSerializerOptions { WriteIndented = true };
`
``
45
`+
var jsonString = JsonSerializer.Serialize(finalConfig, serializerOptions);
`
``
46
`+
var outputPath = Path.Combine(options.ConfigOutputPath!, ConfigExtension);
`
``
47
+
``
48
`+
File.WriteAllText(outputPath, jsonString);
`
``
49
`+
Console.WriteLine($"Successfully created the final .vsconfig at {outputPath}");
`
``
50
`+
}
`
``
51
`+
else
`
``
52
`+
{
`
``
53
`+
// output to a command line
`
``
54
`+
var output = CreateCommandLineOutput(finalConfig);
`
``
55
`+
Console.WriteLine(output);
`
``
56
`+
}
`
``
57
`+
}
`
``
58
+
``
59
`+
private static string CreateCommandLineOutput(VSConfig finalConfig)
`
``
60
`+
{
`
``
61
`+
var output = new StringBuilder(Add + " ");
`
``
62
+
``
63
`+
foreach (var component in finalConfig.Components)
`
``
64
`+
{
`
``
65
`+
if (!string.IsNullOrEmpty(component))
`
``
66
`+
{
`
``
67
`+
output.AppendFormat("{0} ", component);
`
``
68
`+
}
`
``
69
`+
}
`
``
70
+
``
71
`+
return output.ToString();
`
``
72
`+
}
`
``
73
+
``
74
`+
public static string[] ReadComponents(CommandLineOptions options)
`
``
75
`+
{
`
``
76
`+
var fileSystem = new FileSystem();
`
``
77
`+
var pathsToVsConfigs = fileSystem.GetFileSystemEntries(options.FolderPath, ConfigExtension, true);
`
``
78
+
``
79
`+
HashSet componentsSet = new HashSet();
`
``
80
`+
var serializerOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
`
``
81
+
``
82
`+
foreach (var path in pathsToVsConfigs)
`
``
83
`+
{
`
``
84
`+
var stream = fileSystem.OpenFile(path);
`
``
85
`+
var components = JsonSerializer.Deserialize(stream, serializerOptions).Components;
`
``
86
+
``
87
`+
if (components != null)
`
``
88
`+
{
`
``
89
`+
componentsSet.UnionWith(components);
`
``
90
`+
}
`
``
91
+
``
92
`+
stream.Close();
`
``
93
`+
};
`
``
94
+
``
95
`+
return componentsSet.ToArray();
`
``
96
`+
}
`
45
97
`}
`
46
``
`-
}
`
``
98
`+
}
`