Skip to content

Commit

Permalink
Replaced IOUtil with built-in FileUtil, removed semi columns
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullahtellioglu committed Sep 6, 2024
1 parent 3df88ab commit b1823eb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ConfigurationCheckPostStartupProjectActivity : ProjectActivity {
checkVcsAddConfirmationSetting(project)
RunOnceUtil.runOnceForApp("hotswap-version-check-" + CopilotPluginUtil.getPluginVersion()) {
VaadinHomeUtil.checkBundledAndCurrentVersionAndReplaceIfBundledOneIsNewer()
};
}

}

Expand Down
70 changes: 37 additions & 33 deletions src/main/kotlin/com/vaadin/plugin/utils/VaadinHomeUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package com.vaadin.plugin.utils

import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.util.io.FileUtil
import org.apache.commons.io.FileUtils
import java.io.File
import java.io.IOException
import java.util.Properties
import java.util.*
import java.util.jar.JarFile

object VaadinHomeUtil {
private val LOG: Logger = Logger.getInstance(VaadinHomeUtil::class.java)
private const val PROPERTY_USER_HOME: String = "user.home"
private const val VAADIN_FOLDER_NAME: String = ".vaadin"
private const val HOTSWAP_AGENT_JAR_FILE_NAME = "hotswap-agent.jar"

/**
* Get Vaadin home directory.
*
Expand All @@ -36,7 +36,7 @@ object VaadinHomeUtil {
if (!hotswapAgentJar.exists()) {
// Try to copy the agent to the JDK
val bundledHotswap = this.javaClass.classLoader.getResource(HOTSWAP_AGENT_JAR_FILE_NAME)
?: throw IllegalStateException("The plugin package is broken: no hotswap-agent.jar found");
?: throw IllegalStateException("The plugin package is broken: no hotswap-agent.jar found")
if (!intellijFolder.exists()) {
check(intellijFolder.mkdirs()) { "Unable to create directory for hotswap-agent.jar" }
}
Expand All @@ -46,67 +46,71 @@ object VaadinHomeUtil {
throw IllegalStateException("Unable to copy hotswap-agent.jar to " + hotswapAgentJar.absolutePath, e)
}
}
return hotswapAgentJar;
return hotswapAgentJar
}
fun checkBundledAndCurrentVersionAndReplaceIfBundledOneIsNewer(){
try{

fun checkBundledAndCurrentVersionAndReplaceIfBundledOneIsNewer() {
try {
val hotswapAgentJar = File(intellijFolder, HOTSWAP_AGENT_JAR_FILE_NAME)
val bundledHotswap = this.javaClass.classLoader.getResource(HOTSWAP_AGENT_JAR_FILE_NAME)
?: throw IllegalStateException("The plugin package is broken: no hotswap-agent.jar found");
if(isBundledVersionNewer()){
?: throw IllegalStateException("The plugin package is broken: no hotswap-agent.jar found")
if (isBundledVersionNewer()) {
FileUtil.copy(bundledHotswap.openStream(), hotswapAgentJar.outputStream())
}
}catch(e: Exception){
} catch (e: Exception) {
LOG.error(e.message, e)
}
}

private fun isBundledVersionNewer(): Boolean {
val hotswapAgentJar = File(intellijFolder, HOTSWAP_AGENT_JAR_FILE_NAME)
val hotswapAgentVersionInVaadinFolder = getHotswapAgentVersion(hotswapAgentJar);
val hotswapAgentVersionInVaadinFolder = getHotswapAgentVersion(hotswapAgentJar)
val bundledHotswapAgentVersion = getBundledHotswapAgentVersion()
if(bundledHotswapAgentVersion != null && hotswapAgentVersionInVaadinFolder != null){
if (bundledHotswapAgentVersion != null && hotswapAgentVersionInVaadinFolder != null) {
return bundledHotswapAgentVersion.compareTo(hotswapAgentVersionInVaadinFolder) == 1
}
return false
}

private fun getBundledHotswapAgentVersion(): String? {
var tempFile: File? = null;
try{
tempFile = File.createTempFile("bundled-hotswap-agent", "jar");
tempFile.deleteOnExit();
FileUtil.copy(this.javaClass.classLoader.getResource(HOTSWAP_AGENT_JAR_FILE_NAME)?.openStream() ?:
throw IllegalStateException("Unable to copy hotswap-agent.jar to temporary file ")
, tempFile.outputStream())
var tempFile: File? = null
try {
tempFile = File.createTempFile("bundled-hotswap-agent", ".jar")
FileUtil.copy(
this.javaClass.classLoader.getResource(HOTSWAP_AGENT_JAR_FILE_NAME)?.openStream()
?: throw IllegalStateException("Unable to copy hotswap-agent.jar to temporary file "),
tempFile.outputStream()
)
return getHotswapAgentVersion(tempFile)
}catch (e: IOException) {
e.printStackTrace()
} catch (e: IOException) {
LOG.error(e.message, e)
return null
}
finally {
if(tempFile != null) {
FileUtils.deleteQuietly(tempFile);
} finally {
if (tempFile != null) {
FileUtil.asyncDelete(tempFile)
}
}
}
private fun getHotswapAgentVersion(file: File): String?{
val jarFile = JarFile(file);

private fun getHotswapAgentVersion(file: File): String? {
val jarFile = JarFile(file)
val entries = jarFile.entries()
while (entries.hasMoreElements()) {
val entry = entries.nextElement();
if(entry.name == "version.properties"){
val entry = entries.nextElement()
if (entry.name == "version.properties") {
val inputStream = jarFile.getInputStream(entry)
val properties = Properties()
properties.load(inputStream)
var version = properties.getProperty("version");
if(version.indexOf('-') != -1){
version = version.substring(0, version.indexOf('-'));
var version = properties.getProperty("version")
if (version.indexOf('-') != -1) {
version = version.substring(0, version.indexOf('-'))
}
return version;
return version
}
}
return null;
return null
}

private val intellijFolder: File
get() = File(resolveVaadinHomeDirectory(), "intellij-plugin")
}

0 comments on commit b1823eb

Please sign in to comment.