Skip to content

Instantly share code, notes, and snippets.

@AbrarSyed
Last active August 29, 2015 13:56
Show Gist options
  • Save AbrarSyed/8834390 to your computer and use it in GitHub Desktop.
Save AbrarSyed/8834390 to your computer and use it in GitHub Desktop.
import groovy.json.JsonSlurper
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
// This script renames files in the specified directory that have the name format something-something-version.ext
// they will be renamed to something-0something-mcversion-version.ext
// the mcversion information is taken from the mcmod.info file in each file.
// Files that do not follow the above naming convention, or do not have an mcmod.info file are ignored.
// takes 1 arg, jarDir.
// this dir should be where all the jar files are. they will be renbamed and the old ones will be deleted.
// list all files in this dir.
File root;
if (args.length == 0)
root = new File('.').getCanonicalFile()
else
root = new File(args[0])
def slurper = new JsonSlurper()
root.listFiles().each { oldFile ->
if (oldFile.isDirectory())
return; // ignore dirs and anything thats not a jar.
def name = oldFile.name
// SecretRoomsMod-universal-4.6.1.286.zip
def matcher = name =~ /(\w+)-(\w+)-([\d.]+)(.\w+)/
if (matcher.matches()) // this is the old name format.
{
def mcVersion = null;
// open it for reading.
ZipFile zip = new ZipFile(oldFile)
for (ZipEntry zentry in zip.entries())
{
if (zentry.name == "mcmod.info")
{
// read out MCVersion.
def ins = zip.getInputStream(zentry)
def results = slurper.parseText(ins.text)
ins.close();
mcVersion = results.mcversion[0]
break;
}
}
zip.close();
if (!mcVersion)
return;
// new name.
def newName = matcher[0][1] + '-' + matcher[0][2] + '-' + mcVersion + '-' + matcher[0][3] + matcher[0][4]
println name + " -> " + newName
def newFile = new File(root, newName)
newFile.delete()
newFile << oldFile.bytes
oldFile.delete()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment