Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DiffLockTask: use lazy apis #254

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import nebula.plugin.dependencylock.tasks.MigrateToCoreLocksTask
import nebula.plugin.dependencylock.tasks.SaveLockTask
import nebula.plugin.dependencylock.tasks.UpdateLockTask
import nebula.plugin.dependencylock.utils.ConfigurationUtils
import nebula.plugin.dependencylock.utils.DependencyLockingFeatureFlags
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.UnknownTaskException
Expand Down Expand Up @@ -327,9 +328,14 @@ class DependencyLockTaskConfigurer {
diffTask.mustRunAfter(project.tasks.named(GENERATE_LOCK_TASK_NAME), project.tasks.named(UPDATE_LOCK_TASK_NAME))
def existing = new File(project.projectDir, lockFileName ?: extension.lockFile.get())
if (existing.exists()) {
diffTask.existingLockFile = existing
diffTask.existingLockFile.set(existing)
}
diffTask.updatedLockFile = new File(project.layout.buildDirectory.getAsFile().get(), lockFileName ?: extension.lockFile.get())
diffTask.updatedLockFile.set(new File(project.layout.buildDirectory.getAsFile().get(), lockFileName ?: extension.lockFile.get()))
File dependencyLockFolder = new File(project.layout.buildDirectory.getAsFile().get(), "dependency-lock")
dependencyLockFolder.mkdirs()
File diffFile = new File(dependencyLockFolder, "lockdiff.${this.diffFileExtension()}")
diffTask.outputFile.set(diffFile)
diffTask.isPathAwareDependencyDiffEnabled.set(DependencyLockingFeatureFlags.isPathAwareDependencyDiffEnabled())
}

