Skip to content

Commit

Permalink
Merge pull request #52 from nebula-plugins/static-compilation
Browse files Browse the repository at this point in the history
Static compilation
  • Loading branch information
rpalcolea authored Sep 18, 2019
2 parents be69e47 + 3022657 commit 6b090df
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 133 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.4.0'
}

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)
}
26 changes: 13 additions & 13 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,17 +94,17 @@ class InfoBrokerPlugin implements Plugin<Project> {
manifestEntries = filteredManifestEntries
}

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

def add(String key, Object value) {
def entry = new ManifestEntry(key, value)
ManifestEntry add(String key, Object value) {
ManifestEntry entry = new ManifestEntry(key, value)
addEntry(entry)
}

def addReport(String reportName, Object value) {
void addReport(String reportName, Object value) {
if (project != project.rootProject) {
throw new IllegalStateException('Build reports should only be used from the root project')
}
Expand All @@ -118,7 +118,7 @@ class InfoBrokerPlugin implements Plugin<Project> {
}

private ManifestEntry addEntry(ManifestEntry entry) {
def existing = manifestEntries.find { it.name == entry.name }
ManifestEntry existing = manifestEntries.find { it.name == entry.name }
if (existing) {
resolve(existing)
throw new IllegalStateException("A entry with the key ${entry.name} already exists, with the value \"${existing.value}\"")
Expand Down Expand Up @@ -190,14 +190,14 @@ class InfoBrokerPlugin implements Plugin<Project> {
}

String buildManifestString() {
def attrs = buildManifest()
def manifestStr = attrs.collect { "${it.key}: ${it.value}"}.join('\n ')
Map<String, String> attrs = buildManifest()
String manifestStr = attrs.collect { "${it.key}: ${it.value}"}.join('\n ')
return manifestStr
}

String buildString(String indent = '') {
def attrs = buildManifest()
def manifestStr = attrs.collect { "${indent}${it.key}: ${it.value}"}.join('\n')
Map<String, String> attrs = buildManifest()
String manifestStr = attrs.collect { "${indent}${it.key}: ${it.value}"}.join('\n')
return manifestStr
}

Expand All @@ -208,7 +208,7 @@ class InfoBrokerPlugin implements Plugin<Project> {
* @return String based value of entry
*/
String buildEntry(String key) {
def entry = manifestEntries.find { it.name == key }
ManifestEntry entry = manifestEntries.find { it.name == key }
if (!entry) throw new IllegalArgumentException("Unable to find $key")

resolve(entry)
Expand All @@ -217,7 +217,7 @@ class InfoBrokerPlugin implements Plugin<Project> {

def watch(String key, Closure reaction) {
// If we have the key already, we can process it now
def entry = manifestEntries.find { it.name == key }
ManifestEntry entry = manifestEntries.find { it.name == key }
if (entry) {
callWatcher(entry, reaction)
} else {
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,13 @@ 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', DATE_FORMATTER.format(new Date())).changing = true


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

// TODO Include hostname
}
}

}
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 @@ -29,7 +29,7 @@ abstract class AbstractContinuousIntegrationProvider implements ContinuousIntegr
}

protected static String hostname() {
def currentOs = OperatingSystem.current()
OperatingSystem currentOs = OperatingSystem.current()
if (currentOs.isWindows()) {
try {
return Kernel32Util.getComputerName()
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,8 +50,17 @@ 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) }
ContinuousIntegrationInfoProvider provider = providers.find { it.supports(project) }
if (provider) {
return provider
} else {
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 = [:]
Map 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 {
String 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,11 +30,11 @@ 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 {
def entireMap = manifestPlugin.buildNonChangingManifest()
Map<String, String> entireMap = manifestPlugin.buildNonChangingManifest()
jarTask.inputs.properties(entireMap)
}

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,9 +29,9 @@ 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()
Map<String, String> entireMap = manifestPlugin.buildNonChangingManifest()

return entireMap
}
Expand All @@ -40,15 +40,15 @@ class InfoPropertiesFile extends ConventionTask {
File propertiesFile

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

// Gather all values, in contrast to buildNonChangingManifest
def attrs = basePlugin.buildManifest()
Map<String, String> attrs = basePlugin.buildManifest()

logger.info("Writing manifest values to ${getPropertiesFile()}")

def manifestStr = attrs.collect { "${it.key}=${it.value}"}.join('\n')
String manifestStr = attrs.collect { "${it.key}=${it.value}"}.join('\n')
getPropertiesFile().text = manifestStr
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ abstract class AbstractScmProvider implements ScmInfoProvider {
return null
}

def dirToLookIn = starting
File dirToLookIn = starting
while(dirToLookIn) {
def p4configFile = new File(dirToLookIn, filename)
File p4configFile = new File(dirToLookIn, filename)
if (p4configFile.exists()) {
return p4configFile
}
Expand Down
Loading

0 comments on commit 6b090df

Please sign in to comment.