// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.plugins;

import com.google.gson.stream.JsonWriter;
import com.intellij.openapi.application.ApplicationStarter;
import com.intellij.openapi.util.io.FileUtil;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Ivan Chirkov
 */
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public class BundledPluginsLister implements ApplicationStarter {
  @Override
  public String getCommandName() {
    return "listBundledPlugins";
  }

  @Override
  public void main(String[] args) {
    try {
      OutputStream out;
      if (args.length == 2) {
        File outFile = new File(args[1]);
        FileUtil.createParentDirs(outFile);
        //noinspection IOResourceOpenedButNotSafelyClosed
        out = new FileOutputStream(outFile);
      }
      else {
        out = System.out;
      }

      try (JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8))) {
        IdeaPluginDescriptor[] plugins = PluginManagerCore.getPlugins();

        List<String> modules = Arrays.stream(plugins)
          .flatMap(it -> it instanceof IdeaPluginDescriptorImpl ? ((IdeaPluginDescriptorImpl)it).getModules().stream() : Stream.empty())
          .sorted()
          .collect(Collectors.toList());

        List<String> pluginIds = Arrays.stream(plugins)
                                       .map(plugin -> plugin.getPluginId().getIdString())
                                       .sorted()
                                       .collect(Collectors.toList());

        writer.beginObject();
        writeList(writer, "modules", modules);
        writeList(writer, "plugins", pluginIds);
        writer.endObject();
      }
    }
    catch (IOException e) {
      e.printStackTrace(System.err);
      System.exit(1);
    }

    System.exit(0);
  }

  private static void writeList(JsonWriter writer, String name, List<String> elements) throws IOException {
    writer.name(name).beginArray();
    for (String module : elements) {
      writer.value(module);
    }
    writer.endArray();
  }
}
@@ -267,24 +267,140 @@
 ionStarter;%0A
+import com.intellij.openapi.fileTypes.FileTypeManager;%0Aimport com.intellij.openapi.fileTypes.PlainTextLikeFileType;%0A
 import com.i
@@ -1885,32 +1885,438 @@
 ors.toList());%0A%0A
+        FileTypeManager fileTypeManager = FileTypeManager.getInstance();%0A        List%3CString%3E extensions = Arrays.stream(fileTypeManager.getRegisteredFileTypes()).%0A          filter(type -%3E !(type instanceof PlainTextLikeFileType)).%0A          flatMap(type -%3E fileTypeManager.getAssociations(type).stream()).%0A          map(matcher -%3E matcher.getPresentableString()).%0A          collect(Collectors.toList());%0A%0A
         writer.b
@@ -2421,24 +2421,77 @@
 pluginIds);%0A
+        writeList(writer, %22extensions%22, extensions);%0A
         writ

generate bundled extension list