-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
SentryPlugin.kt
131 lines (114 loc) · 5.34 KB
/
SentryPlugin.kt
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
package io.sentry.android.gradle
import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.gradle.AppExtension
import io.sentry.android.gradle.SentryCliProvider.getSentryCliPath
import io.sentry.android.gradle.SentryTasksProvider.getProcessResourcesProvider
import io.sentry.android.gradle.autoinstall.installDependencies
import io.sentry.android.gradle.extensions.SentryPluginExtension
import io.sentry.android.gradle.tasks.dependencies.SentryExternalDependenciesReportTaskFactory
import io.sentry.android.gradle.util.AgpVersions
import io.sentry.android.gradle.util.SentryPluginUtils.withLogging
import io.sentry.android.gradle.util.info
import java.io.File
import java.util.concurrent.atomic.AtomicBoolean
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.ExtraPropertiesExtension
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.tasks.StopExecutionException
import org.slf4j.LoggerFactory
@Suppress("UnstableApiUsage")
class SentryPlugin : Plugin<Project> {
/**
* Since we're listening for the JavaBasePlugin, there may be multiple plugins inherting from it
* applied to the same project, e.g. Spring Boot + Kotlin Jvm, hence we only want our plugin to
* be configured only once.
*/
private val configuredForJavaProject = AtomicBoolean(false)
override fun apply(project: Project) {
if (AgpVersions.CURRENT < AgpVersions.VERSION_7_0_0) {
throw StopExecutionException(
"""
Using io.sentry.android.gradle:3+ with Android Gradle Plugin < 7 is not supported.
Either upgrade the AGP version to 7+, or use an earlier version of the Sentry
Android Gradle Plugin. For more information check our migration guide
https://docs.sentry.io/platforms/android/migration/#migrating-from-iosentrysentry-android-gradle-plugin-2x-to-iosentrysentry-android-gradle-plugin-300
""".trimIndent()
)
}
val extension = project.extensions.create(
"sentry",
SentryPluginExtension::class.java,
project
)
project.pluginManager.withPlugin("com.android.application") {
val oldAGPExtension = project.extensions.getByType(AppExtension::class.java)
val androidComponentsExt =
project.extensions.getByType(AndroidComponentsExtension::class.java)
val cliExecutable = getSentryCliPath(project)
val extraProperties = project.extensions.getByName("ext")
as ExtraPropertiesExtension
val sentryOrgParameter = runCatching {
extraProperties.get(SENTRY_ORG_PARAMETER).toString()
}.getOrNull()
val sentryProjectParameter = runCatching {
extraProperties.get(SENTRY_PROJECT_PARAMETER).toString()
}.getOrNull()
// new API configuration
androidComponentsExt.configure(
project,
extension,
cliExecutable,
sentryOrgParameter,
sentryProjectParameter
)
// old API configuration
oldAGPExtension.configure(
project,
extension,
cliExecutable,
sentryOrgParameter,
sentryProjectParameter
)
project.installDependencies(extension)
}
project.pluginManager.withPlugin("org.gradle.java") {
if (project.pluginManager.hasPlugin("com.android.application")) {
// AGP also applies JavaBasePlugin, but since we have a separate setup for it,
// we just bail here
logger.info { "The Sentry Gradle plugin was already configured for AGP" }
return@withPlugin
}
if (configuredForJavaProject.getAndSet(true)) {
logger.info { "The Sentry Gradle plugin was already configured" }
return@withPlugin
}
val javaExtension = project.extensions.getByType(JavaPluginExtension::class.java)
val sentryResDir = project.layout.buildDirectory.dir("generated${sep}sentry")
javaExtension.sourceSets.getByName("main").resources { sourceSet ->
sourceSet.srcDir(sentryResDir)
}
val reportDependenciesTask = SentryExternalDependenciesReportTaskFactory.register(
project = project,
configurationName = "runtimeClasspath",
attributeValueJar = "jar",
includeReport = extension.includeDependenciesReport,
output = sentryResDir
)
val resourcesTask = withLogging(project.logger, "processResources") {
getProcessResourcesProvider(project)
}
resourcesTask?.configure { task -> task.dependsOn(reportDependenciesTask) }
}
}
companion object {
const val SENTRY_ORG_PARAMETER = "sentryOrg"
const val SENTRY_PROJECT_PARAMETER = "sentryProject"
internal const val SENTRY_SDK_VERSION = "6.15.0"
internal val sep = File.separator
// a single unified logger used by instrumentation
internal val logger by lazy {
LoggerFactory.getLogger(SentryPlugin::class.java)
}
}
}