001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.batch;
021
022 import com.google.common.collect.ImmutableList;
023 import com.google.common.collect.Iterables;
024 import org.apache.commons.io.FileUtils;
025 import org.apache.maven.project.MavenProject;
026 import org.sonar.api.batch.FileFilter;
027 import org.sonar.api.batch.bootstrap.ProjectDefinition;
028 import org.sonar.api.resources.DefaultProjectFileSystem;
029 import org.sonar.api.resources.Languages;
030 import org.sonar.api.resources.Project;
031 import org.sonar.api.utils.SonarException;
032
033 import java.io.File;
034 import java.io.IOException;
035 import java.util.List;
036
037 /**
038 * Implementation of {@link org.sonar.api.resources.ProjectFileSystem} based on {@link ProjectDefinition} and {@link MavenProject}.
039 */
040 public class DefaultProjectFileSystem2 extends DefaultProjectFileSystem {
041
042 private ProjectDefinition def;
043 private MavenProject pom;
044
045 public DefaultProjectFileSystem2(Project project, Languages languages, ProjectDefinition def, FileFilter[] fileFilters) {
046 super(project, languages, fileFilters);
047 this.def = def;
048 }
049
050 /**
051 * For Maven.
052 */
053 public DefaultProjectFileSystem2(Project project, Languages languages, ProjectDefinition def, MavenProject pom, FileFilter[] fileFilters) {
054 this(project, languages, def, fileFilters);
055 this.pom = pom;
056 }
057
058 public DefaultProjectFileSystem2(Project project, Languages languages, ProjectDefinition def) {
059 this(project, languages, def, new FileFilter[0]);
060 }
061
062 /**
063 * For Maven.
064 */
065 public DefaultProjectFileSystem2(Project project, Languages languages, ProjectDefinition def, MavenProject pom) {
066 this(project, languages, def, pom, new FileFilter[0]);
067 }
068
069 public File getBasedir() {
070 return def.getBaseDir();
071 }
072
073 public File getBuildDir() {
074 if (pom != null) {
075 return resolvePath(pom.getBuild().getDirectory());
076 } else {
077 // TODO workaround
078 return new File(getSonarWorkingDirectory(), "target");
079 }
080 }
081
082 public File getBuildOutputDir() {
083 if (pom != null) {
084 return resolvePath(pom.getBuild().getOutputDirectory());
085 } else {
086 if (def.getBinaries().isEmpty()) {
087 // workaround for IndexOutOfBoundsException
088 return new File(getBuildDir(), "classes");
089 }
090 // TODO we assume that there is only one directory which contains compiled code
091 return resolvePath(def.getBinaries().get(0));
092 }
093 }
094
095 @Override
096 public List<File> getSourceDirs() {
097 List<File> unfiltered = resolvePaths(def.getSourceDirs());
098 return ImmutableList.copyOf(Iterables.filter(unfiltered, DIRECTORY_EXISTS));
099 }
100
101 /**
102 * @deprecated since 2.6, because should be immutable
103 */
104 @Deprecated
105 public DefaultProjectFileSystem addSourceDir(File dir) {
106 if (dir == null) {
107 throw new IllegalArgumentException("Can not add null to project source dirs");
108 }
109 if (pom != null) {
110 pom.getCompileSourceRoots().add(0, dir.getAbsolutePath());
111 }
112 def.addSourceDirs(dir.getAbsolutePath());
113 return this;
114 }
115
116 /**
117 * Maven can modify test directories during Sonar execution - see MavenPhaseExecutor.
118 */
119 @Override
120 public List<File> getTestDirs() {
121 List<File> unfiltered = resolvePaths(def.getTestDirs());
122 return ImmutableList.copyOf(Iterables.filter(unfiltered, DIRECTORY_EXISTS));
123 }
124
125 /**
126 * @deprecated since 2.6, because should be immutable
127 */
128 @Deprecated
129 public DefaultProjectFileSystem addTestDir(File dir) {
130 if (dir == null) {
131 throw new IllegalArgumentException("Can not add null to project test dirs");
132 }
133 if (pom != null) {
134 pom.getTestCompileSourceRoots().add(0, dir.getAbsolutePath());
135 }
136 def.addTestDirs(dir.getAbsolutePath());
137 return this;
138 }
139
140 /**
141 * TODO Godin: seems that used only by Cobertura and Clover
142 */
143 public File getReportOutputDir() {
144 if (pom != null) {
145 return resolvePath(pom.getReporting().getOutputDirectory());
146 } else {
147 return new File(getBuildDir(), "site");
148 }
149 }
150
151 @Override
152 public File getSonarWorkingDirectory() {
153 try {
154 FileUtils.forceMkdir(def.getWorkDir());
155 return def.getWorkDir();
156 } catch (IOException e) {
157 throw new SonarException("Unable to retrieve Sonar working directory.", e);
158 }
159 }
160
161 @Override
162 protected List<File> getInitialSourceFiles() {
163 return resolvePaths(def.getSourceFiles());
164 }
165
166 @Override
167 protected List<File> getInitialTestFiles() {
168 return resolvePaths(def.getTestFiles());
169 }
170 }