From 2d9392c7392c5eda455d257edd2d793327cbbf2e Mon Sep 17 00:00:00 2001 From: Konstantin Bulenkov Date: Tue, 2 May 2017 22:59:21 +0200 Subject: [PATCH] add Update Plugins command for better integration with Toolbox App --- .../command/impl/UpdatePluginsApp.java | 108 ++++++++++++++++++ .../updateSettings/impl/UpdateChecker.kt | 6 +- .../src/META-INF/PlatformExtensions.xml | 1 + 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 platform/platform-impl/src/com/intellij/openapi/command/impl/UpdatePluginsApp.java diff --git a/platform/platform-impl/src/com/intellij/openapi/command/impl/UpdatePluginsApp.java b/platform/platform-impl/src/com/intellij/openapi/command/impl/UpdatePluginsApp.java new file mode 100644 index 000000000000..bbe7b0e950b9 --- /dev/null +++ b/platform/platform-impl/src/com/intellij/openapi/command/impl/UpdatePluginsApp.java @@ -0,0 +1,108 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.openapi.command.impl; + +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.ApplicationStarterEx; +import com.intellij.openapi.progress.EmptyProgressIndicator; +import com.intellij.openapi.updateSettings.impl.PluginDownloader; +import com.intellij.openapi.updateSettings.impl.UpdateChecker; +import com.intellij.openapi.updateSettings.impl.UpdateInstaller; +import com.intellij.openapi.updateSettings.impl.UpdateSettings; +import com.intellij.openapi.util.ActionCallback; +import com.intellij.openapi.util.BuildNumber; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @author Konstantin Bulenkov + */ +public class UpdatePluginsApp extends ApplicationStarterEx { + @Override + public boolean isHeadless() { + return true; + } + + @Override + public String getCommandName() { + return "update"; + } + + @Override + public void premain(String[] args) { + + } + + @Override + public void main(String[] args) { + List pluginsToUpdate = getPluginsToUpdate(args); + ActionCallback callback = new ActionCallback() + .doWhenDone(() -> System.exit(0)) + .doWhenRejected(() -> System.exit(1)); + + Runnable update = () -> updateAllPlugins(callback, pluginsToUpdate); + ApplicationManager.getApplication().executeOnPooledThread(update); + } + + @Nullable + private static List getPluginsToUpdate(String[] args) { + if (args.length < 2) return null; + ArrayList pluginIds = new ArrayList<>(args.length - 1); + //skip "update" command and read plugin ids + for (int i = 1; i < args.length; i++) { + pluginIds.add(i - 1, args[i]); + } + return pluginIds; + } + + private static void updateAllPlugins(ActionCallback callback, @Nullable List plugins) { + Collection availableUpdates = UpdateChecker.checkPluginsUpdate(UpdateSettings.getInstance(), + new EmptyProgressIndicator(), + new HashSet<>(), + BuildNumber.currentVersion()); + if (availableUpdates == null) { + log("All plugins up to date."); + callback.setDone(); + return; + } + + Collection pluginsToUpdate = plugins == null || plugins.isEmpty() + ? availableUpdates + : availableUpdates.stream() + .filter(downloader -> plugins.contains(downloader.getPluginId())) + .collect(Collectors.toList()); + + + log("Plugins to update:"); + pluginsToUpdate.forEach(d -> log("\t" + d.getPluginName())); + + if (UpdateInstaller.installPluginUpdates(pluginsToUpdate, new EmptyProgressIndicator())) { + callback.setDone(); + } else { + log("Update failed"); + callback.setRejected(); + } + } + + private static void log(String msg) { + System.out.println(msg); + } +} \ No newline at end of file diff --git a/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/UpdateChecker.kt b/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/UpdateChecker.kt index 093132648c48..2576ccbf9587 100644 --- a/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/UpdateChecker.kt +++ b/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/UpdateChecker.kt @@ -107,7 +107,8 @@ object UpdateChecker { }) } - private fun doUpdateAndShowResult(project: Project?, + @JvmStatic + fun doUpdateAndShowResult(project: Project?, fromSettings: Boolean, manualCheck: Boolean, updateSettings: UpdateSettings, @@ -193,7 +194,8 @@ object UpdateChecker { return strategy.checkForUpdates() } - private fun checkPluginsUpdate(updateSettings: UpdateSettings, + @JvmStatic + fun checkPluginsUpdate(updateSettings: UpdateSettings, indicator: ProgressIndicator?, incompatiblePlugins: MutableCollection?, buildNumber: BuildNumber?): Collection? { diff --git a/platform/platform-resources/src/META-INF/PlatformExtensions.xml b/platform/platform-resources/src/META-INF/PlatformExtensions.xml index f890d5cab479..5ba5f565dc1b 100644 --- a/platform/platform-resources/src/META-INF/PlatformExtensions.xml +++ b/platform/platform-resources/src/META-INF/PlatformExtensions.xml @@ -4,6 +4,7 @@ +