1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* mailto:contact AT sonarsource DOT com
*
* Sonar 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.
*
* Sonar 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 Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.batch;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import org.apache.commons.io.FileUtils;
import org.apache.maven.project.MavenProject;
import org.sonar.api.batch.FileFilter;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.resources.DefaultProjectFileSystem;
import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.SonarException;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* Implementation of {@link org.sonar.api.resources.ProjectFileSystem} based on {@link ProjectDefinition} and {@link MavenProject}.
*/
public class DefaultProjectFileSystem2 extends DefaultProjectFileSystem {
private ProjectDefinition def;
private MavenProject pom;
public DefaultProjectFileSystem2(Project project, Languages languages, ProjectDefinition def, FileFilter[] fileFilters) {
super(project, languages, fileFilters);
this.def = def;
}
/**
* For Maven.
*/
public DefaultProjectFileSystem2(Project project, Languages languages, ProjectDefinition def, MavenProject pom, FileFilter[] fileFilters) {
this(project, languages, def, fileFilters);
this.pom = pom;
}
public DefaultProjectFileSystem2(Project project, Languages languages, ProjectDefinition def) {
this(project, languages, def, new FileFilter[0]);
}
/**
* For Maven.
*/
public DefaultProjectFileSystem2(Project project, Languages languages, ProjectDefinition def, MavenProject pom) {
this(project, languages, def, pom, new FileFilter[0]);
}
@Override
public File getBasedir() {
return def.getBaseDir();
}
@Override
public File getBuildDir() {
if (pom != null) {
return resolvePath(pom.getBuild().getDirectory());
} else {
// TODO workaround
return new File(getSonarWorkingDirectory(), "target");
}
}
@Override
public File getBuildOutputDir() {
if (pom != null) {
return resolvePath(pom.getBuild().getOutputDirectory());
} else {
if (def.getBinaries().isEmpty()) {
// workaround for IndexOutOfBoundsException
return new File(getBuildDir(), "classes");
}
// TODO we assume that there is only one directory which contains compiled code
return resolvePath(def.getBinaries().get(0));
}
}
@Override
public List<File> getSourceDirs() {
List<File> unfiltered = resolvePaths(def.getSourceDirs());
return ImmutableList.copyOf(Iterables.filter(unfiltered, DIRECTORY_EXISTS));
}
/**
* @deprecated since 2.6, because should be immutable
*/
@Override
@Deprecated
public DefaultProjectFileSystem addSourceDir(File dir) {
if (dir == null) {
throw new IllegalArgumentException("Can not add null to project source dirs");
}
if (pom != null) {
pom.getCompileSourceRoots().add(0, dir.getAbsolutePath());
}
def.addSourceDirs(dir.getAbsolutePath());
return this;
}
/**
* Maven can modify test directories during Sonar execution - see MavenPhaseExecutor.
*/
@Override
public List<File> getTestDirs() {
List<File> unfiltered = resolvePaths(def.getTestDirs());
return ImmutableList.copyOf(Iterables.filter(unfiltered, DIRECTORY_EXISTS));
}
/**
* @deprecated since 2.6, because should be immutable
*/
@Override
@Deprecated
public DefaultProjectFileSystem addTestDir(File dir) {
if (dir == null) {
throw new IllegalArgumentException("Can not add null to project test dirs");
}
if (pom != null) {
pom.getTestCompileSourceRoots().add(0, dir.getAbsolutePath());
}
def.addTestDirs(dir.getAbsolutePath());
return this;
}
/**
* TODO Godin: seems that used only by Cobertura and Clover
*/
@Override
public File getReportOutputDir() {
if (pom != null) {
return resolvePath(pom.getReporting().getOutputDirectory());
} else {
return new File(getBuildDir(), "site");
}
}
@Override
public File getSonarWorkingDirectory() {
try {
FileUtils.forceMkdir(def.getWorkDir());
return def.getWorkDir();
} catch (IOException e) {
throw new SonarException("Unable to retrieve Sonar working directory.", e);
}
}
@Override
protected List<File> getInitialSourceFiles() {
return resolvePaths(def.getSourceFiles());
}
@Override
protected List<File> getInitialTestFiles() {
return resolvePaths(def.getTestFiles());
}
// Hack to instantiate DefaultProjectFileSystem2 before executing Phases.
// TODO all the project bootstrapping must be refactored. It's overcomplicated.
public void start() {
}
}
|