ProjectTemplates.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * Interface around a project template
  25. */
  26. export interface ProjectTemplateDefinition {
  27. name: string;
  28. desc: string;
  29. screenshot: string;
  30. templates: [ProjectTemplateDetail];
  31. folder: string;
  32. module: string;
  33. }
  34. /**
  35. * Inner details of the project template.
  36. */
  37. export interface ProjectTemplateDetail {
  38. language: string;
  39. folder: string;
  40. }
  41. /**
  42. * Returns the structured template definition for the provided project type.
  43. * Note that the path to the templates folder will be fully resolved
  44. * @param {string} projectType A template type. One of project_empty, project_2d, project_3d
  45. * @return {ProjectTemplateDefinition} the template definition for the proved project type
  46. */
  47. export function getNewProjectTemplateDefinition(projectType: string): ProjectTemplateDefinition {
  48. var env = ToolCore.toolEnvironment;
  49. var projectTemplateFolder = env.toolDataDir + "ProjectTemplates/";
  50. var projectTemplateJsonFile = projectTemplateFolder + "ProjectTemplates.json";
  51. let jsonFile = new Atomic.File(projectTemplateJsonFile, Atomic.FILE_READ);
  52. if (!jsonFile.isOpen()) {
  53. return null;
  54. }
  55. // Update all the paths to a more fully qualified path
  56. let json = JSON.parse(jsonFile.readText());
  57. let projectTemplate = <ProjectTemplateDefinition>json[projectType];
  58. if (projectTemplate) {
  59. projectTemplate.templates.forEach(template => {
  60. template.folder = projectTemplateFolder + template.folder + "/";
  61. });
  62. }
  63. return projectTemplate;
  64. }
  65. /**
  66. * Return an array of all of the example project definitions.
  67. * Note that the paths in both screenshot and folder will be fully resolved.
  68. * @return {[ProjectTemplateDefinition]} Array of example project definitions.
  69. */
  70. export function getExampleProjectTemplateDefinitions(): [ProjectTemplateDefinition] {
  71. let env = ToolCore.toolEnvironment;
  72. let exampleInfoDir = env.toolDataDir + "ExampleInfo/";
  73. let exampleJsonFile = exampleInfoDir + "Examples.json";
  74. let jsonFile = new Atomic.File(exampleJsonFile, Atomic.FILE_READ);
  75. if (!jsonFile.isOpen()) {
  76. return;
  77. }
  78. let exampleJson = JSON.parse(jsonFile.readText());
  79. let examples = <[ProjectTemplateDefinition]>exampleJson.examples;
  80. // Update all the paths to a more fully qualified path
  81. examples.forEach(example => {
  82. example.screenshot = exampleInfoDir + example.screenshot;
  83. example.templates.forEach(template => {
  84. template.folder = env.toolDataDir + "AtomicExamples/" + template.folder + "/";
  85. });
  86. });
  87. return exampleJson.examples;
  88. }
  89. /**
  90. * Return an array of all of the file type templates for the provided file type
  91. * @param {string} fileTemplateType
  92. * @return {[FileTemplateDefinition]}
  93. */
  94. export function GetNewFileTemplateDefinitions(fileTemplateType: Editor.Templates.TemplateType) : Editor.Templates.FileTemplateDefinition[] {
  95. const templateDefinitions = "AtomicEditor/templates/file_template_definitions.json";
  96. const file = Atomic.cache.getFile(templateDefinitions);
  97. if (!file) {
  98. return [];
  99. }
  100. const templates = JSON.parse(file.readText());
  101. return templates[fileTemplateType] || [];
  102. }