SingleAutoGenEditorWindow.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 SingleAutoGenEditorWindow : EditorWindow
  20. {
  21. [SerializeField]
  22. private static string lastFileDirectory = string.Empty;
  23. [SerializeField]
  24. private static string lastOutputDirectory = string.Empty;
  25. private string inFilePath = "";
  26. private string outFilePath = Path.Combine(System.Environment.CurrentDirectory, "Assets", "RosSharpMessages");
  27. private string rosPackageName = "";
  28. protected abstract string GenerationType { get; }
  29. protected abstract string FileExtension { get; }
  30. protected virtual void OnGUI()
  31. {
  32. GUILayout.Label("Single " + GenerationType + " auto generation", EditorStyles.boldLabel);
  33. EditorGUILayout.BeginHorizontal();
  34. inFilePath = EditorGUILayout.TextField("Input File Path", inFilePath);
  35. if (GUILayout.Button("Browse File...", GUILayout.Width(120)))
  36. {
  37. inFilePath = EditorUtility.OpenFilePanel("Select " + GenerationType + " File...", lastFileDirectory, FileExtension);
  38. if (!inFilePath.Equals(""))
  39. {
  40. string[] directoryLevels = inFilePath.Split('/');
  41. rosPackageName = directoryLevels[directoryLevels.Length - 3];
  42. }
  43. }
  44. EditorGUILayout.EndHorizontal();
  45. rosPackageName = EditorGUILayout.TextField("ROS Package Name:", rosPackageName);
  46. EditorGUILayout.BeginHorizontal();
  47. outFilePath = EditorGUILayout.TextField("Output File Location", outFilePath);
  48. if (GUILayout.Button("Select Folder...", GUILayout.Width(120)))
  49. {
  50. outFilePath = EditorUtility.OpenFolderPanel("Select Folder...", lastOutputDirectory, "");
  51. }
  52. EditorGUILayout.EndHorizontal();
  53. if (GUILayout.Button("GENERATE!"))
  54. {
  55. if (inFilePath.Equals(""))
  56. {
  57. EditorUtility.DisplayDialog(
  58. title: "Error",
  59. message: "Empty input file path!\nPlease specify input file",
  60. ok: "Bricks without straw");
  61. }
  62. else
  63. {
  64. lastFileDirectory = inFilePath;
  65. lastOutputDirectory = outFilePath;
  66. try
  67. {
  68. List<string> warnings = Generate(inFilePath, outFilePath, rosPackageName);
  69. AssetDatabase.Refresh();
  70. if (warnings.Count == 0)
  71. {
  72. EditorUtility.DisplayDialog(
  73. title: "Code Generation Complete",
  74. message: "Output at: " + outFilePath,
  75. ok: "Thank you!");
  76. }
  77. else
  78. {
  79. foreach (string w in warnings)
  80. {
  81. Debug.LogWarning(w);
  82. }
  83. EditorUtility.DisplayDialog(
  84. title: "Code Generation Complete",
  85. message: "Output at: " + outFilePath + "\nYou have " + warnings.Count + " warning(s)",
  86. ok: "I like to live dangerously");
  87. }
  88. }
  89. catch (MessageTokenizerException e)
  90. {
  91. Debug.LogError(e.ToString() + e.Message);
  92. EditorUtility.DisplayDialog(
  93. title: "Message Tokenizer Exception",
  94. message: e.Message,
  95. ok: "Wait. That's illegal");
  96. }
  97. catch (MessageParserException e)
  98. {
  99. Debug.LogError(e.ToString() + e.Message);
  100. EditorUtility.DisplayDialog(
  101. title: "Message Parser Exception",
  102. message: e.Message,
  103. ok: "Sorry but you can't ignore errors.");
  104. }
  105. }
  106. }
  107. }
  108. private void OnInspectorUpdate()
  109. {
  110. Repaint();
  111. }
  112. protected abstract List<string> Generate(string inPath, string outPath, string rosPackageName = "");
  113. }
  114. }