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

feat: Copy resources to build output #112

Closed
wants to merge 5 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import java.io.File
import java.nio.file.Files
import java.util.Base64
import java.util.*
Copy link
Member

Choose a reason for hiding this comment

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

Let’s not use * here either

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I'm reverting, IntelliJ is changing it automatically


class WriteBase64FileHandler(project: Project, data: Map<String, Any>) : WriteFileHandler(project, data) {

override fun doWrite(vfsFile: VirtualFile?, doc: Document?, content: String) {
vfsFile?.setBinaryContent(Base64.getDecoder().decode(content))
vfsFile!!.setBinaryContent(Base64.getDecoder().decode(content))
}

override fun doCreate(ioFile: File, content: String): PsiFile {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import com.intellij.psi.PsiFileFactory
import com.intellij.psi.PsiManager
import com.vaadin.plugin.actions.VaadinCompileOnSaveAction
import com.vaadin.plugin.utils.IdeUtil
import com.vaadin.plugin.utils.VaadinProjectUtil.Companion.copyResource
import com.vaadin.plugin.utils.VaadinProjectUtil.Companion.isResource
import io.netty.handler.codec.http.HttpResponseStatus
import java.io.File

Expand All @@ -39,6 +41,9 @@ open class WriteFileHandler(project: Project, data: Map<String, Any>) : Abstract
runInEdt {
if (ReadonlyStatusHandler.ensureFilesWritable(project, vfsFile)) {
writeAndFlush(vfsFile)
if (isResource(project, vfsFile)) {
copyResource(project, vfsFile)
}
} else {
LOG.warn("File $ioFile is not writable")
}
Expand All @@ -61,35 +66,29 @@ open class WriteFileHandler(project: Project, data: Map<String, Any>) : Abstract
}

private fun writeAndFlush(vfsFile: VirtualFile) {
vfsFile.findDocument()?.let {
CommandProcessor.getInstance()
.executeCommand(
project,
{
WriteCommandAction.runWriteCommandAction(project) {
doWrite(vfsFile, it, content)
commitAndFlush(it)
LOG.info("File $ioFile contents saved")
val doc = vfsFile.findDocument()
CommandProcessor.getInstance()
.executeCommand(
project,
{
WriteCommandAction.runWriteCommandAction(project) {
doWrite(vfsFile, doc, content)
commitAndFlush(doc)
LOG.info("File $ioFile contents saved")

compile(vfsFile)
openFileInEditor(vfsFile)
}
},
undoLabel ?: "Vaadin Copilot Write File",
DocCommandGroupId.noneGroupId(it),
UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION,
)
}
compile(vfsFile)
openFileInEditor(vfsFile)
}
},
undoLabel ?: "Vaadin Copilot Write File",
if (doc != null) DocCommandGroupId.noneGroupId(doc) else null,
UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION,
)
}

private fun create() {
val psiDir = runReadAction {
val parentDir = getOrCreateParentDir()
if (parentDir != null) {
return@runReadAction PsiManager.getInstance(project).findDirectory(parentDir)
}
return@runReadAction null
}
val parentDir = VfsUtil.createDirectories(ioFile.parent)
val psiDir = runReadAction { PsiManager.getInstance(project).findDirectory(parentDir) }

if (psiDir != null) {
runInEdt {
Expand All @@ -106,6 +105,9 @@ open class WriteFileHandler(project: Project, data: Map<String, Any>) : Abstract
VfsUtil.findFileByIoFile(ioFile, true)?.let { vfsFile ->
compile(vfsFile)
openFileInEditor(vfsFile)
if (isResource(project, vfsFile)) {
copyResource(project, vfsFile)
}
}
}
LOG.info("File $ioFile contents saved")
Expand All @@ -127,14 +129,6 @@ open class WriteFileHandler(project: Project, data: Map<String, Any>) : Abstract
VaadinCompileOnSaveAction().compile(project, vfsFile)
}

private fun getOrCreateParentDir(): VirtualFile? {
if (!ioFile.parentFile.exists() && !ioFile.parentFile.mkdirs()) {
LOG.warn("Cannot create parent directories for ${ioFile.parent}")
return null
}
return VfsUtil.findFileByIoFile(ioFile.parentFile, true)
}

open fun doCreate(ioFile: File, content: String): PsiFile {
val fileType = FileTypeManager.getInstance().getFileTypeByFileName(ioFile.name)
return PsiFileFactory.getInstance(project).createFileFromText(ioFile.name, fileType, content)
Expand Down
25 changes: 25 additions & 0 deletions src/main/kotlin/com/vaadin/plugin/utils/VaadinProjectUtil.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.vaadin.plugin.utils

import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.compiler.CompilerPaths
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.observable.properties.GraphProperty
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.roots.libraries.Library
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.util.download.DownloadableFileService
import com.intellij.util.io.ZipUtil
Expand All @@ -17,6 +22,7 @@ import java.io.IOException
import java.nio.file.Path
import java.util.*
import java.util.zip.ZipFile
import org.jetbrains.jps.model.java.JavaResourceRootType

class VaadinProjectUtil {

Expand Down Expand Up @@ -89,5 +95,24 @@ class VaadinProjectUtil {
}
return hasVaadin
}

fun isResource(project: Project, vfsFile: VirtualFile): Boolean {
val module = ProjectRootManager.getInstance(project).fileIndex.getModuleForFile(vfsFile)!!
val list = ModuleRootManager.getInstance(module).getSourceRoots(JavaResourceRootType.RESOURCE)
// find matching resource root for given resource file
return list.any { vfsFile.path.startsWith(it.path) }
}

fun copyResource(project: Project, vfsFile: VirtualFile) {
val module = ProjectRootManager.getInstance(project).fileIndex.getModuleForFile(vfsFile)!!
val list = ModuleRootManager.getInstance(module).getSourceRoots(JavaResourceRootType.RESOURCE)
// find matching resource root for given resource file
val resourceRoot = list.find { vfsFile.path.startsWith(it.path) }
val resourceRelativeParentPath = vfsFile.parent.path.substringAfter(resourceRoot!!.path)
val output = CompilerPaths.getModuleOutputPath(module, false)
val resourceOutput = VfsUtil.createDirectories(output + resourceRelativeParentPath)
LOG.info("Copying resource: ${vfsFile.path} to $resourceOutput")
runWriteAction { VfsUtil.copyFile(this, vfsFile, resourceOutput) }
}
}
}