Skip to content

Commit

Permalink
more strong typing for the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rpalcolea committed Sep 18, 2019
1 parent db1f5bb commit 3022657
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 87 deletions.
20 changes: 10 additions & 10 deletions src/main/groovy/nebula/plugin/info/InfoBrokerPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ class InfoBrokerPlugin implements Plugin<Project> {
}

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

ManifestEntry add(String key, Object value) {
def entry = new ManifestEntry(key, 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 @@ -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 @@ -60,7 +60,7 @@ class ContinuousIntegrationInfoPlugin implements Plugin<Project>, InfoCollectorP
}

ContinuousIntegrationInfoProvider findProvider() {
def provider = providers.find { it.supports(project) }
ContinuousIntegrationInfoProvider provider = providers.find { it.supports(project) }
if (provider) {
return provider
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ class DependenciesInfoPlugin implements Plugin<Project>, InfoCollectorPlugin {
void apply(Project project) {
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 { ResolvableDependencies resolvableDependencies ->
if (project.configurations.contains(conf)) {
def resolvedDependencies = resolvableDependencies.resolutionResult.allComponents.findAll {
String resolvedDependencies = resolvableDependencies.resolutionResult.allComponents.findAll {
it.id instanceof ModuleComponentIdentifier
}*.moduleVersion
.sort(true, { m1, m2 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class InfoJarManifestPlugin implements Plugin<Project>, InfoReporterPlugin {
// 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 @@ -31,7 +31,7 @@ class InfoPropertiesFile extends ConventionTask {
Map<String, ?> getManifest() {
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() {
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
22 changes: 10 additions & 12 deletions src/main/groovy/nebula/plugin/info/scm/GitScmProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,35 @@ class GitScmProvider extends AbstractScmProvider {
}

@Override
def calculateModuleOrigin(File projectDir) {
String calculateModuleOrigin(File projectDir) {
Repository repository = getRepository(projectDir)
Config storedConfig = repository.getConfig();
String url = storedConfig.getString('remote', 'origin', 'url');
return url
Config storedConfig = repository.getConfig()
return storedConfig.getString('remote', 'origin', 'url')
}

@Override
def calculateModuleSource(File projectDir) {
String calculateModuleSource(File projectDir) {
Repository repository = getRepository(projectDir)
def gitDir = repository.directory
def relative = projectDir.absolutePath - gitDir.parentFile.absolutePath
return relative
File gitDir = repository.directory
return projectDir.absolutePath - gitDir.parentFile.absolutePath
}

@Override
String calculateChange(File projectDir) {
def hash = System.getenv('GIT_COMMIT') // From Jenkins
if (hash==null) {
String hash = System.getenv('GIT_COMMIT') // From Jenkins
if (!hash) {
def head = getRepository(projectDir).resolve(Constants.HEAD)
if (!head) {
return null
}
hash = head.name
}
def shortHash = hash?.substring(0,7)
String shortHash = hash?.substring(0,7)
return shortHash
}

@Override
def calculateBranch(File projectDir) {
String calculateBranch(File projectDir) {
return getRepository(projectDir).branch
}
}
41 changes: 6 additions & 35 deletions src/main/groovy/nebula/plugin/info/scm/PerforceScmProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,16 @@ class PerforceScmProvider extends AbstractScmProvider {

@Override
String calculateModuleSource(File projectDir) {
def workspace = new File(System.getenv('WORKSPACE'))
File workspace = new File(System.getenv('WORKSPACE'))
return calculateModuleSource(workspace, projectDir)
}

String calculateModuleSource(File workspace, File projectDir) {
// TODO Don't hardcode depot
def relativePath = projectDir.getAbsolutePath() - (workspace.getAbsolutePath() + '/')
String relativePath = projectDir.getAbsolutePath() - (workspace.getAbsolutePath() + '/')
return "//depot/${relativePath}"
// withPerforce(projectDir) { IServer server, IClient client ->
// def relativePath = projectDir.getAbsolutePath() - (workspace.getAbsolutePath() + '/')
// List<IFileSpec> specs = FileSpecBuilder.makeFileSpecList(relativePath)
// List<IFileSpec> mapped = client.where(specs)
// mapped.each { IFileSpec spec ->
// println spec.getClientPathString()
// println spec.getDepotPathString()
// }
// }
}

// def scmBase() {
// // TOOD use p4java instead of command line call
// Process process = "p4 workspace -o".execute(null, project.rootDir)
// def output = process.text // Potential save for later
// def matcher = output =~ /Root:\s+(\S+)\n/
// if(matcher) {
// return matcher[0][1]
// }
//
// return null
// }
// /**
// * macro.xml -> viewFromPath
// */
// def viewFromPath(File baseDir, String depot) {
// // TODO Previously used workspace, which then made a relative path from that location prepended with depot
// // We always knew workspace because if it wasn't provided we could
// baseDir.absoluteFile
// }


@Override
String calculateModuleOrigin(File projectDir) {
Map<String, String> defaults = perforceDefaults(projectDir)
Expand All @@ -87,14 +58,14 @@ class PerforceScmProvider extends AbstractScmProvider {
}

@Override
def calculateBranch(File projectDir) {
String calculateBranch(File projectDir) {
return null // unsupported in perforce
}

@PackageScope
<T> T withPerforce(File projectDir, Closure<T> closure) {
Map<String, String> defaults = perforceDefaults(projectDir)
def uri = getUrl(defaults)
String uri = getUrl(defaults)
IServer server = ServerFactory.getServer(uri, null);
server.connect()
if (defaults.P4PASSWD) {
Expand Down Expand Up @@ -147,7 +118,7 @@ class PerforceScmProvider extends AbstractScmProvider {
// First look for P4CONFIG name
findP4Config(projectDir) // Might be noop
if (p4configFile) {
def props = new Properties()
Properties props = new Properties()
props.load(new FileReader(p4configFile))
defaults = overrideFromMap(defaults, props as Map<String, String>)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ScmInfoPlugin implements Plugin<Project>, InfoCollectorPlugin {
}

ScmInfoProvider findProvider() {
def provider = providers.find { it.supports(project) }
ScmInfoProvider provider = providers.find { it.supports(project) }
if (provider) {
return provider
} else {
Expand Down
27 changes: 12 additions & 15 deletions src/main/groovy/nebula/plugin/info/scm/SvnScmProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package nebula.plugin.info.scm

import org.gradle.api.Project
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions
import org.tmatesoft.svn.core.wc.*

class SvnScmProvider extends AbstractScmProvider {
Expand All @@ -28,33 +29,29 @@ class SvnScmProvider extends AbstractScmProvider {
}

private SVNWCClient getWorkingCopyClient() {
def options = SVNWCUtil.createDefaultOptions(true);
def clientManager = SVNClientManager.newInstance(options);
def client = clientManager.getWCClient()
return client
DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
SVNClientManager clientManager = SVNClientManager.newInstance(options);
return clientManager.getWCClient()
}

private SVNInfo getInfo(File projectDir) {
def info = getWorkingCopyClient().doInfo(projectDir, SVNRevision.WORKING)
return info
return getWorkingCopyClient().doInfo(projectDir, SVNRevision.WORKING)
}

@Override
def calculateModuleOrigin(File projectDir) {
def url = getInfo(projectDir).getURL().toString()
return url
String calculateModuleOrigin(File projectDir) {
return getInfo(projectDir).getURL().toString()
}

@Override
def calculateModuleSource(File projectDir) {
def svnDir = getInfo(projectDir).getWorkingCopyRoot()
def relative = projectDir.absolutePath - svnDir.parentFile.absolutePath
return relative
String calculateModuleSource(File projectDir) {
File svnDir = getInfo(projectDir).getWorkingCopyRoot()
return projectDir.absolutePath - svnDir.parentFile.absolutePath
}

@Override
String calculateChange(File projectDir) {
def revision = System.getenv('SVN_REVISION') // From Jenkins
String revision = System.getenv('SVN_REVISION') // From Jenkins
if (!revision) {
def base = getInfo(projectDir).getRevision()
if (!base) {
Expand All @@ -66,7 +63,7 @@ class SvnScmProvider extends AbstractScmProvider {
}

@Override
def calculateBranch(File projectDir) {
String calculateBranch(File projectDir) {
return null // unsupported in svn
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class UnknownScmProvider extends AbstractScmProvider {
}

@Override
def calculateModuleOrigin(File projectDir) {
String calculateModuleOrigin(File projectDir) {
return LOCAL
}

@Override
def calculateModuleSource(File projectDir) {
String calculateModuleSource(File projectDir) {
return projectDir.absolutePath
}

Expand All @@ -43,7 +43,7 @@ class UnknownScmProvider extends AbstractScmProvider {
}

@Override
def calculateBranch(File projectDir) {
String calculateBranch(File projectDir) {
return null
}
}

0 comments on commit 3022657

Please sign in to comment.