Skip to content

Commit 4e4665d

Browse files
kitbellewAlbert MeltzerSimonJPegg
authored
Read all main and test sources from project (#149)
* Refactor getting sources into separate method * Read default main and test sources from project * Read all main and test sources from project Co-authored-by: Albert Meltzer <[email protected]> Co-authored-by: Ciaran Kearney <[email protected]>
1 parent 12812d5 commit 4e4665d

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
<artifactId>scala-library</artifactId>
8787
<version>${version.scala.major}${version.scala.minor}</version>
8888
</dependency>
89+
<dependency>
90+
<groupId>org.apache.maven</groupId>
91+
<artifactId>maven-core</artifactId>
92+
<version>${version.maven}</version>
93+
</dependency>
8994
<dependency>
9095
<groupId>org.apache.maven</groupId>
9196
<artifactId>maven-plugin-api</artifactId>
@@ -126,11 +131,6 @@
126131
<version>${version.scalatest}</version>
127132
<scope>test</scope>
128133
</dependency>
129-
<dependency>
130-
<groupId>org.apache.maven</groupId>
131-
<artifactId>maven-project</artifactId>
132-
<version>2.2.1</version>
133-
</dependency>
134134
<dependency>
135135
<groupId>org.scala-lang.modules</groupId>
136136
<artifactId>scala-xml_${version.scala.major}</artifactId>

src/main/java/org/antipathy/mvn_scalafmt/FormatMojo.java

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.antipathy.mvn_scalafmt;
22

33
import org.antipathy.mvn_scalafmt.model.Summary;
4+
import org.apache.maven.model.Build;
45
import org.apache.maven.plugin.AbstractMojo;
56
import org.apache.maven.plugin.MojoExecutionException;
67
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -10,7 +11,9 @@
1011
import org.apache.maven.model.Repository;
1112

1213
import java.io.File;
14+
import java.io.IOException;
1315
import java.util.ArrayList;
16+
import java.util.HashSet;
1417
import java.util.List;
1518

1619
/**
@@ -25,9 +28,9 @@ public class FormatMojo extends AbstractMojo {
2528
private boolean skipTestSources;
2629
@Parameter(property = "format.skipSources", defaultValue = "false")
2730
private boolean skipSources;
28-
@Parameter(defaultValue = "${project.build.sourceDirectory}/../scala", required = true)
31+
@Parameter()
2932
private List<File> sourceDirectories;
30-
@Parameter(defaultValue = "${project.build.testSourceDirectory}/../scala", required = true)
33+
@Parameter()
3134
private List<File> testSourceDirectories;
3235
@Parameter(property = "format.respectVersion", defaultValue = "false", required = true)
3336
private boolean respectVersion;
@@ -62,19 +65,13 @@ private List<String> getRepositoriesUrls(List<Repository> repositories) {
6265

6366
public void execute() throws MojoExecutionException {
6467

65-
List<File> sources = new ArrayList<>();
66-
67-
if (!skipSources) {
68-
sources.addAll(sourceDirectories);
69-
} else {
70-
getLog().warn("format.skipSources set, ignoring main directories");
68+
List<File> sources;
69+
try {
70+
sources = getSources();
71+
} catch (IOException exception) {
72+
throw new MojoExecutionException("Couldn't determine canonical sources", exception);
7173
}
7274

73-
if (!skipTestSources) {
74-
sources.addAll(testSourceDirectories);
75-
} else {
76-
getLog().warn("format.skipTestSources set, ignoring validateOnly directories");
77-
}
7875
if (!sources.isEmpty()) {
7976
try {
8077

@@ -87,7 +84,9 @@ public void execute() throws MojoExecutionException {
8784
showReformattedOnly,
8885
branch,
8986
project.getBasedir(),
90-
useSpecifiedRepositories ? getRepositoriesUrls(mavenRepositories) : new ArrayList<String>()
87+
useSpecifiedRepositories ?
88+
getRepositoriesUrls(project.getRepositories()) :
89+
new ArrayList<String>()
9190
).format(sources);
9291
getLog().info(result.toString());
9392
if (validateOnly && result.unformattedFiles() != 0) {
@@ -101,4 +100,51 @@ public void execute() throws MojoExecutionException {
101100
getLog().warn("No sources specified, skipping formatting");
102101
}
103102
}
103+
104+
private List<File> getSources() throws IOException {
105+
HashSet<File> sources = new HashSet<>();
106+
Build build = project.getBuild();
107+
108+
if (skipSources) {
109+
getLog().warn("format.skipSources set, ignoring main directories");
110+
} else if (sourceDirectories == null || sourceDirectories.isEmpty()) {
111+
appendCanonicalSources(
112+
sources,
113+
project.getCompileSourceRoots(),
114+
build.getSourceDirectory()
115+
);
116+
} else {
117+
sources.addAll(sourceDirectories);
118+
}
119+
120+
if (skipTestSources) {
121+
getLog().warn("format.skipTestSources set, ignoring validateOnly directories");
122+
} else if (testSourceDirectories == null || testSourceDirectories.isEmpty()) {
123+
appendCanonicalSources(
124+
sources,
125+
project.getTestCompileSourceRoots(),
126+
build.getTestSourceDirectory()
127+
);
128+
} else {
129+
sources.addAll(testSourceDirectories);
130+
}
131+
132+
return new ArrayList<>(sources);
133+
}
134+
135+
private void appendCanonicalSources(
136+
HashSet<File> sources,
137+
List<String> sourceRoots,
138+
String defaultSource
139+
) throws IOException {
140+
for (String source : sourceRoots) {
141+
sources.add(getCanonicalFile(source));
142+
}
143+
sources.add(getCanonicalFile(defaultSource + "/../scala"));
144+
}
145+
146+
private File getCanonicalFile(String relative) throws IOException {
147+
return new File(project.getBasedir(), relative).getCanonicalFile();
148+
}
149+
104150
}

0 commit comments

Comments
 (0)