|
1 | 1 | using System; |
| 2 | +using System.IO; |
2 | 3 | using System.Linq; |
3 | 4 | using Entitas.CodeGeneration.CodeGenerator; |
4 | 5 | using Entitas.Utils; |
@@ -74,5 +75,72 @@ static void checkCanGenerate() { |
74 | 75 | Debug.Log("There are compile errors! Generated code will be based on last compiled executable."); |
75 | 76 | } |
76 | 77 | } |
| 78 | + |
| 79 | + [MenuItem("Tools/Entitas/Generate with CLI %&g", false, 101)] |
| 80 | + public static void GenerateWithCLI() { |
| 81 | + Debug.Log("Generating..."); |
| 82 | + |
| 83 | + Preferences.sharedInstance.Refresh(); |
| 84 | + var config = new CodeGeneratorConfig(); |
| 85 | + config.Configure(Preferences.sharedInstance); |
| 86 | + |
| 87 | + var projectRoot = Application.dataPath.Substring(0, Application.dataPath.Length - "/Assets".Length); |
| 88 | + var cli = Path.Combine(projectRoot, config.cli); |
| 89 | + if (!File.Exists(cli)) { |
| 90 | + Debug.Log(cli + " does not exist!"); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + if (Application.platform == RuntimePlatform.WindowsEditor) { |
| 95 | + runCommand(cli, "gen", projectRoot); |
| 96 | + } else { |
| 97 | + runCommand(config.mono, cli + " gen", projectRoot); |
| 98 | + } |
| 99 | + |
| 100 | + Debug.Log("Generating done."); |
| 101 | + |
| 102 | + AssetDatabase.Refresh(); |
| 103 | + } |
| 104 | + |
| 105 | + static void runCommand(string fileName, string arguments, string workingDirectory) { |
| 106 | + var startInfo = createStartInfo(workingDirectory); |
| 107 | + startInfo.FileName = fileName; |
| 108 | + startInfo.Arguments = arguments; |
| 109 | + startProcess(startInfo); |
| 110 | + } |
| 111 | + |
| 112 | + static System.Diagnostics.ProcessStartInfo createStartInfo(string workingDirectory) { |
| 113 | + var startInfo = new System.Diagnostics.ProcessStartInfo(); |
| 114 | + startInfo.WorkingDirectory = workingDirectory; |
| 115 | + startInfo.RedirectStandardOutput = true; |
| 116 | + startInfo.RedirectStandardError = true; |
| 117 | + startInfo.UseShellExecute = false; |
| 118 | + startInfo.CreateNoWindow = true; |
| 119 | + return startInfo; |
| 120 | + } |
| 121 | + |
| 122 | + static void startProcess(System.Diagnostics.ProcessStartInfo startInfo) { |
| 123 | + var process = new System.Diagnostics.Process(); |
| 124 | + process.StartInfo = startInfo; |
| 125 | + process.OutputDataReceived += OnDataReceivedEventHandler; |
| 126 | + process.ErrorDataReceived += OnErrorReceivedEventHandler; |
| 127 | + process.Start(); |
| 128 | + process.BeginErrorReadLine(); |
| 129 | + process.BeginOutputReadLine(); |
| 130 | + process.WaitForExit(); |
| 131 | + process.Close(); |
| 132 | + } |
| 133 | + |
| 134 | + static void OnDataReceivedEventHandler(object sender, System.Diagnostics.DataReceivedEventArgs e) { |
| 135 | + if (e.Data != null) { |
| 136 | + UnityEngine.Debug.Log(e.Data); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + static void OnErrorReceivedEventHandler(object sender, System.Diagnostics.DataReceivedEventArgs e) { |
| 141 | + if (e.Data != null) { |
| 142 | + UnityEngine.Debug.Log(e.Data); |
| 143 | + } |
| 144 | + } |
77 | 145 | } |
78 | 146 | } |
0 commit comments