GradlePropertiesParser.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. let fs = require('fs');
  18. let path = require('path');
  19. let propertiesParser = require('properties-parser');
  20. let events = require('cordova-common').events;
  21. class GradlePropertiesParser {
  22. /**
  23. * Loads and Edits Gradle Properties File.
  24. *
  25. * @param {String} platformDir is the path of the Android platform directory
  26. */
  27. constructor (platformDir) {
  28. this._defaults = {
  29. // 10 seconds -> 6 seconds
  30. 'org.gradle.daemon': 'true',
  31. // to allow dex in process
  32. 'org.gradle.jvmargs': '-Xmx2048m',
  33. // allow NDK to be used - required by Gradle 1.5 plugin
  34. 'android.useDeprecatedNdk': 'true'
  35. // Shaves another 100ms, but produces a "try at own risk" warning. Not worth it (yet):
  36. // 'org.gradle.parallel': 'true'
  37. };
  38. this.gradleFilePath = path.join(platformDir, 'gradle.properties');
  39. }
  40. configure (userConfigs) {
  41. events.emit('verbose', '[Gradle Properties] Preparing Configuration');
  42. this._initializeEditor();
  43. events.emit('verbose', '[Gradle Properties] Appending default configuration properties');
  44. this._configureProperties(this._defaults);
  45. events.emit('verbose', '[Gradle Properties] Appending custom configuration properties');
  46. this._configureProperties(userConfigs);
  47. this._save();
  48. }
  49. /**
  50. * Initialize the properties editor for parsing, setting, etc.
  51. */
  52. _initializeEditor () {
  53. // Touch empty gradle.properties file if missing.
  54. if (!fs.existsSync(this.gradleFilePath)) {
  55. events.emit('verbose', '[Gradle Properties] File missing, creating file with Cordova defaults.');
  56. fs.writeFileSync(this.gradleFilePath, '', 'utf-8');
  57. }
  58. // Create an editor for parsing, getting, and setting configurations.
  59. this.gradleFile = propertiesParser.createEditor(this.gradleFilePath);
  60. }
  61. /**
  62. * Validate that defaults or user configuration properties are set and
  63. * set the missing items.
  64. */
  65. _configureProperties (properties) {
  66. // Iterate though the properties and set only if missing.
  67. Object.keys(properties).forEach(key => {
  68. let value = this.gradleFile.get(key);
  69. if (!value) {
  70. // Handles the case of adding missing defaults or new properties that are missing.
  71. events.emit('verbose', `[Gradle Properties] Appending configuration item: ${key}=${properties[key]}`);
  72. this.gradleFile.set(key, properties[key]);
  73. } else if (value !== properties[key]) {
  74. if (this._defaults[key] && this._defaults[key] !== properties[key]) {
  75. // Since the value does not match default, we will notify the discrepancy with Cordova's recommended value.
  76. events.emit('info', `[Gradle Properties] Detected Gradle property "${key}" with the value of "${properties[key]}", Cordova's recommended value is "${this._defaults[key]}"`);
  77. } else {
  78. // When the current value exists but does not match the new value or does matches the default key value, the new value it set.
  79. events.emit('verbose', `[Gradle Properties] Updating Gradle property "${key}" with the value of "${properties[key]}"`);
  80. }
  81. // We will set the new value in either case.
  82. this.gradleFile.set(key, properties[key]);
  83. }
  84. });
  85. }
  86. /**
  87. * Saves any changes that has been made to the properties file.
  88. */
  89. _save () {
  90. events.emit('verbose', '[Gradle Properties] Updating and Saving File');
  91. this.gradleFile.save();
  92. }
  93. }
  94. module.exports = GradlePropertiesParser;