-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RNGP - Setup the RunAutolinkingConfigTask to run the config command
Summary: This diff is part of RFC0759 react-native-community/discussions-and-proposals#759 Here I'm creating the `runAutolinkingConfig` task. This task is responsible of either: - Invoking the `npx react-native-community/cli config` command (or the one specified by the user) - Copying the config output file specified by the user (if any). The task re-executes only if any of the lockfile are actually changed otherwise it just returns as "UP-TO-DATE" This allows us to Changelog: [Internal] [Changed] - RNGP - Setup the RunAutolinkingConfigTask to run the config command Reviewed By: cipolleschi, blakef Differential Revision: D55475596 fbshipit-source-id: 3c687f965c59eb82fc447546ebd936ba401f34f2
- Loading branch information
1 parent
6262158
commit 1df1570
Showing
5 changed files
with
261 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/RunAutolinkingConfigTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.tasks | ||
|
||
import com.facebook.react.utils.windowsAwareCommandLine | ||
import java.io.FileOutputStream | ||
import org.gradle.api.file.FileCollection | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.provider.ListProperty | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.* | ||
|
||
/** | ||
* A task that will run @react-native-community/cli config if necessary to generate the autolinking | ||
* configuration file. | ||
*/ | ||
abstract class RunAutolinkingConfigTask : Exec() { | ||
|
||
init { | ||
group = "react" | ||
} | ||
|
||
@get:Input abstract val autolinkConfigCommand: ListProperty<String> | ||
|
||
/* | ||
* We don't want to re-run config if the lockfiles haven't changed. | ||
* So we have the lockfiles as @InputFiles for this task. | ||
*/ | ||
@get:InputFiles abstract val autolinkLockFiles: Property<FileCollection> | ||
|
||
@get:InputFile @get:Optional abstract val autolinkConfigFile: RegularFileProperty | ||
|
||
@get:OutputFile abstract val autolinkOutputFile: RegularFileProperty | ||
|
||
override fun exec() { | ||
wipeOutputDir() | ||
setupCommandLine() | ||
super.exec() | ||
} | ||
|
||
internal fun setupCommandLine() { | ||
if (!autolinkConfigFile.isPresent || !autolinkConfigFile.get().asFile.exists()) { | ||
setupConfigCommandLine() | ||
} else { | ||
setupConfigCopyCommandLine() | ||
} | ||
} | ||
|
||
internal fun wipeOutputDir() { | ||
autolinkOutputFile.asFile.get().apply { | ||
deleteRecursively() | ||
parentFile.mkdirs() | ||
} | ||
} | ||
|
||
internal fun setupConfigCommandLine() { | ||
workingDir(project.projectDir) | ||
standardOutput = FileOutputStream(autolinkOutputFile.get().asFile) | ||
commandLine( | ||
windowsAwareCommandLine( | ||
*autolinkConfigCommand.get().toTypedArray(), | ||
)) | ||
} | ||
|
||
internal fun setupConfigCopyCommandLine() { | ||
workingDir(project.projectDir) | ||
commandLine( | ||
windowsAwareCommandLine( | ||
"cp", | ||
autolinkConfigFile.get().asFile.absolutePath, | ||
autolinkOutputFile.get().asFile.absolutePath)) | ||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
...ve-gradle-plugin/src/test/kotlin/com/facebook/react/tasks/RunAutolinkingConfigTaskTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.tasks | ||
|
||
import com.facebook.react.tests.createProject | ||
import com.facebook.react.tests.createTestTask | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.TemporaryFolder | ||
|
||
class RunAutolinkingConfigTaskTest { | ||
|
||
@get:Rule val tempFolder = TemporaryFolder() | ||
|
||
@Test | ||
fun runAutolinkingConfigTask_groupIsSetCorrectly() { | ||
val task = createTestTask<BundleHermesCTask> {} | ||
assertEquals("react", task.group) | ||
} | ||
|
||
@Test | ||
fun runAutolinkingConfigTask_staticInputs_areSetCorrectly() { | ||
val project = createProject() | ||
|
||
val task = | ||
createTestTask<RunAutolinkingConfigTask> { | ||
it.autolinkConfigCommand.set(listOf("rm", "-rf", "/")) | ||
it.autolinkLockFiles.set(project.files("packager.lock", "another-packager.lock")) | ||
it.autolinkConfigFile.set(tempFolder.newFile("dependencies.json")) | ||
it.autolinkOutputFile.set(tempFolder.newFile("output.json")) | ||
} | ||
|
||
assertEquals(3, task.inputs.files.files.size) | ||
task.autolinkLockFiles.get().files.forEach { | ||
assertTrue( | ||
it.name == "depedencies.json" || | ||
it.name == "packager.lock" || | ||
it.name == "another-packager.lock") | ||
} | ||
|
||
assertTrue(task.inputs.properties.containsKey("autolinkConfigCommand")) | ||
assertEquals(1, task.outputs.files.files.size) | ||
assertEquals(File(tempFolder.root, "output.json"), task.outputs.files.singleFile) | ||
assertEquals(listOf("rm", "-rf", "/"), task.autolinkConfigCommand.get()) | ||
|
||
assertEquals(2, task.autolinkLockFiles.get().files.size) | ||
task.autolinkLockFiles.get().files.forEach { | ||
assertTrue(it.name == "packager.lock" || it.name == "another-packager.lock") | ||
} | ||
|
||
assertEquals(File(tempFolder.root, "dependencies.json"), task.autolinkConfigFile.get().asFile) | ||
assertEquals(File(tempFolder.root, "output.json"), task.autolinkOutputFile.get().asFile) | ||
} | ||
|
||
@Test | ||
fun wipeOutputDir_worksCorrectly() { | ||
val outputDir = | ||
tempFolder.newFolder("output").apply { | ||
File(this, "output.json").createNewFile() | ||
File(this, "NothingToSeeHere.java").createNewFile() | ||
} | ||
|
||
val task = createTestTask<RunAutolinkingConfigTask> { it.autolinkOutputFile.set(outputDir) } | ||
task.wipeOutputDir() | ||
|
||
assertFalse(outputDir.exists()) | ||
} | ||
|
||
@Test | ||
fun setupConfigCommandLine_worksCorrectly() { | ||
val project = createProject() | ||
|
||
val task = | ||
createTestTask<RunAutolinkingConfigTask>(project) { | ||
it.autolinkConfigCommand.set(listOf("rm", "-rf", "/")) | ||
it.autolinkOutputFile.set(tempFolder.newFile("output.json")) | ||
} | ||
task.setupConfigCommandLine() | ||
|
||
assertEquals(project.projectDir, task.workingDir) | ||
assertTrue(task.standardOutput is FileOutputStream) | ||
assertEquals(listOf("rm", "-rf", "/"), task.commandLine) | ||
} | ||
|
||
@Test | ||
fun setupConfigCopyCommandLine_worksCorrectly() { | ||
val project = createProject() | ||
|
||
val task = | ||
createTestTask<RunAutolinkingConfigTask>(project) { | ||
it.autolinkConfigFile.set(tempFolder.newFile("dependencies.json")) | ||
it.autolinkOutputFile.set(tempFolder.newFile("output.json")) | ||
} | ||
task.setupConfigCopyCommandLine() | ||
|
||
assertEquals(project.projectDir, task.workingDir) | ||
assertTrue(task.standardOutput !is FileOutputStream) | ||
assertEquals("cp", task.commandLine[0]) | ||
assertEquals(File(tempFolder.root, "dependencies.json").absolutePath, task.commandLine[1]) | ||
assertEquals(File(tempFolder.root, "output.json").absolutePath, task.commandLine[2]) | ||
} | ||
|
||
@Test | ||
fun setupCommandLine_withoutAutolinkConfigFileConfigured_invokesCommand() { | ||
val project = createProject() | ||
|
||
val task = | ||
createTestTask<RunAutolinkingConfigTask>(project) { | ||
it.autolinkConfigCommand.set(listOf("rm", "-rf", "/")) | ||
it.autolinkOutputFile.set(tempFolder.newFile("output.json")) | ||
} | ||
task.setupCommandLine() | ||
|
||
assertEquals(listOf("rm", "-rf", "/"), task.commandLine) | ||
} | ||
|
||
@Test | ||
fun setupCommandLine_withoutMissingConfigFile_invokesCommand() { | ||
val project = createProject() | ||
|
||
val task = | ||
createTestTask<RunAutolinkingConfigTask>(project) { | ||
it.autolinkConfigCommand.set(listOf("rm", "-rf", "/")) | ||
it.autolinkConfigFile.set(File(tempFolder.root, "dependencies.json")) | ||
it.autolinkOutputFile.set(tempFolder.newFile("output.json")) | ||
} | ||
task.setupCommandLine() | ||
|
||
assertEquals(listOf("rm", "-rf", "/"), task.commandLine) | ||
} | ||
|
||
@Test | ||
fun setupCommandLine_withoutExistingConfigFile_invokesCp() { | ||
val project = createProject() | ||
val configFile = tempFolder.newFile("dependencies.json").apply { writeText("¯\\_(ツ)_/¯") } | ||
|
||
val task = | ||
createTestTask<RunAutolinkingConfigTask>(project) { | ||
it.autolinkConfigCommand.set(listOf("rm", "-rf", "/")) | ||
it.autolinkConfigFile.set(configFile) | ||
it.autolinkOutputFile.set(tempFolder.newFile("output.json")) | ||
} | ||
task.setupCommandLine() | ||
|
||
assertEquals( | ||
listOf("cp", configFile.absolutePath, File(tempFolder.root, "output.json").absolutePath), | ||
task.commandLine) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters