3 * Copyright (C) 2009-2017 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.internal.DefaultInputModule;
37 import org.sonar.api.scan.filesystem.PathResolver;
38 import org.sonar.api.utils.log.Logger;
39 import org.sonar.api.utils.log.Loggers;
41 import static org.sonar.scanner.config.DefaultConfiguration.parseAsCsv;
45 public class ModuleFileSystemInitializer {
47 private static final Logger LOG = Loggers.get(ModuleFileSystemInitializer.class);
49 private final List<Path> sourceDirsOrFiles;
50 private final List<Path> testDirsOrFiles;
51 private final Charset encoding;
53 public ModuleFileSystemInitializer(DefaultInputModule inputModule, ProjectDefinition module) {
54 logDir("Base dir: ", inputModule.getBaseDir());
55 logDir("Working dir: ", inputModule.getWorkDir());
56 sourceDirsOrFiles = initSources(module, inputModule.getBaseDir(), ProjectDefinition.SOURCES_PROPERTY, "Source paths: ");
57 testDirsOrFiles = initSources(module, inputModule.getBaseDir(), ProjectDefinition.TESTS_PROPERTY, "Test paths: ");
58 encoding = initEncoding(module);
61 private static List<Path> initSources(ProjectDefinition module, Path baseDir, String propertyKey, String logLabel) {
62 List<Path> result = new ArrayList<>();
63 PathResolver pathResolver = new PathResolver();
64 String srcPropValue = module.properties().get(propertyKey);
65 if (srcPropValue != null) {
66 for (String sourcePath : parseAsCsv(propertyKey, srcPropValue)) {
67 File dirOrFile = pathResolver.relativeFile(module.getBaseDir(), sourcePath);
68 if (dirOrFile.exists()) {
69 result.add(dirOrFile.toPath());
73 logPaths(logLabel, baseDir, result);
77 private static Charset initEncoding(ProjectDefinition module) {
78 String encodingStr = module.properties().get(CoreProperties.ENCODING_PROPERTY);
80 if (StringUtils.isNotEmpty(encodingStr)) {
81 result = Charset.forName(StringUtils.trim(encodingStr));
82 LOG.info("Source encoding: {}, default locale: {}", result.displayName(), Locale.getDefault());
84 result = Charset.defaultCharset();
85 LOG.warn("Source encoding is platform dependent ({}), default locale: {}", result.displayName(), Locale.getDefault());
90 List<Path> sources() {
91 return sourceDirsOrFiles;
95 return testDirsOrFiles;
98 public Charset defaultEncoding() {
102 private static void logPaths(String label, Path baseDir, List<Path> paths) {
103 if (!paths.isEmpty()) {
104 StringBuilder sb = new StringBuilder(label);
105 for (Iterator<Path> it = paths.iterator(); it.hasNext();) {
106 Path file = it.next();
107 Optional<String> relativePathToBaseDir = PathResolver.relativize(baseDir, file);
108 if (!relativePathToBaseDir.isPresent()) {
110 } else if (StringUtils.isBlank(relativePathToBaseDir.get())) {
113 sb.append(relativePathToBaseDir.get());
119 LOG.info(sb.toString());
123 private static void logDir(String label, @Nullable Path dir) {
125 LOG.info(label + dir.toAbsolutePath().toString());