PackageAutoGenEditorWindow.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. © Siemens AG, 2019
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. <http://www.apache.org/licenses/LICENSE-2.0>.
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. using UnityEditor;
  17. using UnityEngine;
  18. namespace RosSharp.RosBridgeClient.MessageGeneration
  19. {
  20. public abstract class PackageAutoGenEditorWindow : EditorWindow
  21. {
  22. [SerializeField]
  23. private static string lastPackageDirectory = string.Empty;
  24. [SerializeField]
  25. private static string lastOutputDirectory = string.Empty;
  26. private string inPkgPath = "";
  27. private string rosPackageName = "";
  28. private string outPkgPath = Path.Combine(System.Environment.CurrentDirectory, "Assets", "RosSharpMessages");
  29. protected abstract string GenerationType { get; }
  30. protected abstract string FileExtension { get; }
  31. protected virtual void OnGUI()
  32. {
  33. GUILayout.Label("Package " + GenerationType + " auto generation", EditorStyles.boldLabel);
  34. EditorGUILayout.BeginHorizontal();
  35. inPkgPath = EditorGUILayout.TextField("Input Package Path", inPkgPath);
  36. if (GUILayout.Button("Browse Package...", GUILayout.Width(150)))
  37. {
  38. inPkgPath = EditorUtility.OpenFolderPanel("Select Package...", lastPackageDirectory, "");
  39. if (!inPkgPath.Equals(""))
  40. {
  41. rosPackageName = inPkgPath.Split('/').Last();
  42. }
  43. }
  44. EditorGUILayout.EndHorizontal();
  45. rosPackageName = EditorGUILayout.TextField("ROS Package Name:", rosPackageName);
  46. EditorGUILayout.BeginHorizontal();
  47. outPkgPath = EditorGUILayout.TextField("Output Location", outPkgPath);
  48. if (GUILayout.Button("Select Folder...", GUILayout.Width(150)))
  49. {
  50. outPkgPath = EditorUtility.OpenFolderPanel("Select Folder...", lastOutputDirectory, "");
  51. }
  52. EditorGUILayout.EndHorizontal();
  53. if (GUILayout.Button("GENERATE!"))
  54. {
  55. if (inPkgPath.Equals(""))
  56. {
  57. EditorUtility.DisplayDialog(
  58. title: "Error",
  59. message: "Empty input package path!\nPlease specify input package",
  60. ok: "Bricks without straw");
  61. }
  62. else
  63. {
  64. lastPackageDirectory = inPkgPath;
  65. lastOutputDirectory = outPkgPath;
  66. try
  67. {
  68. string[] files = Directory.GetFiles(Path.Combine(inPkgPath, FileExtension), "*." + FileExtension);
  69. if (files.Length == 0)
  70. {
  71. EditorUtility.DisplayDialog(
  72. title: "No action files found!",
  73. message: "No action files found!",
  74. ok: "Bricks without straw");
  75. Reset();
  76. }
  77. else
  78. {
  79. // Keep a list of warnings
  80. List<string> warnings = new List<string>();
  81. for (int i = 0; i < files.Length; i++)
  82. {
  83. string file = files[i];
  84. EditorUtility.DisplayProgressBar(
  85. "Working...(" + (i + 1) + "/" + files.Length + ")",
  86. "Parsing " + file,
  87. (i + 1) / (float)files.Length);
  88. try
  89. {
  90. warnings.AddRange(Generate(file, outPkgPath, rosPackageName));
  91. }
  92. catch (MessageTokenizerException e)
  93. {
  94. Debug.LogError(e.ToString() + e.Message);
  95. EditorUtility.DisplayDialog(
  96. title: "Message Tokenizer Exception",
  97. message: e.Message,
  98. ok: "Wait. That's illegal");
  99. }
  100. catch (MessageParserException e)
  101. {
  102. Debug.LogError(e.ToString() + e.Message);
  103. EditorUtility.DisplayDialog(
  104. title: "Message Parser Exception",
  105. message: e.Message,
  106. ok: "Sorry but you can't ignore errors.");
  107. }
  108. }
  109. // Done
  110. EditorUtility.ClearProgressBar();
  111. AssetDatabase.Refresh();
  112. if (warnings.Count > 0)
  113. {
  114. EditorUtility.DisplayDialog(
  115. title: "Code Generation Complete",
  116. message: "Output at: " + outPkgPath + "\nYou have " + warnings.Count + " warning(s)",
  117. ok: "I like to live dangerously");
  118. foreach (string w in warnings)
  119. {
  120. Debug.LogWarning(w);
  121. }
  122. }
  123. else
  124. {
  125. EditorUtility.DisplayDialog(
  126. title: "Code Generation Complete",
  127. message: "Output at: " + outPkgPath,
  128. ok: "Thank you!");
  129. }
  130. Reset();
  131. }
  132. }
  133. catch (DirectoryNotFoundException e)
  134. {
  135. EditorUtility.DisplayDialog(
  136. title: "Message Folder not found",
  137. message: e.Message,
  138. ok: "Bricks without straw");
  139. Reset();
  140. }
  141. }
  142. }
  143. }
  144. private void OnInspectorUpdate()
  145. {
  146. Repaint();
  147. }
  148. private void Reset()
  149. {
  150. inPkgPath = "";
  151. rosPackageName = "";
  152. outPkgPath = Path.Combine(System.Environment.CurrentDirectory, "Assets", "RosSharpMessages");
  153. }
  154. protected abstract List<string> Generate(string inPath, string outPath, string rosPackageName = "");
  155. }
  156. }