Skip to content

Commit

Permalink
Introduce static compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
rpalcolea committed Sep 18, 2019
1 parent ce9a673 commit 16ddbe7
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 47 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ plugins {
id 'nebula.plugin-plugin' version '12.3.5'
}

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

description 'Gradle plugin collect and provide information about the environment'

contacts {
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)
}
6 changes: 3 additions & 3 deletions src/main/groovy/nebula/plugin/info/InfoBrokerPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class InfoBrokerPlugin implements Plugin<Project> {
project.rootProject.gradle.addBuildListener(new BuildAdapter() {
@Override
void buildFinished(BuildResult buildResult) {
this.buildFinished.set(true)
buildFinished.set(true)
}
})

Expand Down Expand Up @@ -94,12 +94,12 @@ class InfoBrokerPlugin implements Plugin<Project> {
manifestEntries = filteredManifestEntries
}

def add(String key, Closure closure) {
ManifestEntry add(String key, Closure closure) {
def entry = new ManifestEntry(key, closure)
addEntry(entry)
}

def add(String key, Object value) {
ManifestEntry add(String key, Object value) {
def entry = new ManifestEntry(key, value)
addEntry(entry)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import nebula.plugin.info.InfoCollectorPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project

import java.text.SimpleDateFormat

import static java.util.jar.Attributes.Name.*
/**
* Simple provider, for common fields, like build status. Current values:
Expand All @@ -36,6 +38,7 @@ import static java.util.jar.Attributes.Name.*
* </ul>
*/
class BasicInfoPlugin implements Plugin<Project>, InfoCollectorPlugin {
private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat('yyyy-MM-dd_HH:mm:ss')

// Sample from commons-lang, and hence via Maven
// Manifest-Version: 1.0
Expand Down Expand Up @@ -65,11 +68,15 @@ class BasicInfoPlugin implements Plugin<Project>, InfoCollectorPlugin {
manifestPlugin.add('Built-OS', System.getProperty('os.name'))

// Makes list of attributes not idempotent, which can throw off "changed" checks
manifestPlugin.add('Build-Date', new Date().format('yyyy-MM-dd_HH:mm:ss')).changing = true
manifestPlugin.add('Build-Date', formattedBuildDate()).changing = true

manifestPlugin.add('Gradle-Version', { project.gradle.gradleVersion })

// TODO Include hostname
}
}

private static String formattedBuildDate() {
DATE_FORMATTER.format(new Date())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@


package nebula.plugin.info.basic

import nebula.plugin.contacts.BaseContactsPlugin
import nebula.plugin.info.InfoBrokerPlugin
import org.gradle.api.Plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package nebula.plugin.info.ci

import groovy.transform.CompileDynamic
import nebula.plugin.info.InfoBrokerPlugin
import nebula.plugin.info.InfoCollectorPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.internal.ConventionMapping
import org.gradle.api.internal.IConventionAware

class ContinuousIntegrationInfoPlugin implements Plugin<Project>, InfoCollectorPlugin {
Expand All @@ -32,18 +34,14 @@ class ContinuousIntegrationInfoPlugin implements Plugin<Project>, InfoCollectorP
void apply(Project project) {
this.project = project

providers = [new DroneProvider(), new GitlabProvider(), new JenkinsProvider(), new TravisProvider(), new UnknownContinuousIntegrationProvider()]
providers = [new DroneProvider(), new GitlabProvider(), new JenkinsProvider(), new TravisProvider(), new UnknownContinuousIntegrationProvider()] as List<ContinuousIntegrationInfoProvider>
selectedProvider = findProvider()

extension = project.extensions.create('ciinfo', ContinuousIntegrationInfoExtension)

def extMapping = ((IConventionAware) extension).getConventionMapping()
extMapping.host = { selectedProvider.calculateHost(project) }
extMapping.job = { selectedProvider.calculateJob(project) }
extMapping.buildNumber = { selectedProvider.calculateBuildNumber(project) }
extMapping.buildId = { selectedProvider.calculateBuildId(project) }
configureExtMapping()

project.plugins.withType(InfoBrokerPlugin) { manifestPlugin ->
project.plugins.withType(InfoBrokerPlugin) { InfoBrokerPlugin manifestPlugin ->
manifestPlugin.add('Build-Host') { extension.host }
manifestPlugin.add('Build-Job') { extension.job }
manifestPlugin.add('Build-Number') { extension.buildNumber }
Expand All @@ -52,6 +50,15 @@ class ContinuousIntegrationInfoPlugin implements Plugin<Project>, InfoCollectorP

}

@CompileDynamic
private void configureExtMapping() {
ConventionMapping extMapping = ((IConventionAware) extension).getConventionMapping()
extMapping.host = { selectedProvider.calculateHost(project) }
extMapping.job = { selectedProvider.calculateJob(project) }
extMapping.buildNumber = { selectedProvider.calculateBuildNumber(project) }
extMapping.buildId = { selectedProvider.calculateBuildId(project) }
}

ContinuousIntegrationInfoProvider findProvider() {
def provider = providers.find { it.supports(project) }
if (provider) {
Expand Down
5 changes: 2 additions & 3 deletions src/main/groovy/nebula/plugin/info/ci/POSIXUtil.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ package nebula.plugin.info.ci
import com.sun.jna.LastErrorException
import com.sun.jna.Library
import com.sun.jna.Native

class POSIXUtil {
private static final C c = (C) Native.loadLibrary("c", C.class);

private static interface C extends Library {
public int gethostname(byte[] name, int size_t) throws LastErrorException;
int gethostname(byte[] name, int size_t) throws LastErrorException;
}

public static String getHostName() {
static String getHostName() {
byte[] hostname = new byte[256];
c.gethostname(hostname, hostname.length)
return Native.toString(hostname)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,43 @@
*/
package nebula.plugin.info.dependencies

import groovy.transform.CompileDynamic
import nebula.plugin.info.InfoBrokerPlugin
import nebula.plugin.info.InfoCollectorPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ResolvableDependencies
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.VersionInfo
import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.strategy.DefaultVersionComparator
import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.strategy.VersionParser

class DependenciesInfoPlugin implements Plugin<Project>, InfoCollectorPlugin {
def versionComparator = new DefaultVersionComparator()
private final DefaultVersionComparator versionComparator = new DefaultVersionComparator()
private final VersionParser versionParser = new VersionParser()

@Override
void apply(Project project) {
if (!project.rootProject.hasProperty('nebulaInfoDependencies')) {
project.rootProject.ext.nebulaInfoDependencies = [:]
}
setInfoDependencies(project)
def dependencyMap = project.rootProject.property('nebulaInfoDependencies')
def dependencies = [:]
project.plugins.withType(InfoBrokerPlugin) { InfoBrokerPlugin manifestPlugin ->
project.configurations.all( { Configuration conf ->
conf.incoming.afterResolve {
conf.incoming.afterResolve { ResolvableDependencies resolvableDependencies ->
if (project.configurations.contains(conf)) {
def resolvedDependencies = it.resolutionResult.allComponents.findAll {
def resolvedDependencies = resolvableDependencies.resolutionResult.allComponents.findAll {
it.id instanceof ModuleComponentIdentifier
}*.moduleVersion
.sort(true, { m1, m2 ->
if (m1.group != m2.group)
return m1.group <=> m2.group ?: -1
if (m1.name != m2.name)
return m1.name <=> m2.name // name is required
versionComparator.compare(new VersionInfo(m1.version), new VersionInfo(m2.version))
versionComparator.compare(new VersionInfo(versionParser.transform(m1.version)), new VersionInfo(versionParser.transform(m2.version)))
})*.toString().join(',')
if (resolvedDependencies) {
dependencies.put("Resolved-Dependencies-${it.name.capitalize()}", resolvedDependencies)
dependencies.put("Resolved-Dependencies-${resolvableDependencies.name.capitalize()}", resolvedDependencies)
}
}
}
Expand All @@ -61,4 +63,11 @@ class DependenciesInfoPlugin implements Plugin<Project>, InfoCollectorPlugin {
}
}
}

@CompileDynamic
private void setInfoDependencies(Project project) {
if (!project.rootProject.hasProperty('nebulaInfoDependencies')) {
project.rootProject.ext.nebulaInfoDependencies = [:]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ package nebula.plugin.info.reporting

import nebula.plugin.info.InfoBrokerPlugin
import nebula.plugin.info.InfoReporterPlugin
import org.gradle.api.InvalidUserDataException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.bundling.Jar

/**
Expand All @@ -32,7 +30,7 @@ class InfoJarManifestPlugin implements Plugin<Project>, InfoReporterPlugin {

void apply(Project project) {

project.plugins.withType(InfoBrokerPlugin) { manifestPlugin ->
project.plugins.withType(InfoBrokerPlugin) { InfoBrokerPlugin manifestPlugin ->
// Searching the Gradle code base shows that Archive Tasks are the primary consumers of project.version
project.tasks.withType(Jar) { Jar jarTask ->
project.afterEvaluate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import nebula.plugin.info.InfoBrokerPlugin
import nebula.plugin.info.InfoReporterPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.internal.file.copy.CopySpecWrapper
import org.gradle.api.tasks.bundling.Jar

/**
Expand All @@ -30,12 +31,12 @@ class InfoJarPropertiesFilePlugin implements Plugin<Project>, InfoReporterPlugin
void apply(Project project) {

project.plugins.withType(InfoBrokerPlugin) { manifestPlugin ->
def propFilePlugin = project.plugins.apply(InfoPropertiesFilePlugin)
def manifestTask = propFilePlugin.getManifestTask()
InfoPropertiesFilePlugin propFilePlugin = project.plugins.apply(InfoPropertiesFilePlugin) as InfoPropertiesFilePlugin
InfoPropertiesFile manifestTask = propFilePlugin.getManifestTask()

project.tasks.withType(Jar) { Jar jarTask ->
jarTask.from(manifestTask.outputs) {
into "META-INF"
jarTask.from(manifestTask.outputs) { CopySpecWrapper copySpecWrapper ->
copySpecWrapper.into "META-INF"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class InfoPropertiesFile extends ConventionTask {

@Input
Map<String, ?> getManifest() {
InfoBrokerPlugin manifestPlugin = project.plugins.getPlugin(InfoBrokerPlugin)
InfoBrokerPlugin manifestPlugin = project.plugins.getPlugin(InfoBrokerPlugin) as InfoBrokerPlugin

def entireMap = manifestPlugin.buildNonChangingManifest()

Expand All @@ -41,7 +41,7 @@ class InfoPropertiesFile extends ConventionTask {

@TaskAction
def writeOut() {
InfoBrokerPlugin basePlugin = project.plugins.getPlugin(InfoBrokerPlugin)
InfoBrokerPlugin basePlugin = project.plugins.getPlugin(InfoBrokerPlugin) as InfoBrokerPlugin

// Gather all values, in contrast to buildNonChangingManifest
def attrs = basePlugin.buildManifest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class GitScmProvider extends AbstractScmProvider {
def hash = System.getenv('GIT_COMMIT') // From Jenkins
if (hash==null) {
def head = getRepository(projectDir).resolve(Constants.HEAD)
if (head==null) {
if (!head) {
return null
}
hash = head.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,21 @@ class PerforceScmProvider extends AbstractScmProvider {
}

@PackageScope
def Map<String, String> perforceDefaults(File projectDir) {
Map<String, String> perforceDefaults(File projectDir) {
// Set some default values then look for overrides
def defaults = [
Map<String, String> defaults = [
P4CLIENT: null,
P4USER: 'rolem',
P4PASSWD: '',
P4PORT: 'perforce:1666'
]
] as Map<String, String>

// First look for P4CONFIG name
findP4Config(projectDir) // Might be noop
if (p4configFile) {
def props = new Properties()
props.load(new FileReader(p4configFile))
defaults = overrideFromMap(defaults, props)
defaults = overrideFromMap(defaults, props as Map<String, String>)
}

// Second user environment variables
Expand All @@ -159,8 +159,8 @@ class PerforceScmProvider extends AbstractScmProvider {
}

@PackageScope
def Map<String, String> overrideFromMap(Map<String, String> orig, Map<String, String> override) {
def dest = [:]
Map<String, String> overrideFromMap(Map<String, String> orig, Map<String, String> override) {
Map<String, String> dest = [:]
orig.keySet().each { String key ->
dest[key] = override.keySet().contains(key) ? override[key] : orig[key]
}
Expand Down
22 changes: 15 additions & 7 deletions src/main/groovy/nebula/plugin/info/scm/ScmInfoPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package nebula.plugin.info.scm

import groovy.transform.CompileDynamic
import nebula.plugin.info.InfoBrokerPlugin
import nebula.plugin.info.InfoCollectorPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.internal.ConventionMapping
import org.gradle.api.internal.IConventionAware
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
Expand All @@ -37,25 +39,31 @@ class ScmInfoPlugin implements Plugin<Project>, InfoCollectorPlugin {
this.project = project

// TODO Delay findProvider() as long as possible
providers = [new GitScmProvider(), new PerforceScmProvider(), new SvnScmProvider(), new UnknownScmProvider()]
providers = [new GitScmProvider(), new PerforceScmProvider(), new SvnScmProvider(), new UnknownScmProvider()] as List<ScmInfoProvider>
selectedProvider = findProvider()

extension = project.extensions.create('scminfo', ScmInfoExtension)

def extMapping = ((IConventionAware) extension).getConventionMapping()
extMapping.origin = { selectedProvider.calculateOrigin(project) }
extMapping.source = { selectedProvider.calculateSource(project)?.replace(File.separatorChar, '/' as char) }
extMapping.change = { selectedProvider.calculateChange(project) }
extMapping.branch = { selectedProvider.calculateBranch(project) }
configureExtMapping()

project.plugins.withType(InfoBrokerPlugin) { manifestPlugin ->
project.plugins.withType(InfoBrokerPlugin) { InfoBrokerPlugin manifestPlugin ->
manifestPlugin.add('Module-Source') { extension.source }
manifestPlugin.add('Module-Origin') { extension.origin }
manifestPlugin.add('Change') { extension.change }
manifestPlugin.add('Branch') { extension.branch }
}
}

@CompileDynamic
private void configureExtMapping() {
ConventionMapping extMapping = ((IConventionAware) extension).getConventionMapping()
extMapping.origin = { selectedProvider.calculateOrigin(project) }
extMapping.source = { selectedProvider.calculateSource(project)?.replace(File.separatorChar, '/' as char) }
extMapping.change = { selectedProvider.calculateChange(project) }
extMapping.branch = { selectedProvider.calculateBranch(project) }

}

ScmInfoProvider findProvider() {
def provider = providers.find { it.supports(project) }
if (provider) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/groovy/nebula/plugin/info/scm/SvnScmProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class SvnScmProvider extends AbstractScmProvider {
@Override
String calculateChange(File projectDir) {
def revision = System.getenv('SVN_REVISION') // From Jenkins
if (revision==null) {
if (!revision) {
def base = getInfo(projectDir).getRevision()
if (base==null) {
if (!base) {
return null
}
revision = base.getNumber()
Expand Down

0 comments on commit 16ddbe7

Please sign in to comment.