-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathposthog.gradle
More file actions
373 lines (307 loc) · 17.5 KB
/
posthog.gradle
File metadata and controls
373 lines (307 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// adapted from https://github.com/getsentry/sentry-react-native/blob/e76d0d388228437e82f235546de00f4e748fcbda/packages/core/sentry.gradle
import org.apache.tools.ant.taskdefs.condition.Os
import java.util.regex.Matcher
import java.util.regex.Pattern
interface InjectedExecOps {
@Inject //@javax.inject.Inject
ExecOperations getExecOps()
}
plugins.withId('com.android.application') {
def androidComponents = extensions.getByName("androidComponents")
androidComponents.onVariants(androidComponents.selector().all()) { v ->
if (!v.name.toLowerCase().contains("debug")) {
// separately we then hook into the bundle task of react native to inject
// sourcemap generation parameters. In case for whatever reason no release
// was found for the asset folder we just bail.
def bundleTasks = tasks.findAll { task -> (task.name.startsWith("createBundle") || task.name.startsWith("bundle")) && task.name.endsWith("JsAndAssets") && !task.name.contains("Debug") && task.enabled }
// this is the task that calls react-native bundle {params}
bundleTasks.each { bundleTask ->
def shouldCleanUp
def sourcemapOutput
def bundleOutput
def packagerSourcemapOutput
def bundleCommand
def props = bundleTask.getProperties()
def reactRoot = props.get("workingDir")
if (reactRoot == null) {
reactRoot = props.get("root").get() // RN 0.71 and above
}
(shouldCleanUp, bundleOutput, sourcemapOutput, packagerSourcemapOutput, bundleCommand) = forceSourceMapOutputFromBundleTask(bundleTask)
// Lets leave this here if we need to debug
// println bundleTask.properties
// .sort{it.key}
// .collect{it}
// .findAll{!['class', 'active'].contains(it.key)}
// .join('\n')
def currentVariants = extractCurrentVariants(bundleTask, v)
if (currentVariants == null) return
def previousCliTask = null
def applicationVariant = null
def nameCleanup = "${bundleTask.name}_PostHogUploadCleanUp"
// Upload the source map several times if necessary: once for each release and versionCode.
currentVariants.each { key, currentVariant ->
def variant = currentVariant[0]
def releaseName = currentVariant[1]
def versionCode = currentVariant[2]
applicationVariant = currentVariant[3]
try {
if (versionCode instanceof String) {
versionCode = Integer.parseInt(versionCode)
versionCode = Math.abs(versionCode)
}
} catch (NumberFormatException e) {
project.logger.info("versionCode: '$versionCode' isn't an Integer, using the plain value.")
}
def nameCliTask = "${bundleTask.name}_PostHogUpload_${releaseName}_${versionCode}"
// If several outputs have the same releaseName and versionCode, we'd do the exact same
// upload for each of them. No need to repeat.
try { tasks.named(nameCliTask); return } catch (Exception e) {}
/** Upload source map file to the PostHog server via CLI call. */
def cliTask = tasks.register(nameCliTask) {
description = "upload sourcemaps to PostHog"
group = 'posthog.com'
def extraArgs = []
def injected = project.objects.newInstance(InjectedExecOps)
doFirst {
injected.execOps.exec {
workingDir reactRoot
def cliPackage = resolvePostHogCliPackagePath(reactRoot)
def args = [cliPackage]
args.addAll(["exp", "hermes", "clone",
"--minified-map-path", packagerSourcemapOutput, // The path to a sourcemap
"--composed-map-path", sourcemapOutput // The path of the composed source map
])
args.addAll(extraArgs)
project.logger.info("posthog-cli clone arguments: ${args}")
def osCompatibility = Os.isFamily(Os.FAMILY_WINDOWS) ? ['cmd', '/c', 'node'] : []
commandLine(*osCompatibility, *args)
}
}
doLast {
injected.execOps.exec {
workingDir reactRoot
def cliPackage = resolvePostHogCliPackagePath(reactRoot)
def args = [cliPackage]
def sourcemapDir = sourcemapOutput.getParent()
args.addAll(["exp", "hermes", "upload",
"--directory", sourcemapDir // The path to a sourcemap that should be uploaded.
])
args.addAll(extraArgs)
project.logger.info("posthog-cli upload arguments: ${args}")
def osCompatibility = Os.isFamily(Os.FAMILY_WINDOWS) ? ['cmd', '/c', 'node'] : []
commandLine(*osCompatibility, *args)
}
}
enabled true
}
// chain the upload tasks so they run sequentially in order to run
// the cliCleanUpTask after the final upload task is run
if (previousCliTask != null) {
previousCliTask.configure { finalizedBy cliTask }
} else {
bundleTask.configure { finalizedBy cliTask }
}
previousCliTask = cliTask
}
/** Delete sourcemap files */
def cliCleanUpTask = tasks.register(nameCleanup, Delete) {
description = "clean up extra sourcemap"
group = 'posthog.com'
delete sourcemapOutput
delete "$buildDir/intermediates/assets/release/index.android.bundle.map"
// react native default bundle dir
}
// register clean task extension
cliCleanUpTask.configure { onlyIf { shouldCleanUp } }
// due to chaining the last value of previousCliTask will be the final
// upload task, after which the cleanup can be done
previousCliTask.configure { finalizedBy cliCleanUpTask }
def packageTasks = tasks.matching {
task -> ("package${applicationVariant}".equalsIgnoreCase(task.name) || "package${applicationVariant}Bundle".equalsIgnoreCase(task.name)) && task.enabled
}
}
}
}
}
def resolvePostHogCliPackagePath(reactRoot) {
// First, try to find @posthog/cli folder from require.resolve
try {
def resolvedPath = new File(["node", "--print", "require.resolve('@posthog/cli/package.json')"].execute(null, rootDir).text.trim()).getParentFile()
if (resolvedPath != null && resolvedPath.exists()) {
def runPostHogCliFile = new File(resolvedPath, "run-posthog-cli.js")
if (runPostHogCliFile.exists()) {
project.logger.info("resolved posthog-cli dynamically: `${runPostHogCliFile.getAbsolutePath()}`")
return runPostHogCliFile.getAbsolutePath()
}
}
} catch (Throwable ignored) {}
// Second, check if @posthog/cli exists in $reactRoot/node_modules
def nodeModulesPath = new File("$reactRoot/node_modules/@posthog/cli")
if (nodeModulesPath.exists()) {
def runPostHogCliFile = new File(nodeModulesPath, "run-posthog-cli.js")
if (runPostHogCliFile.exists()) {
project.logger.info("resolved posthog-cli hard-coded path (yarn or npm): `${runPostHogCliFile.getAbsolutePath()}`")
return runPostHogCliFile.getAbsolutePath()
}
}
// Third, check for pnpm installation with node_modules/.bin/posthog-cli
def pnpmBinPath = new File("$reactRoot/node_modules/.bin/posthog-cli")
if (pnpmBinPath.exists()) {
project.logger.info("resolved posthog-cli hard-coded path (pnpm): `${pnpmBinPath.getAbsolutePath()}`")
return pnpmBinPath.getAbsolutePath()
}
// Fourth, check using npm root for local installation
try {
def npmRoot = ["npm", "root"].execute(null, rootDir).text.trim()
def npmPostHogCliPath = new File(npmRoot, "@posthog/cli")
if (npmPostHogCliPath.exists()) {
def runPostHogCliFile = new File(npmPostHogCliPath, "run-posthog-cli.js")
if (runPostHogCliFile.exists()) {
project.logger.info("resolved posthog-cli via npm root: `${runPostHogCliFile.getAbsolutePath()}`")
return runPostHogCliFile.getAbsolutePath()
}
}
} catch (Throwable ignored) {}
// Fifth, check for global npm installation of @posthog/cli
try {
def globalPrefix = ["npm", "prefix", "-g"].execute().text.trim()
def globalPostHogCliPath = new File(globalPrefix, "lib/node_modules/@posthog/cli")
if (globalPostHogCliPath.exists()) {
def runPostHogCliFile = new File(globalPostHogCliPath, "run-posthog-cli.js")
if (runPostHogCliFile.exists()) {
project.logger.info("resolved posthog-cli from global npm installation: `${runPostHogCliFile.getAbsolutePath()}`")
return runPostHogCliFile.getAbsolutePath()
}
}
} catch (Throwable ignored) {}
// Finally, fallback to global install
project.logger.info("falling back to global posthog-cli")
return "posthog-cli"
}
def resolvePostHogReactNativeSDKPath(reactRoot) {
def resolvedPath = null
try {
resolvedPath = new File(["node", "--print", "require.resolve('posthog-react-native/package.json')"].execute(null, rootDir).text.trim()).getParentFile();
} catch (Throwable ignored) {} // if the resolve fails we fallback to the default path
def packagePath = resolvedPath != null && resolvedPath.exists() ? resolvedPath.getAbsolutePath() : "$reactRoot/node_modules/posthog-react-native"
return packagePath
}
/** Extract from arguments collection bundle and sourcemap files output names. */
static extractBundleTaskArgumentsLegacy(cmdArgs, Project project) {
def bundleOutput = null
def sourcemapOutput = null
def packagerSourcemapOutput = null
// packagerBundleOutput doesn't exist, because packager output is overwritten by Hermes
cmdArgs.eachWithIndex { String arg, int i ->
if (arg == "--bundle-output") {
bundleOutput = cmdArgs[i + 1]
project.logger.info("--bundle-output: `${bundleOutput}`")
} else if (arg == "--sourcemap-output") {
sourcemapOutput = cmdArgs[i + 1]
packagerSourcemapOutput = sourcemapOutput
project.logger.info("--sourcemap-output param: `${sourcemapOutput}`")
}
}
// Best thing would be if we just had access to the local gradle variables here:
// https://github.com/facebook/react-native/blob/ff3b839e9a5a6c9e398a1327cde6dd49a3593092/react.gradle#L89-L97
// Now, the issue is that hermes builds have a different pipeline:
// `metro -> hermes -> compose-source-maps`, which then combines both intermediate sourcemaps into the final one.
// In this function here, we only grep through the first `metro` step, which only generates an intermediate sourcemap,
// which is wrong. We need the final one. Luckily, we can just generate the path from the `bundleOutput`, since
// the paths seem to be well defined.
// if sourcemapOutput is null, it means there's no source maps at all
// if hermes is enabled and has intermediates folder, we need to fix paths
// if hermes is disabled, sourcemapOutput is already ok
def enableHermes = project.ext.react.get("enableHermes", false);
project.logger.info("enableHermes: `${enableHermes}`")
if (bundleOutput != null && sourcemapOutput != null && enableHermes) {
// react-native < 0.60.1
def pattern = Pattern.compile("(/|\\\\)intermediates\\1sourcemaps\\1react\\1")
Matcher matcher = pattern.matcher(sourcemapOutput)
// if its intermediates/sourcemaps/react then it should be generated/sourcemaps/react
if (matcher.find()) {
project.logger.info("sourcemapOutput has the wrong path, let's fix it.")
// replacing from bundleOutput which is more reliable
sourcemapOutput = bundleOutput.replaceAll("(/|\\\\)generated\\1assets\\1react\\1", "\$1generated\$1sourcemaps\$1react\$1") + ".map"
project.logger.info("sourcemapOutput new path: `${sourcemapOutput}`")
}
}
// get the current bundle command, if not peresent use default plain "bundle"
// we use this later to decide how to upload source maps
def bundleCommand = project.ext.react.get("bundleCommand", "bundle")
return [bundleOutput, sourcemapOutput, packagerSourcemapOutput, bundleCommand]
}
/** Extract bundle and sourcemap paths from bundle task props.
* Based on https://github.com/facebook/react-native/blob/93c17cd0c43ba7fe3bc09f547b64771984992fbb/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/BundleHermesCTask.kt
* Output source map path is the same for both Hermes and JSC.
*/
static extractBundleTaskArgumentsRN71AndAbove(bundleTask, logger) {
def props = bundleTask.getProperties()
def bundleAssetName = props.bundleAssetName?.get()
if (bundleAssetName == null) {
return [null, null]
}
def bundleCommand = props.bundleCommand.get()
def bundleFile = new File(props.jsBundleDir.get().asFile.absolutePath, bundleAssetName)
def outputSourceMap = new File(props.jsSourceMapsDir.get().asFile.absolutePath, "${bundleAssetName}.map")
def packagerOutputSourceMap = new File(props.jsIntermediateSourceMapsDir.get().asFile.absolutePath, "${bundleAssetName}.packager.map")
logger.info("bundleFile: `${bundleFile}`")
logger.info("outputSourceMap: `${outputSourceMap}`")
logger.info("packagerOutputSourceMap: `${packagerOutputSourceMap}`")
return [bundleFile, outputSourceMap, packagerOutputSourceMap, bundleCommand]
}
/** Force Bundle task to produce sourcemap files if they are not pre-configured by user yet. */
def forceSourceMapOutputFromBundleTask(bundleTask) {
def props = bundleTask.getProperties()
def cmd = props.get("commandLine") as List<String>
def cmdArgs = props.get("args") as List<String>
def shouldCleanUp = false
def bundleOutput = null
def sourcemapOutput = null
def packagerSourcemapOutput = null
def bundleCommand = null
(bundleOutput, sourcemapOutput, packagerSourcemapOutput, bundleCommand) = extractBundleTaskArgumentsRN71AndAbove(bundleTask, logger)
if (bundleOutput == null) {
(bundleOutput, sourcemapOutput, packagerSourcemapOutput, bundleCommand) = extractBundleTaskArgumentsLegacy(cmdArgs, project)
}
if (sourcemapOutput == null) {
sourcemapOutput = bundleOutput + ".map"
cmd.addAll(["--sourcemap-output", sourcemapOutput])
cmdArgs.addAll(["--sourcemap-output", sourcemapOutput])
shouldCleanUp = true
bundleTask.setProperty("commandLine", cmd)
bundleTask.setProperty("args", cmdArgs)
project.logger.info("forced sourcemap file output for `${bundleTask.name}` task")
} else {
project.logger.info("Info: used pre-configured source map files: ${sourcemapOutput}")
}
return [shouldCleanUp, bundleOutput, sourcemapOutput, packagerSourcemapOutput, bundleCommand]
}
/** compose array with one item - current build flavor name */
static extractCurrentVariants(bundleTask, variant) {
// examples: bundleLocalReleaseJsAndAssets, createBundleYellowDebugJsAndAssets
def pattern = Pattern.compile("(?:create)?(?:B|b)undle([A-Z][A-Za-z0-9_]+)JsAndAssets")
def currentRelease = ""
Matcher matcher = pattern.matcher(bundleTask.name)
if (matcher.find()) {
def match = matcher.group(1)
currentRelease = match.substring(0, 1).toLowerCase() + match.substring(1)
}
def currentVariants = null
if (variant.name.equalsIgnoreCase(currentRelease)) {
currentVariants = [:]
def variantName = variant.name
variant.outputs.each { output ->
def defaultVersionCode = output.versionCode.getOrElse(0)
def versionCode = defaultVersionCode
def appId = variant.applicationId.get()
def versionName = output.versionName.getOrElse('') // may be empty if not set
def defaultReleaseName = "${appId}@${versionName}+${versionCode}"
def releaseName = defaultReleaseName
def outputName = output.baseName
if (currentVariants[outputName] == null) currentVariants[outputName] = []
currentVariants[outputName] = [outputName, releaseName, versionCode, variantName]
}
}
return currentVariants
}