DirectoryAutoGenEditorWindow.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 UnityEditor;
  16. using UnityEngine;
  17. namespace RosSharp.RosBridgeClient.MessageGeneration
  18. {
  19. public abstract class DirectoryAutoGenEditorWindow : EditorWindow
  20. {
  21. [SerializeField]
  22. private static string lastInputDirectory = string.Empty;
  23. [SerializeField]
  24. private static string lastOutputDirectory = string.Empty;
  25. private string inPath = "";
  26. private string outPath = Path.Combine(System.Environment.CurrentDirectory, "Assets", "RosSharpMessages");
  27. protected abstract string GenerationType { get; }
  28. protected abstract string FileExtension { get; }
  29. protected virtual void OnGUI()
  30. {
  31. GUILayout.Label("Directory " + GenerationType + " auto generation", EditorStyles.boldLabel);
  32. EditorGUILayout.BeginHorizontal();
  33. inPath = EditorGUILayout.TextField("Input Path", inPath);
  34. if (GUILayout.Button("Select Folder...", GUILayout.Width(150)))
  35. {
  36. inPath = EditorUtility.OpenFolderPanel("Select Folder...", lastInputDirectory, "");
  37. }
  38. EditorGUILayout.EndHorizontal();
  39. EditorGUILayout.BeginHorizontal();
  40. outPath = EditorGUILayout.TextField("Output Location", outPath);
  41. if (GUILayout.Button("Select Folder...", GUILayout.Width(150)))
  42. {
  43. outPath = EditorUtility.OpenFolderPanel("Select Folder...", lastOutputDirectory, "");
  44. }
  45. EditorGUILayout.EndHorizontal();
  46. if (GUILayout.Button("GENERATE!"))
  47. {
  48. if (inPath.Equals(""))
  49. {
  50. EditorUtility.DisplayDialog(
  51. title: "Error",
  52. message: "Empty input path!\nPlease specify input path",
  53. ok: "Bricks without straw");
  54. }
  55. else
  56. {
  57. lastInputDirectory = inPath;
  58. lastOutputDirectory = outPath;
  59. try
  60. {
  61. List<string> warnings = new List<string>();
  62. string[] files = Directory.GetFiles(inPath, "*." + FileExtension, SearchOption.AllDirectories);
  63. if (files.Length == 0)
  64. {
  65. EditorUtility.DisplayDialog(
  66. title: "No " + GenerationType + " files found!",
  67. message: "No " + GenerationType + " files found!",
  68. ok: "Bricks without straw");
  69. Reset();
  70. }
  71. else
  72. {
  73. for (int i = 0; i < files.Length; i++)
  74. {
  75. string file = files[i];
  76. string[] hierarchy = file.Split(new char[] { '/', '\\' });
  77. string rosPackageName = hierarchy[hierarchy.Length - 3];
  78. try
  79. {
  80. EditorUtility.DisplayProgressBar(
  81. "Working...(" + (i + 1) + "/" + files.Length + ") Checkout xkcd.com/303",
  82. "Parsing " + file,
  83. (i + 1) / (float)files.Length);
  84. warnings.AddRange(Generate(file, outPath, rosPackageName));
  85. }
  86. catch (MessageTokenizerException e)
  87. {
  88. Debug.LogError(e.ToString() + e.Message);
  89. EditorUtility.DisplayDialog(
  90. title: "Message Tokenizer Exception",
  91. message: e.Message,
  92. ok: "Wait. That's illegal");
  93. }
  94. catch (MessageParserException e)
  95. {
  96. Debug.LogError(e.ToString() + e.Message);
  97. EditorUtility.DisplayDialog(
  98. title: "Message Parser Exception",
  99. message: e.Message,
  100. ok: "Sorry but you can't ignore errors.");
  101. }
  102. }
  103. // Done
  104. EditorUtility.ClearProgressBar();
  105. AssetDatabase.Refresh();
  106. if (warnings.Count > 0)
  107. {
  108. EditorUtility.DisplayDialog(
  109. title: "Code Generation Complete",
  110. message: "Output at: " + outPath + "\nYou have " + warnings.Count + " warning(s)",
  111. ok: "I like to live dangerously");
  112. foreach (string w in warnings)
  113. {
  114. Debug.LogWarning(w);
  115. }
  116. }
  117. else
  118. {
  119. EditorUtility.DisplayDialog(
  120. title: "Code Generation Complete",
  121. message: "Output at: " + outPath,
  122. ok: "Thank you!");
  123. }
  124. Reset();
  125. }
  126. }
  127. catch (DirectoryNotFoundException e)
  128. {
  129. EditorUtility.DisplayDialog(
  130. title: "Folder not found",
  131. message: e.Message,
  132. ok: "Bricks without straw");
  133. Reset();
  134. }
  135. }
  136. }
  137. }
  138. private void OnInspectorUpdate()
  139. {
  140. Repaint();
  141. }
  142. private void Reset()
  143. {
  144. inPath = "";
  145. outPath = Path.Combine(System.Environment.CurrentDirectory, "Assets", "RosSharpMessages");
  146. }
  147. protected abstract List<string> Generate(string inPath, string outPath, string rosPackageName = "");
  148. }
  149. }