Skip to content

Commit

Permalink
Guard better against programmer errors
Browse files Browse the repository at this point in the history
  • Loading branch information
opwvhk committed Apr 8, 2024
1 parent 3cdd50a commit 4d12a66
Showing 1 changed file with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationAction;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import com.intellij.openapi.ui.Messages;
import opwvhk.intellij.avro_idl.actions.AvroIdlNotifications;
import opwvhk.intellij.avro_idl.language.AvroIdlUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -32,6 +34,8 @@
* and the installation account are inaccessible).</p>
*/
public class AvroIdlPluginUpdateStartupActivity implements StartupActivity.DumbAware {
private static final Logger LOG = Logger.getInstance(AvroIdlUtil.class);

private static final PluginId MY_PLUGIN_ID = PluginId.getId("net.sf.opk.avro-schema-support");
public static final PluginId OLD_PLUGIN_ID = PluginId.getId("claims.bold.intellij.avro");
private static final String SNAPSHOT_SUFFIX = "-SNAPSHOT";
Expand All @@ -52,21 +56,12 @@ public void runActivity(@NotNull Project project) {

String oldVersion = settings.getPluginVersion();
String newVersion = versionOf(plugin);
LOG.info("Collecting changes for the Avro Schema Plugin (%s) since version %s".formatted(newVersion, oldVersion));

notifyUserOfUpdate(project, plugin, newVersion, oldVersion);
settings.setPluginVersion(newVersion);
}

@NotNull
private String versionOf(IdeaPluginDescriptor plugin) {
String pluginVersion = plugin.getVersion();
if (pluginVersion.endsWith(SNAPSHOT_SUFFIX)) {
return pluginVersion.substring(0, pluginVersion.length() - SNAPSHOT_SUFFIX.length());
} else {
return pluginVersion;
}
}

private void checkForReplacedPlugin(@NotNull Project project, String myName) {
IdeaPluginDescriptor descriptor = PluginManagerCore.getPlugin(OLD_PLUGIN_ID);
if (descriptor != null && !PluginManagerCore.isDisabled(OLD_PLUGIN_ID)) {
Expand All @@ -88,6 +83,16 @@ private void checkForReplacedPlugin(@NotNull Project project, String myName) {
}
}

@NotNull
private String versionOf(IdeaPluginDescriptor plugin) {
String pluginVersion = plugin.getVersion();
if (pluginVersion.endsWith(SNAPSHOT_SUFFIX)) {
return pluginVersion.substring(0, pluginVersion.length() - SNAPSHOT_SUFFIX.length());
} else {
return pluginVersion;
}
}


private void notifyUserOfUpdate(@NotNull Project project, @NotNull IdeaPluginDescriptor plugin,
@NotNull String newVersion, @Nullable String oldVersion) {
Expand All @@ -109,8 +114,8 @@ private void notifyUserOfUpdate(@NotNull Project project, @NotNull IdeaPluginDes
};

if (oldVersion != null) {
CharSequence changes = collectNewChanges(oldVersion, changeNotes);
if (changes.length() > 0) {
CharSequence changes = collectNewChanges(newVersion, oldVersion, changeNotes);
if (!changes.isEmpty()) {
AvroIdlNotifications.showNotification(project, NotificationType.INFORMATION,
notificationTitle, "This is what has changed:</b><br/><br/>" + changes,
addNotificationActions);
Expand All @@ -122,8 +127,11 @@ private void notifyUserOfUpdate(@NotNull Project project, @NotNull IdeaPluginDes
}

@NotNull
private static CharSequence collectNewChanges(@NotNull String oldVersion, String changeNotes) {
if (changeNotes == null) {
private static CharSequence collectNewChanges(@NotNull String newVersion, @NotNull String oldVersion,
String changeNotes) {
// Check if we need to do anything. Change notes are only missing in case of a programmer error,
// and the 2nd check is to guard against bugs in the change notes.
if (changeNotes == null || newVersion.equals(oldVersion)) {
return "";
}

Expand All @@ -135,7 +143,7 @@ private static CharSequence collectNewChanges(@NotNull String oldVersion, String
.limit(3)
.forEach(mr -> {
final String version = mr.group(1);
changes.append(version).append(":").append(matcher.group());
changes.append("<p>").append(version).append(":</p>").append(matcher.group());
});
return changes;
}
Expand Down

0 comments on commit 4d12a66

Please sign in to comment.