Skip to content

Commit

Permalink
Offline cache all module files (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse authored Jan 12, 2024
1 parent f5c59bd commit b31e0bc
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class OfflineDependenciesPlugin implements Plugin<Project> {

def extension = project.extensions.create(EXTENSION_NAME, OfflineDependenciesExtension, repositoryHandler)

project.dependencies {
components {
all(io.pry.gradle.offline_dependencies.maven.DirectMetadataAccessVariantRule)
}
}

project.logger.info("Offline dependencies root configured at '${project.ext.offlineRepositoryRoot}'")

project.task('updateOfflineRepository', type: UpdateOfflineRepositoryTask) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import org.gradle.api.artifacts.UnknownConfigurationException
import org.gradle.api.artifacts.component.ComponentIdentifier
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.api.artifacts.result.UnresolvedArtifactResult
import org.gradle.api.attributes.DocsType
import org.gradle.api.model.ObjectFactory
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.InputFiles
Expand All @@ -33,6 +35,8 @@ import org.gradle.util.GFileUtils
import static io.pry.gradle.offline_dependencies.Utils.addToMultimap

class UpdateOfflineRepositoryTask extends DefaultTask {
@javax.inject.Inject
ObjectFactory getObjects() { }

@Internal
def EMPTY_DEPENDENCIES_ARRAY = new Dependency[0]
Expand Down Expand Up @@ -189,6 +193,27 @@ class UpdateOfflineRepositoryTask extends DefaultTask {
private void collectPoms(Set<ComponentIdentifier> componentIds, Map<ComponentIdentifier, Set<File>> repositoryFiles) {
logger.trace("Collecting pom files")

def deps = []

componentIds.each {
def dep = project.dependencies.create("${it.group}:${it.module}:${it.version}")
deps << dep
}

def cfg = project.configurations.detachedConfiguration(deps.toArray(EMPTY_DEPENDENCIES_ARRAY))

def view = cfg.incoming.artifactView {
withVariantReselection()
lenient = true
attributes {
attribute(DocsType.DOCS_TYPE_ATTRIBUTE, this.objects.named(DocsType, "modules"))
}
}

view.artifacts.each {
addToMultimap(repositoryFiles, it.variant.owner, it.file)
}

def mavenArtifacts = project.dependencies.createArtifactResolutionQuery()
.forComponents(componentIds)
.withArtifacts(MavenModule, MavenPomArtifact)
Expand Down Expand Up @@ -219,6 +244,10 @@ class UpdateOfflineRepositoryTask extends DefaultTask {
pomModelResolver.componentCache().each { componentId, pomFile ->
addToMultimap(repositoryFiles, componentId, pomFile)
}

pomModelResolver.moduleComponentCache().each { componentId, moduleFile ->
addToMultimap(repositoryFiles, componentId, moduleFile)
}
}

private Model resolvePom(PomDependencyModelResolver pomModelResolver, File pomFile) {
Expand Down Expand Up @@ -317,4 +346,3 @@ class UpdateOfflineRepositoryTask extends DefaultTask {
new File("${getRoot()}".toString(), "${ci.group.tokenize(".").join("/")}/${ci.module}/${ci.version}")
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.pry.gradle.offline_dependencies.maven

import org.gradle.api.artifacts.ComponentMetadataContext
import org.gradle.api.artifacts.ComponentMetadataRule
import org.gradle.api.attributes.Category
import org.gradle.api.attributes.DocsType
import org.gradle.api.model.ObjectFactory

class DirectMetadataAccessVariantRule implements ComponentMetadataRule {
@javax.inject.Inject
ObjectFactory getObjects() { }
void execute(ComponentMetadataContext context) {
def id = context.details.id
// context.details.addVariant("withPom") {
// attributes {
// attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.DOCUMENTATION))
// attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, "poms"))
// }
// withFiles {
// addFile("${id.name}-${id.version}.pom")
// }
// }
context.details.addVariant("withModules") {
attributes {
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.DOCUMENTATION))
attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, "modules"))
}
withFiles {
removeAllFiles()
addFile("${id.name}-${id.version}.module")
}
}
// context.details.addVariant("withSources") {
// attributes {
// attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.DOCUMENTATION))
// attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, "srcs"))
// }
// withFiles {
// removeAllFiles()
// addFile("${id.name}-${id.version}-sources.jar")
// }
// }
// context.details.addVariant("withJavadoc") {
// attributes {
// attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.DOCUMENTATION))
// attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, "jdcs"))
// }
// withFiles {
// removeAllFiles()
// addFile("${id.name}-${id.version}-javadoc.jar")
// }
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ import org.apache.maven.model.resolution.UnresolvableModelException
import org.gradle.api.artifacts.result.UnresolvedArtifactResult
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.api.Project
import org.gradle.api.attributes.DocsType
import org.gradle.api.model.ObjectFactory
import org.gradle.internal.component.external.model.DefaultModuleComponentIdentifier
import org.gradle.api.internal.artifacts.DefaultModuleVersionIdentifier
import org.gradle.maven.MavenModule
import org.gradle.maven.MavenPomArtifact
import org.gradle.api.artifacts.Dependency

class PomDependencyModelResolver implements ModelResolver {

def EMPTY_DEPENDENCIES_ARRAY = new Dependency[0]

private Project project
private Map<String, FileModelSource> pomCache = [:]
private Map<String, FileModelSource> moduleCache = [:]
private Map<ModuleComponentIdentifier, File> componentCache = [:]
private Map<ModuleComponentIdentifier, File> moduleComponentCache = [:]

public PomDependencyModelResolver(Project project) {
this.project = project
Expand All @@ -34,6 +41,26 @@ class PomDependencyModelResolver implements ModelResolver {
ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException {
def id = "$groupId:$artifactId:$version"

if (!moduleCache.containsKey(id)) {
def dep = project.dependencies.create(id)
def cfg = project.configurations.detachedConfiguration([dep].toArray(EMPTY_DEPENDENCIES_ARRAY))

def view = cfg.incoming.artifactView {
withVariantReselection()
lenient = true
attributes {
attribute(DocsType.DOCS_TYPE_ATTRIBUTE, project.objects.named(DocsType, "modules"))
}
}

view.artifacts.each {
def moduleFile = it.file
def module = new FileModelSource(moduleFile)
moduleCache[id] = module
moduleComponentCache[it.variant.owner] = moduleFile
}
}

if (!pomCache.containsKey(id)) {
def mavenArtifacts = project.dependencies.createArtifactResolutionQuery()
.forComponents(DefaultModuleComponentIdentifier.newId(new DefaultModuleVersionIdentifier(groupId, artifactId, version)))
Expand Down Expand Up @@ -80,4 +107,8 @@ class PomDependencyModelResolver implements ModelResolver {
public componentCache() {
return this.componentCache
}

public moduleComponentCache() {
return this.moduleComponentCache
}
}

0 comments on commit b31e0bc

Please sign in to comment.