CSharpLanguageExtension.ts 8.5 KB

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