fix: migrate from StartupActivity to ProjectActivity

This commit is contained in:
Carl-Robert Linnupuu 2024-03-27 21:48:48 +02:00
parent 9d28e3e009
commit b72ddfccd6
5 changed files with 137 additions and 141 deletions

View file

@ -1,76 +0,0 @@
package ee.carlrobert.codegpt;
import static ee.carlrobert.codegpt.CodeGPTPlugin.CODEGPT_ID;
import com.intellij.ide.plugins.InstalledPluginsState;
import com.intellij.notification.NotificationAction;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import com.intellij.openapi.updateSettings.impl.UpdateChecker;
import com.intellij.openapi.updateSettings.impl.UpdateSettings;
import com.intellij.util.concurrency.AppExecutorUtil;
import ee.carlrobert.codegpt.settings.configuration.ConfigurationSettings;
import ee.carlrobert.codegpt.ui.OverlayUtil;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.NotNull;
public class CodeGPTUpdateStartupActivity implements StartupActivity.Background {
@Override
public void runActivity(@NotNull Project project) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
return;
}
schedulePluginUpdateChecks(project);
}
private void schedulePluginUpdateChecks(Project project) {
AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(() -> {
if (project != null && ConfigurationSettings.getCurrentState().isCheckForPluginUpdates()) {
new CheckForUpdatesTask(project).queue();
}
}, 0, 4L, TimeUnit.HOURS);
}
private static class CheckForUpdatesTask extends Task.Backgroundable {
public CheckForUpdatesTask(@NotNull Project project) {
super(project, CodeGPTBundle.get("checkForUpdatesTask.title"), true);
}
private static void installCodeGPTUpdate(Project project) {
var settingsCopy = new UpdateSettings();
var settingsState = settingsCopy.getState();
settingsState.copyFrom(UpdateSettings.getInstance().getState());
settingsState.setCheckNeeded(true);
settingsState.setPluginsCheckNeeded(true);
settingsState.setShowWhatsNewEditor(true);
settingsState.setThirdPartyPluginsAllowed(true);
UpdateChecker.updateAndShowResult(project, settingsCopy);
}
public void run(@NotNull ProgressIndicator indicator) {
if (!myProject.isDisposed()) {
if (InstalledPluginsState.getInstance().hasNewerVersion(CODEGPT_ID)) {
OverlayUtil.getDefaultNotification(
CodeGPTBundle.get("checkForUpdatesTask.notification.message"),
NotificationType.IDE_UPDATE)
.addAction(NotificationAction.createSimpleExpiring(
CodeGPTBundle.get("checkForUpdatesTask.notification.installButton"),
() -> ApplicationManager.getApplication()
.executeOnPooledThread(() -> installCodeGPTUpdate(myProject))))
.addAction(NotificationAction.createSimpleExpiring(
CodeGPTBundle.get("checkForUpdatesTask.notification.hideButton"),
() -> ConfigurationSettings.getCurrentState().setCheckForPluginUpdates(false)))
.notify(myProject);
}
}
}
}
}

View file

@ -1,63 +0,0 @@
package ee.carlrobert.codegpt;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import ee.carlrobert.codegpt.actions.editor.EditorActionsUtil;
import ee.carlrobert.codegpt.completions.you.YouUserManager;
import ee.carlrobert.codegpt.completions.you.auth.AuthenticationHandler;
import ee.carlrobert.codegpt.completions.you.auth.YouAuthenticationError;
import ee.carlrobert.codegpt.completions.you.auth.YouAuthenticationService;
import ee.carlrobert.codegpt.completions.you.auth.response.YouAuthenticationResponse;
import ee.carlrobert.codegpt.credentials.YouCredentialManager;
import ee.carlrobert.codegpt.settings.GeneralSettings;
import ee.carlrobert.codegpt.settings.service.you.YouSettings;
import ee.carlrobert.codegpt.ui.OverlayUtil;
import org.jetbrains.annotations.NotNull;
public class PluginStartupActivity implements StartupActivity {
private static final Logger LOG = Logger.getInstance(PluginStartupActivity.class);
@Override
public void runActivity(@NotNull Project project) {
EditorActionsUtil.refreshActions();
var authenticationResponse = YouUserManager.getInstance().getAuthenticationResponse();
if (authenticationResponse == null) {
ApplicationManager.getApplication()
.executeOnPooledThread(this::handleYouServiceAuthentication);
}
}
private void handleYouServiceAuthentication() {
var settings = YouSettings.getCurrentState();
var password = YouCredentialManager.getInstance().getCredential();
if (!settings.getEmail().isEmpty() && password != null && !password.isEmpty()) {
YouAuthenticationService.getInstance()
.signInAsync(settings.getEmail(), password, new AuthenticationHandler() {
@Override
public void handleAuthenticated(YouAuthenticationResponse authenticationResponse) {
OverlayUtil.showNotification(
"Authentication successful.",
NotificationType.INFORMATION);
}
@Override
public void handleGenericError() {
OverlayUtil.showNotification(
"Something went wrong while trying to authenticate.",
NotificationType.ERROR);
}
@Override
public void handleError(YouAuthenticationError youAuthenticationError) {
OverlayUtil.showNotification(
youAuthenticationError.getErrorMessage(),
NotificationType.ERROR);
}
});
}
}
}

View file

