You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultLanguagesLoader.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  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.sonar.scanner.repository.language;
  21. import com.google.gson.Gson;
  22. import java.io.Reader;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.function.Function;
  26. import java.util.stream.Collectors;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import org.sonar.api.config.Configuration;
  30. import org.sonar.scanner.http.DefaultScannerWsClient;
  31. import org.sonarqube.ws.client.GetRequest;
  32. public class DefaultLanguagesLoader implements LanguagesLoader {
  33. private static final Logger LOG = LoggerFactory.getLogger(DefaultLanguagesLoader.class);
  34. private static final String LANGUAGES_WS_URL = "/api/languages/list";
  35. private static final Map<String, String> PROPERTY_FRAGMENT_MAP = Map.of(
  36. "js", "javascript",
  37. "ts", "typescript",
  38. "py", "python",
  39. "web", "html"
  40. );
  41. private final DefaultScannerWsClient wsClient;
  42. private final Configuration properties;
  43. public DefaultLanguagesLoader(DefaultScannerWsClient wsClient, Configuration properties) {
  44. this.wsClient = wsClient;
  45. this.properties = properties;
  46. }
  47. @Override
  48. public Map<String, Language> load() {
  49. GetRequest getRequest = new GetRequest(LANGUAGES_WS_URL);
  50. LanguagesWSResponse response;
  51. try (Reader reader = wsClient.call(getRequest).contentReader()) {
  52. response = new Gson().fromJson(reader, LanguagesWSResponse.class);
  53. } catch (Exception e) {
  54. throw new IllegalStateException("Fail to parse response of " + LANGUAGES_WS_URL, e);
  55. }
  56. return response.languages.stream()
  57. .map(this::populateFileSuffixesAndPatterns)
  58. .collect(Collectors.toMap(Language::key, Function.identity()));
  59. }
  60. private Language populateFileSuffixesAndPatterns(SupportedLanguageDto lang) {
  61. lang.setFileSuffixes(getFileSuffixes(lang.getKey()));
  62. lang.setFilenamePatterns(getFilenamePatterns(lang.getKey()));
  63. if (lang.filenamePatterns() == null && lang.getFileSuffixes() == null) {
  64. LOG.debug("Language '{}' cannot be detected as it has neither suffixes nor patterns.", lang.getName());
  65. }
  66. return new Language(lang);
  67. }
  68. private String[] getFileSuffixes(String languageKey) {
  69. return getPropertyForLanguage("sonar.%s.file.suffixes", languageKey);
  70. }
  71. private String[] getFilenamePatterns(String languageKey) {
  72. return getPropertyForLanguage("sonar.%s.file.patterns", languageKey);
  73. }
  74. private String[] getPropertyForLanguage(String propertyPattern, String languageKey) {
  75. String propName = String.format(propertyPattern, PROPERTY_FRAGMENT_MAP.getOrDefault(languageKey, languageKey));
  76. return properties.getStringArray(propName);
  77. }
  78. private static class LanguagesWSResponse {
  79. List<SupportedLanguageDto> languages;
  80. }
  81. }