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.consumer.PerVariantConsumerExtensionManager
20 import androidx.baselineprofile.gradle.utils.maybeRegister
21 import com.android.build.api.variant.Variant
22 import org.gradle.api.DefaultTask
23 import org.gradle.api.Project
24 import org.gradle.api.provider.Property
25 import org.gradle.api.tasks.Input
26 import org.gradle.api.tasks.TaskAction
27 import org.gradle.work.DisableCachingByDefault
28 
29 /**
30  * Only used for testing, this task does not have description or group so that it doesn't show up in
31  * the task list. It prints internal properties to facilitate assertions in integration tests.
32  */
33 @DisableCachingByDefault(because = "Not worth caching. Used only for tests.")
34 abstract class PrintConfigurationForVariantTask : DefaultTask() {
35 
36     companion object {
37 
38         private const val TASK_NAME_PREFIX = "printBaselineProfileExtensionForVariant"
39 
registerForVariantnull40         internal fun registerForVariant(
41             project: Project,
42             variant: Variant,
43             variantConfig: PerVariantConsumerExtensionManager.VariantConfigurationProxy
44         ) {
45             project.tasks.maybeRegister<PrintConfigurationForVariantTask>(
46                 TASK_NAME_PREFIX,
47                 variant.name
48             ) {
49                 it.text.set(
50                     """
51                     mergeIntoMain=`${variantConfig.mergeIntoMain}`
52                     baselineProfileOutputDir=`${variantConfig.baselineProfileOutputDir}`
53                     baselineProfileRulesRewrite=`${variantConfig.baselineProfileRulesRewrite}`
54                     dexLayoutOptimization=`${variantConfig.dexLayoutOptimization}`
55                     saveInSrc=`${variantConfig.saveInSrc}`
56                     automaticGenerationDuringBuild=`${variantConfig.automaticGenerationDuringBuild}`
57                     """
58                         .trimIndent()
59                 )
60             }
61         }
62     }
63 
64     @get:Input abstract val text: Property<String>
65 
66     @TaskAction
execnull67     fun exec() {
68         logger.warn(text.get())
69     }
70 }
71