-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
173 lines (156 loc) · 5.88 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
// Filename: Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
public static class Program
{
[STAThread]
public static void Main(string[] args)
{
if (args.Length > 0)
{
HandleCommandLineArgs(args);
}
else
{
RunInGuiMode();
}
}
private static void RunInGuiMode()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
private static void HandleCommandLineArgs(string[] args)
{
string sourceFolder = args[0];
string? outputFile = null;
bool changeOutputFile = false;
bool quietMode = false;
var additionalFolders = new List<FolderStatus>();
for (int i = 1; i < args.Length; i++)
{
switch (args[i])
{
case "?":
ShowHelp();
return;
case var o when o.StartsWith("-o:"):
outputFile = o.Substring(3);
break;
case var oc when oc.StartsWith("-oc:"):
outputFile = oc.Substring(4);
changeOutputFile = true;
break;
case var a when a.StartsWith("-a:"):
additionalFolders.Add(new FolderStatus { FolderPath = a.Substring(3), Included = true });
break;
case var ac when ac.StartsWith("-ac:"):
additionalFolders.Add(new FolderStatus { FolderPath = ac.Substring(4), Included = true });
break;
case "-q":
quietMode = true;
break;
}
}
if (quietMode)
{
// Execute without showing any windows
RunAggregation(sourceFolder, outputFile, changeOutputFile, additionalFolders);
}
else
{
var selection = SelectionManager.GetFolderSelection(sourceFolder);
if (outputFile != null)
{
selection.OutputFile = outputFile;
if (changeOutputFile)
{
SelectionManager.UpdateFolderSelection(selection);
}
}
if (additionalFolders.Any())
{
foreach (var folder in additionalFolders)
{
if (!Directory.Exists(Path.Combine(sourceFolder, folder.FolderPath)) && !File.Exists(Path.Combine(sourceFolder, folder.FolderPath)))
{
Console.WriteLine(folder.FolderPath.EndsWith(".ext") ? "2" : "1");
return;
}
selection.SelectedFolders.Add(folder);
}
if (additionalFolders.Any(f => f.FolderPath.StartsWith("-ac:")))
{
SelectionManager.UpdateFolderSelection(selection);
}
}
try
{
FileAggregator.AggregateFiles(sourceFolder, selection.SelectedFolders.Where(f => f.Included).Select(f => Path.Combine(sourceFolder, f.FolderPath)), selection.OutputFile);
Console.WriteLine("0");
}
catch
{
Console.WriteLine("3");
}
}
}
private static void RunAggregation(string sourceFolder, string? outputFile, bool changeOutputFile, List<FolderStatus> additionalFolders)
{
var selection = SelectionManager.GetFolderSelection(sourceFolder);
if (outputFile != null)
{
selection.OutputFile = outputFile;
if (changeOutputFile)
{
SelectionManager.UpdateFolderSelection(selection);
}
}
if (additionalFolders.Any())
{
foreach (var folder in additionalFolders)
{
if (!Directory.Exists(Path.Combine(sourceFolder, folder.FolderPath)) && !File.Exists(Path.Combine(sourceFolder, folder.FolderPath)))
{
Console.WriteLine(folder.FolderPath.EndsWith(".ext") ? "2" : "1");
return;
}
selection.SelectedFolders.Add(folder);
}
if (additionalFolders.Any(f => f.FolderPath.StartsWith("-ac:")))
{
SelectionManager.UpdateFolderSelection(selection);
}
}
try
{
FileAggregator.AggregateFiles(sourceFolder, selection.SelectedFolders.Where(f => f.Included).Select(f => Path.Combine(sourceFolder, f.FolderPath)), selection.OutputFile);
Console.WriteLine("0");
}
catch
{
Console.WriteLine("3");
}
}
private static void ShowHelp()
{
Console.WriteLine("Usage: CodeAggregator [source_folder] [options]");
Console.WriteLine("Options:");
Console.WriteLine(" -o:<output_file> Specify the output file path.");
Console.WriteLine(" -oc:<output_file> Specify and change the output file path in the JSON.");
Console.WriteLine(" -a:<folder_or_file> Add a folder or file for this aggregation.");
Console.WriteLine(" -ac:<folder_or_file> Add a folder or file for this aggregation and save to JSON.");
Console.WriteLine(" -q Run in quiet mode (no output).");
Console.WriteLine(" ? Show this help message.");
Console.WriteLine("Error Codes:");
Console.WriteLine(" 0 - Success.");
Console.WriteLine(" 1 - Folder not found.");
Console.WriteLine(" 2 - File not found.");
Console.WriteLine(" 3 - Error outputting to output file.");
Console.WriteLine(" 4 - Source folder not found.");
}
}