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

create.js 15KB

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