Skip to content

Commit

Permalink
Merge pull request #200 from nebula-plugins/remove-requested-attribut…
Browse files Browse the repository at this point in the history
…e-from-locks

Remove 'requested' field from lock files
  • Loading branch information
OdysseusLives committed Aug 28, 2020
2 parents a8cf0de + f66a338 commit 7b4563c
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package nebula.plugin.dependencylock
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import groovy.transform.TupleConstructor
import nebula.plugin.dependencylock.model.LockKey
import nebula.plugin.dependencylock.model.LockValue
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import static DependencyLockTaskConfigurer.OVERRIDE_FILE

import static DependencyLockTaskConfigurer.GLOBAL_LOCK_CONFIG
import static DependencyLockTaskConfigurer.OVERRIDE_FILE

@TupleConstructor
class DependencyLockReader {
Expand All @@ -22,21 +25,28 @@ class DependencyLockReader {
this.project = project
}

Map readLocks(Configuration conf, File dependenciesLock, Collection<String> updates = []) {
Map readLocks(Configuration conf, File dependenciesLock, Map<LockKey, LockValue> requestedDependencies, Collection<String> updates = []) {
logger.info("Using ${dependenciesLock.name} to lock dependencies in $conf")

if(!dependenciesLock.exists())
if (!dependenciesLock.exists())
return null

Map locks = parseLockFile(dependenciesLock)

if (updates) {
locks = locks.collectEntries { configurationName, deps ->
if (configurationName != conf.name) {
// short-circuit if this is not the relevant configuration
return [(configurationName): []]
}
[(configurationName): deps.findAll { coord, info ->
def notUpdate = !updates.contains(coord)
def isFirstLevel = info?.transitive == null && info?.requested != null
def coordinateSections = coord.split(":")
def lockValue = requestedDependencies.get(new LockKey(group: coordinateSections[0], artifact: coordinateSections[1], configuration: configurationName)) ?: new LockValue()
def requestedAVersion = lockValue.requested != null
def isFirstLevel = info?.transitive == null
def isFirstLevelTransitive = info?.transitive?.any { deps[it]?.project }
notUpdate && (isFirstLevel || isFirstLevelTransitive)
notUpdate && ((isFirstLevel && requestedAVersion) || isFirstLevelTransitive)
}]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ class DependencyLockWriter {
} else {
depMap['project'] = true
}
if (lock.requested) {
depMap['requested'] = lock.requested
}
if (lock.viaOverride) {
depMap['viaOverride'] = lock.viaOverride
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,6 @@ class GenerateLockTask extends AbstractLockTask {
def peers = project.rootProject.allprojects.collect { new LockKey(group: it.group, artifact: it.name) }

confs.each { Configuration configuration ->
// Capture the version of each dependency as requested in the build script for reference.
def externalDependencies = configuration.allDependencies.withType(ExternalDependency)
def filteredExternalDependencies = externalDependencies.findAll { Dependency dependency ->
filter(dependency.group, dependency.name, dependency.version)
}
filteredExternalDependencies.each { ExternalDependency dependency ->
def key = new LockKey(group: dependency.group, artifact: dependency.name, configuration: configuration.name)
deps[key].requested = dependency.version
}

// Lock the version of each dependency specified in the build script as resolved by Gradle.
def resolvedDependencies = configuration.resolvedConfiguration.firstLevelModuleDependencies
def filteredResolvedDependencies = resolvedDependencies.findAll { ResolvedDependency resolved ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import nebula.plugin.dependencylock.utils.CoreLockingHelper
import org.gradle.api.BuildCancelledException
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction

class MigrateLockedDepsToCoreLocksTask extends AbstractMigrateToCoreLocksTask {
Expand Down Expand Up @@ -57,7 +55,7 @@ class MigrateLockedDepsToCoreLocksTask extends AbstractMigrateToCoreLocksTask {

def migrateConfigurationClosure = {
def dependenciesForConf = new ArrayList()
def locks = lockReader.readLocks(it, getInputLockFile())
def locks = lockReader.readLocks(it, getInputLockFile(), new HashMap<>())

if (locks != null) {
for (Map.Entry<String, ArrayList<String>> entry : locks.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package nebula.plugin.dependencylock

import com.netflix.nebula.interop.onResolve
import nebula.plugin.dependencylock.exceptions.DependencyLockException
import nebula.plugin.dependencylock.model.LockKey
import nebula.plugin.dependencylock.model.LockValue
import nebula.plugin.dependencylock.utils.CoreLocking
import nebula.plugin.dependencylock.utils.CoreLockingHelper
import nebula.plugin.dependencyverifier.DependencyResolutionVerifier
Expand All @@ -26,6 +28,7 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.DependencyResolveDetails
import org.gradle.api.artifacts.ExternalDependency
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.util.NameMatcher
Expand Down Expand Up @@ -232,7 +235,16 @@ class DependencyLockPlugin : Plugin<Project> {

private fun applyLock(conf: Configuration, dependenciesLock: File, updates: Set<String> = emptySet()) {
LOGGER.info("Using ${dependenciesLock.name} to lock dependencies in $conf")
val locks = lockReader.readLocks(conf, dependenciesLock, updates)
// Capture the version of each dependency as requested for reference on which use version recommendations vs request a particular version
val deps = mutableMapOf<LockKey, LockValue>().withDefault { LockValue() }
val externalDependencies = conf.allDependencies.withType(ExternalDependency::class.java)
externalDependencies.forEach({ dependency: ExternalDependency ->
val key = LockKey(dependency.group, dependency.name, conf.name)
deps.put(key, LockValue())
deps[key]!!.requested = dependency.version
})

val locks = lockReader.readLocks(conf, dependenciesLock, deps, updates)
if (locks != null) {
// Non-project locks are the top-level dependencies, and possibly transitive thereof, of this project which are
// locked by the lock file. There may also be dependencies on other projects. These are not captured here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,49 +63,43 @@ class DependencyLockLauncherSpec extends IntegrationSpec {
{
"compileClasspath": {
"test.example:foo": {
"locked": "1.0.0",
"requested": "1.+"
"locked": "1.0.0"
}
}
}
'''.stripIndent()

static final String DEPRECATED_LOCK_FORMAT = '''\
{
"test.example:foo": { "locked": "1.0.0", "requested": "1.+" }
"test.example:foo": { "locked": "1.0.0" }
}
'''.stripIndent()

static final String PRE_DIFF_FOO_LOCK = LockGenerator.duplicateIntoConfigsWhenUsingImplementationConfigurationOnly('''\
"test.example:foo": {
"locked": "1.0.0",
"requested": "1.+"
"locked": "1.0.0"
}
'''.stripIndent())

static final String FOO_LOCK = LockGenerator.duplicateIntoConfigsWhenUsingImplementationConfigurationOnly('''\
"test.example:foo": {
"locked": "1.0.1",
"requested": "1.+"
"locked": "1.0.1"
}
'''.stripIndent())

static final String NEW_FOO_LOCK = LockGenerator.duplicateIntoConfigsWhenUsingImplementationConfigurationOnly('''\
"test.example:foo": {
"locked": "2.0.1",
"requested": "1.+",
"viaOverride": "2.0.1"
}
'''.stripIndent())

static final String REMOVE_UPDATE_LOCK = LockGenerator.duplicateIntoConfigsWhenUsingImplementationConfigurationOnly('''\
"test.example:baz": {
"locked": "1.1.0",
"requested": "1.+"
"locked": "1.1.0"
},
"test.example:foo": {
"locked": "1.0.1",
"requested": "1.+"
"locked": "1.0.1"
}
'''.stripIndent())

Expand Down Expand Up @@ -396,8 +390,7 @@ class DependencyLockLauncherSpec extends IntegrationSpec {

def lockWithSkips = LockGenerator.duplicateIntoConfigsWhenUsingImplementationConfigurationOnly('''\
"test.example:baz": {
"locked": "1.1.0",
"requested": "1.+"
"locked": "1.1.0"
}
'''.stripIndent())

Expand Down Expand Up @@ -624,15 +617,13 @@ class DependencyLockLauncherSpec extends IntegrationSpec {
String lockText1 = LockGenerator.duplicateIntoConfigsWhenUsingImplementationConfigurationOnly('''\
"test.example:foo": {
"locked": "2.0.0",
"requested": "2.+",
"viaOverride": "2.0.0"
}
'''.stripIndent())
new File(sub1, 'dependencies.lock').text == lockText1
String lockText2 = LockGenerator.duplicateIntoConfigsWhenUsingImplementationConfigurationOnly('''\
"test.example:baz": {
"locked": "1.0.0",
"requested": "1.+",
"viaOverride": "1.0.0"
}
'''.stripIndent())
Expand Down Expand Up @@ -975,15 +966,13 @@ class DependencyLockLauncherSpec extends IntegrationSpec {
new File(projectDir, 'dependencies.lock').text.replaceAll(' ', '') == lockText
String lockText1 = LockGenerator.duplicateIntoConfigsWhenUsingImplementationConfigurationOnly('''\
"test.example:foo": {
"locked": "2.0.0",
"requested": "2.0.0"
"locked": "2.0.0"
}
'''.stripIndent())
new File(projectDir, 'sub1/dependencies.lock').text == lockText1
String lockText2 = LockGenerator.duplicateIntoConfigsWhenUsingImplementationConfigurationOnly('''\
"test.example:foo": {
"locked": "1.0.1",
"requested": "1.+"
"locked": "1.0.1"
}
'''.stripIndent())
new File(projectDir, 'sub2/dependencies.lock').text == lockText2
Expand All @@ -994,15 +983,13 @@ class DependencyLockLauncherSpec extends IntegrationSpec {
new File(projectDir, 'dependencies.lock').text = '{}'
new File(projectDir, 'sub1/dependencies.lock').text = LockGenerator.duplicateIntoConfigsWhenUsingImplementationConfigurationOnly('''\
"test.example:foo": {
"locked": "2.0.0",
"requested": "2.0.0"
"locked": "2.0.0"
}
'''.stripIndent())

new File(projectDir, 'sub2/dependencies.lock').text = LockGenerator.duplicateIntoConfigsWhenUsingImplementationConfigurationOnly('''\
"test.example:foo": {
"locked": "1.0.0",
"requested": "1.+"
"locked": "1.0.0"
}
'''.stripIndent())

Expand Down Expand Up @@ -1150,12 +1137,10 @@ class DependencyLockLauncherSpec extends IntegrationSpec {
def lockText = LockGenerator.duplicateIntoConfigsWhenUsingImplementationConfigurationOnly(
'''\
"test.example:bar": {
"locked": "1.0.0",
"requested": "1.+"
"locked": "1.0.0"
},
"test.example:qux": {
"locked": "1.0.0",
"requested": "latest.release"
"locked": "1.0.0"
},
"test.example:foo": {
"locked": "1.0.0",
Expand All @@ -1170,8 +1155,7 @@ class DependencyLockLauncherSpec extends IntegrationSpec {
def updatedLock = LockGenerator.duplicateIntoConfigsWhenUsingImplementationConfigurationOnly(
'''\
"test.example:bar": {
"locked": "1.1.0",
"requested": "1.+"
"locked": "1.1.0"
},
"test.example:foo": {
"locked": "1.0.1",
Expand All @@ -1181,8 +1165,7 @@ class DependencyLockLauncherSpec extends IntegrationSpec {
]
},
"test.example:qux": {
"locked": "1.0.0",
"requested": "latest.release"
"locked": "1.0.0"
}'''.stripIndent()
)

Expand Down Expand Up @@ -1221,8 +1204,7 @@ class DependencyLockLauncherSpec extends IntegrationSpec {
"locked": "1.0.0"
},
"test.example:qux": {
"locked": "1.0.0",
"requested": "latest.release"
"locked": "1.0.0"
},
"test.example:foo": {
"locked": "1.0.0",
Expand All @@ -1247,8 +1229,7 @@ class DependencyLockLauncherSpec extends IntegrationSpec {
]
},
"test.example:qux": {
"locked": "2.0.0",
"requested": "latest.release"
"locked": "2.0.0"
}'''.stripIndent()
)

Expand Down Expand Up @@ -1701,8 +1682,7 @@ class DependencyLockLauncherSpec extends IntegrationSpec {
]
},
"test.nebula:foo": {
"locked": "1.0.0",
"requested": "1.+"
"locked": "1.0.0"
}
'''.stripIndent())

Expand Down Expand Up @@ -1837,8 +1817,7 @@ class DependencyLockLauncherSpec extends IntegrationSpec {
def deplock = new File(projectDir, 'dependencies.lock')
deplock.text = LockGenerator.duplicateIntoConfigs('''\
"test.nebula:foo": {
"locked": "1.0.0",
"requested": "1.+"
"locked": "1.0.0"
}
'''.stripIndent())

Expand Down Expand Up @@ -1873,12 +1852,10 @@ class DependencyLockLauncherSpec extends IntegrationSpec {
def deplock = new File(projectDir, 'dependencies.lock')
deplock.text = LockGenerator.duplicateIntoConfigs('''\
"test.nebula:bar": {
"locked": "1.0.0",
"requested": "1.+"
"locked": "1.0.0"
},
"test.nebula:foo": {
"locked": "1.0.0",
"requested": "1.+"
"locked": "1.0.0"
}
'''.stripIndent())

Expand Down

0 comments on commit 7b4563c

Please sign in to comment.