Skip to content

Commit

Permalink
Deduplicate sign task inputs
Browse files Browse the repository at this point in the history
Multiple inputs can be defined that point at the same file.
  • Loading branch information
jjohannes committed Aug 22, 2019
1 parent 5e26cdb commit 3c94002
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,30 @@ class SigningConfigurationsIntegrationSpec extends SigningIntegrationSpec {
and:
file("build", "libs", "sign-1.0.jar.asc").text
}

def "duplicated inputs are handled"() {
given:
buildFile << """
signing {
${signingConfiguration()}
sign configurations.archives
}
${keyInfo.addAsPropertiesScript()}
artifacts {
// depend directly on 'jar' task in addition to dependency through 'archives'
archives jar
}
"""

when:
run "buildSignatures"

then:
executedAndNotSkipped ":signArchives"

and:
file("build", "libs", "sign-1.0.jar.asc").text
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public Iterable<File> getInputFiles() {
public Map<String, File> getOutputFiles() {
SingleMessageLogger.nagUserOfDiscontinuedMethod("Sign.getOutputFiles()",
"Please use Sign.getSignatures() and Signature.getFile() instead.");
return signaturesForExsitingFiles().stream().collect(toMap(Signature::toKey, Signature::getFile));
// will be removed in 6.0
return sanitizedSignatures().entrySet().stream().collect(toMap(Map.Entry::getKey, entry -> entry.getValue().getFile()));
}

/**
Expand Down Expand Up @@ -224,7 +225,7 @@ public void generate() {
throw new InvalidUserDataException("Cannot perform signing task \'" + getPath() + "\' because it has no configured signatory");
}

for (Signature signature : signaturesForExsitingFiles()) {
for (Signature signature : sanitizedSignatures().values()) {
signature.generate();
}
}
Expand All @@ -244,11 +245,14 @@ public DomainObjectSet<Signature> getSignatures() {
@Nested
@Incubating
public Map<String, Signature> getSignaturesByKey() {
return signaturesForExsitingFiles().stream().collect(toMap(Signature::toKey, identity()));
return sanitizedSignatures();
}

private DomainObjectSet<Signature> signaturesForExsitingFiles() {
return signatures.matching(signature -> signature.getToSign().exists());
/**
* Returns signatures mapped by their key with duplicated and non-existing inputs removed.
*/
private Map<String, Signature> sanitizedSignatures() {
return signatures.matching(signature -> signature.getToSign().exists()).stream().collect(toMap(Signature::toKey, identity(), (signature, duplicate) -> signature));
}

/**
Expand All @@ -259,10 +263,11 @@ private DomainObjectSet<Signature> signaturesForExsitingFiles() {
*/
@Internal
public Signature getSingleSignature() {
if (signatures.size() == 1) {
return signatures.iterator().next();
Map<String, Signature> sanitizedSignatures = sanitizedSignatures();
if (sanitizedSignatures.size() == 1) {
return sanitizedSignatures.values().iterator().next();
}
throw new IllegalStateException("Expected %s to contain exactly one signature, however, it contains " + signatures.size() + " signatures.");
throw new IllegalStateException("Expected %s to contain exactly one signature, however, it contains " + sanitizedSignatures.size() + " signatures.");
}

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SigningTasksSpec extends SigningProjectSpec {
def "sign jar with defaults"() {
given:
useJavadocAndSourceJars()
createJarTaskOutputFile('jar', 'sourcesJar', 'javadocJar')

when:
signing {
Expand Down Expand Up @@ -68,15 +69,13 @@ class SigningTasksSpec extends SigningProjectSpec {
useJavadocAndSourceJars()
applyPlugin()
addSigningProperties()
createJarTaskOutputFile('jar')

when:
Sign signTask = signing.sign(jar).first()

then:
def jarFile = jar.outputs.files.singleFile
File libsDir = jarFile.parentFile
libsDir.mkdirs()
jarFile.createNewFile()
def libsDir = jar.outputs.files.singleFile.parentFile
signTask.outputFiles == ["test.jar.asc:jar.asc:asc:": new File(libsDir, "test.jar.asc")]
signTask.signaturesByKey == ["test.jar.asc:jar.asc:asc:": signTask.singleSignature]
}
Expand All @@ -96,6 +95,23 @@ class SigningTasksSpec extends SigningProjectSpec {
signTask.signaturesByKey == [:]
}

def "files to sign are de-duplicated"() {
given:
useJavadocAndSourceJars()
applyPlugin()
addSigningProperties()
createJarTaskOutputFile('jar')

when:
Sign signTask = signing.sign(jar).first()
signTask.sign('', jar.outputs.files.singleFile) // add jar task output again, this time directly as File

then:
signTask.signatures.size() == 2
noExceptionThrown()
signTask.signaturesByKey == ["test.jar.asc:jar.asc:asc:": signTask.singleSignature]
}

def "sign task has description"() {
given:
useJavadocAndSourceJars()
Expand All @@ -109,4 +125,14 @@ class SigningTasksSpec extends SigningProjectSpec {
signJar.description == "Signs the archive produced by the 'jar' task."
signSourcesJar.description == "Signs the archive produced by the 'sourcesJar' task."
}

private createJarTaskOutputFile(String... tasksToSimulate) {
for (def task : tasksToSimulate) {
def jarFile = tasks.getByName(task).outputs.files.singleFile
File libsDir = jarFile.parentFile
libsDir.mkdirs()
jarFile.createNewFile()
}

}
}

0 comments on commit 3c94002

Please sign in to comment.