Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Last active April 9, 2017 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkandalov/5502872 to your computer and use it in GitHub Desktop.
Save dkandalov/5502872 to your computer and use it in GitHub Desktop.
Create .jar patch file (intellij plugin)
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.roots.CompilerModuleExtension
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.vcs.changes.ChangeListManager
import com.intellij.util.io.ZipUtil
import java.util.zip.ZipOutputStream
import com.intellij.openapi.application.PathManager
import com.intellij.openapi.vfs.VirtualFile
import java.lang.reflect.Field
import java.lang.reflect.Modifier
import static liveplugin.PluginUtil.*
import static com.intellij.openapi.util.io.FileUtil.toCanonicalPath as canonicalPathOf
// This is a micro-plugin to create .jar "patch file" from current change list.
// It assumes that project is already compiled.
//
// (Note that it can only be executed within this plugin https://github.com/dkandalov/live-plugin)
if (isIdeStartup) return
def modules = ModuleManager.getInstance(project).modules
def sourceRootPaths = modules.collectMany { module ->
ModuleRootManager.getInstance(module).sourceRoots.collect { canonicalPathOf(it.path) }
}
def compilerOutputPaths = modules.collect { module ->
def extension = ModuleRootManager.getInstance(module).getModuleExtension(CompilerModuleExtension.class)
canonicalPathOf(extension.compilerOutputPath.path)
}
def relativePathOf = { path ->
(sourceRootPaths + compilerOutputPaths).inject(path) { result, pathToExclude -> result.replace(pathToExclude, "") }
}
def srcFiles = ChangeListManager.getInstance(project).defaultChangeList.changes.collect { it.virtualFile }
def classFiles = srcFiles.collectMany { srcFile ->
def relativePathToFileParent = relativePathOf(canonicalPathOf(srcFile.parent.path))
compilerOutputPaths
.collectMany { new File(it + relativePathToFileParent).listFiles().toList() }
.findAll { classFile -> classFile.name.startsWith(srcFile.name.replace(".java", "")) }
.collect { [it, relativePathOf(canonicalPathOf(it.absolutePath))] }
}
def patchFile = new File("c:/work/temp/patch-temp.jar")
if (patchFile.exists()) patchFile.delete()
ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(patchFile))
classFiles.each { entry ->
def (file, classFileRelativePath) = entry
ZipUtil.addFileToZip(outputStream, file, classFileRelativePath, null, null)
}
outputStream.close()
show("Created $patchFile")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment