TypscriptLanguageExtension.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. import * as EditorEvents from "../../editor/EditorEvents";
  8. /**
  9. * Resource extension that supports the web view typescript extension
  10. */
  11. export default class TypescriptLanguageExtension implements Editor.HostExtensions.ResourceService, Editor.HostExtensions.ProjectService {
  12. name: string = "HostTypeScriptLanguageExtension";
  13. description: string = "This service supports the typscript webview extension.";
  14. /**
  15. * Indicates if this project contains typescript files.
  16. * @type {Boolean}
  17. */
  18. private isTypescriptProject = false;
  19. private serviceRegistry: Editor.HostExtensions.HostServiceLocator = null;
  20. /**
  21. * Determines if the file name/path provided is something we care about
  22. * @param {string} path
  23. * @return {boolean}
  24. */
  25. private isValidFiletype(path: string): boolean {
  26. if (this.isTypescriptProject) {
  27. const ext = Atomic.getExtension(path);
  28. if (ext == ".ts") {
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. /**
  35. * Seed the language service with all of the relevant files in the project. This updates the tsconifg.atomic file in
  36. * the root of the resources directory.
  37. */
  38. private loadProjectFiles() {
  39. let projectFiles: Array<string> = [];
  40. //scan all the files in the project for any typescript files so we can determine if this is a typescript project
  41. Atomic.fileSystem.scanDir(ToolCore.toolSystem.project.resourcePath, "*.ts", Atomic.SCAN_FILES, true).forEach(filename => {
  42. projectFiles.push(Atomic.addTrailingSlash(ToolCore.toolSystem.project.resourcePath) + filename);
  43. this.isTypescriptProject = true;
  44. });
  45. // only build out a tsconfig.atomic if we actually have typescript files in the project
  46. if (this.isTypescriptProject) {
  47. // First we need to load in a copy of the lib.core.d.ts that is necessary for the hosted typescript compiler
  48. projectFiles.push(Atomic.addTrailingSlash(Atomic.addTrailingSlash(ToolCore.toolEnvironment.toolDataDir) + "TypeScriptSupport") + "lib.core.d.ts");
  49. // Load up a copy of the duktape.d.ts
  50. projectFiles.push(Atomic.addTrailingSlash(Atomic.addTrailingSlash(ToolCore.toolEnvironment.toolDataDir) + "TypeScriptSupport") + "duktape.d.ts");
  51. // Look in a 'typings' directory for any typescript definition files
  52. const typingsDir = Atomic.addTrailingSlash(ToolCore.toolSystem.project.projectPath) + "typings";
  53. Atomic.fileSystem.scanDir(typingsDir, "*.d.ts", Atomic.SCAN_FILES, true).forEach(filename => {
  54. projectFiles.push(Atomic.addTrailingSlash(typingsDir) + filename);
  55. });
  56. let files = projectFiles.map((f: string) => {
  57. if (f.indexOf(ToolCore.toolSystem.project.resourcePath) != -1) {
  58. // if we are in the resources directory, just pass back the path from resources down
  59. return f.replace(Atomic.addTrailingSlash(ToolCore.toolSystem.project.projectPath), "");
  60. } else {
  61. // otherwise return the full path
  62. return f;
  63. }
  64. });
  65. let tsConfig = {
  66. files: files
  67. };
  68. let filename = Atomic.addTrailingSlash(ToolCore.toolSystem.project.resourcePath) + "tsconfig.atomic";
  69. let script = new Atomic.File(filename, Atomic.FILE_WRITE);
  70. try {
  71. script.writeString(JSON.stringify(tsConfig));
  72. script.flush();
  73. } finally {
  74. script.close();
  75. }
  76. }
  77. }
  78. /**
  79. * Inject this language service into the registry
  80. * @return {[type]} True if successful
  81. */
  82. initialize(serviceRegistry: Editor.HostExtensions.HostServiceLocator) {
  83. // We care about both resource events as well as project events
  84. serviceRegistry.resourceServices.register(this);
  85. serviceRegistry.projectServices.register(this);
  86. this.serviceRegistry = serviceRegistry;
  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. if (this.isValidFiletype(ev.path)) {
  94. console.log(`${this.name}: received a delete resource event`);
  95. // Delete the corresponding js file
  96. let jsFile = ev.path.replace(/\.ts$/, ".js");
  97. let jsFileAsset = ToolCore.assetDatabase.getAssetByPath(jsFile);
  98. if (jsFileAsset) {
  99. console.log(`${this.name}: deleting corresponding .js file`);
  100. ToolCore.assetDatabase.deleteAsset(jsFileAsset);
  101. let eventData: EditorEvents.DeleteResourceEvent = {
  102. path: jsFile
  103. };
  104. this.serviceRegistry.sendEvent(EditorEvents.DeleteResourceNotification, eventData);
  105. // rebuild the tsconfig.atomic
  106. this.loadProjectFiles();
  107. }
  108. }
  109. }
  110. /**
  111. * Handle the rename. Should rename the corresponding .js file
  112. * @param {Editor.EditorEvents.RenameResourceEvent} ev
  113. */
  114. rename(ev: Editor.EditorEvents.RenameResourceEvent) {
  115. if (this.isValidFiletype(ev.path)) {
  116. console.log(`${this.name}: received a rename resource event`);
  117. // Rename the corresponding js file
  118. let jsFile = ev.path.replace(/\.ts$/, ".js");
  119. let jsFileNew = ev.newPath.replace(/\.ts$/, ".js"); // rename doesn't want extension
  120. let jsFileAsset = ToolCore.assetDatabase.getAssetByPath(jsFile);
  121. if (jsFileAsset) {
  122. console.log(`${this.name}: renaming corresponding .js file`);
  123. jsFileAsset.rename(ev.newName);
  124. let eventData: EditorEvents.RenameResourceEvent = {
  125. path: jsFile,
  126. newPath: jsFileNew,
  127. newName: ev.newName,
  128. asset: jsFileAsset
  129. };
  130. this.serviceRegistry.sendEvent(EditorEvents.RenameResourceNotification, eventData);
  131. // rebuild the tsconfig.atomic
  132. this.loadProjectFiles();
  133. }
  134. }
  135. }
  136. /**
  137. * Handles the save event and detects if a typescript file has been added to a non-typescript project
  138. * @param {Editor.EditorEvents.SaveResourceEvent} ev
  139. * @return {[type]}
  140. */
  141. save(ev: Editor.EditorEvents.SaveResourceEvent) {
  142. // let's check to see if we have created a typescript file
  143. if (!this.isTypescriptProject) {
  144. if (Atomic.getExtension(ev.path) == ".ts") {
  145. this.isTypescriptProject = true;
  146. this.loadProjectFiles();
  147. }
  148. }
  149. }
  150. /*** ProjectService implementation ****/
  151. /**
  152. * Called when the project is being loaded to allow the typscript language service to reset and
  153. * possibly compile
  154. */
  155. projectLoaded(ev: Editor.EditorEvents.LoadProjectEvent) {
  156. // got a load, we need to reset the language service
  157. console.log(`${this.name}: received a project loaded event for project at ${ev.path}`);
  158. this.loadProjectFiles();
  159. }
  160. }