Skip to content

Commit

Permalink
Merge pull request #110 from nebula-plugins/static-compilation
Browse files Browse the repository at this point in the history
introduce static compilation for groovy artifacts
  • Loading branch information
rpalcolea authored Sep 19, 2019
2 parents 0781e15 + 7c44fef commit a295ad0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ contacts {
}
}

compileGroovy.groovyOptions.configurationScript = file('src/groovyCompile/groovycConfig.groovy')

dependencies {
compile 'com.netflix.nebula:nebula-gradle-interop:latest.release'
compile 'org.apache.maven:maven-model-builder:3.+'
Expand Down
6 changes: 6 additions & 0 deletions src/groovyCompile/groovycConfig.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder
import groovy.transform.CompileStatic

CompilerCustomizationBuilder.withConfig(configuration) {
ast(CompileStatic)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package netflix.nebula.dependency.recommender.provider;

import groovy.lang.Closure;

public class CustomRecommendationProvider extends AbstractRecommendationProvider {
private Closure versionFunction;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ class DependencyLockProvider extends FileBasedRecommendationProvider {
String getVersion(String org, String name) throws Exception {
if (!recommendations) {
input.withCloseable {
final locks = new JsonSlurper().parse(it)
final isDependencyLock4Format = locks.every {
final Map<String, Map<String, String>> locks = (Map<String, Map<String, String>>) new JsonSlurper().parse(it)
final boolean isDependencyLock4Format = locks.every {
it.value.every {
it.value instanceof Map
}
}

recommendations = (isDependencyLock4Format ? locks.collectEntries { it.value } : locks).collectEntries {
[(it.key): it.value.locked]
}
[(it.key): it.value["locked"]]
} as Map<String, String>
}
}
recommendations[org + ':' + name]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package netflix.nebula.dependency.recommender.provider

import groovy.transform.CompileDynamic
import org.gradle.api.Project

class IvyRecommendationProvider extends FileBasedRecommendationProvider {
Map<String, String> versionsByCoord

IvyRecommendationProvider(Project p) { super(p) }

@CompileDynamic
@Override
String getVersion(String org, String name) throws Exception {
if (versionsByCoord == null) {
Expand All @@ -23,7 +25,7 @@ class IvyRecommendationProvider extends FileBasedRecommendationProvider {

@SuppressWarnings("unchecked")
@Override
public InputStreamProvider setModule(Object dependencyNotation) {
InputStreamProvider setModule(Object dependencyNotation) {
if (dependencyNotation == null)
throw new IllegalArgumentException("Module may not be null")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 Netflix, Inc.
* Copyright 2016-2019 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,10 +14,11 @@
* limitations under the License.
*/
package netflix.nebula.dependency.recommender.publisher

import groovy.transform.CompileDynamic
import netflix.nebula.dependency.recommender.ModuleNotationParser
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.ModuleVersionIdentifier
import org.gradle.api.artifacts.ResolvedDependency
import org.gradle.api.publish.maven.MavenPublication
Expand All @@ -30,33 +31,34 @@ class MavenBomXmlGenerator {
}

void fromConfigurations(Closure configurationsClosure) {
MavenPublication pub = configurationsClosure.delegate.delegate
MavenPublication pub = getMavenPublication(configurationsClosure)

Iterable<Configuration> configurations

def configurationsRet = configurationsClosure()
if(configurationsRet instanceof Configuration)
configurations = [(Configuration) configurationsRet]
else if(Iterable.class.isAssignableFrom(configurationsRet.class))
configurations = configurationsRet
configurations = configurationsRet as Iterable<Configuration>

generateDependencyManagementXml(pub, { configurations.collect { getManagedDependencies(it) }.flatten() })
}

void withDependencies(Closure dependenciesClosure) {
MavenPublication pub = dependenciesClosure.delegate.delegate
MavenPublication pub = getMavenPublication(dependenciesClosure)

Iterable<String> dependencies = null

def dependenciesRet = dependenciesClosure()
if(dependenciesRet instanceof String)
dependencies = [(String) dependenciesRet]
else if(Iterable.class.isAssignableFrom(dependenciesRet.class))
dependencies = dependenciesRet
dependencies = dependenciesRet as Iterable<String>

generateDependencyManagementXml(pub, { dependencies.collect { ModuleNotationParser.parse(it) } })
}

@CompileDynamic
protected static generateDependencyManagementXml(MavenPublication pub, Closure<Iterable<ModuleVersionIdentifier>> deps) {
pub.pom.withXml {
Node root = it.asNode()
Expand All @@ -78,6 +80,11 @@ class MavenBomXmlGenerator {
}
}

@CompileDynamic
private MavenPublication getMavenPublication(Closure configurationsClosure) {
return configurationsClosure.delegate.delegate
}

protected static Set<ModuleVersionIdentifier> getManagedDependencies(Configuration configuration) {
getManagedDependenciesRecursive(configuration.resolvedConfiguration.firstLevelModuleDependencies,
new HashSet<ModuleVersionIdentifier>())
Expand Down

0 comments on commit a295ad0

Please sign in to comment.