-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
107 lines (84 loc) · 2.84 KB
/
Copy pathProgram.cs
File metadata and controls
107 lines (84 loc) · 2.84 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
using IndentWriter;
using System.Text;
var target = new StringBuilder();
var writer = new IndentStackWriter(target);
var someNamespace = new SomeNamespace() { DisplayString = "Example" };
var someType = new SomeTypeSnapshot()
{
ContainingNamespace = true ? someNamespace : null,
Name = "ExampleClass",
Properties = [
new SomePropertySnapshot () { Name = "Integer1", TypeDisplayString = "int" },
new SomePropertySnapshot () { Name = "String1", TypeDisplayString = "string" },
new SomePropertySnapshot () { Name = "DateTime", TypeDisplayString = nameof(DateTime) },
new SomePropertySnapshot () { Name = "Smth", TypeDisplayString = "object" },
]
};
GenerateConsolePrinterExtension(writer, someType);
var code = target.ToString();
Console.WriteLine(code);
Console.Read();
IndentedInterpolatedStringHandler GenerateConsolePrinterExtension(IndentStackWriter _, SomeTypeSnapshot type)
{
if(type.ContainingNamespace != null)
{
return writer[$$"""
using System;
namespace {{type.ContainingNamespace.DisplayString}}
{
{{_.Scope[AppendExtensionClass(_, type)]}}
}
"""];
}
return writer[$$"""
using System;
{{_.Scope[AppendExtensionClass(_, type)]}}
"""
];
}
IndentedInterpolatedStringHandler AppendExtensionClass(IndentStackWriter _, SomeTypeSnapshot type)
{
return _[$$"""
internal static partial class {{type.Name}}Extensions
{
{{_[AppendConsolePrinter(_, type)]}}
}
"""];
}
IndentedInterpolatedStringHandler AppendConsolePrinter(IndentStackWriter _, SomeTypeSnapshot type)
{
var properties = type.Properties.Take(type.Properties.Count - 1);
var lastProperty = type.Properties.Last();
return _.Scope[$$"""
public static void PrintToConsole(this {{type.DisplayString}} target)
{
Console.WriteLine("class {{type.DisplayString}}");
Console.WriteLine("{");
{{_.Scope.ForEach(
properties,
(_, prop) =>_[$$"""Console.WriteLine(" \"{{prop.Name}}\" : \"{0}\",", target.{{prop.Name}});"""],
joinBy: "\n")
}}
{{_[$$"""Console.WriteLine(" \"{{lastProperty.Name}}\" : \"{0}\"", target.{{lastProperty.Name}});"""]}}
Console.WriteLine("}");
}
"""];
}
file sealed class SomeTypeSnapshot
{
public List<SomePropertySnapshot> Properties = [];
public SomeNamespace? ContainingNamespace;
public string Name { get; set; }
public string DisplayString => ContainingNamespace != null
? ContainingNamespace.DisplayString + "." + Name
: Name;
}
file sealed class SomePropertySnapshot
{
public string Name { get; set; }
public string TypeDisplayString { get; set; } = "int";
}
file sealed class SomeNamespace
{
public string DisplayString { get; set; } = nameof(System);
}