react-native-navigation的迁移库

gradleLinker.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // @ts-check
  2. var path = require('./path');
  3. var fs = require('fs');
  4. var { warnn, errorn, logn, infon, debugn } = require('./log');
  5. var { insertString } = require('./stringUtils');
  6. var DEFAULT_KOTLIN_VERSION = '1.3.61';
  7. // This should be the minSdkVersion required for RNN.
  8. var DEFAULT_MIN_SDK_VERSION = 19
  9. class GradleLinker {
  10. constructor() {
  11. this.gradlePath = path.rootGradle;
  12. }
  13. link() {
  14. logn('Linking root build.gradle...');
  15. if (this.gradlePath) {
  16. var contents = fs.readFileSync(this.gradlePath, 'utf8');
  17. contents = this._setKotlinVersion(contents);
  18. contents = this._setKotlinPluginDependency(contents);
  19. contents = this._setMinSdkVersion(contents);
  20. fs.writeFileSync(this.gradlePath, contents);
  21. infon('Root build.gradle linked successfully!\n');
  22. } else {
  23. warnn(' Root build.gradle not found!');
  24. }
  25. }
  26. _setKotlinPluginDependency(contents) {
  27. if (this._isKotlinPluginDeclared(contents)) {
  28. warnn(' Kotlin plugin already declared')
  29. return contents;
  30. }
  31. var match = /classpath\s*\(*["']com\.android\.tools\.build:gradle:/.exec(contents);
  32. if (match) {
  33. debugn(" Adding Kotlin plugin");
  34. return insertString(contents, match.index, `classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${DEFAULT_KOTLIN_VERSION}"\n `);
  35. } else {
  36. errorn(" Could not add kotlin plugin dependency")
  37. }
  38. return contents;
  39. }
  40. _setKotlinVersion(contents) {
  41. if (this._isKotlinVersionSpecified(contents)) {
  42. warnn(" Kotlin version already specified");
  43. } else {
  44. var kotlinVersion = this._getKotlinVersion(contents);
  45. if (this._hasExtensionVariablesBlock(contents)) {
  46. debugn(" Adding RNNKotlinVersion to extension block");
  47. return contents.replace(/ext\s*{/, `ext {\n RNNKotlinVersion = ${kotlinVersion}`);
  48. } else {
  49. debugn(" Adding RNNKotlinVersion extension variable");
  50. return contents.replace(/buildscript\s*{/, `buildscript {\n ext.RNNKotlinVersion = ${kotlinVersion}`);
  51. }
  52. }
  53. return contents;
  54. }
  55. /**
  56. * Check the current minSdkVersion specified and if it's lower than
  57. * the required version, set it to the required version otherwise leave as it is.
  58. */
  59. _setMinSdkVersion(contents) {
  60. var minSdkVersion = this._getMinSdkVersion(contents)
  61. // If user entered minSdkVersion is lower than the default, set it to default.
  62. if (minSdkVersion < DEFAULT_MIN_SDK_VERSION) {
  63. debugn(` Updating minSdkVersion to ${DEFAULT_MIN_SDK_VERSION}`)
  64. return contents.replace(/minSdkVersion\s{0,}=\s{0,}\d*/, `minSdkVersion = ${DEFAULT_MIN_SDK_VERSION}`)
  65. }
  66. debugn(` Already specified minSdkVersion ${minSdkVersion}`)
  67. return contents.replace(/minSdkVersion\s{0,}=\s{0,}\d*/, `minSdkVersion = ${minSdkVersion}`)
  68. }
  69. /**
  70. * @param { string } contents
  71. */
  72. _getKotlinVersion(contents) {
  73. var hardCodedVersion = contents.match(/(?<=kotlin-gradle-plugin:)\$*[\d\.]{3,}/);
  74. if (hardCodedVersion && hardCodedVersion.length > 0) {
  75. return `"${hardCodedVersion[0]}"`;
  76. }
  77. var extensionVariableVersion = contents.match(/(?<=kotlin-gradle-plugin:)\$*[a-zA-Z\d\.]*/);
  78. if (extensionVariableVersion && extensionVariableVersion.length > 0) {
  79. return extensionVariableVersion[0].replace("$", "");
  80. }
  81. return `"${DEFAULT_KOTLIN_VERSION}"`;
  82. }
  83. /**
  84. * Get the minSdkVersion value.
  85. * @param { string } contents
  86. */
  87. _getMinSdkVersion(contents) {
  88. var minSdkVersion = contents.match(/minSdkVersion\s{0,}=\s{0,}(\d*)/)
  89. if (minSdkVersion && minSdkVersion[1]) {
  90. // It'd be something like 16 for a fresh React Native project.
  91. return +minSdkVersion[1]
  92. }
  93. return DEFAULT_MIN_SDK_VERSION
  94. }
  95. /**
  96. * @param {string} contents
  97. */
  98. _hasExtensionVariablesBlock(contents) {
  99. return /ext\s*{/.test(contents);
  100. }
  101. /**
  102. * @param {string} contents
  103. */
  104. _isKotlinVersionSpecified(contents) {
  105. return /RNNKotlinVersion/.test(contents);
  106. }
  107. /**
  108. * @param {string} contents
  109. */
  110. _isKotlinPluginDeclared(contents) {
  111. return /org.jetbrains.kotlin:kotlin-gradle-plugin:/.test(contents);
  112. }
  113. }
  114. module.exports = GradleLinker;