Skip to content

Commit

Permalink
Read all main and test sources from project (#149)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people committed Jul 1, 2021
1 parent 12812d5 commit 4e4665d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 19 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
<artifactId>scala-library</artifactId>
<version>${version.scala.major}${version.scala.minor}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${version.maven}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Expand Down Expand Up @@ -126,11 +131,6 @@
<version>${version.scalatest}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.scala-lang.modules</groupId>
<artifactId>scala-xml_${version.scala.major}</artifactId>
Expand Down
74 changes: 60 additions & 14 deletions src/main/java/org/antipathy/mvn_scalafmt/FormatMojo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.antipathy.mvn_scalafmt;

import org.antipathy.mvn_scalafmt.model.Summary;
import org.apache.maven.model.Build;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand All @@ -10,7 +11,9 @@
import org.apache.maven.model.Repository;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
Expand All @@ -25,9 +28,9 @@ public class FormatMojo extends AbstractMojo {
private boolean skipTestSources;
@Parameter(property = "format.skipSources", defaultValue = "false")
private boolean skipSources;
@Parameter(defaultValue = "${project.build.sourceDirectory}/../scala", required = true)
@Parameter()
private List<File> sourceDirectories;
@Parameter(defaultValue = "${project.build.testSourceDirectory}/../scala", required = true)
@Parameter()
private List<File> testSourceDirectories;
@Parameter(property = "format.respectVersion", defaultValue = "false", required = true)
private boolean respectVersion;
Expand Down Expand Up @@ -62,19 +65,13 @@ private List<String> getRepositoriesUrls(List<Repository> repositories) {

public void execute() throws MojoExecutionException {

List<File> sources = new ArrayList<>();

if (!skipSources) {
sources.addAll(sourceDirectories);
} else {
getLog().warn("format.skipSources set, ignoring main directories");
List<File> sources;
try {
sources = getSources();
} catch (IOException exception) {
throw new MojoExecutionException("Couldn't determine canonical sources", exception);
}

if (!skipTestSources) {
sources.addAll(testSourceDirectories);
} else {
getLog().warn("format.skipTestSources set, ignoring validateOnly directories");
}
if (!sources.isEmpty()) {
try {

Expand All @@ -87,7 +84,9 @@ public void execute() throws MojoExecutionException {
showReformattedOnly,
branch,
project.getBasedir(),
useSpecifiedRepositories ? getRepositoriesUrls(mavenRepositories) : new ArrayList<String>()
useSpecifiedRepositories ?
getRepositoriesUrls(project.getRepositories()) :
new ArrayList<String>()
).format(sources);
getLog().info(result.toString());
if (validateOnly && result.unformattedFiles() != 0) {
Expand All @@ -101,4 +100,51 @@ public void execute() throws MojoExecutionException {
getLog().warn("No sources specified, skipping formatting");
}
}

private List<File> getSources() throws IOException {
HashSet<File> sources = new HashSet<>();
Build build = project.getBuild();

if (skipSources) {
getLog().warn("format.skipSources set, ignoring main directories");
} else if (sourceDirectories == null || sourceDirectories.isEmpty()) {
appendCanonicalSources(
sources,
project.getCompileSourceRoots(),
build.getSourceDirectory()
);
} else {
sources.addAll(sourceDirectories);
}

if (skipTestSources) {
getLog().warn("format.skipTestSources set, ignoring validateOnly directories");
} else if (testSourceDirectories == null || testSourceDirectories.isEmpty()) {
appendCanonicalSources(
sources,
project.getTestCompileSourceRoots(),
build.getTestSourceDirectory()
);
} else {
sources.addAll(testSourceDirectories);
}

return new ArrayList<>(sources);
}

private void appendCanonicalSources(
HashSet<File> sources,
List<String> sourceRoots,
String defaultSource
) throws IOException {
for (String source : sourceRoots) {
sources.add(getCanonicalFile(source));
}
sources.add(getCanonicalFile(defaultSource + "/../scala"));
}

private File getCanonicalFile(String relative) throws IOException {
return new File(project.getBasedir(), relative).getCanonicalFile();
}

}

0 comments on commit 4e4665d

Please sign in to comment.