|
| 1 | +/* |
| 2 | + * SonarLint Core - Implementation |
| 3 | + * Copyright (C) 2016-2025 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 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. |
| 10 | + * |
| 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. |
| 15 | + * |
| 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. |
| 19 | + */ |
| 20 | +package org.sonarsource.sonarlint.core.plugin.resolvers; |
| 21 | + |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.function.Function; |
| 27 | +import java.util.stream.Collectors; |
| 28 | +import javax.annotation.Nullable; |
| 29 | +import org.sonarsource.sonarlint.core.commons.api.SonarLanguage; |
| 30 | +import org.sonarsource.sonarlint.core.plugin.ArtifactSource; |
| 31 | +import org.sonarsource.sonarlint.core.plugin.ArtifactState; |
| 32 | +import org.sonarsource.sonarlint.core.plugin.PluginJarUtils; |
| 33 | +import org.sonarsource.sonarlint.core.plugin.PluginStatus; |
| 34 | +import org.sonarsource.sonarlint.core.plugin.ResolvedArtifact; |
| 35 | +import org.sonarsource.sonarlint.core.plugin.commons.loading.SonarPluginManifest; |
| 36 | +import org.sonarsource.sonarlint.core.rpc.protocol.backend.initialize.InitializeParams; |
| 37 | +import org.sonarsource.sonarlint.core.rpc.protocol.backend.initialize.LanguageSpecificRequirements; |
| 38 | +import org.sonarsource.sonarlint.core.rpc.protocol.backend.initialize.OmnisharpRequirementsDto; |
| 39 | + |
| 40 | +public class EmbeddedArtifactResolver implements ArtifactResolver, CompanionPluginResolver { |
| 41 | + |
| 42 | + private final Map<String, Path> standaloneEmbeddedPathsByKey; |
| 43 | + private final Map<String, Path> connectedModeEmbeddedPathsByKey; |
| 44 | + @Nullable |
| 45 | + private final Path csharpStandalonePluginPath; |
| 46 | + |
| 47 | + public EmbeddedArtifactResolver(InitializeParams params) { |
| 48 | + this.standaloneEmbeddedPathsByKey = buildPluginKeyToPathMap(params.getEmbeddedPluginPaths()); |
| 49 | + this.connectedModeEmbeddedPathsByKey = params.getConnectedModeEmbeddedPluginPathsByKey(); |
| 50 | + this.csharpStandalonePluginPath = Optional.ofNullable(params.getLanguageSpecificRequirements()) |
| 51 | + .map(LanguageSpecificRequirements::getOmnisharpRequirements) |
| 52 | + .map(OmnisharpRequirementsDto::getOssAnalyzerPath) |
| 53 | + .orElse(null); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Optional<ResolvedArtifact> resolve(SonarLanguage language, @Nullable String connectionId) { |
| 58 | + return Optional.ofNullable(resolvePath(language, connectionId)) |
| 59 | + .map(EmbeddedArtifactResolver::toResolvedArtifact); |
| 60 | + } |
| 61 | + |
| 62 | + private Map<String, PluginStatus> standaloneCompanionPlugins; |
| 63 | + private Map<String, PluginStatus> connectedModeCompanionPlugins; |
| 64 | + |
| 65 | + @Override |
| 66 | + public Map<String, PluginStatus> resolveCompanionPlugins(@Nullable String connectionId) { |
| 67 | + if (connectionId != null) { |
| 68 | + if (connectedModeCompanionPlugins == null) { |
| 69 | + connectedModeCompanionPlugins = computeCompanionPlugins(connectedModeEmbeddedPathsByKey); |
| 70 | + } |
| 71 | + return connectedModeCompanionPlugins; |
| 72 | + } |
| 73 | + if (standaloneCompanionPlugins == null) { |
| 74 | + standaloneCompanionPlugins = computeCompanionPlugins(standaloneEmbeddedPathsByKey); |
| 75 | + } |
| 76 | + return standaloneCompanionPlugins; |
| 77 | + } |
| 78 | + |
| 79 | + private static Map<String, PluginStatus> computeCompanionPlugins(Map<String, Path> pathsByKey) { |
| 80 | + return pathsByKey.entrySet().stream() |
| 81 | + .filter(e -> !SonarLanguage.ALL_PLUGIN_KEYS.contains(e.getKey())) |
| 82 | + .collect(Collectors.toUnmodifiableMap( |
| 83 | + Map.Entry::getKey, |
| 84 | + e -> PluginStatus.forCompanion(e.getKey(), ArtifactState.ACTIVE, ArtifactSource.EMBEDDED, e.getValue()))); |
| 85 | + } |
| 86 | + |
| 87 | + @Nullable |
| 88 | + private Path resolvePath(SonarLanguage language, @Nullable String connectionId) { |
| 89 | + return connectionId != null ? resolveConnected(language) : resolveStandalone(language); |
| 90 | + } |
| 91 | + |
| 92 | + private static ResolvedArtifact toResolvedArtifact(Path path) { |
| 93 | + return new ResolvedArtifact(ArtifactState.ACTIVE, path, ArtifactSource.EMBEDDED, PluginJarUtils.readVersion(path)); |
| 94 | + } |
| 95 | + |
| 96 | + @Nullable |
| 97 | + private Path resolveConnected(SonarLanguage language) { |
| 98 | + return connectedModeEmbeddedPathsByKey.get(language.getPluginKey()); |
| 99 | + } |
| 100 | + |
| 101 | + @Nullable |
| 102 | + private Path resolveStandalone(SonarLanguage language) { |
| 103 | + var found = standaloneEmbeddedPathsByKey.get(language.getPluginKey()); |
| 104 | + if (found == null && language == SonarLanguage.CS) { |
| 105 | + return csharpStandalonePluginPath; |
| 106 | + } |
| 107 | + return found; |
| 108 | + } |
| 109 | + |
| 110 | + private static Map<String, Path> buildPluginKeyToPathMap(Set<Path> embeddedPaths) { |
| 111 | + return embeddedPaths.stream() |
| 112 | + .collect(Collectors.toMap(p -> SonarPluginManifest.fromJar(p).getKey(), Function.identity())); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments