Skip to content

Commit

Permalink
Merge pull request #73 from nebula-plugins/revert-72-remove-forUseAtC…
Browse files Browse the repository at this point in the history
…onfigurationTime

Revert "Remove usage of Provider..forUseAtConfigurationTime() to make it compatible with gradle 6.x"
  • Loading branch information
rpalcolea authored Jul 8, 2020
2 parents ba7bd18 + 7cbb094 commit ffd85d6
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 18 deletions.
13 changes: 11 additions & 2 deletions src/main/groovy/nebula/plugin/info/basic/BasicInfoPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ class BasicInfoPlugin implements Plugin<Project>, InfoCollectorPlugin {
// Implementation-Vendor-Id: org.apache
// X-Compile-Source-JDK: 1.3
// X-Compile-Target-JDK: 1.2
private final ProviderFactory providers

@Inject
BasicInfoPlugin(ProviderFactory providerFactory) {
this.providers = providerFactory
}

void apply(Project project) {

// All fields are known upfront, so we pump these in immediately.
Expand All @@ -66,8 +73,10 @@ class BasicInfoPlugin implements Plugin<Project>, InfoCollectorPlugin {
manifestPlugin.add(IMPLEMENTATION_VERSION.toString()) { project.version }
manifestPlugin.add('Built-Status') { project.status } // Could be promoted, so this is the actual status necessarily

manifestPlugin.add('Built-By', System.getProperty('user.name'))
manifestPlugin.add('Built-OS', System.getProperty('os.name'))
String builtBy = providers.systemProperty("user.name").forUseAtConfigurationTime().get()
String builtOs = providers.systemProperty("os.name").forUseAtConfigurationTime().get()
manifestPlugin.add('Built-By', builtBy)
manifestPlugin.add('Built-OS', builtOs)

// Makes list of attributes not idempotent, which can throw off "changed" checks
manifestPlugin.add('Build-Date', DATE_FORMATTER.format(new Date())).changing = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ import java.util.logging.Level
@Log
abstract class AbstractContinuousIntegrationProvider implements ContinuousIntegrationInfoProvider {

private final ProviderFactory providerFactory

AbstractContinuousIntegrationProvider(ProviderFactory providerFactory) {
this.providerFactory = providerFactory
}

protected String getEnvironmentVariable(String envKey) {
return System.getenv(envKey)
return providerFactory.environmentVariable(envKey).forUseAtConfigurationTime().present ? providerFactory.environmentVariable(envKey).forUseAtConfigurationTime().get() : null
}

protected static String hostname() {
Expand Down
3 changes: 3 additions & 0 deletions src/main/groovy/nebula/plugin/info/ci/CircleCIProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import org.gradle.api.Project
import org.gradle.api.provider.ProviderFactory

class CircleCIProvider extends AbstractContinuousIntegrationProvider {
CircleCIProvider(ProviderFactory providerFactory) {
super(providerFactory)
}

@Override
boolean supports(Project project) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/groovy/nebula/plugin/info/ci/CirrusCIProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import org.gradle.api.Project
import org.gradle.api.provider.ProviderFactory

class CirrusCIProvider extends AbstractContinuousIntegrationProvider {
CirrusCIProvider(ProviderFactory providerFactory) {
super(providerFactory)
}

@Override
boolean supports(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ class ContinuousIntegrationInfoPlugin implements Plugin<Project>, InfoCollectorP
ContinuousIntegrationInfoProvider selectedProvider
ContinuousIntegrationInfoExtension extension

private final ProviderFactory providerFactory

@Inject
ContinuousIntegrationInfoPlugin(ProviderFactory providerFactory) {
this.providerFactory = providerFactory
}

@Override
void apply(Project project) {
this.project = project

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

extension = project.extensions.create('ciinfo', ContinuousIntegrationInfoExtension)
Expand Down
4 changes: 4 additions & 0 deletions src/main/groovy/nebula/plugin/info/ci/DroneProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import org.gradle.api.provider.ProviderFactory

class DroneProvider extends AbstractContinuousIntegrationProvider {

DroneProvider(ProviderFactory providerFactory) {
super(providerFactory)
}

@Override
boolean supports(Project project) {
getEnvironmentVariable('DRONE')
Expand Down
4 changes: 4 additions & 0 deletions src/main/groovy/nebula/plugin/info/ci/GitlabProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import org.gradle.api.provider.ProviderFactory

class GitlabProvider extends AbstractContinuousIntegrationProvider {

GitlabProvider(ProviderFactory providerFactory) {
super(providerFactory)
}

@Override
boolean supports(Project project) {
getEnvironmentVariable('GITLAB_CI')
Expand Down
4 changes: 4 additions & 0 deletions src/main/groovy/nebula/plugin/info/ci/JenkinsProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import org.gradle.api.provider.ProviderFactory

class JenkinsProvider extends AbstractContinuousIntegrationProvider {

JenkinsProvider(ProviderFactory providerFactory) {
super(providerFactory)
}

@Override
boolean supports(Project project) {
getEnvironmentVariable('BUILD_NUMBER') && getEnvironmentVariable('JOB_NAME')
Expand Down
3 changes: 3 additions & 0 deletions src/main/groovy/nebula/plugin/info/ci/TravisProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import org.gradle.api.Project
import org.gradle.api.provider.ProviderFactory

class TravisProvider extends AbstractContinuousIntegrationProvider {
TravisProvider(ProviderFactory providerFactory) {
super(providerFactory)
}

@Override
boolean supports(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import org.gradle.api.provider.ProviderFactory

class UnknownContinuousIntegrationProvider extends AbstractContinuousIntegrationProvider {
public static final String LOCAL = 'LOCAL'

UnknownContinuousIntegrationProvider(ProviderFactory providerFactory) {
super(providerFactory)
}

@Override
boolean supports(Project project) {
return true
Expand Down
15 changes: 13 additions & 2 deletions src/main/groovy/nebula/plugin/info/java/InfoJavaPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,22 @@ class InfoJavaPlugin implements Plugin<Project>, InfoCollectorPlugin {
static final String SOURCE_PROPERTY = 'X-Compile-Source-JDK'
static final String TARGET_PROPERTY = 'X-Compile-Target-JDK'

private final ProviderFactory providers

@Inject
InfoJavaPlugin(ProviderFactory providerFactory) {
this.providers = providerFactory
}

void apply(Project project) {
// This can't change, so we can commit it early
project.plugins.withType(InfoBrokerPlugin) { InfoBrokerPlugin manifestPlugin ->
manifestPlugin.add(CREATED_PROPERTY, "${System.getProperty('java.runtime.version')} (${System.getProperty('java.vm.vendor')})")
manifestPlugin.add(JDK_PROPERTY, System.getProperty('java.version'))
String javaRuntimeVersion = providers.systemProperty("java.runtime.version").forUseAtConfigurationTime().get()
String javaVmVendor = providers.systemProperty("java.vm.vendor").forUseAtConfigurationTime().get()
String javaVersion = providers.systemProperty("java.version").forUseAtConfigurationTime().get()

manifestPlugin.add(CREATED_PROPERTY, "$javaRuntimeVersion ($javaVmVendor)")
manifestPlugin.add(JDK_PROPERTY, javaVersion)
}

// After-evaluating, because we need to give user a chance to effect the extension
Expand Down
10 changes: 10 additions & 0 deletions src/main/groovy/nebula/plugin/info/scm/AbstractScmProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ import org.gradle.api.provider.ProviderFactory
abstract class AbstractScmProvider implements ScmInfoProvider {
abstract calculateModuleSource(File projectDir)

private final ProviderFactory providerFactory

AbstractScmProvider(ProviderFactory providerFactory) {
this.providerFactory = providerFactory
}

protected ProviderFactory getProviderFactory() {
return this.providerFactory
}

@Override
String calculateSource(Project project) {
return calculateModuleSource(project.projectDir)
Expand Down
12 changes: 10 additions & 2 deletions src/main/groovy/nebula/plugin/info/scm/GitScmProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import org.slf4j.LoggerFactory

class GitScmProvider extends AbstractScmProvider {
private Logger logger = LoggerFactory.getLogger(GitScmProvider)

GitScmProvider(ProviderFactory providerFactory) {
super(providerFactory)
}

@Override
boolean supports(Project project) {
// TODO When we can make p4java optional, we'll add a classForName check here.
Expand Down Expand Up @@ -62,13 +67,16 @@ class GitScmProvider extends AbstractScmProvider {

@Override
String calculateFullChange(File projectDir) {
String hash = System.getenv('GIT_COMMIT') // From Jenkins
if (!hash) {
boolean isHashPresent = providerFactory.environmentVariable('GIT_COMMIT').forUseAtConfigurationTime().present
String hash
if (!isHashPresent) {
def head = getRepository(projectDir).resolve(Constants.HEAD)
if (!head) {
return null
}
hash = head.name
} else {
hash = providerFactory.environmentVariable('GIT_COMMIT').forUseAtConfigurationTime().get()
}
return hash
}
Expand Down
15 changes: 10 additions & 5 deletions src/main/groovy/nebula/plugin/info/scm/PerforceScmProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,37 @@ import com.perforce.p4java.server.IServer
import com.perforce.p4java.server.ServerFactory
import groovy.transform.PackageScope
import org.gradle.api.Project
import org.gradle.api.provider.ProviderFactory
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class PerforceScmProvider extends AbstractScmProvider {


File p4configFile

private Logger logger = LoggerFactory.getLogger(PerforceScmProvider)

private static final String DEFAULT_WORKSPACE = '.'

PerforceScmProvider(ProviderFactory providerFactory) {
super(providerFactory)
}

@Override
boolean supports(Project project) {
// Pretty poor way to check, but Perforce leave no indication of where the current tree came from
// Better to check git first, since it can make a more intelligent guess
// TODO When we can make p4java optional, we'll add a classForName check here.
try {
return (System.getenv('WORKSPACE') && System.getenv('P4CLIENT')) || findFile(project.projectDir, System.getenv('P4CONFIG')) } catch(Exception e) {
return (providerFactory.environmentVariable('WORKSPACE').forUseAtConfigurationTime().present && providerFactory.environmentVariable('P4CLIENT').forUseAtConfigurationTime().present) || findFile(project.projectDir, providerFactory.environmentVariable('P4CONFIG').forUseAtConfigurationTime().get())
} catch(Exception e) {
return false
}
}

@Override
String calculateModuleSource(File projectDir) {
String workspacePath = System.getenv('WORKSPACE') ?: {
String workspacePath = providerFactory.environmentVariable('WORKSPACE').forUseAtConfigurationTime().present ? providerFactory.environmentVariable('WORKSPACE').forUseAtConfigurationTime().get() : {
logger.info("WORKSPACE environment variable is not set. Using ${DEFAULT_WORKSPACE}")
DEFAULT_WORKSPACE
}.call()
Expand All @@ -68,7 +73,7 @@ class PerforceScmProvider extends AbstractScmProvider {

@Override
String calculateChange(File projectDir) {
return System.getenv('P4_CHANGELIST')
return providerFactory.environmentVariable('P4_CHANGELIST').forUseAtConfigurationTime().get()
}

@Override
Expand Down Expand Up @@ -160,7 +165,7 @@ class PerforceScmProvider extends AbstractScmProvider {
@PackageScope
void findP4Config(File starting) {
if (p4configFile == null) {
p4configFile = findFile(starting, System.getenv('P4CONFIG'))
p4configFile = findFile(starting, providerFactory.environmentVariable('P4CONFIG').forUseAtConfigurationTime().get())
}
}
}
9 changes: 8 additions & 1 deletion src/main/groovy/nebula/plugin/info/scm/ScmInfoPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@ class ScmInfoPlugin implements Plugin<Project>, InfoCollectorPlugin {
ScmInfoProvider selectedProvider
ScmInfoExtension extension

private final ProviderFactory providerFactory

@Inject
ScmInfoPlugin(ProviderFactory providerFactory) {
this.providerFactory = providerFactory
}

@Override
void apply(Project project) {
this.project = project

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

extension = project.extensions.create('scminfo', ScmInfoExtension)
Expand Down
12 changes: 9 additions & 3 deletions src/main/groovy/nebula/plugin/info/scm/SvnScmProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import org.tmatesoft.svn.core.wc.*

class SvnScmProvider extends AbstractScmProvider {

SvnScmProvider(ProviderFactory providerFactory) {
super(providerFactory)
}

@Override
boolean supports(Project project) {
// TODO When we can make p4java optional, we'll add a classForName check here.
Expand Down Expand Up @@ -52,18 +56,20 @@ class SvnScmProvider extends AbstractScmProvider {

@Override
String calculateChange(File projectDir) {
String revision = System.getenv('SVN_REVISION') // From Jenkins
if (!revision) {
boolean isRevisionPresent = providerFactory.environmentVariable('SVN_REVISION').forUseAtConfigurationTime().present
String revision
if (!isRevisionPresent) {
def base = getInfo(projectDir).getRevision()
if (!base) {
return null
}
revision = base.getNumber()
} else {
revision = providerFactory.environmentVariable('SVN_REVISION').forUseAtConfigurationTime().get()
}
return revision
}


@Override
def calculateFullChange(File projectDir) {
calculateChange(projectDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class UnknownScmProvider extends AbstractScmProvider {

public static final String LOCAL = 'LOCAL'

UnknownScmProvider(ProviderFactory providerFactory) {
super(providerFactory)
}

@Override
boolean supports(Project project) {
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package nebula.plugin.info.scm

import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.junit.Rule
import org.junit.contrib.java.lang.system.EnvironmentVariables
import org.junit.rules.TemporaryFolder
Expand All @@ -24,7 +26,10 @@ class PerforceScmProviderSpec extends Specification {
@Rule
TemporaryFolder temp

def provider = new PerforceScmProvider()
ProviderFactory providerFactoryMock = Mock(ProviderFactory)
Provider<String> providerStringMock = Mock(Provider)

def provider = new PerforceScmProvider(providerFactoryMock)

@Rule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables()
Expand Down Expand Up @@ -107,11 +112,20 @@ class PerforceScmProviderSpec extends Specification {
then:
mapped == '//depot/Tools/nebula-boot'

2 * providerFactoryMock.environmentVariable('WORKSPACE') >> providerStringMock
2 * providerStringMock.forUseAtConfigurationTime() >> providerStringMock
1 * providerStringMock.present >> true
1 * providerStringMock.get() >> workspace.path

when:
String origin = provider.calculateModuleOrigin(fakeProjectDir)

then:
origin == 'p4java://perforce:1666?userName=rolem'

1 * providerFactoryMock.environmentVariable('P4CONFIG') >> providerStringMock
1 * providerStringMock.forUseAtConfigurationTime() >> providerStringMock
1 * providerStringMock.get() >> fakeProjectDir.path
}

def 'calculate module status - WORKSPACE is null'() {
Expand All @@ -127,10 +141,19 @@ class PerforceScmProviderSpec extends Specification {
then:
mapped == '//depot//Users/jryan/Workspaces/jryan_uber/Tools/nebula-boot'

1 * providerFactoryMock.environmentVariable('WORKSPACE') >> providerStringMock
1 * providerStringMock.forUseAtConfigurationTime() >> providerStringMock
1 * providerStringMock.present >> false
0 * providerStringMock.get()

when:
String origin = provider.calculateModuleOrigin(fakeProjectDir)

then:
origin == 'p4java://perforce:1666?userName=rolem'

1 * providerFactoryMock.environmentVariable('P4CONFIG') >> providerStringMock
1 * providerStringMock.forUseAtConfigurationTime() >> providerStringMock
1 * providerStringMock.get() >> fakeProjectDir.path
}
}

0 comments on commit ffd85d6

Please sign in to comment.