Browse Source

Fix autolink script (#5990)

This PR fixes the autolink script including:
* Specifying the minSdkVersion for Android. Closes #5983.
* Removing the RNN Pod added by the react-native link script.
Jin Shin 4 years ago
parent
commit
4ce0e89b06
No account linked to committer's email address

+ 34
- 0
autolink/postlink/gradleLinker.js View File

@@ -4,6 +4,8 @@ var fs = require('fs');
4 4
 var { warnn, errorn, logn, infon, debugn } = require('./log');
5 5
 var { insertString } = require('./stringUtils');
6 6
 var DEFAULT_KOTLIN_VERSION = '1.3.61';
7
+// This should be the minSdkVersion required for RNN.
8
+var DEFAULT_MIN_SDK_VERSION = 19
7 9
 
8 10
 class GradleLinker {
9 11
   constructor() {
@@ -16,6 +18,7 @@ class GradleLinker {
16 18
       var contents = fs.readFileSync(this.gradlePath, 'utf8');
17 19
       contents = this._setKotlinVersion(contents);
18 20
       contents = this._setKotlinPluginDependency(contents);
21
+      contents = this._setMinSdkVersion(contents);
19 22
       fs.writeFileSync(this.gradlePath, contents);
20 23
       infon('Root build.gradle linked successfully!\n');
21 24
     } else {
@@ -54,6 +57,22 @@ class GradleLinker {
54 57
     return contents;
55 58
   }
56 59
 
60
+  /**
61
+   * Check the current minSdkVersion specified and if it's lower than
62
+   * the required version, set it to the required version otherwise leave as it is.
63
+   */
64
+  _setMinSdkVersion(contents) {
65
+    var minSdkVersion = this._getMinSdkVersion(contents)
66
+    // If user entered minSdkVersion is lower than the default, set it to default.
67
+    if (minSdkVersion < DEFAULT_MIN_SDK_VERSION) {
68
+      debugn(`   Updating minSdkVersion to ${DEFAULT_MIN_SDK_VERSION}`)
69
+      return contents.replace(/minSdkVersion\s{0,}=\s{0,}\d*/, `minSdkVersion = ${DEFAULT_MIN_SDK_VERSION}`)
70
+    } 
71
+
72
+    debugn(`   Already specified minSdkVersion ${minSdkVersion}`)
73
+    return contents.replace(/minSdkVersion\s{0,}=\s{0,}\d*/, `minSdkVersion = ${minSdkVersion}`)
74
+  }
75
+
57 76
   /**
58 77
    * @param { string } contents
59 78
    */
@@ -69,6 +88,21 @@ class GradleLinker {
69 88
     return `"${DEFAULT_KOTLIN_VERSION}"`;
70 89
   }
71 90
 
91
+  /**
92
+   * Get the minSdkVersion value.
93
+   * @param { string } contents
94
+   */
95
+  _getMinSdkVersion(contents) {
96
+    var minSdkVersion = contents.match(/minSdkVersion\s{0,}=\s{0,}(\d*)/)
97
+
98
+    if (minSdkVersion && minSdkVersion[1]) {
99
+      // It'd be something like 16 for a fresh React Native project.
100
+      return +minSdkVersion[1] 
101
+    }
102
+
103
+    return DEFAULT_MIN_SDK_VERSION
104
+  }
105
+
72 106
   /**
73 107
    * @param {string} contents
74 108
    */

+ 2
- 1
autolink/postlink/path.js View File

@@ -9,4 +9,5 @@ var mainApplicationJava = glob.sync("**/MainApplication.java", ignoreFolders)[0]
9 9
 exports.mainApplicationJava = mainApplicationJava;
10 10
 exports.rootGradle = mainApplicationJava.replace(/android\/app\/.*\.java/, "android/build.gradle");;
11 11
 
12
-exports.appDelegate = glob.sync("**/AppDelegate.m", ignoreFolders)[0];
12
+exports.appDelegate = glob.sync("**/AppDelegate.m", ignoreFolders)[0];
13
+exports.podFile = glob.sync("**/Podfile", ignoreFolders)[0]

+ 39
- 0
autolink/postlink/podfileLinker.js View File

@@ -0,0 +1,39 @@
1
+// @ts-check
2
+var path = require('./path');
3
+var fs = require("fs");
4
+var {logn, debugn, infon} = require("./log");
5
+
6
+class PodfileLinker {
7
+  constructor() {
8
+    this.podfilePath = path.podFile;
9
+  }
10
+
11
+  link() {
12
+    if (this.podfilePath) {
13
+      logn("Updating Podfile...")
14
+      var podfileContent = fs.readFileSync(this.podfilePath, "utf8");
15
+
16
+      podfileContent = this._removeRNNPodLink(podfileContent);
17
+
18
+      fs.writeFileSync(this.podfilePath, podfileContent);
19
+      infon("Podfile updated successfully!\n")
20
+    }
21
+  }
22
+
23
+  /**
24
+   * Removes the RNN pod added by react-native link script. 
25
+   */
26
+  _removeRNNPodLink(contents) {
27
+    const rnnPodLink  = contents.match(/\s+.*pod 'ReactNativeNavigation'.+react-native-navigation'/)
28
+
29
+    if (!rnnPodLink) {
30
+      debugn("   RNN Pod has not been added to Podfile")
31
+      return contents
32
+    }
33
+
34
+    debugn("   Removing RNN Pod from Podfile")
35
+    return contents.replace(rnnPodLink, "")    
36
+  }
37
+}
38
+
39
+module.exports = PodfileLinker

+ 2
- 0
autolink/postlink/postLinkIOS.js View File

@@ -1,7 +1,9 @@
1 1
 // @ts-check
2 2
 var AppDelegateLinker = require("./appDelegateLinker");
3
+var PodfileLinker = require('./podfileLinker');
3 4
 
4 5
 module.exports = () => {
5 6
   console.log("Running iOS postlink script\n");
6 7
   new AppDelegateLinker().link();
8
+  new PodfileLinker().link();
7 9
 }