ProjectTemplates.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. appID: string;
  107. platforms: string[];
  108. projectFolder: string;
  109. }
  110. var atomicNETProjectInfo:AtomicNETProjectInfo;
  111. /**
  112. * Processes an AtomicNET template, replacing strings with settings
  113. * @param {string} filename
  114. * @param {string} templateFilename
  115. * @return {boolean}
  116. */
  117. function processAtomicNETTemplate(filename:string, templateFilename:string) : boolean {
  118. let file = new Atomic.File(templateFilename, Atomic.FILE_READ);
  119. if (!file.isOpen()) {
  120. console.log("Failed to open: ", templateFilename);
  121. return false;
  122. }
  123. let text = file.readText();
  124. text = text.split("$$APPLICATION_NAME$$").join(atomicNETProjectInfo.name);
  125. text = text.split("$$APPLICATION_ID$$").join(atomicNETProjectInfo.appID);
  126. let fileOut = new Atomic.File(filename, Atomic.FILE_WRITE);
  127. if (!fileOut.isOpen()) {
  128. console.log("Failed to open for write: ", filename);
  129. return false;
  130. }
  131. fileOut.writeString(text);
  132. file.close();
  133. return true;
  134. }
  135. /**
  136. * Generates the Android portion of an AtomicNET project
  137. * @return {boolean}
  138. */
  139. function generateAtomicNETAndroidProject():boolean {
  140. let env = ToolCore.toolEnvironment;
  141. let utils = new Editor.FileUtils();
  142. let templateFolder = env.toolDataDir + "AtomicNET/ProjectTemplate/";
  143. let androidFolder = Atomic.addTrailingSlash(atomicNETProjectInfo.projectFolder) + "Project/AtomicNET/Platforms/Android/";
  144. let fileSystem = Atomic.fileSystem;
  145. // Create necessary folders
  146. let folders = ["Properties", "Resources/drawable", "Resources/values"];
  147. for (var i = 0; i < folders.length; i++) {
  148. let folder = androidFolder + folders[i];
  149. if (!fileSystem.dirExists(folder)) {
  150. if (!utils.createDirs(folder))
  151. return false;
  152. }
  153. }
  154. let textFiles = [".cs", ".xml"];
  155. let files = ["MainActivity.cs", "Resources/Resource.Designer.cs", "Resources/drawable/icon.png",
  156. "Resources/values/Strings.xml", "Properties/AndroidManifest.xml", "Properties/AssemblyInfo.cs"];
  157. for (var i = 0; i < files.length; i++) {
  158. let templateName = templateFolder + "Platforms/Android/" + files[i];
  159. let filename = androidFolder + files[i];
  160. if (textFiles.indexOf(Atomic.getExtension(templateName)) == -1) {
  161. if (!fileSystem.copy(templateName, filename)) {
  162. console.log("Failed to copy: ", templateName, " to ", filename);
  163. return false;
  164. }
  165. } else {
  166. if (!processAtomicNETTemplate(filename, templateName)) {
  167. return false;
  168. }
  169. }
  170. }
  171. return true;
  172. }
  173. /**
  174. * Generates the iOS portion of an AtomicNET project
  175. * @return {boolean}
  176. */
  177. function generateAtomicNETIOSProject():boolean {
  178. let env = ToolCore.toolEnvironment;
  179. let utils = new Editor.FileUtils();
  180. let templateFolder = env.toolDataDir + "AtomicNET/ProjectTemplate/";
  181. let iosFolder = Atomic.addTrailingSlash(atomicNETProjectInfo.projectFolder) + "Project/AtomicNET/Platforms/iOS/";
  182. let fileSystem = Atomic.fileSystem;
  183. // Create necessary folders
  184. let folders = ["Properties", "Resources"];
  185. for (var i = 0; i < folders.length; i++) {
  186. let folder = iosFolder + folders[i];
  187. if (!fileSystem.dirExists(folder)) {
  188. if (!utils.createDirs(folder))
  189. return false;
  190. }
  191. }
  192. let textFiles = [".cs", ".plist"];
  193. let files = ["Main.cs", "AppUIDelegate.cs", "Entitlements.plist", "Info.plist",
  194. "Properties/AssemblyInfo.cs"];
  195. for (var i = 0; i < files.length; i++) {
  196. let templateName = templateFolder + "Platforms/iOS/" + files[i];
  197. let filename = iosFolder + files[i];
  198. if (textFiles.indexOf(Atomic.getExtension(templateName)) == -1) {
  199. if (!fileSystem.copy(templateName, filename)) {
  200. console.log("Failed to copy: ", templateName, " to ", filename);
  201. return false;
  202. }
  203. } else {
  204. if (!processAtomicNETTemplate(filename, templateName)) {
  205. return false;
  206. }
  207. }
  208. }
  209. return true;
  210. }
  211. /**
  212. * Generates the Desktop portion of an AtomicNET project
  213. * @return {boolean}
  214. */
  215. function generateAtomicNETDesktopProject():boolean {
  216. let env = ToolCore.toolEnvironment;
  217. let utils = new Editor.FileUtils();
  218. let templateFolder = env.toolDataDir + "AtomicNET/ProjectTemplate/";
  219. let desktopFolder = Atomic.addTrailingSlash(atomicNETProjectInfo.projectFolder) + "Project/AtomicNET/Platforms/Desktop/";
  220. let fileSystem = Atomic.fileSystem;
  221. if (!fileSystem.dirExists(desktopFolder)) {
  222. if (!utils.createDirs(desktopFolder))
  223. return false;
  224. }
  225. if (!fileSystem.copy(templateFolder + "Platforms/Desktop/Program.cs", desktopFolder + "Program.cs")) {
  226. return false;
  227. }
  228. return true;
  229. }
  230. /**
  231. * Generates an AtomicNET project from templates
  232. * @param {AtomicNETProjectInfo} projectInfo
  233. * @return {boolean}
  234. */
  235. export function generateAtomicNETProject(projectInfo:AtomicNETProjectInfo):boolean {
  236. atomicNETProjectInfo = projectInfo;
  237. let env = ToolCore.toolEnvironment;
  238. let templateFolder = env.toolDataDir + "AtomicNET/ProjectTemplate/";
  239. let platformsFolder = Atomic.addTrailingSlash(projectInfo.projectFolder) + "Project/AtomicNET/Platforms/";
  240. let utils = new Editor.FileUtils();
  241. let fileSystem = Atomic.fileSystem;
  242. if (!fileSystem.dirExists(platformsFolder)) {
  243. if (!utils.createDirs(platformsFolder))
  244. return false;
  245. }
  246. if (!generateAtomicNETDesktopProject()) {
  247. return false;
  248. }
  249. if (projectInfo.platforms.indexOf("android") != -1) {
  250. if (!generateAtomicNETAndroidProject()) {
  251. return false;
  252. }
  253. }
  254. if (projectInfo.platforms.indexOf("ios") != -1) {
  255. if (!generateAtomicNETIOSProject()) {
  256. return false;
  257. }
  258. }
  259. return true;
  260. }