Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

create.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. const path = require('path');
  18. const fs = require('fs-extra');
  19. const xmlescape = require('xml-escape');
  20. const ROOT = path.join(__dirname, '..', '..');
  21. const { CordovaError, events } = require('cordova-common');
  22. const utils = require('./utils');
  23. function updateSubprojectHelp () {
  24. console.log('Updates the subproject path of the CordovaLib entry to point to this script\'s version of Cordova.');
  25. console.log('Usage: CordovaVersion/bin/update_cordova_project path/to/your/app.xcodeproj [path/to/CordovaLib.xcodeproj]');
  26. }
  27. function copyJsAndCordovaLib (projectPath, projectName, use_shared, config) {
  28. fs.copySync(path.join(ROOT, 'CordovaLib', 'cordova.js'), path.join(projectPath, 'www/cordova.js'));
  29. fs.copySync(path.join(ROOT, 'cordova-js-src'), path.join(projectPath, 'platform_www/cordova-js-src'));
  30. fs.copySync(path.join(ROOT, 'CordovaLib', 'cordova.js'), path.join(projectPath, 'platform_www/cordova.js'));
  31. /*
  32. * Check if "CordovaLib" already exists with "fs.lstatSync" and remove it.
  33. * Wrapped with try/catch because lstatSync will throw an error if "CordovaLib"
  34. * is missing.
  35. */
  36. try {
  37. const stats = fs.lstatSync(path.join(projectPath, 'CordovaLib'));
  38. if (stats.isSymbolicLink()) {
  39. fs.unlinkSync(path.join(projectPath, 'CordovaLib'));
  40. } else {
  41. fs.removeSync(path.join(projectPath, 'CordovaLib'));
  42. }
  43. } catch (e) { }
  44. if (use_shared) {
  45. update_cordova_subproject([path.join(projectPath, `${projectName}.xcodeproj`, 'project.pbxproj'), config]);
  46. // Symlink not used in project file, but is currently required for plugman because
  47. // it reads the VERSION file from it (instead of using the cordova/version script
  48. // like it should).
  49. fs.symlinkSync(path.join(ROOT, 'CordovaLib'), path.join(projectPath, 'CordovaLib'));
  50. } else {
  51. const r = path.join(projectPath, projectName);
  52. fs.ensureDirSync(path.join(projectPath, 'CordovaLib', 'CordovaLib.xcodeproj'));
  53. fs.copySync(path.join(r, '.gitignore'), path.join(projectPath, '.gitignore'));
  54. fs.copySync(path.join(ROOT, 'CordovaLib', 'Classes'), path.join(projectPath, 'CordovaLib/Classes'));
  55. fs.copySync(path.join(ROOT, 'CordovaLib', 'VERSION'), path.join(projectPath, 'CordovaLib/VERSION'));
  56. fs.copySync(path.join(ROOT, 'CordovaLib', 'cordova.js'), path.join(projectPath, 'CordovaLib/cordova.js'));
  57. fs.copySync(path.join(ROOT, 'CordovaLib', 'CordovaLib_Prefix.pch'), path.join(projectPath, 'CordovaLib/CordovaLib_Prefix.pch'));
  58. fs.copySync(path.join(ROOT, 'CordovaLib', 'CordovaLib.xcodeproj', 'project.pbxproj'), path.join(projectPath, 'CordovaLib', 'CordovaLib.xcodeproj', 'project.pbxproj'));
  59. update_cordova_subproject([path.join(`${r}.xcodeproj`, 'project.pbxproj'), path.join(projectPath, 'CordovaLib', 'CordovaLib.xcodeproj', 'project.pbxproj'), config]);
  60. }
  61. }
  62. function copyScripts (projectPath, projectName) {
  63. const srcScriptsDir = path.join(ROOT, 'bin', 'templates', 'scripts', 'cordova');
  64. const destScriptsDir = path.join(projectPath, 'cordova');
  65. // Delete old scripts directory.
  66. fs.removeSync(destScriptsDir);
  67. // Copy in the new ones.
  68. const binDir = path.join(ROOT, 'bin');
  69. fs.copySync(srcScriptsDir, destScriptsDir);
  70. const nodeModulesDir = path.join(ROOT, 'node_modules');
  71. if (fs.existsSync(nodeModulesDir)) fs.copySync(nodeModulesDir, path.join(destScriptsDir, 'node_modules'));
  72. // Copy the check_reqs script
  73. fs.copySync(path.join(binDir, 'check_reqs'), path.join(destScriptsDir, 'check_reqs'));
  74. fs.copySync(path.join(binDir, 'check_reqs.bat'), path.join(destScriptsDir, 'check_reqs.bat'));
  75. // Copy the version scripts
  76. fs.copySync(path.join(binDir, 'apple_ios_version'), path.join(destScriptsDir, 'apple_ios_version'));
  77. fs.copySync(path.join(binDir, 'apple_osx_version'), path.join(destScriptsDir, 'apple_osx_version'));
  78. fs.copySync(path.join(binDir, 'apple_xcode_version'), path.join(destScriptsDir, 'apple_xcode_version'));
  79. // TODO: the two files being edited on-the-fly here are shared between
  80. // platform and project-level commands. the below `sed` is updating the
  81. // `require` path for the two libraries. if there's a better way to share
  82. // modules across both the repo and generated projects, we should make sure
  83. // to remove/update this.
  84. const path_regex = /templates\/scripts\/cordova\//;
  85. utils.replaceFileContents(path.join(destScriptsDir, 'check_reqs'), path_regex, '');
  86. utils.replaceFileContents(path.join(destScriptsDir, 'apple_ios_version'), path_regex, '');
  87. utils.replaceFileContents(path.join(destScriptsDir, 'apple_osx_version'), path_regex, '');
  88. utils.replaceFileContents(path.join(destScriptsDir, 'apple_xcode_version'), path_regex, '');
  89. // CB-11792 do a token replace for __PROJECT_NAME__ in .xcconfig
  90. const project_name_esc = projectName.replace(/&/g, '\\&');
  91. utils.replaceFileContents(path.join(destScriptsDir, 'build-debug.xcconfig'), /__PROJECT_NAME__/g, project_name_esc);
  92. utils.replaceFileContents(path.join(destScriptsDir, 'build-release.xcconfig'), /__PROJECT_NAME__/g, project_name_esc);
  93. }
  94. /*
  95. * Copy project template files into cordova project.
  96. *
  97. * @param {String} project_path path to cordova project
  98. * @param {String} project_name name of cordova project
  99. * @param {String} project_template_dir path to cordova-ios template directory
  100. * @parm {BOOL} use_cli true if cli project
  101. */
  102. function copyTemplateFiles (project_path, project_name, project_template_dir, package_name) {
  103. const r = path.join(project_path, project_name);
  104. fs.removeSync(path.join(`${r}.xcodeproj`));
  105. fs.copySync(path.join(project_template_dir, '__TEMP__.xcodeproj'), path.join(`${project_path}/__TEMP__.xcodeproj`));
  106. fs.moveSync(path.join(project_path, '__TEMP__.xcodeproj'), path.join(`${r}.xcodeproj`));
  107. fs.removeSync(path.join(project_path, `${project_name}.xcworkspace`));
  108. fs.copySync(path.join(project_template_dir, '__TEMP__.xcworkspace'), path.join(`${project_path}/__TEMP__.xcworkspace`));
  109. fs.moveSync(path.join(project_path, '__TEMP__.xcworkspace'), path.join(`${r}.xcworkspace`));
  110. fs.moveSync(path.join(`${r}.xcworkspace`, 'xcshareddata', 'xcschemes', '__PROJECT_NAME__.xcscheme'), path.join(`${r}.xcworkspace`, 'xcshareddata', 'xcschemes', `${project_name}.xcscheme`));
  111. fs.removeSync(r);
  112. fs.copySync(path.join(project_template_dir, '__PROJECT_NAME__'), path.join(`${project_path}/__PROJECT_NAME__`));
  113. fs.moveSync(path.join(project_path, '__PROJECT_NAME__'), r);
  114. fs.moveSync(path.join(r, '__PROJECT_NAME__-Info.plist'), path.join(r, `${project_name}-Info.plist`));
  115. fs.moveSync(path.join(r, '__PROJECT_NAME__-Prefix.pch'), path.join(r, `${project_name}-Prefix.pch`));
  116. fs.moveSync(path.join(r, 'gitignore'), path.join(r, '.gitignore'));
  117. /* replace __PROJECT_NAME__ and __PROJECT_ID__ with ACTIVITY and ID strings, respectively, in:
  118. *
  119. * - ./__PROJECT_NAME__.xcodeproj/project.pbxproj
  120. * - ./__PROJECT_NAME__/Classes/AppDelegate.h
  121. * - ./__PROJECT_NAME__/Classes/AppDelegate.m
  122. * - ./__PROJECT_NAME__/Classes/MainViewController.h
  123. * - ./__PROJECT_NAME__/Classes/MainViewController.m
  124. * - ./__PROJECT_NAME__/Resources/main.m
  125. * - ./__PROJECT_NAME__/Resources/__PROJECT_NAME__-info.plist
  126. * - ./__PROJECT_NAME__/Resources/__PROJECT_NAME__-Prefix.plist
  127. */
  128. // https://issues.apache.org/jira/browse/CB-12402 - Encode XML characters properly
  129. const project_name_xml_esc = xmlescape(project_name);
  130. utils.replaceFileContents(path.join(`${r}.xcworkspace`, 'contents.xcworkspacedata'), /__PROJECT_NAME__/g, project_name_xml_esc);
  131. utils.replaceFileContents(path.join(`${r}.xcworkspace`, 'xcshareddata', 'xcschemes', `${project_name}.xcscheme`), /__PROJECT_NAME__/g, project_name_xml_esc);
  132. const project_name_esc = project_name.replace(/&/g, '\\&');
  133. utils.replaceFileContents(path.join(`${r}.xcodeproj`, 'project.pbxproj'), /__PROJECT_NAME__/g, project_name_esc);
  134. utils.replaceFileContents(path.join(`${r}.xcodeproj`, 'project.pbxproj'), /__PROJECT_ID__/g, package_name);
  135. utils.replaceFileContents(path.join(r, 'Classes', 'AppDelegate.h'), /__PROJECT_NAME__/g, project_name_esc);
  136. utils.replaceFileContents(path.join(r, 'Classes', 'AppDelegate.m'), /__PROJECT_NAME__/g, project_name_esc);
  137. utils.replaceFileContents(path.join(r, 'Classes', 'MainViewController.h'), /__PROJECT_NAME__/g, project_name_esc);
  138. utils.replaceFileContents(path.join(r, 'Classes', 'MainViewController.m'), /__PROJECT_NAME__/g, project_name_esc);
  139. utils.replaceFileContents(path.join(r, 'main.m'), /__PROJECT_NAME__/g, project_name_esc);
  140. utils.replaceFileContents(path.join(r, `${project_name}-Info.plist`), /__PROJECT_NAME__/g, project_name_esc);
  141. utils.replaceFileContents(path.join(r, `${project_name}-Prefix.pch`), /__PROJECT_NAME__/g, project_name_esc);
  142. }
  143. function AbsParentPath (_path) {
  144. return path.resolve(path.dirname(_path));
  145. }
  146. function AbsProjectPath (relative_path) {
  147. let absolute_path = path.resolve(relative_path);
  148. if (/.pbxproj$/.test(absolute_path)) {
  149. absolute_path = AbsParentPath(absolute_path);
  150. } else if (!(/.xcodeproj$/.test(absolute_path))) {
  151. throw new Error(`The following is not a valid path to an Xcode project: ${absolute_path}`);
  152. }
  153. return absolute_path;
  154. }
  155. function relpath (_path, start) {
  156. start = start || process.cwd();
  157. return path.relative(path.resolve(start), path.resolve(_path));
  158. }
  159. /*
  160. * Creates a new iOS project with the following options:
  161. *
  162. * - --link (optional): Link directly against the shared copy of the CordovaLib instead of a copy of it
  163. * - --cli (optional): Use the CLI-project template
  164. * - <path_to_new_project>: Path to your new Cordova iOS project
  165. * - <package_name>: Package name, following reverse-domain style convention
  166. * - <project_name>: Project name
  167. * - <project_template_dir>: Path to a project template (override)
  168. *
  169. */
  170. exports.createProject = (project_path, package_name, project_name, opts, config) => {
  171. package_name = package_name || 'my.cordova.project';
  172. project_name = project_name || 'CordovaExample';
  173. const use_shared = !!opts.link;
  174. const bin_dir = path.join(ROOT, 'bin');
  175. const project_parent = path.dirname(project_path);
  176. const project_template_dir = opts.customTemplate || path.join(bin_dir, 'templates', 'project');
  177. // check that project path doesn't exist
  178. if (fs.existsSync(project_path)) {
  179. return Promise.reject(new CordovaError('Project already exists'));
  180. }
  181. // check that parent directory does exist so cp -r will not fail
  182. if (!fs.existsSync(project_parent)) {
  183. return Promise.reject(new CordovaError(`Parent directory "${project_parent}" of given project path does not exist`));
  184. }
  185. events.emit('log', 'Creating Cordova project for the iOS platform:');
  186. events.emit('log', `\tPath: ${path.relative(process.cwd(), project_path)}`);
  187. events.emit('log', `\tPackage: ${package_name}`);
  188. events.emit('log', `\tName: ${project_name}`);
  189. events.emit('verbose', `Copying iOS template project to ${project_path}`);
  190. // create the project directory and copy over files
  191. fs.ensureDirSync(project_path);
  192. fs.copySync(path.join(project_template_dir, 'www'), path.join(project_path, 'www'));
  193. // Copy project template files
  194. copyTemplateFiles(project_path, project_name, project_template_dir, package_name);
  195. // Copy xcconfig files
  196. fs.copySync(path.join(project_template_dir, 'pods-debug.xcconfig'), path.join(project_path, 'pods-debug.xcconfig'));
  197. fs.copySync(path.join(project_template_dir, 'pods-release.xcconfig'), path.join(project_path, 'pods-release.xcconfig'));
  198. // CordovaLib stuff
  199. copyJsAndCordovaLib(project_path, project_name, use_shared, config);
  200. copyScripts(project_path, project_name);
  201. events.emit('log', generateDoneMessage('create', use_shared));
  202. return Promise.resolve();
  203. };
  204. exports.updateProject = (projectPath, opts) => {
  205. const errorString =
  206. 'An in-place platform update is not supported. \n' +
  207. 'The `platforms` folder is always treated as a build artifact.\n' +
  208. 'To update your platform, you have to remove, then add your ios platform again.\n' +
  209. 'Make sure you save your plugins beforehand using `cordova plugin save`, and save a copy of the platform first if you had manual changes in it.\n' +
  210. '\tcordova plugin save\n' +
  211. '\tcordova platform rm ios\n' +
  212. '\tcordova platform add ios\n';
  213. return Promise.reject(new CordovaError(errorString));
  214. };
  215. function generateDoneMessage (type, link) {
  216. const pkg = require('../../package');
  217. let msg = `iOS project ${type === 'update' ? 'updated' : 'created'} with ${pkg.name}@${pkg.version}`;
  218. if (link) {
  219. msg += ' and has a linked CordovaLib';
  220. }
  221. return msg;
  222. }
  223. function update_cordova_subproject (argv) {
  224. if (argv.length < 1 || argv.length > 3) {
  225. updateSubprojectHelp();
  226. throw new Error('Usage error for update_cordova_subproject');
  227. }
  228. const projectPath = AbsProjectPath(argv[0]);
  229. let cordovaLibXcodePath;
  230. if (argv.length < 3) {
  231. cordovaLibXcodePath = path.join(ROOT, 'CordovaLib', 'CordovaLib.xcodeproj');
  232. } else {
  233. cordovaLibXcodePath = AbsProjectPath(argv[1]);
  234. }
  235. const parentProjectPath = AbsParentPath(projectPath);
  236. const subprojectPath = relpath(cordovaLibXcodePath, parentProjectPath);
  237. const projectPbxprojPath = path.join(projectPath, 'project.pbxproj');
  238. const line = utils.grep(
  239. projectPbxprojPath,
  240. /(.+CordovaLib.xcodeproj.+PBXFileReference.+wrapper.pb-project.+)(path = .+?;)(.*)(sourceTree.+;)(.+)/
  241. );
  242. if (!line) {
  243. throw new Error(`Entry not found in project file for sub-project: ${subprojectPath}`);
  244. }
  245. let newLine = line
  246. .replace(/path = .+?;/, `path = ${subprojectPath};`)
  247. .replace(/sourceTree.+?;/, 'sourceTree = \"<group>\";'); /* eslint no-useless-escape : 0 */
  248. if (!newLine.match('name')) {
  249. newLine = newLine.replace('path = ', 'name = CordovaLib.xcodeproj; path = ');
  250. }
  251. utils.replaceFileContents(projectPbxprojPath, line, newLine);
  252. }
  253. exports.updateSubprojectHelp = updateSubprojectHelp;
  254. exports.update_cordova_subproject = update_cordova_subproject;