Skip to content

Commit

Permalink
configuration cache initial compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
xvik committed Jan 15, 2025
1 parent 3e589c0 commit a9cae70
Show file tree
Hide file tree
Showing 15 changed files with 787 additions and 58 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
* Add new properties to AnimalSniffer task to simplify detailed configuration:
- targetType: type of task (SourceSet, MultiplatformTarget, AndroidVariant)
- targetName: name of target (used in animalsniffer task name)
* Call animalsniffer directly instead of ant tasks
* Call animalsniffer directly instead of ant tasks
* Configuration cache compatibility

### 1.7.2 (2024-11-18)
* Update animalsniffer 1.23 -> 1.24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ class AnimalSnifferPlugin implements Plugin<Project> {
}
checkTask.configure {
// skip if no signatures configured or no sources to check
onlyIf { !getAnimalsnifferSignatures().empty && getSource().size() > 0 }
onlyIf { !it.getAnimalsnifferSignatures().empty && it.getSource().size() > 0 }
conventionMapping.with {
classpath = {
extension.cache.enabled ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import org.gradle.work.DisableCachingByDefault
Expand Down Expand Up @@ -57,9 +58,17 @@ class PrintAnimalsnifferSourceInfoTask extends DefaultTask {
@Input
boolean printClasspath = true

private final File rootProjectDir
private final Provider<DebugModel> modelProvider

PrintAnimalsnifferSourceInfoTask() {
rootProjectDir = project.rootProject.rootDir
modelProvider = project.provider { createModel(project, printClasspath) }
}

@TaskAction
void action() {
DebugModel model = createModel()
DebugModel model = modelProvider.get()

if (printPlugins) {
println '\n\n== [Plugins] ==============================================================='
Expand All @@ -68,27 +77,27 @@ class PrintAnimalsnifferSourceInfoTask extends DefaultTask {

if (printCompileTasks) {
println '\n\n== [Compile Tasks] ==============================================================='
printCompileTasks(project, model.compileTasks)
printCompileTasks(rootProjectDir, model.compileTasks)
}

if (printSourceSets) {
println '\n\n== [SourceSets] ==============================================================='

printSourceSets(project, model)
printSourceSets(rootProjectDir, model)
}

if (printKotlinTargets && model.kotlinTargets) {
printTargets(project, model.kotlinTargets)
printTargets(rootProjectDir, model.kotlinTargets)
}

if (printAndroidVariants && model.androidVariants) {
printVariants(project, model.androidVariants)
printVariants(rootProjectDir, model.androidVariants)
}

println '\n\\===========================================================================================\n'
}

private DebugModel createModel() {
static DebugModel createModel(Project project, boolean printClasspath) {
DebugModel model = new DebugModel()
model.plugins = new PluginsCollector().collect(project)

Expand Down Expand Up @@ -134,7 +143,7 @@ class PrintAnimalsnifferSourceInfoTask extends DefaultTask {
}

@CompileStatic(TypeCheckingMode.SKIP)
private static void printCompileTasks(Project project, CompileTasksModel model) {
private static void printCompileTasks(File rootProjectDir, CompileTasksModel model) {
println title(1,
"Tasks containing 'compile' in name ------------------------------------ (${model.allTasks.size()})")
println()
Expand All @@ -154,7 +163,7 @@ class PrintAnimalsnifferSourceInfoTask extends DefaultTask {

println title(1, "Java compile tasks --------------------------------------------- (${model.javaTasks.size()})")
model.javaTasks.each {
printInOutClasspath(project, 2, "[${it.name}] -----",
printInOutClasspath(rootProjectDir, 2, "[${it.name}] -----",
it.sourceDirs,
it.classes,
it.classpath
Expand All @@ -166,7 +175,7 @@ class PrintAnimalsnifferSourceInfoTask extends DefaultTask {
"Kotlin compile tasks ------------------------------------------ (${model.kotlinTasks.size()})")

model.kotlinTasks.each {
printInOutClasspath(project, 2, "[${it.name}] -----",
printInOutClasspath(rootProjectDir, 2, "[${it.name}] -----",
it.sourceDirs,
it.classes,
it.classpath
Expand Down Expand Up @@ -196,32 +205,32 @@ class PrintAnimalsnifferSourceInfoTask extends DefaultTask {
}
}

private static void printSourceSets(Project project, DebugModel model) {
private static void printSourceSets(File rootProjectDir, DebugModel model) {
if (model.java) {
printSourceSets(project, 1,
printSourceSets(rootProjectDir, 1,
'Java Source Sets -------------------------------------------------------', model.java)
}
if (model.kotlin) {
printSourceSets(project, 1,
printSourceSets(rootProjectDir, 1,
'Kotlin Multiplatform Source Sets -------------------------------------------------------',
model.kotlin, model.kotlinTargetSourceSetsIndex)
if (model.android) {
printKotlinDifference(project, 1, 'Android sources NOT COVERED by kotlin source sets',
printKotlinDifference(rootProjectDir, 1, 'Android sources NOT COVERED by kotlin source sets',
model.android, model.kotlin)
}
}
if (model.android) {
printSourceSets(project, 1, "Android ${model.androidLibrary ? 'library' : 'application'} " +
printSourceSets(rootProjectDir, 1, "Android ${model.androidLibrary ? 'library' : 'application'} " +
'Source Sets ------------------------------------------------------',
model.android, model.androidVariantsSourceSetsIndex)
if (model.kotlin) {
printKotlinDifference(project, 1, 'Kotlin sources NOT COVERED by android source sets',
printKotlinDifference(rootProjectDir, 1, 'Kotlin sources NOT COVERED by android source sets',
model.kotlin, model.android)
}
}
}

private static void printKotlinDifference(Project project, int shift, String msg,
private static void printKotlinDifference(File rootProjectDir, int shift, String msg,
List<SourceSetInfo> koltin, List<SourceSetInfo> android) {
Map<File, String> kotlinIdx = [:]
koltin.each { set -> set.sourceDirs.each { file -> kotlinIdx[file] = set.name } }
Expand All @@ -236,25 +245,25 @@ class PrintAnimalsnifferSourceInfoTask extends DefaultTask {
println title(shift, msg)
notCoveredByKotlin.each {
println String.format(buildPrefix(shift + 1) + '%-70s (%s)',
project.rootProject.relativePath(it), kotlinIdx[it])
it.canonicalPath.replace(getRootPath(rootProjectDir), ''), kotlinIdx[it])
}
}
}

private static void printVariants(Project project, List<AndroidVariantInfo> info) {
private static void printVariants(File rootProjectDir, List<AndroidVariantInfo> info) {
println title(
"== [Android Variants] ========================================================== (${info.size()})")

info.each {
println title(1, "${it.name} ===== (compiled by ${it.compileTaskName} task)")
printSourceSets(project, 2, 'Source sets', it.sourceSets)
printSourceSets(rootProjectDir, 2, 'Source sets', it.sourceSets)

// sources rendered above (level 1 because no title and actual level would be 2)
printInOutClasspath(project, 1, null, null, it.classes, it.classpath)
printInOutClasspath(rootProjectDir, 1, null, null, it.classes, it.classpath)
}
}

private static void printTargets(Project project, List<KotlinTargetInfo> targets) {
private static void printTargets(File rootProjectDir, List<KotlinTargetInfo> targets) {
println title(
"== [Kotlin targets] ========================================================= (${targets.size()})")

Expand All @@ -265,11 +274,11 @@ class PrintAnimalsnifferSourceInfoTask extends DefaultTask {
println title(2, "$it.name (compiled by $it.compileTaskName task)")

if (it.sourceSets) {
printSourceSets(project, 3, 'Source sets', it.sourceSets)
printSourceSets(rootProjectDir, 3, 'Source sets', it.sourceSets)
}

// sources rendered above
printInOutClasspath(project, 2, null, null, it.classes, it.classpath)
printInOutClasspath(rootProjectDir, 2, null, null, it.classes, it.classpath)

if (it.associatedCompilations) {
println title(3, "Associated compilations (${it.associatedCompilations.size()})")
Expand All @@ -281,12 +290,12 @@ class PrintAnimalsnifferSourceInfoTask extends DefaultTask {
}
}

private static void printSourceSets(Project project, int shift, String name, List<SourceSetInfo> info,
private static void printSourceSets(File rootProjectDir, int shift, String name, List<SourceSetInfo> info,
Map<String, String> inclusiveIndex = null) {
println title(shift, "$name (${info.size()})")

info.each {
printInOutClasspath(project, shift + 1,
printInOutClasspath(rootProjectDir, shift + 1,
"$it.name -----" + (inclusiveIndex?.get(it.name)
? " (${inclusiveIndex.get(it.name)})" : ''),
it.sourceDirs, it.classes, it.classpath
Expand All @@ -295,7 +304,7 @@ class PrintAnimalsnifferSourceInfoTask extends DefaultTask {
}

@SuppressWarnings('ParameterCount')
private static void printInOutClasspath(Project project,
private static void printInOutClasspath(File rootProjectDir,
int shift,
String msg,
Collection<File> sources,
Expand All @@ -308,21 +317,21 @@ class PrintAnimalsnifferSourceInfoTask extends DefaultTask {
if (sources) {
if (classes || classpath) {
println title(shift + 1, 'Sources')
println renderSources(shift + 2, sources, project)
println renderSources(shift + 2, sources, rootProjectDir)
} else {
// short notion for kotlin and android source sets
println renderSources(shift + 1, sources, project)
println renderSources(shift + 1, sources, rootProjectDir)
}
}

if (classes) {
println title(shift + 1, 'Output')
println renderClasses(shift + 2, classes, project)
println renderClasses(shift + 2, classes, rootProjectDir)
}

if (classpath) {
println title(shift + 1, 'Classpath')
println renderClasspath(project, shift + 2, classpath)
println renderClasspath(rootProjectDir, shift + 2, classpath)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ class PrintAnimalsnifferTasksTask extends DefaultTask {
}
println "\t\tdepends on: ${tasks.sort().join(', ')}"
Set<File> sigs = (sigTask?.signatures ?: task.animalsnifferSignatures).files
File rootDir = project.rootProject.rootDir
println "\t\tsignatures: ${sigTask ? '(cached signature)' : ''}\n" +
(sigs.empty ? '\t\t\t<No signatures>' : PrintUtils.renderClasspath(project, 3, sigs))
(sigs.empty ? '\t\t\t<No signatures>'
: PrintUtils.renderClasspath(rootDir, 3, sigs))
println '\t\tclasses:\n' + (task.classesDirs.empty ? '\t\t\t<empty>' : PrintUtils.renderClasses(
3, task.classesDirs, project))
3, task.classesDirs, rootDir))
println '\t\tsources:\n' + PrintUtils.renderSources(
3, task.sourcesDirs.files, project, true)
3, task.sourcesDirs.files, rootDir, true)
}
println "\n*use [$PrintAnimalsnifferSourceInfoTask.NAME] task to see project sources configuration details\n"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ class AndroidSourceSetsInfoCollector {
new AndroidSourceSetReactor(project).onTarget {
Set<File> classpath = []
if (collectClasspath) {
Configuration configuration = project.configurations
.create("resolve$it.implementationConfigurationName")
// not resolvable configuration (need another configuration to resolve it)
configuration.extendsFrom(project.configurations.getByName(it.implementationConfigurationName))
configuration.canBeResolved = true
configuration.attributes {
it.attributes.attribute(Attribute.of('ui', String), 'awt')
String resolveConfName = "resolve$it.implementationConfigurationName"
Configuration configuration = project.configurations.findByName(resolveConfName)
if (configuration == null) {
configuration = project.configurations.create(resolveConfName)
// not resolvable configuration (need another configuration to resolve it)
configuration.extendsFrom(project.configurations.getByName(it.implementationConfigurationName))
configuration.canBeResolved = true
configuration.attributes {
it.attributes.attribute(Attribute.of('ui', String), 'awt')
}
}

classpath = PrintUtils.resolve(configuration, "for $it.name android source set")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ class MultiplatformSourceSetsInfoCollector {
Set<File> cp = []
if (collectClasspath) {
// using extra configuration to specify resolution attributes
Configuration configuration = project.configurations
.create("resolve$it.implementationConfigurationName")
configuration.extendsFrom(project.configurations.getByName(it.compileDependencyConfigurationName))
configuration.canBeResolved = true
configuration.attributes {
attributes.attribute(Attribute.of('ui', String), 'awt')
String resolveConfName = "resolve$it.implementationConfigurationName"
Configuration configuration = project.configurations.findByName(resolveConfName)
if (configuration == null) {
configuration = project.configurations.create(resolveConfName)
configuration.extendsFrom(project.configurations
.getByName(it.compileDependencyConfigurationName))
configuration.canBeResolved = true
configuration.attributes {
attributes.attribute(Attribute.of('ui', String), 'awt')
}
}

cp = PrintUtils.resolve(configuration, "for kotlin compilation $it.name")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,29 @@ class PrintUtils {
return NL + buildPrefix(shift) + title
}

static String renderSources(int shift, Collection<File> sourceDirs, Project project, boolean existsMarker = true) {
static String renderSources(int shift, Collection<File> sourceDirs, File rootDir,
boolean existsMarker = true) {
String prefix = buildPrefix(shift)
return sourceDirs.collect {
String path = project.rootProject.relativePath(it)
String path = it.canonicalPath.replace(getRootPath(rootDir), '')
it.exists() ? "$prefix$path" : String.format("$prefix%-80s %s", path,
!existsMarker || it.exists() ? '' : 'NOT EXISTS')
}.unique().sort().join(NL)
}

static String renderClasses(int shift, Collection<File> classDirs, Project project) {
static String renderClasses(int shift, Collection<File> classDirs, File rootDir) {
String prefix = buildPrefix(shift)
return classDirs.collect {
prefix + project.rootProject.relativePath(it)
prefix + it.canonicalPath.replace(getRootPath(rootDir), '')
}.unique().sort().join(NL)
}

static String renderClasspath(Project project, int shift, Collection<File> files) {
static String renderClasspath(File rootDir, int shift, Collection<File> files) {
String prefix = buildPrefix(shift)
files.collect {
prefix + (it.canonicalPath.startsWith(project.rootDir.canonicalPath)
? project.rootProject.relativePath(it) : it.name)
String rootPath = getRootPath(rootDir)
prefix + (it.canonicalPath.startsWith(rootPath)
? it.canonicalPath.replace(rootPath, '') : it.name)
}.sort().unique().join(NL)
}

Expand All @@ -128,6 +130,10 @@ class PrintUtils {
return res
}

static String getRootPath(File dir) {
return dir.canonicalPath + File.separator
}

@CompileStatic(TypeCheckingMode.SKIP)
@SuppressWarnings(['CouldBeSwitchStatement', 'BlockEndsWithBlankLine'])
private static Set<Task> resolveDependentTask(Project project, Task task, Object depends) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AndroidTaskConfigurationProvider implements AnimalsnifferTaskConfiguration

private final String name
private final String desc
private final TaskProvider<AndroidClassesCollector> classesTask
private final String classesTask
private final Provider<FileCollection> classes
private final Provider<FileCollection> classpath
private final Provider<FileCollection> sources
Expand All @@ -31,7 +31,7 @@ class AndroidTaskConfigurationProvider implements AnimalsnifferTaskConfiguration
TaskProvider<AndroidClassesCollector> classesTask) {
name = variant.name
desc = "for '$name' android ${name.containsIgnoreCase('test') ? 'test component' : 'variant'}"
this.classesTask = classesTask
this.classesTask = classesTask.name
classes = providers.provider {
objects.fileCollection().from(classesTask.flatMap { it.classesDirs })
}
Expand Down Expand Up @@ -79,6 +79,6 @@ class AndroidTaskConfigurationProvider implements AnimalsnifferTaskConfiguration

@Override
String getCompileTaskName() {
return classesTask.name
return classesTask
}
}
Loading

0 comments on commit a9cae70

Please sign in to comment.