-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
174 lines (146 loc) · 5.15 KB
/
Program.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
162
163
164
165
166
167
168
169
170
171
172
173
174
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using SC_NewUniversalUpload.Utilities;
namespace SC_NewUniversalUpload;
internal partial class Program
{
public static readonly ConfigWrapper Config = ConfigWrapper.Read(Path.Join(typeof(Program).Assembly.Location.Remove(typeof(Program).Assembly.Location.LastIndexOf('\\')), "config.json"));
public static ArgumentParser Arguments;
private static void Main(string[] args)
{
#if DEBUG
const string releaseType = "DEBUG";
#else
const string releaseType = "RELEASE";
#endif
Console.WriteLine($"NewUniversalUpload - [{releaseType}]\nby Aristeas\n======================");
if (args.Length == 0)
{
Console.WriteLine("No arguments were provided!");
return;
}
Arguments = new ArgumentParser(args);
var thisBranch = File.ReadAllText(
Path.Join(Arguments["--repo"], @".git\HEAD")
).Trim();
thisBranch = thisBranch[(thisBranch.LastIndexOf('/') + 1)..];
switch (args[0])
{
case "deletebranch":
Console.WriteLine("DeleteBranch invoked.");
new Program(args[1]).DeleteBranch();
break;
case "uploadall":
Console.WriteLine("UploadAll invoked.");
new Program(thisBranch).ForceUploadAll();
break;
case "build":
Console.WriteLine("Build invoked.");
new Program(thisBranch).BuildMods(Arguments["--changes"].Split(','));
break;
default:
Console.WriteLine("Upload invoked.");
new Program(thisBranch).LocateAndUploadMods(Arguments["--changes"].Split(','), Arguments["--changelog"] ?? "No changelog specified.");
break;
}
}
public string Branch;
public Program(string branch)
{
Console.WriteLine("Current branch: " + branch);
Branch = branch;
}
#region ForceUploadAll
public void ForceUploadAll()
{
foreach (var mod in LocateAllMods(Arguments["--repo"]))
{
UploadMod(mod, "Force-uploaded.");
}
}
#endregion
#region Static Methods
/// <summary>
/// Recursively locates all folders containing a *.sbmi file.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static string[] LocateAllMods(string path)
{
var modInfos = LocateAllModInfos(path);
var modFolders = new HashSet<string>();
foreach (var modInfo in modInfos)
modFolders.Add(modInfo.Substring(0, modInfo.LastIndexOf(@"\", StringComparison.Ordinal)));
return modFolders.ToArray();
}
/// <summary>
/// Recursively locates all *.sbmi files within a directory.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static string[] LocateAllModInfos(string path)
{
var allSbmis = Directory.GetFiles(path, "*.sbmi").ToList();
foreach (var directory in Directory.GetDirectories(path))
allSbmis.AddRange(LocateAllModInfos(directory));
return allSbmis.ToArray();
}
/// <summary>
/// Triggers a process, and locks the current thread until it is completed.
/// </summary>
/// <param name="args">Space-seperated arguments.</param>
/// <param name="stdout">The console output from the process.</param>
private static void RunCmd(string executablePath, string args, out string stdout)
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = executablePath,
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
RedirectStandardError = true
}
};
process.Start();
var stdoutBuilder = new StringBuilder();
while (!process.HasExited)
{
if (!process.StandardOutput.EndOfStream)
{
var line = process.StandardOutput.ReadToEnd();
#if (DEBUG)
Console.Write(line);
#endif
stdoutBuilder.Append(line);
}
if (!process.StandardError.EndOfStream)
{
var line = process.StandardError.ReadToEnd();
Console.Error.Write(line);
}
}
if (!process.StandardOutput.EndOfStream)
{
var line = process.StandardOutput.ReadToEnd();
#if (DEBUG)
Console.Write(line);
#endif
stdoutBuilder.Append(line);
}
if (!process.StandardError.EndOfStream)
{
var line = process.StandardError.ReadToEnd();
Console.Error.Write(line);
}
stdout = stdoutBuilder.ToString();
}
private static string RetrieveModId(string filePath)
{
return Regex.Match(File.ReadAllText(filePath), @"\d*(?=<\/Id>)").Value;
}
#endregion
}