project.tasks.named(SAVE_LOCK_TASK_NAME).configure { save ->
Expand All @@ -339,4 +345,8 @@ class DependencyLockTaskConfigurer {
diffLockTask
}

private String diffFileExtension() {
DependencyLockingFeatureFlags.isPathAwareDependencyDiffEnabled() ? "json" : "txt"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import groovy.json.JsonSlurper
import nebula.dependencies.comparison.*
import nebula.plugin.dependencylock.diff.DiffReportGenerator
import nebula.plugin.dependencylock.utils.DependencyLockingFeatureFlags
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional
Expand All @@ -18,52 +22,51 @@ import org.gradle.work.DisableCachingByDefault
import java.nio.charset.StandardCharsets

@DisableCachingByDefault
class DiffLockTask extends AbstractLockTask {
abstract class DiffLockTask extends AbstractLockTask {
@Internal
String description = 'Diff existing lock and generated lock file'

@InputFile
@Optional
@PathSensitive(PathSensitivity.NONE)
File existingLockFile
abstract Property<File> getExistingLockFile()

@Internal
File updatedLockFile

@OutputDirectory
File outputDir = new File(project.layout.buildDirectory.getAsFile().get(), "dependency-lock")
abstract Property<File> getUpdatedLockFile()

@OutputFile
File diffFile = new File(outputDir, "lockdiff.${this.diffFileExtension()}")
abstract Property<File> getOutputFile()

private String diffFileExtension() {
DependencyLockingFeatureFlags.isPathAwareDependencyDiffEnabled() ? "json" : "txt"
}
@Input
abstract Property<Boolean> getIsPathAwareDependencyDiffEnabled()

@TaskAction
def diffLocks() {
ConfigurationsSet existingLock = readLocks(existingLockFile)
ConfigurationsSet newLock = readLocks(updatedLockFile)
if (DependencyLockingFeatureFlags.isPathAwareDependencyDiffEnabled()) {
File diffFile = outputFile.get()
if (isPathAwareDependencyDiffEnabled.isPresent() && isPathAwareDependencyDiffEnabled.get()) {
Map<String, List<DependencyDiff>> diffByConfiguration = new DependenciesComparison().performDiffByConfiguration(existingLock, newLock)
DiffReportGenerator generator = Class.forName("nebula.plugin.dependencylock.diff.PathAwareDiffReportGenerator").newInstance() as DiffReportGenerator
def lockDiff = generator.generateDiffReport(project, diffByConfiguration)
outputDir.mkdirs()
diffFile.text = JsonOutput.prettyPrint(JsonOutput.toJson(lockDiff))
} else {
if (newLock.isEmpty()) {
outputDir.mkdirs()
diffFile.withPrintWriter(StandardCharsets.UTF_8.displayName()) { writer ->
writer.println('--no updated locks to diff--')
}
} else {
List<DependencyDiff> diff = new DependenciesComparison().performDiff(existingLock, newLock)
writeDiff(diff)
writeDiff(diffFile, diff)
}
}
}

ConfigurationsSet readLocks(File file) {
ConfigurationsSet readLocks(Provider<File> fileProvider) {
if(!fileProvider.isPresent()) {
return new ConfigurationsSet([:])
}
File file = fileProvider.get()
if (!(file?.exists())) {
return new ConfigurationsSet([:])
}
Expand All @@ -78,9 +81,7 @@ class DiffLockTask extends AbstractLockTask {
return new ConfigurationsSet(lock)
}

void writeDiff(List<DependencyDiff> diff) {
outputDir.mkdirs()

void writeDiff(File diffFile, List<DependencyDiff> diff) {
diffFile.withPrintWriter(StandardCharsets.UTF_8.displayName()) { writer ->
def newDeps = diff.findAll { it.isNew() }
if (!newDeps.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ class DiffLockTaskSpec extends ProjectSpec {
'''.stripIndent())
def task = project.tasks.register("diffLock", DiffLockTask)
task.configure {
it.existingLockFile = existingLock
it.updatedLockFile = newLock
it.existingLockFile.set(existingLock)
it.updatedLockFile.set(newLock)
it.outputFile.set(getDiffFile())
}

when:
Expand All @@ -39,7 +40,7 @@ class DiffLockTaskSpec extends ProjectSpec {
updated:
test.nebula:a: 1.0.0 -> 1.1.0
'''.stripIndent()
realizedTask.diffFile.text == expected
realizedTask.outputFile.get().text == expected
}

def 'should handle new dependency'() {
Expand All @@ -60,8 +61,9 @@ class DiffLockTaskSpec extends ProjectSpec {
'''.stripIndent())
def task = project.tasks.register("diffLock", DiffLockTask)
task.configure {
it.existingLockFile = existingLock
it.updatedLockFile = newLock
it.existingLockFile.set(existingLock)
it.updatedLockFile.set(newLock)
it.outputFile.set(getDiffFile())
}

when:
Expand All @@ -73,7 +75,7 @@ class DiffLockTaskSpec extends ProjectSpec {
new:
test.nebula:a: 1.0.0
'''.stripIndent()
realizedTask.diffFile.text == expected
realizedTask.outputFile.get().text == expected
}

def 'should handle no existing locks'() {
Expand All @@ -91,16 +93,17 @@ class DiffLockTaskSpec extends ProjectSpec {
'''.stripIndent())
def task = project.tasks.register("diffLock", DiffLockTask)
task.configure {
it.existingLockFile = existingLock
it.updatedLockFile = newLock
it.existingLockFile.set(existingLock)
it.updatedLockFile.set(newLock)
it.outputFile.set(getDiffFile())
}

when:
def realizedTask = task.get()
realizedTask.diffLocks()

then:
realizedTask.diffFile.text == '''\
realizedTask.outputFile.get().text == '''\
new:
test.nebula:a: 1.0.0
'''.stripIndent()
Expand All @@ -124,16 +127,17 @@ class DiffLockTaskSpec extends ProjectSpec {
'''.stripIndent())
def task = project.tasks.register("diffLock", DiffLockTask)
task.configure {
it.existingLockFile = existingLock
it.updatedLockFile = newLock
it.existingLockFile.set(existingLock)
it.updatedLockFile.set(newLock)
it.outputFile.set(getDiffFile())
}

when:
def realizedTask = task.get()
realizedTask.diffLocks()

then:
realizedTask.diffFile.text == '''\
realizedTask.outputFile.get().text == '''\
removed:
test.nebula:a
'''.stripIndent()
Expand Down Expand Up @@ -176,16 +180,17 @@ class DiffLockTaskSpec extends ProjectSpec {
'''.stripIndent(), ['testCompile', 'testCompileClasspath', 'testRuntime', 'testRuntimeClasspath'])
def task = project.tasks.register("diffLock", DiffLockTask)
task.configure {
it.existingLockFile = existingLock
it.updatedLockFile = newLock
it.existingLockFile.set(existingLock)
it.updatedLockFile.set(newLock)
it.outputFile.set(getDiffFile())
}

when:
def realizedTask = task.get()
realizedTask.diffLocks()

then:
realizedTask.diffFile.text == '''\
realizedTask.outputFile.get().text == '''\
updated:
test.nebula:a: 1.0.0 -> 1.1.0
test.nebula:testlib: 2.0.0 -> 2.0.2
Expand Down Expand Up @@ -219,8 +224,9 @@ class DiffLockTaskSpec extends ProjectSpec {
'''.stripIndent(), ['testCompileClasspath', 'testRuntimeClasspath'])
def task = project.tasks.register("diffLock", DiffLockTask)
task.configure {
it.existingLockFile = existingLock
it.updatedLockFile = newLock
it.existingLockFile.set(existingLock)
it.updatedLockFile.set(newLock)
it.outputFile.set(getDiffFile())
}

when:
Expand All @@ -234,7 +240,7 @@ class DiffLockTaskSpec extends ProjectSpec {
1.0.0 -> 1.1.0 [compileClasspath,runtimeClasspath]
1.0.0 -> 1.1.1 [testCompileClasspath,testRuntimeClasspath]
'''.stripIndent()
realizedTask.diffFile.text == expected
realizedTask.outputFile.get().text == expected
}

def 'should handle being run when no new locks exist'() {
Expand All @@ -249,17 +255,24 @@ class DiffLockTaskSpec extends ProjectSpec {

def task = project.tasks.register("diffLock", DiffLockTask)
task.configure {
it.existingLockFile = existingLock
it.updatedLockFile = newLock
it.existingLockFile.set(existingLock)
it.updatedLockFile.set(newLock)
it.outputFile.set(getDiffFile())
}

when:
def realizedTask = task.get()
realizedTask.diffLocks()

then:
realizedTask.diffFile.text == '''\
realizedTask.outputFile.get().text == '''\
--no updated locks to diff--
'''.stripIndent()
}

private File getDiffFile() {
File dependencyLockFolder = new File(project.layout.buildDirectory.getAsFile().get(), "dependency-lock")
dependencyLockFolder.mkdirs()
return new File(dependencyLockFolder, "lockdiff.txt")
}
}
Loading