Last active
April 9, 2017 20:35
-
-
Save dkandalov/5502872 to your computer and use it in GitHub Desktop.
Create .jar patch file (intellij plugin)
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 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