Skip to content

Commit 1475d11

Browse files
committed
Replace bundleAndSourceMapFilesProvider with separate providers
1 parent cd17dd7 commit 1475d11

File tree

4 files changed

+85
-275
lines changed

4 files changed

+85
-275
lines changed

embrace-gradle-plugin/src/main/java/io/embrace/android/gradle/plugin/tasks/reactnative/EmbraceRnSourcemapGeneratorTask.kt

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import okio.source
1515
import org.gradle.api.file.DirectoryProperty
1616
import org.gradle.api.file.RegularFileProperty
1717
import org.gradle.api.model.ObjectFactory
18-
import org.gradle.api.provider.MapProperty
19-
import org.gradle.api.tasks.Input
2018
import org.gradle.api.tasks.InputFile
2119
import org.gradle.api.tasks.Optional
2220
import org.gradle.api.tasks.OutputDirectory
@@ -49,23 +47,10 @@ abstract class EmbraceRnSourcemapGeneratorTask @Inject constructor(
4947
@get:OutputFile
5048
val sourcemapAndBundleFile: RegularFileProperty = objectFactory.fileProperty()
5149

52-
@get:Input
53-
val reactProperties: MapProperty<String, Any> = objectFactory.mapProperty(
54-
String::class.java,
55-
Any::class.java
56-
).convention(emptyMap())
57-
5850
@TaskAction
5951
fun onRun() {
60-
val rnFilesFinderUtil = RnFilesFinder(
61-
reactProperties.get(),
62-
project.layout.buildDirectory.get().asFile
63-
)
64-
65-
val bundleFile = rnFilesFinderUtil.fetchJSBundleFile(
66-
bundleFile.orNull?.asFile
67-
)
68-
if (bundleFile == null) {
52+
val bundleFile = bundleFile.orNull?.asFile
53+
if (bundleFile == null || !bundleFile.exists()) {
6954
logger.error("Couldn't find the JSBundle. React native files were not uploaded.")
7055
return
7156
}
@@ -76,10 +61,7 @@ abstract class EmbraceRnSourcemapGeneratorTask @Inject constructor(
7661
* In old React Native Versions, the source map is not exposed as output in the task.
7762
* If the source map is not present, we will search for it in the known location
7863
*/
79-
val sourceMapFile: File? = rnFilesFinderUtil.fetchSourceMapFile(
80-
sourcemap.orNull?.asFile,
81-
variantData.get()
82-
)
64+
val sourceMapFile: File? = sourcemap.orNull?.asFile
8365

8466
if (sourceMapFile == null || !sourceMapFile.exists()) {
8567
logger.error("Couldn't find the Source Map. React native files were not uploaded.")

embrace-gradle-plugin/src/main/java/io/embrace/android/gradle/plugin/tasks/reactnative/EmbraceRnSourcemapGeneratorTaskRegistration.kt

Lines changed: 82 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@ package io.embrace.android.gradle.plugin.tasks.reactnative
33
import io.embrace.android.gradle.plugin.Logger
44
import io.embrace.android.gradle.plugin.gradle.nullSafeMap
55
import io.embrace.android.gradle.plugin.gradle.registerTask
6-
import io.embrace.android.gradle.plugin.gradle.safeFlatMap
76
import io.embrace.android.gradle.plugin.gradle.tryGetTaskProvider
7+
import io.embrace.android.gradle.plugin.model.AndroidCompactedVariantData
88
import io.embrace.android.gradle.plugin.network.EmbraceEndpoint
99
import io.embrace.android.gradle.plugin.tasks.common.RequestParams
1010
import io.embrace.android.gradle.plugin.tasks.registration.EmbraceTaskRegistration
1111
import io.embrace.android.gradle.plugin.tasks.registration.RegistrationParams
12+
import org.gradle.api.Project
1213
import org.gradle.api.Task
13-
import org.gradle.api.plugins.ExtraPropertiesExtension.UnknownPropertyException
14+
import org.gradle.api.provider.Provider
1415
import org.gradle.api.tasks.TaskProvider
16+
import java.io.File
1517

1618
private const val SOURCEMAP_GENERATOR_NAME = "embraceRNSourcemapGeneratorFor"
1719
private const val REACT_PROPERTY = "react"
1820
private const val RN_BUNDLE_FILE_EXTENSION = ".bundle"
1921
private const val RN_SOURCEMAP_FILE_EXTENSION = "bundle.map"
20-
private const val RN_BUNDLE_KEY = ".bundle"
21-
private const val RN_SOURCEMAP_KEY = "bundle.map"
22+
private const val BUNDLE_ASSET_NAME = "bundleAssetName"
23+
private const val DEFAULT_BUNDLE_NAME = "index.android.bundle"
24+
private const val SOURCE_MAP_NAME = "android-embrace.bundle.map"
2225

2326
private const val GENERATED_RESOURCE_PATH = "generated/embrace/res"
2427

@@ -106,64 +109,93 @@ class EmbraceRnSourcemapGeneratorTaskRegistration : EmbraceTaskRegistration {
106109
)
107110
)
108111

109-
val bundleAndSourceMapFilesProvider = generatorTask.safeFlatMap { task ->
110-
return@safeFlatMap task.outputs.files.asFileTree.elements.nullSafeMap { files ->
111-
return@nullSafeMap files.filter { possibleSoFile ->
112-
possibleSoFile.asFile.absolutePath.endsWith(
113-
RN_BUNDLE_FILE_EXTENSION
114-
) ||
115-
possibleSoFile.asFile.absolutePath.endsWith(
116-
RN_SOURCEMAP_FILE_EXTENSION
117-
)
118-
}.associate { fileSystemLocation ->
119-
val file = fileSystemLocation.asFile
120-
val key = when {
121-
file.absolutePath.endsWith(RN_BUNDLE_FILE_EXTENSION) -> RN_BUNDLE_KEY
122-
file.absolutePath.endsWith(RN_SOURCEMAP_FILE_EXTENSION) -> RN_SOURCEMAP_KEY
123-
else -> "none"
124-
}
125-
key to project.layout.file(project.provider { file }).get()
126-
}
127-
}
128-
}
129-
130-
rnTask.bundleFile.set(
131-
bundleAndSourceMapFilesProvider.nullSafeMap {
132-
it[RN_BUNDLE_KEY]
133-
}
134-
)
135-
136-
rnTask.sourcemap.set(
137-
bundleAndSourceMapFilesProvider.nullSafeMap {
138-
it[RN_SOURCEMAP_KEY]
139-
}
140-
)
141-
142-
val reactProperties = try {
143-
@Suppress("UNCHECKED_CAST")
144-
project.extensions.extraProperties.get(REACT_PROPERTY) as? Map<String, Any>
145-
} catch (e: UnknownPropertyException) {
146-
null
147-
}
148-
rnTask.reactProperties.set(reactProperties)
112+
rnTask.bundleFile.set(project.layout.file(getBundleFileProvider(generatorTask, project)))
113+
rnTask.sourcemap.set(project.layout.file(getSourcemapFileProvider(generatorTask, project, data)))
149114

150115
val flavorName = data.flavorName
151116
val buildTypeName = data.buildTypeName
152-
val bundleFileFolder =
153-
if (flavorName.isBlank()) buildTypeName else "$flavorName/$buildTypeName"
117+
val bundleFileFolder = if (flavorName.isBlank()) buildTypeName else "$flavorName/$buildTypeName"
154118
rnTask.sourcemapAndBundleFile.set(
155119
project.layout.buildDirectory.file(
156120
"outputs/embrace/$bundleFileFolder/$FILE_NAME_SOURCE_MAP_JSON"
157121
)
158122
)
123+
124+
rnTask.dependsOn(generatorTask)
159125
} catch (e: Exception) {
160-
logger.error(
161-
"EmbraceRNSourcemapGeneratorTask failed while getting the Bundle and the SourceMap",
162-
e
163-
)
126+
logger.error("EmbraceRNSourcemapGeneratorTask failed while getting the Bundle and the SourceMap", e)
164127
}
165128
}
166129

130+
private fun getBundleFileProvider(generatorTask: TaskProvider<Task>, project: Project): Provider<File?> =
131+
generatorTask.flatMap { task ->
132+
task.outputs.files.asFileTree.elements.flatMap { fileLocations ->
133+
val bundleFile = fileLocations.firstOrNull { location ->
134+
location.asFile.name.endsWith(RN_BUNDLE_FILE_EXTENSION)
135+
}?.asFile
136+
137+
if (bundleFile != null && bundleFile.exists()) {
138+
project.provider { bundleFile }
139+
} else {
140+
findBundleFile(project)
141+
}
142+
}
143+
}
144+
145+
/**
146+
* Iterate through all folders inside build folder to find index.android.bundle
147+
*/
148+
private fun findBundleFile(project: Project): Provider<File?> {
149+
return project.layout.buildDirectory.nullSafeMap { buildDir ->
150+
buildDir.asFile.walk()
151+
.filter { it.isFile && (it.name == "index.android.bundle") }
152+
.firstOrNull()
153+
}
154+
}
155+
156+
private fun getSourcemapFileProvider(
157+
generatorTask: TaskProvider<Task>,
158+
project: Project,
159+
data: AndroidCompactedVariantData,
160+
): Provider<File?> =
161+
generatorTask.flatMap { task ->
162+
task.outputs.files.asFileTree.elements.flatMap { fileLocations ->
163+
val sourcemapFile = fileLocations.firstOrNull { location ->
164+
location.asFile.name.endsWith(RN_SOURCEMAP_FILE_EXTENSION)
165+
}?.asFile
166+
167+
if (sourcemapFile != null && sourcemapFile.exists()) {
168+
project.provider { sourcemapFile }
169+
} else {
170+
findSourcemapFile(project, data)
171+
}
172+
}
173+
}
174+
175+
private fun findSourcemapFile(project: Project, data: AndroidCompactedVariantData): Provider<File?> {
176+
return project.layout.buildDirectory.nullSafeMap { buildDir ->
177+
File(buildDir.asFile, "generated/sourcemaps/$SOURCE_MAP_NAME").takeIf { it.exists() }
178+
?: File(buildDir.asFile, getReactNativeSourcemapFilePath(project, data.name)).takeIf { it.exists() }
179+
}
180+
}
181+
182+
/**
183+
* Look for sourcemap file in generated/sourcemaps/react/variantDir
184+
* If a custom sourcemap file name has been set in a gradle property, use it.
185+
* If not, use the default name.
186+
*/
187+
private fun getReactNativeSourcemapFilePath(project: Project, variantName: String): String {
188+
val sourceMapsDirPath = "generated/sourcemaps/react/$variantName"
189+
val reactProperties = try {
190+
project.extensions.extraProperties.get(REACT_PROPERTY) as? Map<*, *>
191+
} catch (e: Exception) {
192+
null
193+
}
194+
val bundleAssetName = reactProperties?.get(BUNDLE_ASSET_NAME)?.toString() ?: DEFAULT_BUNDLE_NAME
195+
196+
return "$sourceMapsDirPath/$bundleAssetName.map"
197+
}
198+
167199
private companion object {
168200
private const val FILE_NAME_SOURCE_MAP_JSON = "sourcemap.json"
169201
}

embrace-gradle-plugin/src/main/java/io/embrace/android/gradle/plugin/tasks/reactnative/RnFilesFinder.kt

Lines changed: 0 additions & 137 deletions
This file was deleted.

0 commit comments

Comments
 (0)