1 /* 2 * Copyright 2018 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 com.android.tools.build.jetifier.plugin.gradle 18 19 import groovy.lang.Closure 20 import org.gradle.api.Project 21 import org.gradle.api.artifacts.Configuration 22 import org.gradle.api.artifacts.Dependency 23 import org.gradle.api.file.FileCollection 24 import java.nio.file.Paths 25 26 /** 27 * Defines methods that can be used in gradle on the "jetifier" object and triggers [JetifyLibsTask] 28 * or [JetifyGlobalTask] based on its usage. 29 */ 30 open class JetifierExtension(val project: Project) { 31 32 /** 33 * Adds dependency defined via string notation to be processed by jetifyLibs task. 34 * 35 * Example usage in Gradle: 36 * dependencies { 37 * compile jetifier.process('groupId:artifactId:1.0') 38 * } 39 */ 40 fun process(dependencyNotation: String): FileCollection { 41 return process(project.dependencies.create(dependencyNotation)) 42 } 43 44 /** 45 * Adds dependency defined via string notation to be processed by jetifyLibs task. This version 46 * supports Gradle's configuration closure that is passed to the Gradle's DependencyHandler. 47 * 48 * Example usage in Gradle: 49 * dependencies { 50 * compile jetifier.process('groupId:artifactId:1.0') { 51 * exclude group: 'groupId' 52 * 53 * transitive = false 54 * } 55 * } 56 */ 57 fun process(dependencyNotation: String, closure: Closure<Any>): FileCollection { 58 return process(project.dependencies.create(dependencyNotation, closure)) 59 } 60 61 /** 62 * Adds dependency to be processed by jetifyLibs task. 63 */ 64 fun process(dependency: Dependency): FileCollection { 65 val configuration = project.configurations.detachedConfiguration() 66 configuration.dependencies.add(dependency) 67 return process(configuration) 68 } 69 70 /** 71 * Adds dependencies defined via file collection to be processed by jetifyLibs task. 72 * 73 * Example usage in Gradle for a single file: 74 * dependencies { 75 * compile jetifier.process(files('../myFile1.jar')) 76 * compile jetifier.process(files('../myFile2.jar')) 77 * } 78 * 79 * Example usage in Gradle for a configuration: 80 * configurations.create('depToRefactor') 81 * 82 * dependencies { 83 * depToRefactor 'test:myDependency:1.0' 84 * depToRefactor 'test:myDependency2:1.0' 85 * } 86 * 87 * dependencies { 88 * compile jetifier.process(configurations.depToRefactor) 89 * } 90 */ 91 fun process(files: FileCollection): FileCollection { 92 return JetifyLibsTask.resolveTask(project).addFilesToProcess(files) 93 } 94 95 /** 96 * Adds a whole configuration to be processed by jetifyGlobal task. This is the recommended way 97 * if processing a set of dependencies where it is unknown which exactly need to be rewritten. 98 * 99 * This will create a new detached configuration and resolve all the dependencies = obtaining 100 * all the files. Jetifier is then run with all the files and only the files that were rewritten 101 * are added to the given configuration and the original dependencies that didn't have to be 102 * changed are kept. 103 * 104 * Advantage is that all the dependencies that didn't have to be changed are kept intact so 105 * their artifactsIds and groupIds are kept (instead of replacing them with files) which allows 106 * other steps in the build process to use the artifacts information to generate pom files 107 * and other stuff. 108 * 109 * This will NOT resolve the given configuration as the dependencies are resolved in a detached 110 * configuration. If you give it a configuration that was already resolved the process will 111 * end up with exception saying that resolved configuration cannot be changed. This is expected 112 * as Jetifier cannot add new files to an already resolved configuration. 113 * 114 * 115 * Example usage in Gradle: 116 * jetifier.addConfigurationToProcess(configurations.implementation) 117 * afterEvaluate { 118 * tasks.preBuild.dependsOn tasks.jetifyGlobal 119 * } 120 * 121 * 122 */ 123 fun addConfigurationToProcess(config: Configuration) { 124 JetifyGlobalTask.resolveTask(project).addConfigurationToProcess(config) 125 } 126 127 /** 128 * Sets a custom configuration file to be used by Jetifier. 129 */ 130 fun setConfigFile(configFilePath: String) { 131 TasksCommon.configFilePath = Paths.get(configFilePath) 132 } 133 }