react-native-navigation的迁移库

gradleLinker.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. class GradleLinker {
  8. constructor() {
  9. this.gradlePath = path.rootGradle;
  10. }
  11. link() {
  12. logn('Linking root build.gradle...');
  13. if (this.gradlePath) {
  14. var contents = fs.readFileSync(this.gradlePath, 'utf8');
  15. contents = this._setKotlinVersion(contents);
  16. contents = this._setKotlinPluginDependency(contents);
  17. fs.writeFileSync(this.gradlePath, contents);
  18. infon('Root build.gradle linked successfully!\n');
  19. } else {
  20. warnn(' Root build.gradle not found!');
  21. }
  22. }
  23. _setKotlinPluginDependency(contents) {
  24. if (this._isKotlinPluginDeclared(contents)) {
  25. warnn(' Kotlin plugin already declared')
  26. return contents;
  27. }
  28. var match = /classpath\s*\(*["']com\.android\.tools\.build:gradle:/.exec(contents);
  29. if (match) {
  30. debugn(" Adding Kotlin plugin");
  31. return insertString(contents, match.index, `classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${DEFAULT_KOTLIN_VERSION}"\n `);
  32. } else {
  33. errorn(" Could not add kotlin plugin dependency")
  34. }
  35. return contents;
  36. }
  37. _setKotlinVersion(contents) {
  38. if (this._isKotlinVersionSpecified(contents)) {
  39. warnn(" Kotlin version already specified");
  40. } else {
  41. var kotlinVersion = this._getKotlinVersion(contents);
  42. if (this._hasExtensionVariablesBlock(contents)) {
  43. debugn(" Adding RNNKotlinVersion to extension block");
  44. return contents.replace(/ext\s*{/, `ext {\n RNNKotlinVersion = ${kotlinVersion}`);
  45. } else {
  46. debugn(" Adding RNNKotlinVersion extension variable");
  47. return contents.replace(/buildscript\s*{/, `buildscript {\n ext.RNNKotlinVersion = ${kotlinVersion}`);
  48. }
  49. }
  50. return contents;
  51. }
  52. /**
  53. * @param { string } contents
  54. */
  55. _getKotlinVersion(contents) {
  56. var hardCodedVersion = contents.match(/(?<=kotlin-gradle-plugin:)\$*[\d\.]{3,}/);
  57. if (hardCodedVersion && hardCodedVersion.length > 0) {
  58. return `"${hardCodedVersion[0]}"`;
  59. }
  60. var extensionVariableVersion = contents.match(/(?<=kotlin-gradle-plugin:)\$*[a-zA-Z\d\.]*/);
  61. if (extensionVariableVersion && extensionVariableVersion.length > 0) {
  62. return extensionVariableVersion[0].replace("$", "");
  63. }
  64. return `"${DEFAULT_KOTLIN_VERSION}"`;
  65. }
  66. /**
  67. * @param {string} contents
  68. */
  69. _hasExtensionVariablesBlock(contents) {
  70. return /ext\s*{/.test(contents);
  71. }
  72. /**
  73. * @param {string} contents
  74. */
  75. _isKotlinVersionSpecified(contents) {
  76. return /RNNKotlinVersion/.test(contents);
  77. }
  78. /**
  79. * @param {string} contents
  80. */
  81. _isKotlinPluginDeclared(contents) {
  82. return /org.jetbrains.kotlin:kotlin-gradle-plugin:/.test(contents);
  83. }
  84. }
  85. module.exports = GradleLinker;