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

projectActions.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 PluginInfoProvider = require('cordova-common').PluginInfoProvider;
  18. var shell = require('shelljs');
  19. var cp = require('child_process');
  20. var path = require('path');
  21. var util = require('util');
  22. var cordova_bin = path.join(__dirname, '../../../bin');
  23. /**
  24. * Creates a project using platform create script with given parameters
  25. * @param {string} projectname - name of the project
  26. * @param {string} projectid - id of the project
  27. * @param {string} platformpath - path to the platform
  28. * @param {function} callback - function which is called (without arguments) when the project is created or (with error object) when error occurs
  29. */
  30. module.exports.createProject = function (projectname, projectid, platformpath, callback) {
  31. // platformpath is optional
  32. if (!callback && typeof platformpath === 'function') {
  33. callback = platformpath;
  34. platformpath = null;
  35. }
  36. var projectDirName = getDirName(projectid);
  37. var createScriptPath = platformpath ? path.join(platformpath, 'bin/create') : path.join(cordova_bin, 'create');
  38. // remove existing folder
  39. module.exports.removeProject(projectid);
  40. // create the project
  41. var command = util.format('"%s" %s %s "%s"', createScriptPath, projectDirName, projectid, projectname);
  42. cp.exec(command, function (error, stdout, stderr) {
  43. if (error) {
  44. console.log(stdout);
  45. console.error(stderr);
  46. }
  47. callback(error);
  48. });
  49. };
  50. /**
  51. * Updates a project using platform update script with given parameters
  52. * @param {string} projectid - id of the project
  53. * @param {string} platformpath - path to the platform
  54. * @param {function} callback - function which is called (without arguments) when the project is updated or (with error object) when error occurs
  55. */
  56. module.exports.updateProject = function (projectid, platformpath, callback) {
  57. // platformpath is optional
  58. if (!callback && typeof platformpath === 'function') {
  59. callback = platformpath;
  60. platformpath = null;
  61. }
  62. var projectDirName = getDirName(projectid);
  63. var updateScriptPath = platformpath ? path.join(platformpath, 'bin/update') : path.join(cordova_bin, 'update');
  64. var command = util.format('"%s" %s', updateScriptPath, projectDirName);
  65. cp.exec(command, function (error, stdout, stderr) {
  66. if (error) {
  67. console.log(stdout);
  68. console.error(stderr);
  69. }
  70. callback(error);
  71. });
  72. };
  73. /**
  74. * Builds a project using platform build script with given parameters
  75. * @param {string} projectid - id of the project
  76. * @param {function} callback - function which is called (without arguments) when the project is built or (with error object) when error occurs
  77. */
  78. module.exports.buildProject = function (projectid, callback) {
  79. var projectDirName = getDirName(projectid);
  80. var command = path.join(projectDirName, 'cordova/build');
  81. cp.exec(command, function (error, stdout, stderr) {
  82. if (error) {
  83. console.log(stdout);
  84. console.error(stderr);
  85. }
  86. callback(error);
  87. });
  88. };
  89. /**
  90. * Removes a project
  91. * @param {string} projectid - id of the project
  92. */
  93. module.exports.removeProject = function (projectid) {
  94. var projectDirName = getDirName(projectid);
  95. shell.rm('-rf', projectDirName);
  96. };
  97. /**
  98. * Add a plugin to a project using platform api
  99. * @param {string} projectid - id of the project
  100. * @param {string} plugindir - path to a plugin
  101. * @param {function} callback - function which is called (without arguments) when the plugin is added or (with error object) when error occurs
  102. */
  103. module.exports.addPlugin = function (projectid, plugindir, callback) {
  104. var projectDirName = getDirName(projectid);
  105. var pip = new PluginInfoProvider();
  106. var pluginInfo = pip.get(plugindir);
  107. var Api = require(path.join(__dirname, '../../..', projectDirName, 'cordova', 'Api.js'));
  108. var api = new Api('android', projectDirName);
  109. api.addPlugin(pluginInfo).then(function () {
  110. callback(null);
  111. }, function (error) {
  112. console.error(error);
  113. callback(error);
  114. });
  115. };
  116. /**
  117. * Gets a version number from project using platform script
  118. * @param {string} projectid - id of the project
  119. * @param {function} callback - function which is called with platform version as an argument
  120. */
  121. module.exports.getPlatformVersion = function (projectid, callback) {
  122. var command = path.join(getDirName(projectid), 'cordova/version');
  123. cp.exec(command, function (error, stdout, stderr) {
  124. if (error) {
  125. console.log(stdout);
  126. console.error(stderr);
  127. }
  128. callback(stdout.trim());
  129. });
  130. };
  131. function getDirName (projectid) {
  132. return 'test-' + projectid;
  133. }