diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2018-11-15 18:52:56 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2019-01-16 09:43:01 +0100 |
commit | 6a598e7d7973171e04d78483085ede97a4e21dcb (patch) | |
tree | 00ea7758ca6eff77b7d8611e9aed1deccbc76765 /sonar-scanner-engine/src/main/java/org/sonar | |
parent | 059a10d425fff575cbcd1740023db719310d06e8 (diff) | |
download | sonarqube-6a598e7d7973171e04d78483085ede97a4e21dcb.tar.gz sonarqube-6a598e7d7973171e04d78483085ede97a4e21dcb.zip |
SONAR-11459 Remove ModuleFileSystemInitializer
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org/sonar')
11 files changed, 91 insertions, 177 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/config/DefaultConfiguration.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/config/DefaultConfiguration.java index 5567fed1c98..f79c496b962 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/config/DefaultConfiguration.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/config/DefaultConfiguration.java @@ -36,7 +36,7 @@ import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import static java.util.Objects.requireNonNull; import static org.apache.commons.lang.StringUtils.trim; -import static org.sonar.core.config.MultivalueProperty.parseAsCsv; +import static org.sonar.api.config.internal.MultivalueProperty.parseAsCsv; @Immutable public abstract class DefaultConfiguration implements Configuration { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/InputModuleHierarchyProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/InputModuleHierarchyProvider.java index 6392a5dd515..3026296253b 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/InputModuleHierarchyProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/InputModuleHierarchyProvider.java @@ -19,21 +19,33 @@ */ package org.sonar.scanner.scan; +import java.nio.file.Path; import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.Optional; +import org.apache.commons.lang.StringUtils; import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.DefaultInputProject; +import org.sonar.api.scan.filesystem.PathResolver; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; import org.sonar.scanner.scan.filesystem.ScannerComponentIdGenerator; public class InputModuleHierarchyProvider extends ProviderAdapter { + private static final Logger LOG = Loggers.get(InputModuleHierarchyProvider.class); + private DefaultInputModuleHierarchy hierarchy = null; public DefaultInputModuleHierarchy provide(ScannerComponentIdGenerator scannerComponentIdGenerator, DefaultInputProject project) { if (hierarchy == null) { - DefaultInputModule root = new DefaultInputModule(project.definition(), project.scannerId()); + LOG.debug("Creating module hierarchy"); + DefaultInputModule root = createModule(project.definition(), project.scannerId()); Map<DefaultInputModule, DefaultInputModule> parents = createChildren(root, scannerComponentIdGenerator, new HashMap<>()); if (parents.isEmpty()) { hierarchy = new DefaultInputModuleHierarchy(root); @@ -47,10 +59,44 @@ public class InputModuleHierarchyProvider extends ProviderAdapter { private static Map<DefaultInputModule, DefaultInputModule> createChildren(DefaultInputModule parent, ScannerComponentIdGenerator scannerComponentIdGenerator, Map<DefaultInputModule, DefaultInputModule> parents) { for (ProjectDefinition def : parent.definition().getSubProjects()) { - DefaultInputModule child = new DefaultInputModule(def, scannerComponentIdGenerator.getAsInt()); + DefaultInputModule child = createModule(def, scannerComponentIdGenerator.getAsInt()); parents.put(child, parent); createChildren(child, scannerComponentIdGenerator, parents); } return parents; } + + private static DefaultInputModule createModule(ProjectDefinition def, int scannerComponentId) { + LOG.debug(" Init module '{}'", def.getName()); + DefaultInputModule module = new DefaultInputModule(def, scannerComponentId); + LOG.debug(" Base dir: {}", module.getBaseDir().toAbsolutePath().toString()); + LOG.debug(" Working dir: {}", module.getWorkDir().toAbsolutePath().toString()); + LOG.debug(" Module global encoding: {}, default locale: {}", module.getEncoding().displayName(), Locale.getDefault()); + logPaths(" Source paths: ", module.getBaseDir(), module.getSourceDirsOrFiles()); + logPaths(" Test paths: ", module.getBaseDir(), module.getTestDirsOrFiles()); + return module; + } + + private static void logPaths(String label, Path baseDir, List<Path> paths) { + if (!paths.isEmpty()) { + StringBuilder sb = new StringBuilder(label); + for (Iterator<Path> it = paths.iterator(); it.hasNext(); ) { + Path file = it.next(); + Optional<String> relativePathToBaseDir = PathResolver.relativize(baseDir, file); + if (!relativePathToBaseDir.isPresent()) { + sb.append(file); + } else if (StringUtils.isBlank(relativePathToBaseDir.get())) { + sb.append("."); + } else { + sb.append(relativePathToBaseDir.get()); + } + if (it.hasNext()) { + sb.append(", "); + } + } + LOG.info(sb.toString()); + } + } + + } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/InputProjectProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/InputProjectProvider.java index 4363fb6b19e..ea05d64e4b1 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/InputProjectProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/InputProjectProvider.java @@ -19,13 +19,19 @@ */ package org.sonar.scanner.scan; +import java.util.Locale; import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.DefaultInputProject; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; import org.sonar.scanner.scan.filesystem.ScannerComponentIdGenerator; public class InputProjectProvider extends ProviderAdapter { + private static final Logger LOG = Loggers.get(DefaultInputModule.class); + private DefaultInputProject project = null; public DefaultInputProject provide(ProjectBuildersExecutor projectBuildersExecutor, ProjectReactorValidator validator, @@ -37,8 +43,12 @@ public class InputProjectProvider extends ProviderAdapter { // 2 Validate final reactor validator.validate(projectReactor); - // 3 Create modules and the hierarchy + // 3 Create project project = new DefaultInputProject(projectReactor.getRoot(), scannerComponentIdGenerator.getAsInt()); + + LOG.info("Base dir: {}", project.getBaseDir().toAbsolutePath().toString()); + LOG.info("Working dir: {}", project.getWorkDir().toAbsolutePath().toString()); + LOG.debug("Project global encoding: {}, default locale: {}", project.getEncoding().displayName(), Locale.getDefault()); } return project; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java index 5a45671270a..bf101a42de0 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java @@ -52,9 +52,7 @@ import org.sonar.scanner.scan.filesystem.DefaultModuleFileSystem; import org.sonar.scanner.scan.filesystem.ExclusionFilters; import org.sonar.scanner.scan.filesystem.FileIndexer; import org.sonar.scanner.scan.filesystem.InputFileBuilder; -import org.sonar.scanner.scan.filesystem.LanguageDetection; import org.sonar.scanner.scan.filesystem.MetadataGenerator; -import org.sonar.scanner.scan.filesystem.ModuleFileSystemInitializer; import org.sonar.scanner.scan.filesystem.ModuleInputComponentStore; import org.sonar.scanner.scan.report.IssuesReports; import org.sonar.scanner.sensor.DefaultSensorContext; @@ -110,11 +108,9 @@ public class ModuleScanContainer extends ComponentContainer { ExclusionFilters.class, MetadataGenerator.class, FileMetadata.class, - LanguageDetection.class, FileIndexer.class, InputFileBuilder.class, DefaultModuleFileSystem.class, - ModuleFileSystemInitializer.class, QProfileVerifier.class, SensorOptimizer.class, diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java index f9667af57b1..38d0d60733f 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java @@ -45,7 +45,8 @@ import org.sonar.api.utils.log.Profiler; import org.sonar.scanner.bootstrap.ScannerProperties; import org.sonar.scanner.util.ScannerUtils; -import static org.sonar.core.config.MultivalueProperty.parseAsCsv; +import static org.sonar.api.config.internal.MultivalueProperty.parseAsCsv; + /** * Class that creates a project definition based on a set of properties. diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java index 332bb65299a..eb33fe12f42 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java @@ -93,6 +93,7 @@ import org.sonar.scanner.scan.branch.BranchType; import org.sonar.scanner.scan.branch.ProjectBranchesProvider; import org.sonar.scanner.scan.branch.ProjectPullRequestsProvider; import org.sonar.scanner.scan.filesystem.InputComponentStore; +import org.sonar.scanner.scan.filesystem.LanguageDetection; import org.sonar.scanner.scan.filesystem.ScannerComponentIdGenerator; import org.sonar.scanner.scan.filesystem.StatusDetection; import org.sonar.scanner.scan.measure.DefaultMetricFinder; @@ -163,6 +164,7 @@ public class ProjectScanContainer extends ComponentContainer { ScannerComponentIdGenerator.class, new ScmChangedFilesProvider(), StatusDetection.class, + LanguageDetection.class, // rules new ActiveRulesProvider(), diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/DefaultModuleFileSystem.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/DefaultModuleFileSystem.java index 4e9dc9406de..3a962530f8b 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/DefaultModuleFileSystem.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/DefaultModuleFileSystem.java @@ -21,26 +21,26 @@ package org.sonar.scanner.scan.filesystem; import com.google.common.annotations.VisibleForTesting; import org.sonar.api.batch.fs.internal.DefaultFileSystem; -import org.sonar.api.batch.fs.internal.AbstractProjectOrModule; +import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.scanner.analysis.DefaultAnalysisMode; public class DefaultModuleFileSystem extends DefaultFileSystem { - public DefaultModuleFileSystem(ModuleInputComponentStore moduleInputFileCache, AbstractProjectOrModule module, ModuleFileSystemInitializer initializer, DefaultAnalysisMode mode, + public DefaultModuleFileSystem(ModuleInputComponentStore moduleInputFileCache, DefaultInputModule module, DefaultAnalysisMode mode, StatusDetection statusDetection) { super(module.getBaseDir(), moduleInputFileCache); - setFields(module, initializer, mode, statusDetection); + setFields(module, mode, statusDetection); } @VisibleForTesting - public DefaultModuleFileSystem(AbstractProjectOrModule module, ModuleFileSystemInitializer initializer, DefaultAnalysisMode mode, StatusDetection statusDetection) { + public DefaultModuleFileSystem(DefaultInputModule module, DefaultAnalysisMode mode, StatusDetection statusDetection) { super(module.getBaseDir()); - setFields(module, initializer, mode, statusDetection); + setFields(module, mode, statusDetection); } - private void setFields(AbstractProjectOrModule module, ModuleFileSystemInitializer initializer, DefaultAnalysisMode mode, StatusDetection statusDetection) { + private void setFields(DefaultInputModule module, DefaultAnalysisMode mode, StatusDetection statusDetection) { setWorkDir(module.getWorkDir()); - setEncoding(initializer.defaultEncoding()); + setEncoding(module.getEncoding()); // filter the files sensors have access to if (!mode.scanAllFiles()) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java index 7779852b181..5294797c67c 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java @@ -44,10 +44,9 @@ import org.sonar.api.batch.ScannerSide; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFile.Type; import org.sonar.api.batch.fs.InputFileFilter; -import org.sonar.api.batch.fs.InputModule; import org.sonar.api.batch.fs.internal.DefaultInputDir; import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.fs.internal.AbstractProjectOrModule; +import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.api.utils.MessageException; import org.sonar.scanner.scan.DefaultComponentTree; @@ -64,10 +63,9 @@ public class FileIndexer { private final ExclusionFilters exclusionFilters; private final InputFileBuilder inputFileBuilder; private final DefaultComponentTree componentTree; - private final AbstractProjectOrModule module; + private final DefaultInputModule module; private final ScannerComponentIdGenerator scannerComponentIdGenerator; private final InputComponentStore componentStore; - private final ModuleFileSystemInitializer moduleFileSystemInitializer; private ExecutorService executorService; private final List<Future<Void>> tasks; private final DefaultModuleFileSystem defaultModuleFileSystem; @@ -75,16 +73,15 @@ public class FileIndexer { private ProgressReport progressReport; - public FileIndexer(ScannerComponentIdGenerator scannerComponentIdGenerator, InputComponentStore componentStore, InputModule module, ExclusionFilters exclusionFilters, - DefaultComponentTree componentTree, InputFileBuilder inputFileBuilder, ModuleFileSystemInitializer initializer, DefaultModuleFileSystem defaultModuleFileSystem, + public FileIndexer(ScannerComponentIdGenerator scannerComponentIdGenerator, InputComponentStore componentStore, DefaultInputModule module, ExclusionFilters exclusionFilters, + DefaultComponentTree componentTree, InputFileBuilder inputFileBuilder, DefaultModuleFileSystem defaultModuleFileSystem, LanguageDetection languageDetection, InputFileFilter[] filters) { this.scannerComponentIdGenerator = scannerComponentIdGenerator; this.componentStore = componentStore; - this.module = (AbstractProjectOrModule) module; + this.module = module; this.componentTree = componentTree; this.inputFileBuilder = inputFileBuilder; - this.moduleFileSystemInitializer = initializer; this.defaultModuleFileSystem = defaultModuleFileSystem; this.langDetection = languageDetection; this.filters = filters; @@ -92,10 +89,10 @@ public class FileIndexer { this.tasks = new ArrayList<>(); } - public FileIndexer(ScannerComponentIdGenerator scannerComponentIdGenerator, InputComponentStore componentStore, InputModule module, ExclusionFilters exclusionFilters, - DefaultComponentTree componentTree, InputFileBuilder inputFileBuilder, ModuleFileSystemInitializer initializer, DefaultModuleFileSystem defaultModuleFileSystem, + public FileIndexer(ScannerComponentIdGenerator scannerComponentIdGenerator, InputComponentStore componentStore, DefaultInputModule module, ExclusionFilters exclusionFilters, + DefaultComponentTree componentTree, InputFileBuilder inputFileBuilder, DefaultModuleFileSystem defaultModuleFileSystem, LanguageDetection languageDetection) { - this(scannerComponentIdGenerator, componentStore, module, exclusionFilters, componentTree, inputFileBuilder, initializer, defaultModuleFileSystem, languageDetection, + this(scannerComponentIdGenerator, componentStore, module, exclusionFilters, componentTree, inputFileBuilder, defaultModuleFileSystem, languageDetection, new InputFileFilter[0]); } @@ -112,8 +109,8 @@ public class FileIndexer { Progress progress = new Progress(); - indexFiles(moduleFileSystemInitializer.sources(), InputFile.Type.MAIN, progress); - indexFiles(moduleFileSystemInitializer.tests(), InputFile.Type.TEST, progress); + indexFiles(module.getSourceDirsOrFiles(), InputFile.Type.MAIN, progress); + indexFiles(module.getTestDirsOrFiles(), InputFile.Type.TEST, progress); waitForTasksToComplete(progressReport); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputFileBuilder.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputFileBuilder.java index 8d14471b65c..4de7d1bf07e 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputFileBuilder.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputFileBuilder.java @@ -22,43 +22,38 @@ package org.sonar.scanner.scan.filesystem; import java.nio.file.Path; import javax.annotation.Nullable; import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.InputModule; import org.sonar.api.batch.fs.internal.DefaultIndexedFile; import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.fs.internal.AbstractProjectOrModule; -import org.sonar.api.batch.fs.internal.InputModuleHierarchy; +import org.sonar.api.batch.fs.internal.DefaultInputModule; +import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.api.batch.fs.internal.SensorStrategy; import org.sonar.scanner.scan.ScanProperties; public class InputFileBuilder { - private final String moduleKey; - private final Path moduleBaseDir; + private final DefaultInputModule module; private final ScannerComponentIdGenerator idGenerator; private final MetadataGenerator metadataGenerator; private final boolean preloadMetadata; - private final ModuleFileSystemInitializer moduleFileSystemInitializer; private final Path projectBaseDir; private final SensorStrategy sensorStrategy; - public InputFileBuilder(InputModule module, MetadataGenerator metadataGenerator, - ScannerComponentIdGenerator idGenerator, ScanProperties properties, ModuleFileSystemInitializer moduleFileSystemInitializer, InputModuleHierarchy hierarchy, + public InputFileBuilder(DefaultInputProject project, DefaultInputModule module, MetadataGenerator metadataGenerator, + ScannerComponentIdGenerator idGenerator, ScanProperties properties, SensorStrategy sensorStrategy) { this.sensorStrategy = sensorStrategy; - this.projectBaseDir = hierarchy.root().getBaseDir(); - this.moduleFileSystemInitializer = moduleFileSystemInitializer; - this.moduleKey = module.key(); - this.moduleBaseDir = ((AbstractProjectOrModule) module).getBaseDir(); + this.projectBaseDir = project.getBaseDir(); + this.module = module; this.metadataGenerator = metadataGenerator; this.idGenerator = idGenerator; this.preloadMetadata = properties.preloadFileMetadata(); } DefaultInputFile create(InputFile.Type type, Path absolutePath, @Nullable String language) { - DefaultIndexedFile indexedFile = new DefaultIndexedFile(absolutePath, moduleKey, + DefaultIndexedFile indexedFile = new DefaultIndexedFile(absolutePath, module.key(), projectBaseDir.relativize(absolutePath).toString(), - moduleBaseDir.relativize(absolutePath).toString(), + module.getBaseDir().relativize(absolutePath).toString(), type, language, idGenerator.getAsInt(), sensorStrategy); - DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(f, moduleFileSystemInitializer.defaultEncoding())); + DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(f, module.getEncoding())); if (language != null) { inputFile.setPublished(true); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/LanguageDetection.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/LanguageDetection.java index 0b3dfc68e88..1b74abd4aa9 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/LanguageDetection.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/LanguageDetection.java @@ -33,7 +33,6 @@ import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; -import org.sonar.api.batch.ScannerSide; import org.sonar.api.batch.fs.internal.PathPattern; import org.sonar.api.config.Configuration; import org.sonar.api.utils.MessageException; @@ -43,7 +42,6 @@ import org.sonar.scanner.repository.language.LanguagesRepository; /** * Detect language of a source file based on its suffix and configured patterns. */ -@ScannerSide @ThreadSafe public class LanguageDetection { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleFileSystemInitializer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleFileSystemInitializer.java deleted file mode 100644 index 8c32cd62322..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleFileSystemInitializer.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.scanner.scan.filesystem; - -import java.io.File; -import java.nio.charset.Charset; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import java.util.Optional; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.ScannerSide; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.fs.InputModule; -import org.sonar.api.batch.fs.internal.AbstractProjectOrModule; -import org.sonar.api.scan.filesystem.PathResolver; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; - -import static org.sonar.core.config.MultivalueProperty.parseAsCsv; - -@ScannerSide -@Immutable -public class ModuleFileSystemInitializer { - - private static final Logger LOG = Loggers.get(ModuleFileSystemInitializer.class); - - private final List<Path> sourceDirsOrFiles; - private final List<Path> testDirsOrFiles; - private final Charset encoding; - - public ModuleFileSystemInitializer(InputModule inputModule) { - AbstractProjectOrModule inputModuleCasted = (AbstractProjectOrModule) inputModule; - logDir("Base dir: ", inputModuleCasted.getBaseDir()); - logDir("Working dir: ", inputModuleCasted.getWorkDir()); - sourceDirsOrFiles = initSources(inputModuleCasted, ProjectDefinition.SOURCES_PROPERTY, "Source paths: "); - testDirsOrFiles = initSources(inputModuleCasted, ProjectDefinition.TESTS_PROPERTY, "Test paths: "); - encoding = initEncoding(inputModuleCasted); - } - - private static List<Path> initSources(AbstractProjectOrModule module, String propertyKey, String logLabel) { - List<Path> result = new ArrayList<>(); - PathResolver pathResolver = new PathResolver(); - String srcPropValue = module.properties().get(propertyKey); - if (srcPropValue != null) { - for (String sourcePath : parseAsCsv(propertyKey, srcPropValue)) { - File dirOrFile = pathResolver.relativeFile(module.getBaseDir().toFile(), sourcePath); - if (dirOrFile.exists()) { - result.add(dirOrFile.toPath()); - } - } - } - logPaths(logLabel, module.getBaseDir(), result); - return result; - } - - private static Charset initEncoding(AbstractProjectOrModule module) { - String encodingStr = module.properties().get(CoreProperties.ENCODING_PROPERTY); - Charset result; - if (StringUtils.isNotEmpty(encodingStr)) { - result = Charset.forName(StringUtils.trim(encodingStr)); - LOG.info("Source encoding: {}, default locale: {}", result.displayName(), Locale.getDefault()); - } else { - result = Charset.defaultCharset(); - LOG.warn("Source encoding is platform dependent ({}), default locale: {}", result.displayName(), Locale.getDefault()); - } - return result; - } - - List<Path> sources() { - return sourceDirsOrFiles; - } - - List<Path> tests() { - return testDirsOrFiles; - } - - public Charset defaultEncoding() { - return encoding; - } - - private static void logPaths(String label, Path baseDir, List<Path> paths) { - if (!paths.isEmpty()) { - StringBuilder sb = new StringBuilder(label); - for (Iterator<Path> it = paths.iterator(); it.hasNext(); ) { - Path file = it.next(); - Optional<String> relativePathToBaseDir = PathResolver.relativize(baseDir, file); - if (!relativePathToBaseDir.isPresent()) { - sb.append(file); - } else if (StringUtils.isBlank(relativePathToBaseDir.get())) { - sb.append("."); - } else { - sb.append(relativePathToBaseDir.get()); - } - if (it.hasNext()) { - sb.append(", "); - } - } - LOG.info(sb.toString()); - } - } - - private static void logDir(String label, @Nullable Path dir) { - if (dir != null) { - LOG.info(label + dir.toAbsolutePath().toString()); - } - } - -} |