/sonar-go-plugin/src/main/java/org/sonar/go/plugin/GoPathContext.java

https://github.com/SonarSource/sonar-go · Java · 108 lines · 72 code · 9 blank · 27 comment · 10 complexity · f978737632c29633cab209fd1180aa19 MD5 · raw file

  1. /*
  2. * SonarQube Go Plugin
  3. * Copyright (C) 2018-2019 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.go.plugin;
  21. import java.io.File;
  22. import java.util.Arrays;
  23. import java.util.Collections;
  24. import java.util.LinkedHashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Optional;
  28. import java.util.regex.Matcher;
  29. import java.util.regex.Pattern;
  30. import java.util.stream.Collectors;
  31. import javax.annotation.Nullable;
  32. import org.sonar.api.batch.fs.FileSystem;
  33. class GoPathContext {
  34. private static final String LINUX_ABSOLUTE_PREFIX = "_/";
  35. private static final String WINDOWS_ABSOLUTE_PREFIX = "_\\";
  36. private static final Pattern WINDOWS_ABSOLUTE_REGEX = Pattern.compile("^_\\\\(\\w)_\\\\");
  37. private static final int MAX_PATH_CACHE_SIZE = 100;
  38. static final GoPathContext DEFAULT = new GoPathContext(File.separatorChar, File.pathSeparator, System.getenv("GOPATH"));
  39. final char fileSeparator;
  40. final List<String> goSrcPathList;
  41. final Map<String, String> resolvedPaths = new LinkedHashMap<String, String>() {
  42. @Override
  43. protected boolean removeEldestEntry(Map.Entry eldest) {
  44. return size() > MAX_PATH_CACHE_SIZE;
  45. }
  46. };
  47. GoPathContext(char fileSeparator, String pathSeparator, @Nullable String goPath) {
  48. this.fileSeparator = fileSeparator;
  49. List<String> goPathEntries = Collections.emptyList();
  50. if (goPath != null) {
  51. goPathEntries = Arrays.asList(goPath.split(pathSeparator));
  52. }
  53. this.goSrcPathList = goPathEntries.stream()
  54. .filter(path -> !path.isEmpty())
  55. .map(path -> concat(path, "src"))
  56. .collect(Collectors.toList());
  57. }
  58. String concat(String parentPath, String childPath) {
  59. if (parentPath.isEmpty() || parentPath.charAt(parentPath.length() - 1) == fileSeparator) {
  60. return parentPath + childPath;
  61. }
  62. return parentPath + fileSeparator + childPath;
  63. }
  64. /**
  65. * Try to resolve the absolute path of the given filePath. If filePath is absolute
  66. * (start with _) return the absolute path without the '_'. If filePath is relative,
  67. * try to append the first GOPATH entry where this file exists, otherwise return
  68. * a non-existing absolute path using the first GOPATH entry (or just filePath itself
  69. * if GOPATH is empty).
  70. * See {@link GoCoverageReport#findInputFile(String, FileSystem)}
  71. */
  72. String resolve(String filePath) {
  73. return resolvedPaths.computeIfAbsent(filePath, path -> {
  74. if (path.startsWith(LINUX_ABSOLUTE_PREFIX)) {
  75. return path.substring(1);
  76. } else if (path.startsWith(WINDOWS_ABSOLUTE_PREFIX)) {
  77. Matcher matcher = WINDOWS_ABSOLUTE_REGEX.matcher(path);
  78. if (matcher.find()) {
  79. matcher.reset();
  80. return matcher.replaceFirst("$1:\\\\");
  81. }
  82. }
  83. return prefixByFirstValidGoPath(path)
  84. .orElseGet(() -> prefixByFirstGoPath(path));
  85. });
  86. }
  87. private Optional<String> prefixByFirstValidGoPath(String filePath) {
  88. return goSrcPathList.stream()
  89. .map(goPath -> concat(goPath, filePath))
  90. .filter(path -> new File(path).exists())
  91. .findFirst();
  92. }
  93. private String prefixByFirstGoPath(String filePath) {
  94. if (goSrcPathList.isEmpty()) {
  95. return filePath;
  96. }
  97. return concat(goSrcPathList.get(0), filePath);
  98. }
  99. }