Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use VirtualFiles to save dot file, improved logging #115

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 47 additions & 43 deletions src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ import com.intellij.ide.plugins.PluginManagerCore
import com.intellij.notification.Notification
import com.intellij.notification.NotificationType
import com.intellij.notification.Notifications
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.extensions.PluginId
import com.intellij.openapi.fileTypes.FileTypeManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.PsiManager
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.vfs.*
import com.vaadin.plugin.copilot.handler.*
import com.vaadin.plugin.utils.VaadinIcons
import io.netty.handler.codec.http.HttpResponseStatus
import java.io.BufferedWriter
import java.io.File
import java.io.IOException
import java.io.StringWriter
import java.util.*

Expand Down Expand Up @@ -95,53 +91,61 @@ class CopilotPluginUtil {
}
}
props.store(bufferedWriter, "Vaadin Copilot Integration Runtime Properties")

val fileType = FileTypeManager.getInstance().getStdFileType("properties")
runInEdt {
ApplicationManager.getApplication().runWriteAction {
dotFileDirectory.findFile(DOTFILE)?.delete()
val file =
PsiFileFactory.getInstance(project)
.createFileFromText(DOTFILE, fileType, stringWriter.toString())
dotFileDirectory.add(file)
LOG.info("$DOTFILE created in ${dotFileDirectory.virtualFile.path}")
runWriteAction {
val dotFile = dotFileDirectory.findFile(DOTFILE)
dotFile?.let {
try {
it.delete(this)
} catch (e: IOException) {
LOG.error("Failed to delete $DOTFILE: ${e.message}")
}
}
val newFile =
try {
dotFileDirectory.createChildData(this, DOTFILE)
} catch (e: IOException) {
LOG.error("Failed to create $DOTFILE: ${e.message}")
return@runWriteAction
}

try {
VfsUtil.saveText(newFile, stringWriter.toString())
} catch (e: IOException) {
LOG.error("Failed to write to $DOTFILE: ${e.message}")
}
LOG.info("$newFile created")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this info or debug info?

Copy link
Member Author

@MarcinVaadin MarcinVaadin Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://plugins.jetbrains.com/docs/intellij/ide-infrastructure.html#logging INFO and above goes to idea.log by default. As I'm looking into other log files entries ours fits perfectly. It's information about work of plugin so I would keep it as INFO.

}
}
} else {
LOG.error("Cannot create $DOTFILE")
}
}

fun removeDotFile(project: Project) {
ApplicationManager.getApplication().runWriteAction {
val dotFileDirectory = getDotFileDirectory(project)
dotFileDirectory?.findFile(DOTFILE)?.let {
it.delete()
LOG.info("$DOTFILE removed from ${dotFileDirectory.virtualFile.path}")
return@runWriteAction
}
LOG.warn("Cannot remove $DOTFILE")
}
}

private fun getIdeaDir(project: Project): File {
return File(project.basePath, IDEA_DIR)
}

fun getDotFileDirectory(project: Project): PsiDirectory? {
return ApplicationManager.getApplication().runReadAction<PsiDirectory?> {
VfsUtil.findFileByIoFile(getIdeaDir(project), false)?.let {
return@runReadAction PsiManager.getInstance(project).findDirectory(it)
runInEdt {
runWriteAction {
val dotFile = getDotFileDirectory(project)?.findFile(DOTFILE)
dotFile?.let {
try {
it.delete(this)
LOG.info("$it removed")
} catch (e: IOException) {
LOG.error("Failed to delete $DOTFILE: ${e.message}")
}
return@runWriteAction
}
LOG.warn("Cannot remove $dotFile - file does not exist")
}
return@runReadAction null
}
}

fun createIdeaDirectoryIfMissing(project: Project) {
WriteCommandAction.runWriteCommandAction(project) {
val ideaDir = getIdeaDir(project).path
VfsUtil.createDirectoryIfMissing(ideaDir)?.let { LOG.info("$ideaDir created") }
private fun getDotFileDirectory(project: Project): VirtualFile? {
val projectDir = project.guessProjectDir()
if (projectDir == null) {
LOG.error("Cannot guess project directory")
return null
}
LOG.info("Project directory: $projectDir")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this info or debug info?

return projectDir.findOrCreateDirectory(IDEA_DIR)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.project.ProjectManagerListener
import com.vaadin.plugin.copilot.CopilotPluginUtil
import com.vaadin.plugin.copilot.CopilotPluginUtil.Companion.saveDotFile
import com.vaadin.plugin.listeners.VaadinProjectListener

class CopilotVaadinProjectListener : VaadinProjectListener {
Expand All @@ -13,19 +14,11 @@ class CopilotVaadinProjectListener : VaadinProjectListener {
override fun vaadinProjectDetected(project: Project) {
if (!triggered) {
triggered = true
createDotFile(project)
saveDotFile(project)
removeDotFileOnExit(project)
}
}

private fun createDotFile(project: Project) {
val dotFileDirectory = CopilotPluginUtil.getDotFileDirectory(project)
if (dotFileDirectory == null) {
CopilotPluginUtil.createIdeaDirectoryIfMissing(project)
}
CopilotPluginUtil.saveDotFile(project)
}

private fun removeDotFileOnExit(project: Project) {
ProjectManager.getInstance()
.addProjectManagerListener(
Expand Down