@ -0,0 +1,57 @@
package ee.carlrobert.codegpt
import com.intellij.notification.NotificationType
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import ee.carlrobert.codegpt.actions.editor.EditorActionsUtil
import ee.carlrobert.codegpt.completions.you.YouUserManager
import ee.carlrobert.codegpt.completions.you.auth.AuthenticationHandler
import ee.carlrobert.codegpt.completions.you.auth.YouAuthenticationError
import ee.carlrobert.codegpt.completions.you.auth.YouAuthenticationService
import ee.carlrobert.codegpt.completions.you.auth.response.YouAuthenticationResponse
import ee.carlrobert.codegpt.credentials.YouCredentialManager
import ee.carlrobert.codegpt.settings.service.you.YouSettings
import ee.carlrobert.codegpt.ui.OverlayUtil
class CodeGPTProjectActivity : ProjectActivity {
override suspend fun execute(project: Project) {
EditorActionsUtil.refreshActions()
if (YouUserManager.getInstance().authenticationResponse == null) {
ApplicationManager.getApplication()
.executeOnPooledThread { this.handleYouServiceAuthentication() }
}
}
private fun handleYouServiceAuthentication() {
val settings = YouSettings.getCurrentState()
val password = YouCredentialManager.getInstance().credential
if (settings.email.isNotEmpty() && password != null && password.isNotEmpty()) {
YouAuthenticationService.getInstance()
.signInAsync(settings.email, password, object : AuthenticationHandler {
override fun handleAuthenticated(authenticationResponse: YouAuthenticationResponse) {
OverlayUtil.showNotification(
"Authentication successful.",
NotificationType.INFORMATION
)
}
override fun handleGenericError() {
OverlayUtil.showNotification(
"Something went wrong while trying to authenticate.",
NotificationType.ERROR
)
}
override fun handleError(youAuthenticationError: YouAuthenticationError) {
OverlayUtil.showNotification(
youAuthenticationError.errorMessage,
NotificationType.ERROR
)
}
})
}
}
}

View file

@ -0,0 +1,78 @@
package ee.carlrobert.codegpt;
import com.intellij.ide.plugins.InstalledPluginsState
import com.intellij.notification.NotificationAction
import com.intellij.notification.NotificationType
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import com.intellij.openapi.updateSettings.impl.UpdateChecker.updateAndShowResult
import com.intellij.openapi.updateSettings.impl.UpdateSettings
import com.intellij.util.concurrency.AppExecutorUtil
import ee.carlrobert.codegpt.settings.configuration.ConfigurationSettings
import ee.carlrobert.codegpt.ui.OverlayUtil
import java.util.concurrent.TimeUnit
class CodeGPTUpdateActivity : ProjectActivity {
override suspend fun execute(project: Project) {
if (ApplicationManager.getApplication().isUnitTestMode) {
return
}
schedulePluginUpdateChecks(project)
}
private fun schedulePluginUpdateChecks(project: Project) {
val command = {
if (ConfigurationSettings.getCurrentState().isCheckForPluginUpdates) {
CheckForUpdatesTask(project).queue()
}
}
AppExecutorUtil.getAppScheduledExecutorService()
.scheduleWithFixedDelay(command, 0, 4L, TimeUnit.HOURS)
}
private class CheckForUpdatesTask(project: Project) :
Task.Backgroundable(project, CodeGPTBundle.get("checkForUpdatesTask.title"), true) {
override fun run(indicator: ProgressIndicator) {
val isLatestVersion =
!InstalledPluginsState.getInstance().hasNewerVersion(CodeGPTPlugin.CODEGPT_ID);
if (project.isDisposed || isLatestVersion) {
return
}
OverlayUtil.getDefaultNotification(
CodeGPTBundle.get("checkForUpdatesTask.notification.message"),
NotificationType.IDE_UPDATE
)
.addAction(NotificationAction.createSimpleExpiring(
CodeGPTBundle.get("checkForUpdatesTask.notification.installButton")
) {
ApplicationManager.getApplication()
.executeOnPooledThread { installCodeGPTUpdate(project) }
})
.addAction(NotificationAction.createSimpleExpiring(
CodeGPTBundle.get("checkForUpdatesTask.notification.hideButton")
) {
ConfigurationSettings.getCurrentState().isCheckForPluginUpdates = false
})
.notify(project)
}
companion object {
private fun installCodeGPTUpdate(project: Project) {
val settingsCopy = UpdateSettings()
val settingsState = settingsCopy.state
settingsState.copyFrom(UpdateSettings.getInstance().state)
settingsState.isCheckNeeded = true
settingsState.isPluginsCheckNeeded = true
settingsState.isShowWhatsNewEditor = true
settingsState.isThirdPartyPluginsAllowed = true
updateAndShowResult(project, settingsCopy)
}
}
}
}

View file

@ -12,8 +12,8 @@
</projectListeners>
<extensions defaultExtensionNs="com.intellij">
<postStartupActivity implementation="ee.carlrobert.codegpt.CodeGPTUpdateStartupActivity"/>
<postStartupActivity implementation="ee.carlrobert.codegpt.PluginStartupActivity"/>
<postStartupActivity implementation="ee.carlrobert.codegpt.CodeGPTProjectActivity"/>
<postStartupActivity implementation="ee.carlrobert.codegpt.CodeGPTUpdateActivity"/>
<applicationConfigurable id="settings.codegpt" parentId="tools" displayName="CodeGPT"
instance="ee.carlrobert.codegpt.settings.GeneralSettingsConfigurable"/>
<applicationConfigurable id="settings.codegpt.configuration" parentId="settings.codegpt" displayName="Configuration"