-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathInjectSentryMetaPropertiesIntoAssetsTask.kt
More file actions
143 lines (124 loc) · 5.16 KB
/
InjectSentryMetaPropertiesIntoAssetsTask.kt
File metadata and controls
143 lines (124 loc) · 5.16 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
// Adapted from
// https://github.com/android/gradle-recipes/blob/efeedbc78465547280b5c13b3e04a65b70fa1e26/transformDirectory/build-logic/plugins/src/main/kotlin/TransformAssetsTask.kt
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sentry.android.gradle.tasks
import io.sentry.android.gradle.extensions.SentryPluginExtension
import io.sentry.android.gradle.sourcecontext.getAndDelete
import io.sentry.android.gradle.tasks.dependencies.SentryExternalDependenciesReportTaskV2
import io.sentry.android.gradle.telemetry.SentryTelemetryService
import io.sentry.android.gradle.telemetry.withSentryTelemetry
import io.sentry.android.gradle.util.PropertiesUtil
import java.io.File
import java.util.Properties
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.TaskProvider
@CacheableTask
abstract class InjectSentryMetaPropertiesIntoAssetsTask : DefaultTask() {
@get:PathSensitive(PathSensitivity.RELATIVE)
@get:InputFiles
abstract val inputDir: DirectoryProperty
@get:OutputDirectory
val outputDir: DirectoryProperty = project.objects.directoryProperty()
get() {
// AGP < 8.3 sets an output folder which contains the input folder
// input: app/intermediates/assets/release/mergeReleaseAssets
// output: app/intermediates/assets/release/
// re-route output to a sub directory instead,
// as otherwise this breaks the gradle cache functionality
@Suppress("SENSELESS_COMPARISON")
if (field == null || !field.isPresent) {
return field
}
if (!field.get().asFile.name.equals(name)) {
field.set(File(field.get().asFile, name))
}
return field
}
// we only care about file contents
@get:PathSensitive(PathSensitivity.NONE)
@get:InputFiles
abstract val inputPropertyFiles: ConfigurableFileCollection
@get:PathSensitive(PathSensitivity.NONE)
@get:InputFiles
@get:Optional
abstract val dependenciesReportDir: DirectoryProperty
@TaskAction
fun taskAction() {
val input = inputDir.get().asFile
val output = outputDir.getAndDelete()
output.mkdirs()
input.copyRecursively(output, overwrite = true)
// Copy the dependencies report if present
val depReportDir = dependenciesReportDir.orNull?.asFile
if (depReportDir != null && depReportDir.exists()) {
depReportDir.listFiles()?.forEach { file ->
file.copyTo(File(output, file.name), overwrite = true)
}
}
// skip writing the properties file if there are no input property files
// this avoids generating an empty properties file when all Sentry features are disabled
if (inputPropertyFiles.isEmpty) {
return
}
// merge props
val props = Properties()
props.setProperty("io.sentry.build-tool", "gradle")
inputPropertyFiles.forEach { inputFile ->
PropertiesUtil.loadMaybe(inputFile)?.let { props.putAll(it) }
}
val propsFile = File(output, SENTRY_DEBUG_META_PROPERTIES_OUTPUT)
propsFile.writer().use {
PropertiesUtil.store(props, it, "Generated by sentry-android-gradle-plugin")
}
}
companion object {
internal const val SENTRY_DEBUG_META_PROPERTIES_OUTPUT = "sentry-debug-meta.properties"
fun register(
project: Project,
extension: SentryPluginExtension,
sentryTelemetryProvider: Provider<SentryTelemetryService>?,
tasksGeneratingProperties: List<TaskProvider<out PropertiesFileOutputTask>>,
taskSuffix: String = "",
reportDependenciesTask: TaskProvider<SentryExternalDependenciesReportTaskV2>? = null,
): TaskProvider<InjectSentryMetaPropertiesIntoAssetsTask> {
val inputFiles: List<Provider<RegularFile>> =
tasksGeneratingProperties.mapNotNull { it.flatMap { task -> task.outputFile } }
return project.tasks.register(
"injectSentryDebugMetaPropertiesIntoAssets$taskSuffix",
InjectSentryMetaPropertiesIntoAssetsTask::class.java,
) { task ->
task.inputPropertyFiles.setFrom(inputFiles)
reportDependenciesTask?.let { depTask ->
task.dependenciesReportDir.set(depTask.flatMap { it.output })
}
task.withSentryTelemetry(extension, sentryTelemetryProvider)
}
}
}
}