TypscriptLanguageExtension.ts 6.5 KB

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