Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable source context tasks if not enabled #536

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Add release information args to proguard mapping upload task ([#476](https://github.com/getsentry/sentry-android-gradle-plugin/pull/476))
- Auto install Sentry integrations for Java Backend, Desktop, etc. ([#521](https://github.com/getsentry/sentry-android-gradle-plugin/pull/521))

### Fixes

- Disable source context tasks if not enabled ([#536](https://github.com/getsentry/sentry-android-gradle-plugin/pull/536))

### Dependencies

- Bump CLI from v2.19.1 to v2.20.0 ([#520](https://github.com/getsentry/sentry-android-gradle-plugin/pull/520), [#531](https://github.com/getsentry/sentry-android-gradle-plugin/pull/531))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ abstract class BundleSourcesTask : Exec() {
sentryOrg: Provider<String>,
sentryProject: Provider<String>,
sentryAuthToken: Property<String>,
includeSourceContext: Property<Boolean>,
taskSuffix: String = ""
): TaskProvider<BundleSourcesTask> {
return project.tasks.register(
Expand All @@ -157,7 +158,10 @@ abstract class BundleSourcesTask : Exec() {
}
task.bundleIdFile.set(generateDebugIdTask.flatMap { it.outputFile })
task.output.set(output)
task.onlyIf { !task.sourceDir.asFileTree.isEmpty }
task.onlyIf {
includeSourceContext.getOrElse(false) &&
!task.sourceDir.asFileTree.isEmpty
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.Directory
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.InputFiles
Expand Down Expand Up @@ -42,6 +43,7 @@ abstract class CollectSourcesTask : DirectoryOutputTask() {
project: Project,
sourceDirs: Provider<out Collection<Directory>>,
output: Provider<Directory>,
includeSourceContext: Property<Boolean>,
taskSuffix: String = ""
): TaskProvider<CollectSourcesTask> {
return project.tasks.register(
Expand All @@ -50,6 +52,7 @@ abstract class CollectSourcesTask : DirectoryOutputTask() {
) { task ->
task.sourceDirs.setFrom(sourceDirs)
task.output.set(output)
task.onlyIf { includeSourceContext.getOrElse(false) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.util.UUID
import org.gradle.api.Project
import org.gradle.api.file.Directory
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskAction
Expand Down Expand Up @@ -50,13 +51,15 @@ abstract class GenerateBundleIdTask : PropertiesFileOutputTask() {
fun register(
project: Project,
output: Provider<Directory>? = null,
includeSourceContext: Property<Boolean>,
taskSuffix: String = ""
): TaskProvider<GenerateBundleIdTask> {
val generateBundleIdTask = project.tasks.register(
taskName(taskSuffix),
GenerateBundleIdTask::class.java
) { task ->
output?.let { task.output.set(it) }
task.onlyIf { includeSourceContext.getOrElse(false) }
}
return generateBundleIdTask
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ class SourceContext {
val generateBundleIdTask = GenerateBundleIdTask.register(
project,
output = paths.bundleIdDir,
extension.includeSourceContext,
taskSuffix
)

val collectSourcesTask = CollectSourcesTask.register(
project,
sourceFiles,
output = paths.sourceDir,
extension.includeSourceContext,
taskSuffix
)

Expand All @@ -49,6 +51,7 @@ class SourceContext {
sentryOrg?.let { project.provider { it } } ?: extension.org,
sentryProject?.let { project.provider { it } } ?: extension.projectName,
extension.authToken,
extension.includeSourceContext,
taskSuffix
)

Expand All @@ -62,6 +65,7 @@ class SourceContext {
sentryOrg?.let { project.provider { it } } ?: extension.org,
sentryProject?.let { project.provider { it } } ?: extension.projectName,
extension.authToken,
extension.includeSourceContext,
taskSuffix
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ abstract class UploadSourceBundleTask : Exec() {
sentryOrg: Provider<String>,
sentryProject: Provider<String>,
sentryAuthToken: Property<String>,
includeSourceContext: Property<Boolean>,
taskSuffix: String = ""
): TaskProvider<UploadSourceBundleTask> {
return project.tasks.register(
Expand All @@ -138,7 +139,10 @@ abstract class UploadSourceBundleTask : Exec() {
SentryPropertiesFileProvider.getPropertiesFilePath(project, variant)?.let {
task.sentryProperties.set(File(it))
}
task.onlyIf { !task.sourceBundleDir.asFileTree.isEmpty }
task.onlyIf {
includeSourceContext.getOrElse(false) &&
!task.sourceBundleDir.asFileTree.isEmpty
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class SentryGenerateDebugMetaPropertiesTask : DirectoryOutputTask() {
val props = Properties()
props.setProperty("io.sentry.build-tool", "gradle")
inputFiles.forEach { inputFile ->
props.putAll(PropertiesUtil.load(inputFile))
PropertiesUtil.loadMaybe(inputFile)?.let { props.putAll(it) }
}
debugMetaPropertiesFile.writer().use {
props.store(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@ class PropertiesUtil {
}
}
}

fun loadMaybe(file: File): Properties? {
if (!file.exists()) {
return null
}

return load(file)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,42 @@ class SentrySourceContextNonAndroidTest(
assertTrue { "BUILD SUCCESSFUL" in result.output }
}

@Test
fun `skips bundle and upload tasks if disabled`() {
appBuildFile.writeText(
// language=Groovy
"""
plugins {
id "org.jetbrains.kotlin.jvm"
id "io.sentry.jvm.gradle"
}

sentry {
includeSourceContext = false
}
""".trimIndent()
)

sentryPropertiesFile.writeText("")

testProjectDir.withDummyKtFile()
testProjectDir.withDummyJavaFile()

val result = runner
.appendArguments("app:assemble")
.build()

assertEquals(
result.task(":app:sentryUploadSourceBundleJava")?.outcome,
SKIPPED
)
assertEquals(
result.task(":app:sentryBundleSourcesJava")?.outcome,
SKIPPED
)
assertTrue { "BUILD SUCCESSFUL" in result.output }
}

@Test
fun `bundles source context`() {
appBuildFile.writeText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class GenerateBundleIdTaskTest {
GenerateBundleIdTask.register(
project,
project.layout.buildDirectory.dir("dummy/folder/"),
project.objects.property(Boolean::class.java).convention(true),
"test"
)

Expand All @@ -41,6 +42,7 @@ class GenerateBundleIdTaskTest {
GenerateBundleIdTask.register(
project,
project.layout.buildDirectory.dir("dummy/folder/"),
project.objects.property(Boolean::class.java).convention(true),
"test"
)
val expectedFile = File(project.buildDir, "dummy/folder/sentry-bundle-id.properties")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class SentryGenerateDebugMetaPropertiesTaskTest {
val bundleIdTask = GenerateBundleIdTask.register(
project,
project.layout.buildDirectory.dir("dummy/folder/"),
project.objects.property(Boolean::class.java).convention(true),
"test"
)
val proguardIdTask = SentryGenerateProguardUuidTask.register(
Expand Down Expand Up @@ -57,6 +58,7 @@ class SentryGenerateDebugMetaPropertiesTaskTest {
val bundleIdTask = GenerateBundleIdTask.register(
project,
project.layout.buildDirectory.dir("dummy/folder/"),
project.objects.property(Boolean::class.java).convention(true),
"test"
)
val proguardIdTask = SentryGenerateProguardUuidTask.register(
Expand Down