Skip to content

Commit

Permalink
Add bundleForVariant option (#32472)
Browse files Browse the repository at this point in the history
Summary:
Ref #30606 (comment)

## Changelog

[Android] [Added] - Add bundleForVariant option

Pull Request resolved: #32472

Test Plan:
I added the following log into react.gradle and ran the Android build for my app:

```
println("bundleEnabled: ${targetName}, ${bundleForVariant(variant)}")
```

```
# build.gradle

project.ext.react = [
    entryFile: "index.android.js",
    enableHermes: true,  // clean and rebuild if changing
    bundleForVariant: {
         def variant -> variant.name.toLowerCase().contains('release') || variant.name.toLowerCase().contains('live')
    },
]

...

flavorDimensions 'branding'
productFlavors {
    cve {
        dimension 'branding'
    }
    whce {
        dimension 'branding'
    }
}
```

Console output:

```
Reading env from: env/cve/live
bundleEnabled: CveDebug, false
bundleEnabled: CveRelease, true
bundleEnabled: CveLive, true
bundleEnabled: WhceDebug, false
bundleEnabled: WhceRelease, true
bundleEnabled: WhceLive, true
```

Reviewed By: cortinico, ryancat

Differential Revision: D31910406

Pulled By: ShikaSD

fbshipit-source-id: baca5efaddedddad15d974cc7bb8f3c2a4c4f35b
  • Loading branch information
geraintwhite authored and facebook-github-bot committed Oct 26, 2021
1 parent d1439e8 commit d2c10da
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ abstract class ReactExtension @Inject constructor(project: Project) {
val bundleIn: MapProperty<String, Boolean> =
objects.mapProperty(String::class.java, Boolean::class.java).convention(emptyMap())

/**
* Functional interface to toggle the bundle command only on specific [BaseVariant] Default: will
* check [bundleIn] or return True for Release variants and False for Debug variants.
*/
var bundleForVariant: (BaseVariant) -> Boolean = { variant ->
if (bundleIn.getting(variant.name).isPresent) bundleIn.getting(variant.name).get()
else if (bundleIn.getting(variant.buildType.name).isPresent)
bundleIn.getting(variant.buildType.name).get()
else variant.isRelease
}

/** Hermes Config */

/** The command to use to invoke hermes. Default is `hermesc` for the correct OS. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ private const val REACT_GROUP = "react"
@Suppress("SpreadOperator")
internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExtension) {
val targetName = variant.name.capitalize(Locale.ROOT)
val isRelease = variant.isRelease
val targetPath = variant.dirName

// React js bundle directories
Expand All @@ -50,7 +49,7 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExte
val execCommand = nodeExecutableAndArgs + cliPath
val enableHermes = config.enableHermesForVariant(variant)
val cleanup = config.deleteDebugFilesForVariant(variant)
val bundleEnabled = variant.checkBundleEnabled(config)
val bundleEnabled = config.bundleForVariant(variant)

val bundleTask =
tasks.register("createBundle${targetName}JsAndAssets", BundleJsAndAssetsTask::class.java) {
Expand Down Expand Up @@ -258,17 +257,5 @@ private fun Project.cleanupVMFiles(
}
}

private fun BaseVariant.checkBundleEnabled(config: ReactExtension): Boolean {
if (config.bundleIn.getting(name).isPresent) {
return config.bundleIn.getting(name).get()
}

if (config.bundleIn.getting(buildType.name).isPresent) {
return config.bundleIn.getting(buildType.name).get()
}

return isRelease
}

internal val BaseVariant.isRelease: Boolean
get() = name.toLowerCase(Locale.ROOT).contains("release")
19 changes: 13 additions & 6 deletions react.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,18 @@ def hermesFlagsForVariant = config.hermesFlagsForVariant ?: {
// Set disableDevForVariant to a function to configure per variant,
// defaults to `devDisabledIn${targetName}` or True for Release variants and False for debug variants
def disableDevForVariant = config.disableDevForVariant ?: {
def variant -> config."devDisabledIn${variant.name.capitalize()}" || variant.name.toLowerCase().contains("release")
def variant ->
config."devDisabledIn${variant.name.capitalize()}" ||
variant.name.toLowerCase().contains("release")
}

// Set bundleForVariant to a function to configure per variant,
// defaults to `bundleIn${targetName}` or True for Release variants and False for debug variants
def bundleForVariant = config.bundleForVariant ?: {
def variant ->
config."bundleIn${variant.name.capitalize()}" ||
config."bundleIn${variant.buildType.name.capitalize()}" ||
variant.name.toLowerCase().contains("release")
}

// Set deleteDebugFilesForVariant to a function to configure per variant,
Expand Down Expand Up @@ -238,11 +249,7 @@ afterEvaluate {
}
}

enabled config."bundleIn${targetName}" != null
? config."bundleIn${targetName}"
: config."bundleIn${variant.buildType.name.capitalize()}" != null
? config."bundleIn${variant.buildType.name.capitalize()}"
: targetName.toLowerCase().contains("release")
enabled bundleForVariant(variant)
}

// Expose a minimal interface on the application variant and the task itself:
Expand Down

0 comments on commit d2c10da

Please sign in to comment.