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
 var { warnn, errorn, logn, infon, debugn } = require('./log');
4
 var { warnn, errorn, logn, infon, debugn } = require('./log');
5
 var { insertString } = require('./stringUtils');
5
 var { insertString } = require('./stringUtils');
6
 var DEFAULT_KOTLIN_VERSION = '1.3.61';
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
 class GradleLinker {
10
 class GradleLinker {
9
   constructor() {
11
   constructor() {
16
       var contents = fs.readFileSync(this.gradlePath, 'utf8');
18
       var contents = fs.readFileSync(this.gradlePath, 'utf8');
17
       contents = this._setKotlinVersion(contents);
19
       contents = this._setKotlinVersion(contents);
18
       contents = this._setKotlinPluginDependency(contents);
20
       contents = this._setKotlinPluginDependency(contents);
21
+      contents = this._setMinSdkVersion(contents);
19
       fs.writeFileSync(this.gradlePath, contents);
22
       fs.writeFileSync(this.gradlePath, contents);
20
       infon('Root build.gradle linked successfully!\n');
23
       infon('Root build.gradle linked successfully!\n');
21
     } else {
24
     } else {
54
     return contents;
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
    * @param { string } contents
77
    * @param { string } contents
59
    */
78
    */
69
     return `"${DEFAULT_KOTLIN_VERSION}"`;
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
    * @param {string} contents
107
    * @param {string} contents
74
    */
108
    */

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

9
 exports.mainApplicationJava = mainApplicationJava;
9
 exports.mainApplicationJava = mainApplicationJava;
10
 exports.rootGradle = mainApplicationJava.replace(/android\/app\/.*\.java/, "android/build.gradle");;
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

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