Created
September 29, 2015 20:01
-
-
Save rtyler/40c2c0cf7a93e1383e2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildscript { | |
repositories { jcenter() } | |
dependencies { | |
classpath 'com.github.jengelman.gradle.plugins:shadow:[1.2.2,2.0)' | |
classpath 'com.github.jruby-gradle:jruby-gradle-jar-plugin:[1.1.2,2.0)' | |
} | |
} | |
apply plugin: 'groovy' | |
apply plugin: 'com.github.johnrengelman.shadow' | |
import org.apache.tools.zip.ZipOutputStream | |
import org.apache.tools.zip.ZipEntry | |
import org.codehaus.plexus.util.IOUtil | |
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator | |
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer | |
import java.nio.file.Files | |
import java.nio.file.Path | |
import java.nio.file.Paths | |
import com.github.jrubygradle.jar.internal.JRubyDirInfo | |
class JRubyDirTransformer implements Transformer { | |
protected File tmpDir | |
protected JRubyDirInfo info | |
public JRubyDirTransformer() { | |
tmpDir = Files.createTempDirectory('jrubydirinfo').toFile() | |
info = new JRubyDirInfo(tmpDir) | |
} | |
boolean canTransformResource(FileTreeElement element) { | |
info.add(element.relativePath) | |
return false | |
} | |
void transform(String path, InputStream is, List<Relocator> relocators) { | |
/* we don't need to actually perform any transformation of the file */ | |
return | |
} | |
boolean hasTransformedResource() { | |
return true | |
} | |
void processDirectory(ZipOutputStream stream, File directory) { | |
directory.listFiles().each { File file -> | |
if (file.isDirectory()) { | |
processDirectory(stream, file) | |
} | |
else { | |
Path relative = Paths.get(tmpDir.absolutePath).relativize(Paths.get(file.absolutePath)) | |
stream.putNextEntry(new ZipEntry(relative.toString())) | |
IOUtil.copy(new FileInputStream(file), stream) | |
} | |
} | |
} | |
void deleteTempDirectory(File directory) { | |
directory.listFiles().each { File file -> | |
if (file.isDirectory()) { | |
deleteTempDirectory(file) | |
} | |
file.delete() | |
} | |
directory.delete() | |
} | |
void modifyOutputStream(ZipOutputStream os) { | |
processDirectory(os, tmpDir) | |
deleteTempDirectory(tmpDir) | |
} | |
} | |
shadowJar { | |
transform(new JRubyDirTransformer()) | |
into('examples') { from 'examples' } | |
from 'examples' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment