Post install task for installed plugin

Answered

Are there any hooks or any post Install actions I can use inside my plugin while user is installing it? 

I would like to launch as soon as plugin is installed some additional tasks  e.g::

  - NPM INSTALL

 - Fetch additional JAR files and add them to class path

 

Thanks, 

Frank

 

 

0
8 comments

One approach you can use is in your plugin's Application Component detect a new installation based on missing components and/or plugin settings and perform installation completion steps if these are necessary.

 

0

Thanks for your feedback. This is actually a way to solve this. The only thing I worry about is that first initial start might take a while. 

Or i would probably run all this in the background and expecting user will not notice or he/she will start using it before all the post install steps are finished. 

 

The best way would be if there are any extension points even on this Plugin install level and it would happen right there . 

0

In that case you can do the installation actions in a project component by registering after startup action in projectOpened().

class MyProjectComponent extends ProjectComponent {
    @Override
    void projectOpened() {
        StartupManagerEx.getInstance(project).runWhenProjectIsInitialized(() -> {
            if (project.isDisposed) return;

            // do necessary initialization if needed here
        });
    }
}

To handle the case that multiple projects can be loading at the same time, do the initialization in your application component but the call to the method should come from your project component’s projectOpened()runWhenProjectIsInitializedcall back and then use the first method invocation to test if further installation is needed while ignoring subsequent calls.

If after installation you need to restart the IDE you can present a dialog and restart the IDE if the user OKs the action:

class MyProjectComponent {
    public void informRestartIfNeeded() {
        if (restartNeeded) {
            ApplicationManagerEx.getApplication().invokeLater(() -> {
                if (showRestartDialog() == Messages.YES) {
                    ApplicationManagerEx.getApplicationEx().restart(true);
                } else {
                    restartNeededShownFlags = restartNeededReasons;
                }
            }, ModalityState.NON_MODAL);
        }
    }

    @Messages.YesNoResult
    int showRestartDialog() {
        String action = IdeBundle.message(ApplicationManagerEx.getApplicationEx().isRestartCapable() ? "ide.restart.action" : "ide.shutdown.action");
        String message = getRestartMessage(action);
        return Messages.showYesNoDialog(message, restartNeededTitle, action, IdeBundle.message("ide.postpone.action"), Messages.getQuestionIcon());
    }
}

I sometimes add another 5 to 10 seconds delay after initialization is complete before launching extra tasks to prevent delaying the IDE load.

There is probably a better way of doing this, especially in 2018 and 2019 IDEs but I found that this works reliably on IDEs way back to 2016.3 and easy enough to implement.

0

You can use EP com.intellij.ide.ApplicationLoadListener. Please bear in mind that network may not be available on restart after initial plugin installation.

0

Is this EP called at the time Application startup or during Plugin Installation? 

0

During application startup. There is no possibility to hook into plugin installation process.

0

Great. Last thing might be ..

If I use this EP  that kicks in as soon as I open IDE where I can start fetching nessesary resources and save them to plugin folder and To give some feedback back to user is it possible to post some message to the splan screen ? 

I could display there some meaningfull messages that we run post-install task for xyz plugin and about its progress. 

0

No, there is no possibility to draw on splash screen. If you need some progress/messages/UI to interrupt download, then I'd actually recommend Vladimir's approach.

0

Please sign in to leave a comment.