CSharpLanguageExtension.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import * as EditorEvents from "../../editor/EditorEvents";
  23. /**
  24. * Resource extension that supports the web view typescript extension
  25. */
  26. export default class CSharpLanguageExtension implements Editor.HostExtensions.ResourceServicesEventListener, Editor.HostExtensions.ProjectServicesEventListener {
  27. name: string = "HostCSharpLanguageExtension";
  28. description: string = "This service supports the csharp language.";
  29. /**
  30. * Indicates if this project contains NET files.
  31. * @type {Boolean}
  32. */
  33. private isNETProject = false;
  34. private serviceRegistry: Editor.HostExtensions.HostServiceLocator = null;
  35. private menuCreated = false;
  36. /** Reference to the compileOnSaveMenuItem */
  37. private compileOnSaveMenuItem: Atomic.UIMenuItem;
  38. /**
  39. * Determines if the file name/path provided is something we care about
  40. * @param {string} path
  41. * @return {boolean}
  42. */
  43. private isValidFiletype(path: string): boolean {
  44. const ext = Atomic.getExtension(path);
  45. if (ext == ".cs" || ext == ".dll") {
  46. return true;
  47. }
  48. }
  49. /**
  50. * Configures the project to be a Typescript Project
  51. * @return {[type]}
  52. */
  53. private configureNETProjectMenu() {
  54. if (this.isNETProject && !this.menuCreated) {
  55. const isCompileOnSave = this.serviceRegistry.projectServices.getUserPreference(this.name, "CompileOnSave", false);
  56. // Build the menu - First build up an empty menu then manually add the items so we can have reference to them
  57. const menu = this.serviceRegistry.uiServices.createPluginMenuItemSource("AtomicNET", {});
  58. menu.addItem(new Atomic.UIMenuItem("Open Solution", `${this.name}.opensolution`));
  59. menu.addItem(new Atomic.UIMenuItem("Compile Project", `${this.name}.compileproject`));
  60. menu.addItem(new Atomic.UIMenuItem("Generate Solution", `${this.name}.generatesolution`));
  61. menu.addItem(new Atomic.UIMenuItem("Package Resources", `${this.name}.packageresources`));
  62. this.menuCreated = true;
  63. }
  64. }
  65. /**
  66. * Inject this language service into the registry
  67. * @return {[type]} True if successful
  68. */
  69. initialize(serviceLocator: Editor.HostExtensions.HostServiceLocator) {
  70. // We care about both resource events as well as project events
  71. serviceLocator.resourceServices.register(this);
  72. serviceLocator.projectServices.register(this);
  73. serviceLocator.uiServices.register(this);
  74. this.serviceRegistry = serviceLocator;
  75. }
  76. /**
  77. * Handle when a new file is loaded and we have not yet configured the editor for TS.
  78. * This could be when someone adds a CS or assembly file to a vanilla project
  79. * @param {Editor.EditorEvents.EditResourceEvent} ev
  80. */
  81. edit(ev: Editor.EditorEvents.EditResourceEvent) {
  82. if (this.isValidFiletype(ev.path)) {
  83. if (this.isNETProject) {
  84. this.configureNETProjectMenu();
  85. }
  86. }
  87. }
  88. /**
  89. * Handle the delete. This should delete the corresponding javascript file
  90. * @param {Editor.EditorEvents.DeleteResourceEvent} ev
  91. */
  92. delete(ev: Editor.EditorEvents.DeleteResourceEvent) {
  93. }
  94. /**
  95. * Handle the rename. Should rename the corresponding .js file
  96. * @param {Editor.EditorEvents.RenameResourceEvent} ev
  97. */
  98. rename(ev: Editor.EditorEvents.RenameResourceEvent) {
  99. }
  100. /**
  101. * Handles the save event and detects if a cs file has been added to a non-csharp project
  102. * @param {Editor.EditorEvents.SaveResourceEvent} ev
  103. * @return {[type]}
  104. */
  105. save(ev: Editor.EditorEvents.SaveResourceEvent) {
  106. if (Atomic.getExtension(ev.path) != ".cs") {
  107. return;
  108. }
  109. // let's check to see if we have created a csharp file
  110. if (!this.isNETProject) {
  111. this.isNETProject = true;
  112. }
  113. }
  114. /*** ProjectService implementation ****/
  115. /**
  116. * Called when the project is being loaded to allow the typscript language service to reset and
  117. * possibly compile
  118. */
  119. projectLoaded(ev: Editor.EditorEvents.LoadProjectEvent) {
  120. // got a load, we need to reset the language service
  121. this.isNETProject = false;
  122. let found = false;
  123. //scan all the files in the project for any csharp or assembly files so we can determine if this is a csharp project
  124. if (Atomic.fileSystem.scanDir(ToolCore.toolSystem.project.resourcePath, "*.cs", Atomic.SCAN_FILES, true).length > 0) {
  125. found = true;
  126. } else if (Atomic.fileSystem.scanDir(ToolCore.toolSystem.project.resourcePath, "*.dll", Atomic.SCAN_FILES, true).length > 0) {
  127. found = true;
  128. };
  129. if (found) {
  130. this.isNETProject = true;
  131. this.configureNETProjectMenu();
  132. }
  133. }
  134. /**
  135. * Called when the project is unloaded
  136. */
  137. projectUnloaded() {
  138. // Clean up
  139. this.serviceRegistry.uiServices.removePluginMenuItemSource("AtomicNET");
  140. this.menuCreated = false;
  141. this.isNETProject = false;
  142. }
  143. /*** UIService implementation ***/
  144. /**
  145. * Called when a plugin menu item is clicked
  146. * @param {string} refId
  147. * @return {boolean}
  148. */
  149. menuItemClicked(refId: string): boolean {
  150. let [extension, action] = refId.split(".");
  151. if (extension == this.name) {
  152. switch (action) {
  153. case "compileproject":
  154. this.doFullCompile();
  155. return true;
  156. case "opensolution":
  157. this.openSolution();
  158. return true;
  159. case "generatesolution":
  160. this.generateSolution();
  161. return true;
  162. case "packageresources":
  163. this.packageResources();
  164. return true;
  165. }
  166. }
  167. }
  168. /**
  169. * Perform a full compile of the solution
  170. */
  171. doFullCompile() {
  172. if (ToolCore.netProjectSystem)
  173. ToolCore.netProjectSystem.buildAtomicProject();
  174. }
  175. /**
  176. * Open Solution
  177. */
  178. openSolution() {
  179. if (ToolCore.netProjectSystem) {
  180. ToolCore.netProjectSystem.openSolution();
  181. }
  182. }
  183. /**
  184. * Generate Solution
  185. */
  186. generateSolution() {
  187. if (ToolCore.netProjectSystem) {
  188. ToolCore.netProjectSystem.generateSolution();
  189. }
  190. }
  191. /**
  192. * Generate Solution
  193. */
  194. packageResources() {
  195. if (ToolCore.netProjectSystem) {
  196. ToolCore.netProjectSystem.generateResourcePak();
  197. }
  198. }
  199. }