3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectexport.steps;
22 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
23 import java.util.Collection;
24 import org.apache.commons.lang.StringUtils;
25 import org.slf4j.LoggerFactory;
26 import org.sonar.ce.task.step.ComputationStep;
27 import org.sonar.core.platform.PluginInfo;
28 import org.sonar.core.platform.PluginRepository;
29 import org.sonar.updatecenter.common.Version;
31 public class ExportPluginsStep implements ComputationStep {
33 private final PluginRepository pluginRepository;
34 private final DumpWriter dumpWriter;
36 public ExportPluginsStep(PluginRepository pluginRepository, DumpWriter dumpWriter) {
37 this.pluginRepository = pluginRepository;
38 this.dumpWriter = dumpWriter;
42 public void execute(Context context) {
43 try (StreamWriter<ProjectDump.Plugin> writer = dumpWriter.newStreamWriter(DumpElement.PLUGINS)) {
45 Collection<PluginInfo> plugins = pluginRepository.getPluginInfos();
46 for (PluginInfo plugin : plugins) {
47 ProjectDump.Plugin.Builder builder = ProjectDump.Plugin.newBuilder();
48 writer.write(convert(plugin, builder));
50 LoggerFactory.getLogger(getClass()).debug("{} plugins exported", plugins.size());
54 private static ProjectDump.Plugin convert(PluginInfo plugin, ProjectDump.Plugin.Builder builder) {
57 .setKey(plugin.getKey())
58 .setName(StringUtils.defaultString(plugin.getName()));
59 Version version = plugin.getVersion();
60 if (version != null) {
61 builder.setVersion(version.toString());
63 return builder.build();
67 public String getDescription() {
68 return "Export plugins";