1+ using System . IO ;
2+ using System . Text ;
3+ using CommandForgeGenerator . Generator ;
4+ using CommandForgeGenerator . Generator . Semantic ;
15using UnityEditor ;
26using UnityEngine ;
37
4-
58public class CommandTemplateGenerator : EditorWindow
69{
10+ [ SerializeField ] private string commandsYamlPath = string . Empty ;
11+ [ SerializeField ] private string outputDirectory = string . Empty ;
12+
713 [ MenuItem ( "Windows/CommandTemplateGenerator" ) ]
814 private static void ShowWindow ( )
915 {
@@ -12,8 +18,57 @@ private static void ShowWindow()
1218 window . Show ( ) ;
1319 }
1420
15- private void CreateGUI ( )
21+ private void OnGUI ( )
22+ {
23+ EditorGUILayout . LabelField ( "Commands.yaml" , EditorStyles . boldLabel ) ;
24+ commandsYamlPath = EditorGUILayout . TextField ( "Path" , commandsYamlPath ) ;
25+
26+ EditorGUILayout . Space ( ) ;
27+
28+ EditorGUILayout . LabelField ( "Output Directory" , EditorStyles . boldLabel ) ;
29+ outputDirectory = EditorGUILayout . TextField ( "Path" , outputDirectory ) ;
30+
31+ EditorGUILayout . Space ( ) ;
32+
33+ if ( GUILayout . Button ( "Generate" ) )
34+ {
35+ Generate ( ) ;
36+ }
37+ }
38+
39+ private void Generate ( )
1640 {
17-
41+ if ( ! File . Exists ( commandsYamlPath ) )
42+ {
43+ Debug . LogError ( $ "commands.yaml not found: { commandsYamlPath } ") ;
44+ return ;
45+ }
46+
47+ if ( ! Directory . Exists ( outputDirectory ) )
48+ {
49+ Directory . CreateDirectory ( outputDirectory ) ;
50+ }
51+
52+ var yamlText = File . ReadAllText ( commandsYamlPath ) ;
53+ var semantics = CommandSemanticsLoader . GetCommandSemantics ( yamlText ) ;
54+
55+ foreach ( var command in semantics . Commands )
56+ {
57+ var className = command . ClassName ;
58+ var code = $$ """
59+ namespace CommandForgeGenerator.Command
60+ {
61+ public partial class {{ className }} : ICommandForgeCommand
62+ {
63+ }
64+ }
65+ """ ;
66+
67+ var filePath = Path . Combine ( outputDirectory , className + ".cs" ) ;
68+ File . WriteAllText ( filePath , code , Encoding . UTF8 ) ;
69+ }
70+
71+ AssetDatabase . Refresh ( ) ;
72+ Debug . Log ( "Command templates generated." ) ;
1873 }
1974}
0 commit comments