-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
356 lines (306 loc) · 12.5 KB
/
Program.cs
File metadata and controls
356 lines (306 loc) · 12.5 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
using Ideal.Existence;
using System.Text.Json;
namespace Ideal
{
internal static class Program
{
private sealed record Options
{
public string ExistenceId { get; init; } = "033";
public int Cycles { get; init; } = 20;
public string Format { get; init; } = "text";
public bool IncludeEngineLogs { get; init; } = false;
public bool ListInstructionalAssets { get; init; } = false;
public string ExportInstructionalAssetsDir { get; init; } = string.Empty;
public bool PrintInstructionalReadme { get; init; } = false;
}
private sealed class StepOutput
{
public int Step { get; init; }
public string Mood { get; init; } = string.Empty;
public string[] EngineLogs { get; init; } = Array.Empty<string>();
}
private static int Main(string[] args)
{
if (!TryParseArgs(args, out Options options, out string error, out bool showHelp))
{
if (!showHelp)
{
Console.Error.WriteLine(error);
Console.Error.WriteLine();
PrintUsage();
return 1;
}
PrintUsage();
return 0;
}
IExistence existence = CreateExistence(options);
string format = options.Format.ToLowerInvariant();
if (options.ListInstructionalAssets)
{
return ListInstructionalAssets();
}
if (options.PrintInstructionalReadme)
{
return PrintInstructionalReadme();
}
if (!string.IsNullOrWhiteSpace(options.ExportInstructionalAssetsDir))
{
return ExportInstructionalAssets(options.ExportInstructionalAssetsDir);
}
for (int i = 0; i < options.Cycles; i++)
{
(string mood, string[] engineLogs) = RunStep(existence);
if (format == "json")
{
StepOutput output = new StepOutput
{
Step = i,
Mood = mood,
EngineLogs = options.IncludeEngineLogs ? engineLogs : Array.Empty<string>()
};
Console.WriteLine(JsonSerializer.Serialize(output, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
}));
continue;
}
foreach (string line in engineLogs)
Console.WriteLine(line);
Console.WriteLine(i + ": " + mood);
}
return 0;
}
private static (string Mood, string[] EngineLogs) RunStep(IExistence existence)
{
using StringWriter capture = new StringWriter();
TextWriter originalOut = Console.Out;
Console.SetOut(capture);
string mood;
try
{
mood = existence.Step();
}
finally
{
Console.SetOut(originalOut);
}
string logs = capture.ToString();
if (string.IsNullOrWhiteSpace(logs))
return (mood, Array.Empty<string>());
string[] lines = logs
.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
return (mood, lines);
}
private static bool TryParseArgs(string[] args, out Options options, out string error, out bool showHelp)
{
options = new Options();
error = string.Empty;
showHelp = false;
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
switch (arg)
{
case "-h":
case "--help":
showHelp = true;
return false;
case "-e":
case "--existence":
if (!TryReadNext(args, ref i, out string existenceId))
{
error = "Missing value for --existence.";
return false;
}
options = options with { ExistenceId = NormalizeExistence(existenceId) };
if (!IsValidExistence(options.ExistenceId))
{
error = "Invalid --existence value. Use one of: 010, 020, 030, 031, 032, 033, 040.";
return false;
}
break;
case "-c":
case "--cycles":
if (!TryReadNext(args, ref i, out string cyclesRaw))
{
error = "Missing value for --cycles.";
return false;
}
if (!int.TryParse(cyclesRaw, out int cycles) || cycles < 1)
{
error = "Invalid --cycles value. It must be an integer >= 1.";
return false;
}
options = options with { Cycles = cycles };
break;
case "-f":
case "--format":
if (!TryReadNext(args, ref i, out string formatRaw))
{
error = "Missing value for --format.";
return false;
}
string normalizedFormat = formatRaw.ToLowerInvariant();
if (normalizedFormat != "text" && normalizedFormat != "json")
{
error = "Invalid --format value. Use: text or json.";
return false;
}
options = options with { Format = normalizedFormat };
break;
case "--include-engine-logs":
options = options with { IncludeEngineLogs = true };
break;
case "--list-instructional-assets":
options = options with { ListInstructionalAssets = true };
break;
case "--print-instructional-readme":
options = options with { PrintInstructionalReadme = true };
break;
case "--export-instructional-assets":
if (!TryReadNext(args, ref i, out string exportDir))
{
error = "Missing value for --export-instructional-assets.";
return false;
}
options = options with { ExportInstructionalAssetsDir = exportDir };
break;
default:
error = $"Unknown argument: {arg}";
return false;
}
}
return true;
}
private static bool TryReadNext(string[] args, ref int index, out string value)
{
if (index + 1 >= args.Length)
{
value = string.Empty;
return false;
}
index++;
value = args[index];
return true;
}
private static string NormalizeExistence(string value)
{
string cleaned = value.Trim();
if (cleaned.StartsWith("existence", StringComparison.OrdinalIgnoreCase))
cleaned = cleaned["existence".Length..];
return cleaned.PadLeft(3, '0');
}
private static bool IsValidExistence(string id)
{
return id == "010" ||
id == "020" ||
id == "030" ||
id == "031" ||
id == "032" ||
id == "033" ||
id == "040";
}
private static IExistence CreateExistence(Options options)
{
return options.ExistenceId switch
{
"010" => new Existence010(),
"020" => new Existence020(),
"030" => new Existence030(),
"031" => new Existence031(),
"032" => new Existence032(),
"033" => new Existence033(),
_ => new Existence040()
};
}
private static int ListInstructionalAssets()
{
if (!TryGetInstructionalAssetFiles(out string[] files, out string error))
{
Console.Error.WriteLine(error);
return 1;
}
foreach (string file in files)
Console.WriteLine(file);
return 0;
}
private static int ExportInstructionalAssets(string outputDir)
{
if (!TryGetInstructionalAssetFiles(out string[] files, out string error))
{
Console.Error.WriteLine(error);
return 1;
}
Directory.CreateDirectory(outputDir);
foreach (string sourceFile in files)
{
string destinationFile = Path.Combine(outputDir, Path.GetFileName(sourceFile));
File.Copy(sourceFile, destinationFile, overwrite: true);
Console.WriteLine(destinationFile);
}
return 0;
}
private static int PrintInstructionalReadme()
{
if (!TryGetInstructionalAssetFiles(out string[] files, out string error))
{
Console.Error.WriteLine(error);
return 1;
}
string readmePath = files.FirstOrDefault(path =>
string.Equals(Path.GetFileName(path), "README-04-self-programming.md", StringComparison.OrdinalIgnoreCase)) ?? string.Empty;
if (string.IsNullOrWhiteSpace(readmePath))
{
Console.Error.WriteLine("Instructional README not found. Expected README-04-self-programming.md.");
return 1;
}
Console.Write(File.ReadAllText(readmePath));
return 0;
}
private static bool TryGetInstructionalAssetFiles(out string[] files, out string error)
{
string[] candidates = new[]
{
Path.Combine(AppContext.BaseDirectory, "instructions"),
Path.Combine(Directory.GetCurrentDirectory(), "instructions"),
Path.Combine(Directory.GetCurrentDirectory(), "images")
};
List<string> fileList = new List<string>();
foreach (string candidate in candidates)
{
if (Directory.Exists(candidate))
{
fileList.AddRange(Directory.GetFiles(candidate));
}
}
files = fileList
.Distinct(StringComparer.Ordinal)
.OrderBy(path => path, StringComparer.Ordinal)
.ToArray();
if (files.Length > 0)
{
error = string.Empty;
return true;
}
error = "Instructional assets not found. Expected /app/instructions in the container.";
return false;
}
private static void PrintUsage()
{
Console.WriteLine("Usage:");
Console.WriteLine(" ideal [--existence <id>] [--cycles <n>] [--format <text|json>] [--include-engine-logs]");
Console.WriteLine(" ideal --list-instructional-assets");
Console.WriteLine(" ideal --print-instructional-readme");
Console.WriteLine(" ideal --export-instructional-assets <output-dir>");
Console.WriteLine();
Console.WriteLine("Examples:");
Console.WriteLine(" ideal --existence 033 --cycles 20 --format text");
Console.WriteLine(" ideal --existence 033 --cycles 30 --format json --include-engine-logs");
Console.WriteLine(" ideal --print-instructional-readme");
Console.WriteLine(" ideal --export-instructional-assets /output");
Console.WriteLine();
Console.WriteLine("Valid existence IDs: 010, 020, 030, 031, 032, 033, 040");
}
}
}