-
-
Save treymarc/ec3c7cf7aca5883daa3c5e2c88ed9a27 to your computer and use it in GitHub Desktop.
gr
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
import java.security.DigestInputStream | |
import java.security.MessageDigest | |
import java.util.zip.ZipFile | |
import groovy.io.FileType | |
import java.io.File | |
task metadataOld(type: MetadataTask) { distribution = "${rootDir}" } | |
task metadataNew(type: MetadataTask) { distribution = "${rootDir}/stattable-searchers-1.1.15-SNAPSHOT.jar" } | |
task(compareDistrib) { doLast { println "${name}" } } | |
compareDistrib.dependsOn metadataOld | |
compareDistrib.dependsOn metadataNew | |
class FileMetadata { | |
String name, path, hash | |
List<FileMetadata> fileMetadatas = [] | |
FileMetadata(String name, String path, String hash){ | |
this.name = name | |
this.path = path | |
this.hash = hash | |
} | |
def void printMetadata(){ | |
println "${path}${name}, ${hash}" | |
fileMetadatas.each { it.printMetadata()} | |
} | |
} | |
class MetadataTask extends DefaultTask { | |
String distribution = '' | |
def generateMD5(File file) { | |
file.withInputStream { generateMD5(it) } | |
} | |
def generateMD5(InputStream it) { | |
new DigestInputStream(it, MessageDigest.getInstance('MD5')).withStream { | |
it.eachByte {} | |
it.messageDigest.digest().encodeHex() as String | |
} | |
} | |
def FileMetadata createMetadataFromZip(String distributionName, File distributionZipFile, String relativePath) { | |
try{ | |
def fileMetadata = new FileMetadata(distributionName,relativePath,generateMD5(distributionZipFile)) | |
def zf = new ZipFile(distributionZipFile) | |
zf.entries().findAll { !it.directory }.each { | |
if (it.name.matches('(.+?).jar|.zip$')){ | |
// TODO clean unzip file | |
def fOut = new File(it.name.split('/').last(),project.buildDir) // File(+ File.separator + it.name) | |
def is = zf.getInputStream(it) | |
byte[] bytes = new byte[1024] | |
int length | |
fOut.withOutputStream { stream -> | |
while ((length = is.read(bytes)) >= 0) { | |
stream.write(bytes, 0, length); | |
} | |
} | |
is.close() | |
fileMetadata.fileMetadatas.add(createMetadata(it.name.split('/').last(), fOut, fileMetadata.path+distributionName+'/' )) | |
fOut.delete() | |
} else { | |
fileMetadata.fileMetadatas.add(new FileMetadata(it.name.split('/').last(),fileMetadata.path+distributionName+'/',generateMD5(zf.getInputStream(it)))) | |
} | |
} | |
return fileMetadata | |
}catch (Exception e){ | |
throw new GradleException("Can not open distribution zip file ${distributionZipFile}", e) | |
} | |
} | |
def FileMetadata createMetadata(String distributionName,File distributionZipFile, String relativePath) { | |
def fileMetadata = new FileMetadata(distributionName,relativePath,"") | |
if (distributionZipFile.isDirectory()){ | |
distributionZipFile.eachFileRecurse (FileType.FILES) { file -> | |
fileMetadata.fileMetadatas.add(createMetadata(file.getName(), file, relativePath + fileMetadata.name + '/')) | |
} | |
} else if (distributionZipFile.getName().matches('(.+?).jar|.zip$')){ | |
fileMetadata = createMetadataFromZip(distributionName, distributionZipFile, relativePath) | |
} else { | |
fileMetadata.hash = generateMD5(distributionZipFile) | |
} | |
return fileMetadata | |
} | |
@TaskAction | |
def metadata() { | |
project.buildDir.mkdirs() | |
def distributionFile = new File(distribution) | |
if (!distributionFile.exists()){ | |
println "distribution file not found \"${distribution}\"" | |
}else{ | |
def fileMetadata = createMetadata(distributionFile.getName(), distributionFile, '/'); | |
fileMetadata.printMetadata() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment