Skip to content

Commit

Permalink
RNGP - Sanitize the output of the config command (#46482)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #46482

Fixes #46443
Fixes #46134

I'm sanitizing the output of the `config` command + I've added some more logging in case of failure.

Changelog:
[Android] [Fixed] - RNGP - Sanitize the output of the config command

Reviewed By: cipolleschi

Differential Revision: D62641979

fbshipit-source-id: c13d27a42beeb7a973c1802e7204631d49d3d09b
  • Loading branch information
cortinico authored and facebook-github-bot committed Sep 13, 2024
1 parent 3cbaddb commit d7884a6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,19 @@ abstract class GeneratePackageListTask : DefaultTask() {

@TaskAction
fun taskAction() {
val model = JsonUtils.fromAutolinkingConfigJson(autolinkInputFile.get().asFile)
val model =
JsonUtils.fromAutolinkingConfigJson(autolinkInputFile.get().asFile)
?: error(
"""
RNGP - Autolinking: Could not parse autolinking config file:
${autolinkInputFile.get().asFile.absolutePath}
The file is either missing or not containing valid JSON so the build won't succeed.
"""
.trimIndent())

val packageName =
model?.project?.android?.packageName
model.project?.android?.packageName
?: error(
"RNGP - Autolinking: Could not find project.android.packageName in react-native config output! Could not autolink packages without this field.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,23 @@ object JsonUtils {
}

fun fromAutolinkingConfigJson(input: File): ModelAutolinkingConfigJson? =
input.bufferedReader().use {
runCatching { gsonConverter.fromJson(it, ModelAutolinkingConfigJson::class.java) }
input.bufferedReader().use { reader ->
runCatching {
// We sanitize the output of the `config` command as it could contain debug logs
// such as:
//
// > AwesomeProject@0.0.1 npx
// > rnc-cli config
//
// which will render the JSON invalid.
val content =
reader
.readLines()
.filterNot { line -> line.startsWith(">") }
.joinToString("\n")
.trim()
gsonConverter.fromJson(content, ModelAutolinkingConfigJson::class.java)
}
.getOrNull()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,54 @@ class JsonUtilsTest {
assertThat("implementation").isEqualTo(parsed.project!!.android!!.dependencyConfiguration)
}

@Test
fun fromAutolinkingConfigJson_withInfoLogs_sanitizeAndParseIt() {
@Suppress("JsonStandardCompliance")
val validJson =
createJsonFile(
"""
> AwesomeProject@0.0.1 npx
> rnc-cli config
{
"reactNativeVersion": "1000.0.0",
"project": {
"ios": {
"sourceDir": "./packages/rn-tester",
"xcodeProject": {
"name": "RNTesterPods.xcworkspace",
"isWorkspace": true
},
"automaticPodsInstallation": false
},
"android": {
"sourceDir": "./packages/rn-tester",
"appName": "RN-Tester",
"packageName": "com.facebook.react.uiapp",
"applicationId": "com.facebook.react.uiapp",
"mainActivity": ".RNTesterActivity",
"watchModeCommandParams": [
"--mode HermesDebug"
],
"dependencyConfiguration": "implementation"
}
}
}
"""
.trimIndent())
val parsed = JsonUtils.fromAutolinkingConfigJson(validJson)!!

assertThat("./packages/rn-tester").isEqualTo(parsed.project!!.android!!.sourceDir)
assertThat("RN-Tester").isEqualTo(parsed.project!!.android!!.appName)
assertThat("com.facebook.react.uiapp").isEqualTo(parsed.project!!.android!!.packageName)
assertThat("com.facebook.react.uiapp").isEqualTo(parsed.project!!.android!!.applicationId)
assertThat(".RNTesterActivity").isEqualTo(parsed.project!!.android!!.mainActivity)
assertThat("--mode HermesDebug")
.isEqualTo(parsed.project!!.android!!.watchModeCommandParams!![0])
assertThat("implementation").isEqualTo(parsed.project!!.android!!.dependencyConfiguration)
}

@Test
fun fromAutolinkingConfigJson_withDependenciesSpecified_canParseIt() {
val validJson =
Expand Down

0 comments on commit d7884a6

Please sign in to comment.