Skip to content

Commit

Permalink
Fix autolink script (#5990)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jinshin1013 authored Mar 3, 2020
1 parent a2f5dbd commit 4ce0e89
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
34 changes: 34 additions & 0 deletions autolink/postlink/gradleLinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var fs = require('fs');
var { warnn, errorn, logn, infon, debugn } = require('./log');
var { insertString } = require('./stringUtils');
var DEFAULT_KOTLIN_VERSION = '1.3.61';
// This should be the minSdkVersion required for RNN.
var DEFAULT_MIN_SDK_VERSION = 19

class GradleLinker {
constructor() {
Expand All @@ -16,6 +18,7 @@ class GradleLinker {
var contents = fs.readFileSync(this.gradlePath, 'utf8');
contents = this._setKotlinVersion(contents);
contents = this._setKotlinPluginDependency(contents);
contents = this._setMinSdkVersion(contents);
fs.writeFileSync(this.gradlePath, contents);
infon('Root build.gradle linked successfully!\n');
} else {
Expand Down Expand Up @@ -54,6 +57,22 @@ class GradleLinker {
return contents;
}

/**
* Check the current minSdkVersion specified and if it's lower than
* the required version, set it to the required version otherwise leave as it is.
*/
_setMinSdkVersion(contents) {
var minSdkVersion = this._getMinSdkVersion(contents)
// If user entered minSdkVersion is lower than the default, set it to default.
if (minSdkVersion < DEFAULT_MIN_SDK_VERSION) {
debugn(` Updating minSdkVersion to ${DEFAULT_MIN_SDK_VERSION}`)
return contents.replace(/minSdkVersion\s{0,}=\s{0,}\d*/, `minSdkVersion = ${DEFAULT_MIN_SDK_VERSION}`)
}

debugn(` Already specified minSdkVersion ${minSdkVersion}`)
return contents.replace(/minSdkVersion\s{0,}=\s{0,}\d*/, `minSdkVersion = ${minSdkVersion}`)
}

/**
* @param { string } contents
*/
Expand All @@ -69,6 +88,21 @@ class GradleLinker {
return `"${DEFAULT_KOTLIN_VERSION}"`;
}

/**
* Get the minSdkVersion value.
* @param { string } contents
*/
_getMinSdkVersion(contents) {
var minSdkVersion = contents.match(/minSdkVersion\s{0,}=\s{0,}(\d*)/)

if (minSdkVersion && minSdkVersion[1]) {
// It'd be something like 16 for a fresh React Native project.
return +minSdkVersion[1]
}

return DEFAULT_MIN_SDK_VERSION
}

/**
* @param {string} contents
*/
Expand Down
3 changes: 2 additions & 1 deletion autolink/postlink/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ var mainApplicationJava = glob.sync("**/MainApplication.java", ignoreFolders)[0]
exports.mainApplicationJava = mainApplicationJava;
exports.rootGradle = mainApplicationJava.replace(/android\/app\/.*\.java/, "android/build.gradle");;

exports.appDelegate = glob.sync("**/AppDelegate.m", ignoreFolders)[0];
exports.appDelegate = glob.sync("**/AppDelegate.m", ignoreFolders)[0];
exports.podFile = glob.sync("**/Podfile", ignoreFolders)[0]
39 changes: 39 additions & 0 deletions autolink/postlink/podfileLinker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// @ts-check
var path = require('./path');
var fs = require("fs");
var {logn, debugn, infon} = require("./log");

class PodfileLinker {
constructor() {
this.podfilePath = path.podFile;
}

link() {
if (this.podfilePath) {
logn("Updating Podfile...")
var podfileContent = fs.readFileSync(this.podfilePath, "utf8");

podfileContent = this._removeRNNPodLink(podfileContent);

fs.writeFileSync(this.podfilePath, podfileContent);
infon("Podfile updated successfully!\n")
}
}

/**
* Removes the RNN pod added by react-native link script.
*/
_removeRNNPodLink(contents) {
const rnnPodLink = contents.match(/\s+.*pod 'ReactNativeNavigation'.+react-native-navigation'/)

if (!rnnPodLink) {
debugn(" RNN Pod has not been added to Podfile")
return contents
}

debugn(" Removing RNN Pod from Podfile")
return contents.replace(rnnPodLink, "")
}
}

module.exports = PodfileLinker
2 changes: 2 additions & 0 deletions autolink/postlink/postLinkIOS.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @ts-check
var AppDelegateLinker = require("./appDelegateLinker");
var PodfileLinker = require('./podfileLinker');

module.exports = () => {
console.log("Running iOS postlink script\n");
new AppDelegateLinker().link();
new PodfileLinker().link();
}

0 comments on commit 4ce0e89

Please sign in to comment.