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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. var fs = require('fs');
  18. var path = require('path');
  19. var properties_parser = require('properties-parser');
  20. var AndroidManifest = require('./AndroidManifest');
  21. var pluginHandlers = require('./pluginHandlers');
  22. var projectFileCache = {};
  23. function addToPropertyList (projectProperties, key, value) {
  24. var i = 1;
  25. while (projectProperties.get(key + '.' + i)) { i++; }
  26. projectProperties.set(key + '.' + i, value);
  27. projectProperties.dirty = true;
  28. }
  29. function removeFromPropertyList (projectProperties, key, value) {
  30. var i = 1;
  31. var currentValue;
  32. while ((currentValue = projectProperties.get(key + '.' + i))) {
  33. if (currentValue === value) {
  34. while ((currentValue = projectProperties.get(key + '.' + (i + 1)))) {
  35. projectProperties.set(key + '.' + i, currentValue);
  36. i++;
  37. }
  38. projectProperties.set(key + '.' + i);
  39. break;
  40. }
  41. i++;
  42. }
  43. projectProperties.dirty = true;
  44. }
  45. function getRelativeLibraryPath (parentDir, subDir) {
  46. var libraryPath = path.relative(parentDir, subDir);
  47. return (path.sep === '\\') ? libraryPath.replace(/\\/g, '/') : libraryPath;
  48. }
  49. function AndroidProject (projectDir) {
  50. this._propertiesEditors = {};
  51. this._subProjectDirs = {};
  52. this._dirty = false;
  53. this.projectDir = projectDir;
  54. this.platformWww = path.join(this.projectDir, 'platform_www');
  55. this.www = path.join(this.projectDir, 'app/src/main/assets/www');
  56. }
  57. AndroidProject.getProjectFile = function (projectDir) {
  58. if (!projectFileCache[projectDir]) {
  59. projectFileCache[projectDir] = new AndroidProject(projectDir);
  60. }
  61. return projectFileCache[projectDir];
  62. };
  63. AndroidProject.purgeCache = function (projectDir) {
  64. if (projectDir) {
  65. delete projectFileCache[projectDir];
  66. } else {
  67. projectFileCache = {};
  68. }
  69. };
  70. /**
  71. * Reads the package name out of the Android Manifest file
  72. *
  73. * @param {String} projectDir The absolute path to the directory containing the project
  74. *
  75. * @return {String} The name of the package
  76. */
  77. AndroidProject.prototype.getPackageName = function () {
  78. var manifestPath = path.join(this.projectDir, 'app/src/main/AndroidManifest.xml');
  79. return new AndroidManifest(manifestPath).getPackageId();
  80. };
  81. AndroidProject.prototype.getCustomSubprojectRelativeDir = function (plugin_id, src) {
  82. // All custom subprojects are prefixed with the last portion of the package id.
  83. // This is to avoid collisions when opening multiple projects in Eclipse that have subprojects with the same name.
  84. var packageName = this.getPackageName();
  85. var lastDotIndex = packageName.lastIndexOf('.');
  86. var prefix = packageName.substring(lastDotIndex + 1);
  87. var subRelativeDir = path.join(plugin_id, prefix + '-' + path.basename(src));
  88. return subRelativeDir;
  89. };
  90. AndroidProject.prototype.addSubProject = function (parentDir, subDir) {
  91. var parentProjectFile = path.resolve(parentDir, 'project.properties');
  92. var subProjectFile = path.resolve(subDir, 'project.properties');
  93. var parentProperties = this._getPropertiesFile(parentProjectFile);
  94. // TODO: Setting the target needs to happen only for pre-3.7.0 projects
  95. if (fs.existsSync(subProjectFile)) {
  96. var subProperties = this._getPropertiesFile(subProjectFile);
  97. subProperties.set('target', parentProperties.get('target'));
  98. subProperties.dirty = true;
  99. this._subProjectDirs[subDir] = true;
  100. }
  101. addToPropertyList(parentProperties, 'android.library.reference', getRelativeLibraryPath(parentDir, subDir));
  102. this._dirty = true;
  103. };
  104. AndroidProject.prototype.removeSubProject = function (parentDir, subDir) {
  105. var parentProjectFile = path.resolve(parentDir, 'project.properties');
  106. var parentProperties = this._getPropertiesFile(parentProjectFile);
  107. removeFromPropertyList(parentProperties, 'android.library.reference', getRelativeLibraryPath(parentDir, subDir));
  108. delete this._subProjectDirs[subDir];
  109. this._dirty = true;
  110. };
  111. AndroidProject.prototype.addGradleReference = function (parentDir, subDir) {
  112. var parentProjectFile = path.resolve(parentDir, 'project.properties');
  113. var parentProperties = this._getPropertiesFile(parentProjectFile);
  114. addToPropertyList(parentProperties, 'cordova.gradle.include', getRelativeLibraryPath(parentDir, subDir));
  115. this._dirty = true;
  116. };
  117. AndroidProject.prototype.removeGradleReference = function (parentDir, subDir) {
  118. var parentProjectFile = path.resolve(parentDir, 'project.properties');
  119. var parentProperties = this._getPropertiesFile(parentProjectFile);
  120. removeFromPropertyList(parentProperties, 'cordova.gradle.include', getRelativeLibraryPath(parentDir, subDir));
  121. this._dirty = true;
  122. };
  123. AndroidProject.prototype.addSystemLibrary = function (parentDir, value) {
  124. var parentProjectFile = path.resolve(parentDir, 'project.properties');
  125. var parentProperties = this._getPropertiesFile(parentProjectFile);
  126. addToPropertyList(parentProperties, 'cordova.system.library', value);
  127. this._dirty = true;
  128. };
  129. AndroidProject.prototype.removeSystemLibrary = function (parentDir, value) {
  130. var parentProjectFile = path.resolve(parentDir, 'project.properties');
  131. var parentProperties = this._getPropertiesFile(parentProjectFile);
  132. removeFromPropertyList(parentProperties, 'cordova.system.library', value);
  133. this._dirty = true;
  134. };
  135. AndroidProject.prototype.write = function () {
  136. if (!this._dirty) {
  137. return;
  138. }
  139. this._dirty = false;
  140. for (var filename in this._propertiesEditors) {
  141. var editor = this._propertiesEditors[filename];
  142. if (editor.dirty) {
  143. fs.writeFileSync(filename, editor.toString());
  144. editor.dirty = false;
  145. }
  146. }
  147. };
  148. AndroidProject.prototype._getPropertiesFile = function (filename) {
  149. if (!this._propertiesEditors[filename]) {
  150. if (fs.existsSync(filename)) {
  151. this._propertiesEditors[filename] = properties_parser.createEditor(filename);
  152. } else {
  153. this._propertiesEditors[filename] = properties_parser.createEditor();
  154. }
  155. }
  156. return this._propertiesEditors[filename];
  157. };
  158. AndroidProject.prototype.getInstaller = function (type) {
  159. return pluginHandlers.getInstaller(type);
  160. };
  161. AndroidProject.prototype.getUninstaller = function (type) {
  162. return pluginHandlers.getUninstaller(type);
  163. };
  164. /*
  165. * This checks if an Android project is clean or has old build artifacts
  166. */
  167. AndroidProject.prototype.isClean = function () {
  168. var build_path = path.join(this.projectDir, 'build');
  169. // If the build directory doesn't exist, it's clean
  170. return !(fs.existsSync(build_path));
  171. };
  172. module.exports = AndroidProject;