-
Notifications
You must be signed in to change notification settings - Fork 1
/
DotNetCommandExecutor.cs
105 lines (92 loc) · 3.72 KB
/
DotNetCommandExecutor.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
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace Compiler
{
public class DotNetCommandExecutor
{
private const string CsProjFilePattern = "*.csproj";
private const string TargetFramework = "net7.0";
public static void DotnetUpdateApi(string folderPath, Config config)
{
var regex = new Regex(@"PackageReference Include=""([^""]*)"" Version=""([^""]*)""");
foreach (var file in Directory.EnumerateFiles(folderPath, CsProjFilePattern, SearchOption.AllDirectories))
{
string fileContent = File.ReadAllText(file);
var matches = regex.Matches(fileContent);
var packages = new HashSet<string>();
foreach (Match match in matches)
{
if (match.Groups.Count > 1)
{
packages.Add(match.Groups[1].Value);
}
}
foreach (var package in packages)
{
// Check if the Silencelog setting is false before logging
if (config.Silencelog != true)
{
Console.WriteLine($"Update {file} package: {package}");
}
RunDotnetAddPackage(file, package, config);
}
}
}
public static void RunDotnetAddPackage(string filePath, string packageName, Config config)
{
var startInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"add \"{filePath}\" package {packageName}",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (var process = Process.Start(startInfo))
{
process!.WaitForExit();
// Check if the Silencelog setting is false before logging
if (config.Silencelog != true)
{
string result = process.StandardOutput.ReadToEnd();
Console.WriteLine(result);
}
}
}
public static void RunDotNetPublish(string folderPath, Config config)
{
try
{
string folderName = new DirectoryInfo(folderPath).Name;
string outputPath = Path.Combine(folderPath, "../compiled", folderName);
var stopwatch = Stopwatch.StartNew();
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"publish -f {TargetFramework} -c Release -o \"{outputPath}\" \"{folderPath}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
using (Process process = Process.Start(psi)!)
{
process!.WaitForExit();
// Check if the Silencelog setting is false before logging
if (config.Silencelog != true)
{
string log = process.StandardOutput.ReadToEnd();
Console.WriteLine(log);
}
}
ArtifactCleaner.CleanupArtifacts(outputPath);
stopwatch.Stop();
Console.WriteLine($"{folderName} completed in {stopwatch.Elapsed.TotalSeconds} seconds.");
}
catch (Exception ex)
{
Console.WriteLine($"Error running dotnet publish: {ex.Message}");
}
}
}
}