Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved android build scripts #4638

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 29 additions & 24 deletions detox/android/rninfo.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import groovy.json.JsonSlurper

def getRNVersion = { workingDir ->
println("RNInfo: workingDir=$workingDir")
def getRNPackageJsonDir() {
def currentDir = rootDir
while (currentDir != null) {
def nodeModulesDir = new File(currentDir, "node_modules/react-native")
def file = new File(nodeModulesDir, 'package.json')
if (file.exists()) {
return file.path
}
currentDir = currentDir.parentFile
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this continue all the way up to the filesystem's root? I think we should stop at the root project's dir 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't know who is the root. It could be in any structure of projects in mono repo. It is logn. not so bad

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I didn't have performance in mind when I wrote this, rather possible resolutions of wrong node module folders or attempting access to non-priviledged directories (think CI agents).
Anyways, smt to think abt but might be an overkill.

}
throw new GradleException("Unable to find module: $moduleName")
}

def getRNVersion = { rnPackageJsonPath ->
println("RNInfo: package.json=$rnPackageJsonPath")
def jsonSlurper = new JsonSlurper()
def packageFile = "$workingDir/../node_modules/react-native/package.json"
println("RNInfo: reading $packageFile")
Map<String, Object> packageJSON = jsonSlurper.parse(new File(packageFile))
def packageFile =
println("RNInfo: reading $rnPackageJsonPath")
Map<String, Object> packageJSON = jsonSlurper.parse(new File(rnPackageJsonPath))
String rnVersion = packageJSON.get('version')
return rnVersion
}
Expand All @@ -15,24 +28,16 @@ def getMajorVersionInternal = { semanticVersion ->
return rnVersionMajor
}

ext.getRnMajorVersion = { workingDir ->
String rnVersion = getRNVersion(workingDir)
Integer rnVersionMajor = getMajorVersionInternal(rnVersion)
return rnVersionMajor
}

def rnVersion = getRNVersion(rootDir)
def rnVersion = getRNVersion(getRNPackageJsonDir())
def rnMajorVer = getMajorVersionInternal(rnVersion)
if (hasProperty('project')) {
println "[$project] RNInfo: detected React Native version: $rnVersion (major=$rnMajorVer)"
println "RNInfo: detected React Native version: $rnVersion (major=$rnMajorVer)"

project.ext.rnInfo = [
version : rnVersion,
majorVersion : rnMajorVer,
isRN69OrHigher: rnMajorVer >= 69,
isRN70OrHigher: rnMajorVer >= 70,
isRN71OrHigher: rnMajorVer >= 71,
isRN72OrHigher: rnMajorVer >= 72,
isRN73OrHigher: rnMajorVer >= 73,
]
}
ext.rnInfo = [
version : rnVersion,
majorVersion : rnMajorVer,
isRN69OrHigher: rnMajorVer >= 69,
isRN70OrHigher: rnMajorVer >= 70,
isRN71OrHigher: rnMajorVer >= 71,
isRN72OrHigher: rnMajorVer >= 72,
isRN73OrHigher: rnMajorVer >= 73,
]
10 changes: 4 additions & 6 deletions detox/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ include ':detox'

println("RNInfo: rootDir=$rootDir")

def rnMajorVer = getRnMajorVersion(rootDir)
println "[settings] RNInfo: detected React Native version: (major=$rnMajorVer)"
println "[settings] RNInfo: detected React Native version: (major=${ext.rnInfo.version})"

if (rnMajorVer < 72) {
includeBuild('../node_modules/react-native-gradle-plugin')
} else {
if (ext.rnInfo.isRN72OrHigher) {
includeBuild('../node_modules/@react-native/gradle-plugin')
} else {
includeBuild('../node_modules/react-native-gradle-plugin')
}

11 changes: 4 additions & 7 deletions detox/test/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ apply from: file("../../android/rninfo.gradle")
rootProject.name = 'DetoxTest'
include ':app'

def rnMajorVer = getRnMajorVersion(rootDir)
println "[settings] RNInfo: detected React Native version: (major=$rnMajorVer)"
println "[settings] RNInfo: detected React Native version: (major=${ext.rnInfo.version})"

if (rnMajorVer < 72) {
includeBuild('../node_modules/react-native-gradle-plugin')
} else {
if (ext.rnInfo.isRN72OrHigher) {
includeBuild('../node_modules/@react-native/gradle-plugin')
} else {
includeBuild('../node_modules/react-native-gradle-plugin')
}



include ':detox'
project(':detox').projectDir = new File(rootProject.projectDir, '../../android/detox')

Expand Down
10 changes: 4 additions & 6 deletions examples/demo-react-native/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ rootProject.name = 'DetoxRNExample'

include ':app'

println("RNInfo: rootDir=$rootDir")
d4vidi marked this conversation as resolved.
Show resolved Hide resolved
def rnMajorVer = getRnMajorVersion(rootDir)
println("[settings] RNInfo: detected React Native version: (major=$rnMajorVer)")
println("[settings] RNInfo: detected React Native version: (major=${ext.rnInfo.version})")

if (rnMajorVer < 72) {
includeBuild('../node_modules/react-native-gradle-plugin')
} else {
if (ext.rnInfo.isRN72OrHigher) {
includeBuild('../node_modules/@react-native/gradle-plugin')
} else {
includeBuild('../node_modules/react-native-gradle-plugin')
}


Expand Down
Loading