1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.baselineprofile.gradle.consumer.task
18 
19 import androidx.baselineprofile.gradle.utils.BaselineProfilePluginLogger
20 import androidx.baselineprofile.gradle.utils.TASK_NAME_SUFFIX
21 import androidx.baselineprofile.gradle.utils.Warnings
22 import androidx.baselineprofile.gradle.utils.maybeRegister
23 import com.android.build.gradle.internal.tasks.BuildAnalyzer
24 import com.android.buildanalyzer.common.TaskCategory
25 import org.gradle.api.DefaultTask
26 import org.gradle.api.Project
27 import org.gradle.api.provider.Property
28 import org.gradle.api.tasks.Input
29 import org.gradle.api.tasks.TaskAction
30 import org.gradle.api.tasks.TaskProvider
31 import org.gradle.work.DisableCachingByDefault
32 
33 private const val GENERATE_TASK_NAME = "generate"
34 
35 /** This task does nothing and it's only to start the generation process. */
36 @DisableCachingByDefault(because = "Not worth caching.")
37 @BuildAnalyzer(primaryTaskCategory = TaskCategory.OPTIMIZATION)
38 internal abstract class GenerateBaselineProfileTask : DefaultTask() {
39 
40     companion object {
maybeCreatenull41         fun maybeCreate(
42             project: Project,
43             variantName: String,
44             lastTaskProvider: TaskProvider<*>? = null,
45         ) =
46             project.tasks.maybeRegister<GenerateBaselineProfileTask>(
47                 GENERATE_TASK_NAME,
48                 variantName,
49                 TASK_NAME_SUFFIX
50             ) {
51                 if (lastTaskProvider != null) it.dependsOn(lastTaskProvider)
52             }
53     }
54 
55     init {
56         group = "Baseline Profile"
57         description = "Generates a baseline profile for the specified variants or dimensions."
58     }
59 }
60 
61 /**
62  * This task does nothing and it's only to start the generation process. This task differs from
63  * [GenerateBaselineProfileTask] and it's used ONLY to print the warning about
64  * `generateBaselineProfile` generating only for `release` with AGP 8.0.
65  */
66 @DisableCachingByDefault(because = "Not worth caching.")
67 @BuildAnalyzer(primaryTaskCategory = TaskCategory.OPTIMIZATION)
68 internal abstract class MainGenerateBaselineProfileTaskForAgp80Only : DefaultTask() {
69 
70     companion object {
maybeCreatenull71         fun maybeCreate(
72             project: Project,
73             variantName: String,
74             lastTaskProvider: TaskProvider<*>? = null,
75             warnings: Warnings
76         ) =
77             project.tasks.maybeRegister<MainGenerateBaselineProfileTaskForAgp80Only>(
78                 GENERATE_TASK_NAME,
79                 variantName,
80                 TASK_NAME_SUFFIX
81             ) {
82                 if (lastTaskProvider != null) it.dependsOn(lastTaskProvider)
83                 it.printWarningMultipleBuildTypesWithAgp80.set(warnings.multipleBuildTypesWithAgp80)
84             }
85     }
86 
87     init {
88         group = "Baseline Profile"
89         description = "Generates a baseline profile for the `release` variant."
90     }
91 
92     @get:Input abstract val printWarningMultipleBuildTypesWithAgp80: Property<Boolean>
93 
<lambda>null94     private val logger by lazy { BaselineProfilePluginLogger(this.getLogger()) }
95 
96     @TaskAction
execnull97     fun exec() {
98         this.logger.warn(
99             property = { printWarningMultipleBuildTypesWithAgp80.get() },
100             propertyName = "multipleBuildTypesWithAgp80",
101             message =
102                 """
103         The task `generateBaselineProfile` does not support generating baseline profiles for
104         multiple build types with AGP 8.0.
105 
106         Only baseline profile for variants of build type `release` will be generated.
107         With AGP 8.0, this command behaves like `generateReleaseBaselineProfile`.
108 
109         If you intend to generate profiles for multiple build types using AGP 8.0 you'll
110         need to run separate gradle commands for each build type.
111         Example: `generateReleaseBaselineProfile` and `generateAnotherReleaseBaselineProfile`.
112 
113         Details on https://issuetracker.google.com/issue?id=270433400.
114             """
115                     .trimIndent()
116         )
117     }
118 }
119