diff --git a/UnityEditorExtension/CommandForgeGeneratorUtil/Assets/Scripts/Editor/CommandTemplateGenerator.cs b/UnityEditorExtension/CommandForgeGeneratorUtil/Assets/Scripts/Editor/CommandTemplateGenerator.cs index 38acabd..0749a5c 100644 --- a/UnityEditorExtension/CommandForgeGeneratorUtil/Assets/Scripts/Editor/CommandTemplateGenerator.cs +++ b/UnityEditorExtension/CommandForgeGeneratorUtil/Assets/Scripts/Editor/CommandTemplateGenerator.cs @@ -1,9 +1,15 @@ +using System.IO; +using System.Text; +using CommandForgeGenerator.Generator; +using CommandForgeGenerator.Generator.Semantic; using UnityEditor; using UnityEngine; - public class CommandTemplateGenerator : EditorWindow { + [SerializeField] private string commandsYamlPath = string.Empty; + [SerializeField] private string outputDirectory = string.Empty; + [MenuItem("Windows/CommandTemplateGenerator")] private static void ShowWindow() { @@ -12,8 +18,57 @@ private static void ShowWindow() window.Show(); } - private void CreateGUI() + private void OnGUI() + { + EditorGUILayout.LabelField("Commands.yaml", EditorStyles.boldLabel); + commandsYamlPath = EditorGUILayout.TextField("Path", commandsYamlPath); + + EditorGUILayout.Space(); + + EditorGUILayout.LabelField("Output Directory", EditorStyles.boldLabel); + outputDirectory = EditorGUILayout.TextField("Path", outputDirectory); + + EditorGUILayout.Space(); + + if (GUILayout.Button("Generate")) + { + Generate(); + } + } + + private void Generate() { - + if (!File.Exists(commandsYamlPath)) + { + Debug.LogError($"commands.yaml not found: {commandsYamlPath}"); + return; + } + + if (!Directory.Exists(outputDirectory)) + { + Directory.CreateDirectory(outputDirectory); + } + + var yamlText = File.ReadAllText(commandsYamlPath); + var semantics = CommandSemanticsLoader.GetCommandSemantics(yamlText); + + foreach (var command in semantics.Commands) + { + var className = command.ClassName; + var code = $$""" + namespace CommandForgeGenerator.Command + { + public partial class {{className}} : ICommandForgeCommand + { + } + } + """; + + var filePath = Path.Combine(outputDirectory, className + ".cs"); + File.WriteAllText(filePath, code, Encoding.UTF8); + } + + AssetDatabase.Refresh(); + Debug.Log("Command templates generated."); } } \ No newline at end of file