ProjectTemplates.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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: string) : 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. }
  103. // AtomicNET
  104. export interface AtomicNETProjectInfo {
  105. name: string;
  106. platforms: string[];
  107. projectFolder: string;
  108. }
  109. var atomicNETProjectInfo:AtomicNETProjectInfo;
  110. /**
  111. * Processes an AtomicNET template, replacing strings with settings
  112. * @param {string} filename
  113. * @param {string} templateFilename
  114. * @return {boolean}
  115. */
  116. function processAtomicNETTemplate(filename:string, templateFilename:string) : boolean {
  117. let file = new Atomic.File(templateFilename, Atomic.FILE_READ);
  118. if (!file.isOpen()) {
  119. console.log("Failed to open: ", templateFilename);
  120. return false;
  121. }
  122. let text = file.readText();
  123. text = text.split("$$APPLICATION_NAME$$").join(atomicNETProjectInfo.name);
  124. let fileOut = new Atomic.File(filename, Atomic.FILE_WRITE);
  125. if (!fileOut.isOpen()) {
  126. console.log("Failed to open for write: ", filename);
  127. return false;
  128. }
  129. fileOut.writeString(text);
  130. file.close();
  131. return true;
  132. }
  133. /**
  134. * Generates the Android portion of an AtomicNET project
  135. * @return {boolean}
  136. */
  137. function generateAtomicNETAndroidProject():boolean {
  138. let env = ToolCore.toolEnvironment;
  139. let utils = new Editor.FileUtils();
  140. let templateFolder = env.toolDataDir + "AtomicNET/ProjectTemplate/";
  141. let androidFolder = Atomic.addTrailingSlash(atomicNETProjectInfo.projectFolder) + "Project/AtomicNET/Platforms/Android/";
  142. let fileSystem = Atomic.fileSystem;
  143. // Create necessary folders
  144. let folders = ["Properties", "Resources/drawable", "Resources/values"];
  145. for (var i = 0; i < folders.length; i++) {
  146. let folder = androidFolder + folders[i];
  147. if (!fileSystem.dirExists(folder)) {
  148. if (!utils.createDirs(folder))
  149. return false;
  150. }
  151. }
  152. let textFiles = [".cs", ".xml"];
  153. let files = ["MainActivity.cs", "Resources/Resource.Designer.cs", "Resources/drawable/icon.png",
  154. "Resources/values/Strings.xml", "Properties/AndroidManifest.xml", "Properties/AssemblyInfo.cs"];
  155. for (var i = 0; i < files.length; i++) {
  156. let templateName = templateFolder + "Platforms/Android/" + files[i];
  157. let filename = androidFolder + files[i];
  158. if (textFiles.indexOf(Atomic.getExtension(templateName)) == -1) {
  159. if (!fileSystem.copy(templateName, filename)) {
  160. console.log("Failed to copy: ", templateName, " to ", filename);
  161. return false;
  162. }
  163. } else {
  164. if (!processAtomicNETTemplate(filename, templateName)) {
  165. return false;
  166. }
  167. }
  168. }
  169. return true;
  170. }
  171. /**
  172. * Generates the Desktop portion of an AtomicNET project
  173. * @return {boolean}
  174. */
  175. function generateAtomicNETDesktopProject():boolean {
  176. let env = ToolCore.toolEnvironment;
  177. let utils = new Editor.FileUtils();
  178. let templateFolder = env.toolDataDir + "AtomicNET/ProjectTemplate/";
  179. let desktopFolder = Atomic.addTrailingSlash(atomicNETProjectInfo.projectFolder) + "Project/AtomicNET/Platforms/Desktop/";
  180. let fileSystem = Atomic.fileSystem;
  181. if (!fileSystem.dirExists(desktopFolder)) {
  182. if (!utils.createDirs(desktopFolder))
  183. return false;
  184. }
  185. if (!fileSystem.copy(templateFolder + "Platforms/Desktop/Program.cs", desktopFolder + "Program.cs")) {
  186. return false;
  187. }
  188. return true;
  189. }
  190. /**
  191. * Generates an AtomicNET project from templates
  192. * @param {AtomicNETProjectInfo} projectInfo
  193. * @return {boolean}
  194. */
  195. export function generateAtomicNETProject(projectInfo:AtomicNETProjectInfo):boolean {
  196. atomicNETProjectInfo = projectInfo;
  197. let env = ToolCore.toolEnvironment;
  198. let templateFolder = env.toolDataDir + "AtomicNET/ProjectTemplate/";
  199. let platformsFolder = Atomic.addTrailingSlash(projectInfo.projectFolder) + "Project/AtomicNET/Platforms/";
  200. let utils = new Editor.FileUtils();
  201. let fileSystem = Atomic.fileSystem;
  202. if (!fileSystem.dirExists(platformsFolder)) {
  203. if (!utils.createDirs(platformsFolder))
  204. return false;
  205. }
  206. if (!generateAtomicNETDesktopProject()) {
  207. return false;
  208. }
  209. if (projectInfo.platforms.indexOf("android") != -1) {
  210. if (!generateAtomicNETAndroidProject()) {
  211. return false;
  212. }
  213. }
  214. return true;
  215. }