3 * Copyright (C) 2009-2019 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.scanner.scan.filesystem;
23 import java.nio.charset.Charset;
24 import java.nio.file.Path;
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Locale;
29 import java.util.Optional;
30 import javax.annotation.Nullable;
31 import javax.annotation.concurrent.Immutable;
32 import org.apache.commons.lang.StringUtils;
33 import org.sonar.api.CoreProperties;
34 import org.sonar.api.batch.ScannerSide;
35 import org.sonar.api.batch.bootstrap.ProjectDefinition;
36 import org.sonar.api.batch.fs.InputModule;
37 import org.sonar.api.batch.fs.internal.AbstractProjectOrModule;
38 import org.sonar.api.scan.filesystem.PathResolver;
39 import org.sonar.api.utils.log.Logger;
40 import org.sonar.api.utils.log.Loggers;
42 import static org.sonar.core.config.MultivalueProperty.parseAsCsv;
46 public class ModuleFileSystemInitializer {
48 private static final Logger LOG = Loggers.get(ModuleFileSystemInitializer.class);
50 private final List<Path> sourceDirsOrFiles;
51 private final List<Path> testDirsOrFiles;
52 private final Charset encoding;
54 public ModuleFileSystemInitializer(InputModule inputModule) {
55 AbstractProjectOrModule inputModuleCasted = (AbstractProjectOrModule) inputModule;
56 logDir("Base dir: ", inputModuleCasted.getBaseDir());
57 logDir("Working dir: ", inputModuleCasted.getWorkDir());
58 sourceDirsOrFiles = initSources(inputModuleCasted, ProjectDefinition.SOURCES_PROPERTY, "Source paths: ");
59 testDirsOrFiles = initSources(inputModuleCasted, ProjectDefinition.TESTS_PROPERTY, "Test paths: ");
60 encoding = initEncoding(inputModuleCasted);
63 private static List<Path> initSources(AbstractProjectOrModule module, String propertyKey, String logLabel) {
64 List<Path> result = new ArrayList<>();
65 PathResolver pathResolver = new PathResolver();
66 String srcPropValue = module.properties().get(propertyKey);
67 if (srcPropValue != null) {
68 for (String sourcePath : parseAsCsv(propertyKey, srcPropValue)) {
69 File dirOrFile = pathResolver.relativeFile(module.getBaseDir().toFile(), sourcePath);
70 if (dirOrFile.exists()) {
71 result.add(dirOrFile.toPath());
75 logPaths(logLabel, module.getBaseDir(), result);
79 private static Charset initEncoding(AbstractProjectOrModule module) {
80 String encodingStr = module.properties().get(CoreProperties.ENCODING_PROPERTY);
82 if (StringUtils.isNotEmpty(encodingStr)) {
83 result = Charset.forName(StringUtils.trim(encodingStr));
84 LOG.info("Source encoding: {}, default locale: {}", result.displayName(), Locale.getDefault());
86 result = Charset.defaultCharset();
87 LOG.warn("Source encoding is platform dependent ({}), default locale: {}", result.displayName(), Locale.getDefault());
92 List<Path> sources() {
93 return sourceDirsOrFiles;
97 return testDirsOrFiles;
100 public Charset defaultEncoding() {
104 private static void logPaths(String label, Path baseDir, List<Path> paths) {
105 if (!paths.isEmpty()) {
106 StringBuilder sb = new StringBuilder(label);
107 for (Iterator<Path> it = paths.iterator(); it.hasNext(); ) {
108 Path file = it.next();
109 Optional<String> relativePathToBaseDir = PathResolver.relativize(baseDir, file);
110 if (!relativePathToBaseDir.isPresent()) {
112 } else if (StringUtils.isBlank(relativePathToBaseDir.get())) {
115 sb.append(relativePathToBaseDir.get());
121 LOG.info(sb.toString());
125 private static void logDir(String label, @Nullable Path dir) {
127 LOG.info(label + dir.toAbsolutePath().toString());