001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.scan.filesystem;
021    
022    import com.google.common.collect.Lists;
023    import org.apache.commons.io.FileUtils;
024    import org.sonar.api.BatchComponent;
025    import org.sonar.api.batch.bootstrap.ProjectDefinition;
026    import org.sonar.api.scan.filesystem.PathResolver;
027    import org.sonar.api.utils.TempFolder;
028    
029    import java.io.File;
030    import java.util.List;
031    
032    /**
033     * @since 3.5
034     */
035    public class ModuleFileSystemInitializer implements BatchComponent {
036    
037      private File baseDir, workingDir, buildDir;
038      private List<File> sourceDirsOrFiles = Lists.newArrayList();
039      private List<File> testDirsOrFiles = Lists.newArrayList();
040      private List<File> binaryDirs = Lists.newArrayList();
041    
042      public ModuleFileSystemInitializer(ProjectDefinition module, TempFolder tempUtils, PathResolver pathResolver) {
043        baseDir = module.getBaseDir();
044        buildDir = module.getBuildDir();
045        initWorkingDir(module, tempUtils);
046        initBinaryDirs(module, pathResolver);
047        initSources(module, pathResolver);
048        initTests(module, pathResolver);
049      }
050    
051      private void initWorkingDir(ProjectDefinition module, TempFolder tempUtils) {
052        workingDir = module.getWorkDir();
053        if (workingDir == null) {
054          workingDir = tempUtils.newDir("work");
055        } else {
056          try {
057            FileUtils.forceMkdir(workingDir);
058          } catch (Exception e) {
059            throw new IllegalStateException("Fail to create working dir: " + workingDir.getAbsolutePath(), e);
060          }
061        }
062      }
063    
064      private void initSources(ProjectDefinition module, PathResolver pathResolver) {
065        for (String sourcePath : module.sources()) {
066          File dirOrFile = pathResolver.relativeFile(module.getBaseDir(), sourcePath);
067          if (dirOrFile.exists()) {
068            sourceDirsOrFiles.add(dirOrFile);
069          }
070        }
071      }
072    
073      private void initTests(ProjectDefinition module, PathResolver pathResolver) {
074        for (String testPath : module.tests()) {
075          File dirOrFile = pathResolver.relativeFile(module.getBaseDir(), testPath);
076          if (dirOrFile.exists()) {
077            testDirsOrFiles.add(dirOrFile);
078          }
079        }
080      }
081    
082      private void initBinaryDirs(ProjectDefinition module, PathResolver pathResolver) {
083        for (String path : module.getBinaries()) {
084          File dir = pathResolver.relativeFile(module.getBaseDir(), path);
085          binaryDirs.add(dir);
086        }
087      }
088    
089      File baseDir() {
090        return baseDir;
091      }
092    
093      File workingDir() {
094        return workingDir;
095      }
096    
097      File buildDir() {
098        return buildDir;
099      }
100    
101      List<File> sources() {
102        return sourceDirsOrFiles;
103      }
104    
105      List<File> tests() {
106        return testDirsOrFiles;
107      }
108    
109      /**
110       * @deprecated since 4.5.1 use SonarQube Java specific API
111       */
112      @Deprecated
113      List<File> binaryDirs() {
114        return binaryDirs;
115      }
116    }