Skip to content

Commit

Permalink
Merge pull request #199 from chali/FixUsingCustomLockNames
Browse files Browse the repository at this point in the history
Remove feature to be able use generated locks instead of saved locks, it was broken and caused unlocking. Make sure lock names are evaluated lazily so changes for custom lock names are reflected
  • Loading branch information
chali authored Aug 25, 2020
2 parents 316e50b + 9656729 commit 150ac9f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ import static nebula.plugin.dependencylock.tasks.GenerateLockTask.lockableConfig
class DependencyLockTaskConfigurer {
private static final Logger LOGGER = Logging.getLogger(DependencyLockTaskConfigurer)

private static final String LOCK_FILE = 'dependencyLock.lockFile'
private static final String USE_GENERATED_LOCK = 'dependencyLock.useGeneratedLock'
private static final String USE_GENERATED_GLOBAL_LOCK = 'dependencyLock.useGeneratedGlobalLock'

public static final String OVERRIDE_FILE = 'dependencyLock.overrideFile'
public static final String GLOBAL_LOCK_CONFIG = '_global_'

Expand All @@ -65,21 +61,14 @@ class DependencyLockTaskConfigurer {
this.project = project
}

String configureTasks(String globalLockFilename, DependencyLockExtension extension, DependencyLockCommitExtension commitExtension, Map overrides) {
String lockFilename = project.hasProperty(LOCK_FILE) ? project[LOCK_FILE] : null
File lockFileInBuildDir = new File(project.buildDir, lockFilename ?: extension.lockFile)
if (project.hasProperty(USE_GENERATED_LOCK)) {
lockFilename = lockFileInBuildDir.path
}
File lockFileInProjectDir = new File(project.projectDir, lockFilename ?: extension.lockFile)

String configureTasks(String globalLockFilename, String lockFilename, DependencyLockExtension extension, DependencyLockCommitExtension commitExtension, Map overrides) {
TaskProvider<GenerateLockTask> genLockTask = project.tasks.register(GENERATE_LOCK_TASK_NAME, GenerateLockTask)
configureGenerateLockTask(genLockTask, lockFileInBuildDir, extension, overrides)
configureGenerateLockTask(genLockTask, lockFilename, extension, overrides)

TaskProvider<UpdateLockTask> updateLockTask = project.tasks.register(UPDATE_LOCK_TASK_NAME, UpdateLockTask)
configureGenerateLockTask(updateLockTask, lockFileInBuildDir, extension, overrides)
configureGenerateLockTask(updateLockTask, lockFilename, extension, overrides)

TaskProvider<SaveLockTask> saveTask = configureSaveTask(lockFileInBuildDir, lockFileInProjectDir, genLockTask, updateLockTask, extension)
TaskProvider<SaveLockTask> saveTask = configureSaveTask(lockFilename, genLockTask, updateLockTask, extension)
createDeleteLock(saveTask)

configureMigrateToCoreLocksTask(extension)
Expand All @@ -91,19 +80,13 @@ class DependencyLockTaskConfigurer {
TaskProvider<GenerateLockTask> globalLockTask
TaskProvider<UpdateLockTask> globalUpdateLock
if (project == project.rootProject) {
if (project.hasProperty(USE_GENERATED_GLOBAL_LOCK)) {
globalLockFilename = lockFileInBuildDir.path
}
File globalLockFileInBuildDir = new File(project.buildDir, globalLockFilename ?: extension.globalLockFile)
File globalLockFileInProjectDir = new File(project.projectDir, globalLockFilename ?: extension.globalLockFile)

globalLockTask = project.tasks.register(GENERATE_GLOBAL_LOCK_TASK_NAME, GenerateLockTask)
configureGlobalLockTask(globalLockTask, globalLockFileInBuildDir, extension, overrides)
configureGlobalLockTask(globalLockTask, globalLockFilename, extension, overrides)

globalUpdateLock = project.tasks.register(UPDATE_GLOBAL_LOCK_TASK_NAME, UpdateLockTask)
configureGlobalLockTask(globalUpdateLock, globalLockFileInBuildDir, extension, overrides)
configureGlobalLockTask(globalUpdateLock, globalLockFilename, extension, overrides)

globalSave = configureGlobalSaveTask(globalLockFileInBuildDir, globalLockFileInProjectDir, globalLockTask, globalUpdateLock, extension)
globalSave = configureGlobalSaveTask(globalLockFilename, globalLockTask, globalUpdateLock, extension)

createDeleteGlobalLock(globalSave)
}
Expand All @@ -113,6 +96,22 @@ class DependencyLockTaskConfigurer {
lockFilename
}

private File getProjectDirLockFile(String lockFilename, DependencyLockExtension extension) {
new File(project.projectDir, lockFilename ?: extension.lockFile)
}

private File getBuildDirLockFile(String lockFilename, DependencyLockExtension extension) {
new File(project.buildDir, lockFilename ?: extension.lockFile)
}

private File getProjectDirGlobalLockFile(String lockFilename, DependencyLockExtension extension) {
new File(project.projectDir, lockFilename ?: extension.globalLockFile)
}

private File getBuildDirGlobalLockFile(String lockFilename, DependencyLockExtension extension) {
new File(project.buildDir, lockFilename ?: extension.globalLockFile)
}

private void configureCommitTask(String clLockFileName, String globalLockFileName, TaskProvider<SaveLockTask> saveTask, DependencyLockExtension lockExtension,
DependencyLockCommitExtension commitExtension, TaskProvider<SaveLockTask> globalSaveTask = null) {
project.plugins.withType(ScmPlugin) {
Expand Down Expand Up @@ -171,7 +170,7 @@ class DependencyLockTaskConfigurer {
}
}

private TaskProvider<SaveLockTask> configureSaveTask(File lockfileInBuildDir, File lockfileInProjectDir, TaskProvider<GenerateLockTask> lockTask,
private TaskProvider<SaveLockTask> configureSaveTask(String lockFilename, TaskProvider<GenerateLockTask> lockTask,
TaskProvider<UpdateLockTask> updateTask, DependencyLockExtension extension) {
TaskProvider<SaveLockTask> saveLockTask = project.tasks.register(SAVE_LOCK_TASK_NAME, SaveLockTask)

Expand All @@ -183,8 +182,8 @@ class DependencyLockTaskConfigurer {
}
}
saveTask.conventionMapping.with {
generatedLock = { lockfileInBuildDir }
outputLock = { lockfileInProjectDir }
generatedLock = { getBuildDirLockFile(lockFilename, extension) }
outputLock = { getProjectDirLockFile(lockFilename, extension) }
}
}
configureCommonSaveTask(saveLockTask, lockTask, updateTask)
Expand All @@ -206,7 +205,7 @@ class DependencyLockTaskConfigurer {
}
}

private TaskProvider<SaveLockTask> configureGlobalSaveTask(File globalLockFileInBuildDir, File globalLockFileInProjectDir, TaskProvider<GenerateLockTask> globalLockTask,
private TaskProvider<SaveLockTask> configureGlobalSaveTask(String lockFilename, TaskProvider<GenerateLockTask> globalLockTask,
TaskProvider<UpdateLockTask> globalUpdateLockTask, DependencyLockExtension extension) {
TaskProvider<SaveLockTask> globalSaveLockTask = project.tasks.register(SAVE_GLOBAL_LOCK_TASK_NAME, SaveLockTask)

Expand All @@ -220,20 +219,20 @@ class DependencyLockTaskConfigurer {
}
}
globalSaveTask.conventionMapping.with {
generatedLock = { globalLockFileInBuildDir }
outputLock = { globalLockFileInProjectDir }
generatedLock = { getBuildDirGlobalLockFile(lockFilename, extension) }
outputLock = { getProjectDirGlobalLockFile(lockFilename, extension) }
}
}
configureCommonSaveTask(globalSaveLockTask, globalLockTask, globalUpdateLockTask)

globalSaveLockTask
}

private TaskProvider<GenerateLockTask> configureGenerateLockTask(TaskProvider<GenerateLockTask> lockTask, File dependenciesLockFile, DependencyLockExtension extension, Map overrides) {
private TaskProvider<GenerateLockTask> configureGenerateLockTask(TaskProvider<GenerateLockTask> lockTask, String lockFilename, DependencyLockExtension extension, Map overrides) {
setupLockConventionMapping(lockTask, extension, overrides)
lockTask.configure {
it.conventionMapping.with {
dependenciesLock = { dependenciesLockFile }
dependenciesLock = { getBuildDirLockFile(lockFilename, extension) }
configurationNames = { extension.configurationNames }
skippedConfigurationNames = { extension.skippedConfigurationNamesPrefixes }
}
Expand All @@ -255,15 +254,15 @@ class DependencyLockTaskConfigurer {
}
}

private TaskProvider<GenerateLockTask> configureGlobalLockTask(TaskProvider<GenerateLockTask> globalLockTask, File globalLockFileInBuildDir,
private TaskProvider<GenerateLockTask> configureGlobalLockTask(TaskProvider<GenerateLockTask> globalLockTask, String lockFilename,
DependencyLockExtension extension, Map overrides) {
setupLockConventionMapping(globalLockTask, extension, overrides)
globalLockTask.configure { globalGenerateTask ->
globalGenerateTask.doFirst {
project.subprojects.each { sub -> sub.repositories.each { repo -> project.repositories.add(repo) } }
}
globalGenerateTask.conventionMapping.with {
dependenciesLock = { globalLockFileInBuildDir }
dependenciesLock = { getBuildDirGlobalLockFile(lockFilename, extension) }
configurations = {
def subprojects = project.subprojects.collect { subproject ->
def ext = subproject.getExtensions().findByType(DependencyLockExtension)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class DependencyLockPlugin : Plugin<Project> {
const val EXTENSION_NAME = "dependencyLock"
const val COMMIT_EXTENSION_NAME = "commitDependencyLock"
const val DEPENDENCY_RESOLTION_VERIFIER_EXTENSION = "dependencyResolutionVerifierExtension"
const val LOCK_FILE = "dependencyLock.lockFile"
const val GLOBAL_LOCK_FILE = "dependencyLock.globalLockFile"
const val LOCK_AFTER_EVALUATING = "dependencyLock.lockAfterEvaluating"
const val UPDATE_DEPENDENCIES = "dependencyLock.updateDependencies"
Expand Down Expand Up @@ -85,7 +86,8 @@ class DependencyLockPlugin : Plugin<Project> {
}
val overrides = lockReader.readOverrides()
val globalLockFilename = project.findStringProperty(GLOBAL_LOCK_FILE)
val lockFilename = DependencyLockTaskConfigurer(project).configureTasks(globalLockFilename, extension, commitExtension, overrides)
val lockFilename = project.findStringProperty(LOCK_FILE)
DependencyLockTaskConfigurer(project).configureTasks(globalLockFilename, lockFilename, extension, commitExtension, overrides)
if (CoreLocking.isCoreLockingEnabled()) {
LOGGER.warn("${project.name}: coreLockingSupport feature enabled")
val coreLockingHelper = CoreLockingHelper(project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,22 @@ class DependencyLockLauncherSpec extends IntegrationSpec {
!result.standardOutput.contains('test.example:foo:2.0.0 -> 1.0.0')
}

def 'lock file name can be customized'() {
buildFile << BUILD_GRADLE
buildFile << """
dependencyLock {
lockFile = "custom.lock"
}
"""

when:
def result = runTasksSuccessfully('generateLock', 'saveLock')

then:
def dependenciesLock = new File(projectDir, 'custom.lock')
dependenciesLock.exists()
}

def 'override lock file is applied'() {
def dependenciesLock = new File(projectDir, 'dependencies.lock')
dependenciesLock << OLD_FOO_LOCK
Expand Down Expand Up @@ -420,78 +436,6 @@ class DependencyLockLauncherSpec extends IntegrationSpec {
new File(projectDir, 'build/dependency-lock/lockdiff.txt').text == MY_DIFF
}

def 'run with generated lock'() {
def dependenciesLock = new File(projectDir, 'dependencies.lock')
dependenciesLock << OLD_FOO_LOCK
buildFile << BUILD_GRADLE

when:
runTasksSuccessfully('generateLock')

then:
new File(projectDir, 'build/dependencies.lock').text == FOO_LOCK

when:
def result0 = runTasksSuccessfully('dependencies')

then:
result0.standardOutput.contains 'test.example:foo:1.+ -> 1.0.0'

when:
def result1 = runTasksSuccessfully('-PdependencyLock.useGeneratedLock=true', 'dependencies')

then:
result1.standardOutput.contains 'test.example:foo:1.+ -> 1.0.1'
}

def 'run with generated global lock'() {
def globalLock = new File(projectDir, 'global.lock')
globalLock << OLD_FOO_LOCK
buildFile << BUILD_GRADLE

when:
runTasksSuccessfully('generateGlobalLock')

then:
new File(projectDir, 'build/global.lock').text == FOO_LOCK

when:
def result0 = runTasksSuccessfully('dependencies')

then:
result0.standardOutput.contains 'test.example:foo:1.+ -> 1.0.0'

when:
def result1 = runTasksSuccessfully('-PdependencyLock.useGeneratedGlobalLock=true', 'dependencies')

then:
result1.standardOutput.contains 'test.example:foo:1.+ -> 1.0.1'
}

def 'generateLock with deprecated format existing causes no issues'() {
def dependenciesLock = new File(projectDir, 'dependencies.lock')
dependenciesLock << DEPRECATED_LOCK_FORMAT
buildFile << BUILD_GRADLE

when:
runTasksSuccessfully('generateLock')

then:
new File(projectDir, 'build/dependencies.lock').text == FOO_LOCK

when:
def result0 = runTasksSuccessfully('dependencies')

then:
result0.standardOutput.contains 'test.example:foo:1.+ -> 1.0.0'

when:
def result1 = runTasksSuccessfully('-PdependencyLock.useGeneratedLock=true', 'dependencies')

then:
result1.standardOutput.contains 'test.example:foo:1.+ -> 1.0.1'
}

def 'generateLock fails if dependency locks are ignored'() {
buildFile << BUILD_GRADLE

Expand Down

0 comments on commit 150ac9f

Please sign in to comment.