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.util.ArrayList;
24 import java.util.List;
25 import org.apache.commons.io.FileUtils;
26 import org.sonar.api.batch.ScannerSide;
27 import org.sonar.api.batch.bootstrap.ProjectDefinition;
28 import org.sonar.api.scan.filesystem.PathResolver;
29 import org.sonar.api.utils.TempFolder;
31 import static org.sonar.scanner.config.DefaultConfiguration.parseAsCsv;
37 public class ModuleFileSystemInitializer {
40 private File workingDir;
41 private List<File> sourceDirsOrFiles = new ArrayList<>();
42 private List<File> testDirsOrFiles = new ArrayList<>();
44 public ModuleFileSystemInitializer(ProjectDefinition module, TempFolder tempUtils, PathResolver pathResolver) {
45 baseDir = module.getBaseDir();
46 initWorkingDir(module, tempUtils);
47 initSources(module, pathResolver);
48 initTests(module, pathResolver);
51 private void initWorkingDir(ProjectDefinition module, TempFolder tempUtils) {
52 workingDir = module.getWorkDir();
53 if (workingDir == null) {
54 workingDir = tempUtils.newDir("work");
57 FileUtils.forceMkdir(workingDir);
58 } catch (Exception e) {
59 throw new IllegalStateException("Fail to create working dir: " + workingDir.getAbsolutePath(), e);
64 private void initSources(ProjectDefinition module, PathResolver pathResolver) {
65 String srcPropValue = module.properties().get(ProjectDefinition.SOURCES_PROPERTY);
66 if (srcPropValue != null) {
67 for (String sourcePath : parseAsCsv(ProjectDefinition.SOURCES_PROPERTY, srcPropValue)) {
68 File dirOrFile = pathResolver.relativeFile(module.getBaseDir(), sourcePath);
69 if (dirOrFile.exists()) {
70 sourceDirsOrFiles.add(dirOrFile);
76 private void initTests(ProjectDefinition module, PathResolver pathResolver) {
77 String testPropValue = module.properties().get(ProjectDefinition.TESTS_PROPERTY);
78 if (testPropValue != null) {
79 for (String testPath : parseAsCsv(ProjectDefinition.TESTS_PROPERTY, testPropValue)) {
80 File dirOrFile = pathResolver.relativeFile(module.getBaseDir(), testPath);
81 if (dirOrFile.exists()) {
82 testDirsOrFiles.add(dirOrFile);
96 List<File> sources() {
97 return sourceDirsOrFiles;
101 return testDirsOrFiles;