Skip to content

Commit

Permalink
close #70, #71 - apply transformers to project resources
Browse files Browse the repository at this point in the history
  • Loading branch information
johnrengelman committed Jul 16, 2014
1 parent 7bf9b80 commit f7f3a02
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ v1.0.3
+ Make service files root path configurable for `ServiceFileTransformer` ([Issue #72](https://github.com/johnrengelman/shadow/issues/72))
+ [Andres Almiray](https://github.com/aalmiray) Added PropertiesFileTransformer ([Issue #73](https://github.com/johnrengelman/shadow/issues/73))
+ [Brandon Kearby](https://github.com/brandonkearby) Fixed StackOverflow when a cycle occurs in the resolved dependency graph ([Issue #69](https://github.com/johnrengelman/shadow/pull/69))
+ Apply Transformers to project resources ([Issue #70](https://github.com/johnrengelman/shadow/issues/70), [Issue #71](https://github.com/johnrengelman/shadow/issues/71))

v1.0.2
======
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,17 @@ public class ShadowCopyAction implements CopyAction {
try {
boolean isClass = (FilenameUtils.getExtension(fileDetails.path) == 'class')
if (!remapper.hasRelocators() || !isClass) {
String path = fileDetails.relativePath.pathString
ZipEntry archiveEntry = new ZipEntry(path)
archiveEntry.setTime(fileDetails.lastModified)
archiveEntry.unixMode = (UnixStat.FILE_FLAG | fileDetails.mode)
zipOutStr.putNextEntry(archiveEntry)
fileDetails.copyTo(zipOutStr)
zipOutStr.closeEntry()
if (!isTransformable(fileDetails)) {
String path = fileDetails.relativePath.pathString
ZipEntry archiveEntry = new ZipEntry(path)
archiveEntry.setTime(fileDetails.lastModified)
archiveEntry.unixMode = (UnixStat.FILE_FLAG | fileDetails.mode)
zipOutStr.putNextEntry(archiveEntry)
fileDetails.copyTo(zipOutStr)
zipOutStr.closeEntry()
} else {
transform(fileDetails)
}
} else {
remapClass(fileDetails)
}
Expand Down Expand Up @@ -297,13 +301,28 @@ public class ShadowCopyAction implements CopyAction {
}

private void transform(RelativeArchivePath file, ZipFile archive) {
String mappedPath = remapper.map(file.pathString)
InputStream is = archive.getInputStream(file.entry)
transformers.find { it.canTransformResource(file.pathString) }.transform(mappedPath, is, relocators)
transform(file.pathString, archive.getInputStream(file.entry))
}

private void transform(FileCopyDetails file) {
transform(file.path, file.file.newInputStream())
}

private void transform(String path, InputStream is) {
String mappedPath = remapper.map(path)
transformers.find { it.canTransformResource(path) }.transform(mappedPath, is, relocators)
}

private boolean isTransformable(RelativeArchivePath file) {
return transformers.find { it.canTransformResource(file.pathString) } as boolean
return isTransformable(file.pathString)
}

private boolean isTransformable(FileCopyDetails file) {
return isTransformable(file.path)
}

private boolean isTransformable(String path) {
return transformers.any { it.canTransformResource(path) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransf
import com.github.jengelman.gradle.plugins.shadow.transformers.XmlAppendingTransformer
import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification
import org.gradle.testkit.functional.ExecutionResult
import spock.lang.Issue

import java.util.jar.JarInputStream
import java.util.jar.Manifest
Expand Down Expand Up @@ -78,6 +79,51 @@ class TransformerSpec extends PluginSpecification {
|two # NOTE: No newline terminates this line/file'''.stripMargin()
}

@Issue(['SHADOW-70', 'SHADOW-71'])
def 'apply transformers to project resources'() {
given:
File one = buildJar('one.jar').insertFile('META-INF/services/shadow.Shadow',
'one # NOTE: No newline terminates this line/file').write()

repo.module('shadow', 'two', '1.0').insertFile('META-INF/services/shadow.Shadow',
'two # NOTE: No newline terminates this line/file').publish()

buildFile << """
|apply plugin: 'java'
|apply plugin: ${ShadowPlugin.name}
|
|repositories { maven { url "${repo.uri}" } }
|dependencies {
| compile 'shadow:two:1.0'
| compile files('${escapedPath(one)}')
|}
|
|shadowJar {
| baseName = 'shadow'
| classifier = null
| mergeServiceFiles()
|}
""".stripMargin()

file('src/main/resources/META-INF/services/shadow.Shadow') <<
'three # NOTE: No newline terminates this line/file'

when:
runner.arguments << 'shadowJar'
ExecutionResult result = runner.run()

then:
success(result)
assert output.exists()

and:
String text = getJarFileContents(output, 'META-INF/services/shadow.Shadow')
assert text.split('(\r\n)|(\r)|(\n)').size() == 3
assert text == '''|three # NOTE: No newline terminates this line/file
|one # NOTE: No newline terminates this line/file
|two # NOTE: No newline terminates this line/file'''.stripMargin()
}

def 'appending transformer'() {
given:
File one = buildJar('one.jar').insertFile('test.properties',
Expand Down

0 comments on commit f7f3a02

Please sign in to comment.