CSharpLanguageExtension.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. import EditorUI = require("ui/EditorUI");
  24. /**
  25. * Resource extension that supports the web view typescript extension
  26. */
  27. export default class CSharpLanguageExtension implements Editor.HostExtensions.ResourceServicesEventListener, Editor.HostExtensions.ProjectServicesEventListener {
  28. name: string = "HostCSharpLanguageExtension";
  29. description: string = "This service supports the csharp language.";
  30. /**
  31. * Indicates if this project contains NET files.
  32. * @type {Boolean}
  33. */
  34. private isNETProject = false;
  35. private serviceRegistry: Editor.HostExtensions.HostServiceLocator = null;
  36. private menuCreated = false;
  37. /** Reference to the compileOnSaveMenuItem */
  38. private compileOnSaveMenuItem: Atomic.UIMenuItem;
  39. //** A script object so we can take part in event handling
  40. private eventObject = new Atomic.ScriptObject();
  41. /**
  42. * Determines if the file name/path provided is something we care about
  43. * @param {string} path
  44. * @return {boolean}
  45. */
  46. private isValidFiletype(path: string): boolean {
  47. const ext = Atomic.getExtension(path);
  48. if (ext == ".cs" || ext == ".dll") {
  49. return true;
  50. }
  51. }
  52. /**
  53. * Configures the project to be a Typescript Project
  54. * @return {[type]}
  55. */
  56. private configureNETProjectMenu() {
  57. if (this.isNETProject && !this.menuCreated) {
  58. const isCompileOnSave = this.serviceRegistry.projectServices.getUserPreference(this.name, "CompileOnSave", false);
  59. // Build the menu - First build up an empty menu then manually add the items so we can have reference to them
  60. const menu = this.serviceRegistry.uiServices.createPluginMenuItemSource("AtomicNET", {});
  61. menu.addItem(new Atomic.UIMenuItem("Open Solution", `${this.name}.opensolution`));
  62. menu.addItem(new Atomic.UIMenuItem("Compile Project", `${this.name}.compileproject`));
  63. menu.addItem(new Atomic.UIMenuItem("Generate Solution", `${this.name}.generatesolution`));
  64. menu.addItem(new Atomic.UIMenuItem("Package Resources", `${this.name}.packageresources`));
  65. this.menuCreated = true;
  66. }
  67. }
  68. /**
  69. * Inject this language service into the registry
  70. * @return {[type]} True if successful
  71. */
  72. initialize(serviceLocator: Editor.HostExtensions.HostServiceLocator) {
  73. // We care about both resource events as well as project events
  74. serviceLocator.resourceServices.register(this);
  75. serviceLocator.projectServices.register(this);
  76. serviceLocator.uiServices.register(this);
  77. this.serviceRegistry = serviceLocator;
  78. }
  79. /**
  80. * Handle when a new file is loaded and we have not yet configured the editor for TS.
  81. * This could be when someone adds a CS or assembly file to a vanilla project
  82. * @param {Editor.EditorEvents.EditResourceEvent} ev
  83. */
  84. edit(ev: Editor.EditorEvents.EditResourceEvent) {
  85. if (this.isValidFiletype(ev.path)) {
  86. if (this.isNETProject) {
  87. this.configureNETProjectMenu();
  88. }
  89. }
  90. }
  91. /**
  92. * Handle the delete. This should delete the corresponding javascript file
  93. * @param {Editor.EditorEvents.DeleteResourceEvent} ev
  94. */
  95. delete(ev: Editor.EditorEvents.DeleteResourceEvent) {
  96. }
  97. /**
  98. * Handle the rename. Should rename the corresponding .js file
  99. * @param {Editor.EditorEvents.RenameResourceEvent} ev
  100. */
  101. rename(ev: Editor.EditorEvents.RenameResourceEvent) {
  102. }
  103. /**
  104. * Handles the save event and detects if a cs file has been added to a non-csharp project
  105. * @param {Editor.EditorEvents.SaveResourceEvent} ev
  106. * @return {[type]}
  107. */
  108. save(ev: Editor.EditorEvents.SaveResourceEvent) {
  109. if (Atomic.getExtension(ev.path) != ".cs") {
  110. return;
  111. }
  112. // let's check to see if we have created a csharp file
  113. if (!this.isNETProject) {
  114. this.isNETProject = true;
  115. }
  116. }
  117. /*** ProjectService implementation ****/
  118. /**
  119. * Called when the project is being loaded to allow the typscript language service to reset and
  120. * possibly compile
  121. */
  122. projectLoaded(ev: Editor.EditorEvents.LoadProjectEvent) {
  123. // got a load, we need to reset the language service
  124. this.isNETProject = false;
  125. let found = false;
  126. //scan all the files in the project for any csharp or assembly files so we can determine if this is a csharp project
  127. if (Atomic.fileSystem.scanDir(ToolCore.toolSystem.project.resourcePath, "*.cs", Atomic.SCAN_FILES, true).length > 0) {
  128. found = true;
  129. } else if (Atomic.fileSystem.scanDir(ToolCore.toolSystem.project.resourcePath, "*.dll", Atomic.SCAN_FILES, true).length > 0) {
  130. found = true;
  131. };
  132. if (found) {
  133. this.isNETProject = true;
  134. this.configureNETProjectMenu();
  135. this.eventObject.subscribeToEvent("NETBuildResult", (eventData:ToolCore.NETBuildResult) => {
  136. if (!eventData.success) {
  137. let errorText = eventData.errorText;
  138. let index = errorText.lastIndexOf("Errors:");
  139. if (index != -1) {
  140. errorText = errorText.substr(index);
  141. }
  142. EditorUI.getModelOps().showError("NET Build Error", errorText);
  143. }
  144. });
  145. }
  146. }
  147. /**
  148. * Called when the project is unloaded
  149. */
  150. projectUnloaded() {
  151. // Clean up
  152. this.serviceRegistry.uiServices.removePluginMenuItemSource("AtomicNET");
  153. this.menuCreated = false;
  154. this.isNETProject = false;
  155. this.eventObject.unsubscribeFromAllEvents();
  156. }
  157. /*** UIService implementation ***/
  158. /**
  159. * Called when a plugin menu item is clicked
  160. * @param {string} refId
  161. * @return {boolean}
  162. */
  163. menuItemClicked(refId: string): boolean {
  164. let [extension, action] = refId.split(".");
  165. if (extension == this.name) {
  166. switch (action) {
  167. case "compileproject":
  168. this.doFullCompile();
  169. return true;
  170. case "opensolution":
  171. this.openSolution();
  172. return true;
  173. case "generatesolution":
  174. this.generateSolution();
  175. return true;
  176. case "packageresources":
  177. this.packageResources();
  178. return true;
  179. }
  180. }
  181. }
  182. /**
  183. * Perform a full compile of the solution
  184. */
  185. doFullCompile() {
  186. if (ToolCore.netProjectSystem)
  187. ToolCore.netProjectSystem.buildAtomicProject();
  188. }
  189. /**
  190. * Open Solution
  191. */
  192. openSolution() {
  193. if (ToolCore.netProjectSystem) {
  194. ToolCore.netProjectSystem.openSolution();
  195. }
  196. }
  197. /**
  198. * Generate Solution
  199. */
  200. generateSolution() {
  201. if (ToolCore.netProjectSystem) {
  202. ToolCore.netProjectSystem.generateSolution();
  203. }
  204. }
  205. /**
  206. * Generate Solution
  207. */
  208. packageResources() {
  209. if (ToolCore.netProjectSystem) {
  210. ToolCore.netProjectSystem.generateResourcePak();
  211. }
  212. }
  213. }