Tracking progress of VfsUtil.copyDirectory

Hello.

When creating a new project with my plugin, I need to copy a bunch of files from an external directory. To achieve that I added a runnable via runWhenProjectIsInitialized in my ModuleBuilder's setupRootModel method and run a code similar to this:

VirtualFile sourceDir = scriptsDir.createChildDirectory(this, "source");
VirtualFile originScripts = myOriginDirectory.getScriptsDir();
VfsUtil.copyDirectory(this, originScripts, sourceDir, null);

It works, but not without a problem. It seems, that copyDirectory method initially reads through origin files and copies them to memory, which causes IDE window to lock. After that, it releases code execution, and everything written after it is executed. This means that after that initial hiccup the project is considered created and editor becomes operational. However, actual physical files are still being copied at the moment. IDE shows an indexing process while this is happening.

 

What I need is a way to monitor copying process to know when it finishes, is there any way to do that?

Also, before calling copyDirectory I attempt to show a popup, but it only appears after copyDirectory has run it's course and IDE did the freezing. What can be the cause of this?

Finally, can I postpone the moment the project is considered created until after physical files were created?

2 comments
Comment actions Permalink

From what I see, copyDirectory works synchronously, so when it finished, the files should already be copied in full. It also must be invoked on Swing thread, which explains the freeze that you see. If you're not comfortable with that, the best option is to copy under a modal progress using java.io.File and then invoke VfsUtil.markDirtyAndRefresh to sync VFS to the disk state.

To postpone project creation until the copying is finished, why not perform the copying right in setupRootModel?

0
Comment actions Permalink

Peter,

I looked through copyDirectory code, and it would seem indeed, that it works synchronously. But in practice it happens as I described — physical files appear in IntelliJ IDEA and in Windows Explorer over time after copyDirectory is allegedly done. Maybe I am interpreting it wrong, and it just takes some time for the OS, and IntelliJ IDEA by proxy, to pick up on file system changes (there are a lot of files)? If that's the case, would using native java.io.File to copy change anything?

I moved copying from setupRootModel because I thought it was the reason for the freeze, and indeed, it feels like the initial freeze got shorter after that. I was trying to find a way for this process to not block the UI, yet be able to wait for it to finish before signaling that the project is ready. I don't remember the context, but I think IntelliJ IDEA does something like this itself sometimes when creating the project. It shows a popup with a progress bar and limits user interaction, yet does not block UI.

0

Please sign in to leave a comment.