Skip to content

Commit

Permalink
Use /scala as source directory by default as scala-maven-plugin does (#…
Browse files Browse the repository at this point in the history
…35)

* Use /scala as source directory by default as scala-maven-plugin does

* Build proper path when there is no java dir, so we can't just do ../

In such case we have to build canonical path for `java/../scala` path,
which is `scala`.

* Improve type safety in passing source files

* Simplify Java code: move canonical paths cast to ScalaFormatter

* Remove redundant logging

* Document using /scala as source directory by default

* Up artifact version

* Remove unused imports
  • Loading branch information
evis authored and SimonJPegg committed Aug 4, 2019
1 parent 791143c commit 9be3bc3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ passed through to the CLI as is.
<skipmain>false</skipmain> <!-- (Optional) Skip formatting main sources -->
<configLocation>${project.basedir}/path/to/scalafmt.conf</configLocation> <!-- (Optional) config location -->
<configRequired>false</configRequired><!-- (Optional) raise an error if configLocation is missing or invalid (otherwise use Scalafmt defaults) -->
<sourceDirectories> <!-- (Optional) Paths to source-directories. Overrides ${project.build.sourceDirectory} -->
<sourceDirectories> <!-- (Optional) Paths to source-directories. Overrides ${project.build.sourceDirectory}/../scala -->
<param>${project.basedir}/src/main/scala</param>
</sourceDirectories>
<testSourceDirectories> <!-- (Optional) Paths to test-source-directories. Overrides ${project.build.testSourceDirectory} -->
<testSourceDirectories> <!-- (Optional) Paths to test-source-directories. Overrides ${project.build.testSourceDirectory}/../scala -->
<param>${project.basedir}/src/test/scala</param>
</testSourceDirectories>
</configuration>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>org.antipathy</groupId>
<artifactId>mvn-scalafmt_${version.scala.binary}</artifactId>
<packaging>maven-plugin</packaging>
<version>0.11_${version.scalafmt}</version>
<version>0.12_${version.scalafmt}</version>
<name>ScalaFmt Maven Plugin (${version.scala.binary})</name>
<description>Maven plugin for ScalaFmt</description>
<url>https://github.com/SimonJPegg/mvn_scalafmt</url>
Expand Down
22 changes: 5 additions & 17 deletions src/main/java/org/antipathy/mvn_scalafmt/FormatMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -26,32 +26,20 @@ public class FormatMojo extends AbstractMojo {
private boolean skipTestSources;
@Parameter(property = "format.skipmain", defaultValue = "false")
private boolean skipSources;
@Parameter(defaultValue = "${project.build.sourceDirectory}", required = true)
@Parameter(defaultValue = "${project.build.sourceDirectory}/../scala", required = true)
private List<File> sourceDirectories;
@Parameter(defaultValue = "${project.build.testSourceDirectory}", required = true)
@Parameter(defaultValue = "${project.build.testSourceDirectory}/../scala", required = true)
private List<File> testSourceDirectories;

public void execute() throws MojoExecutionException {

ArrayList<Object> sources = new ArrayList<>();
ArrayList<Object> testSources = new ArrayList<>();

if (!skipSources) {
sources.addAll(sourceDirectories);
}

if (!skipTestSources) {
testSources.addAll(testSourceDirectories);
}

if(!skip) {
try {
ScalaFormatter.format(
configLocation,
configRequired,
parameters,
sources,
testSources,
skipSources ? Collections.emptyList() : sourceDirectories,
skipTestSources ? Collections.emptyList() : testSourceDirectories,
getLog());
} catch (Exception e) {
throw new MojoExecutionException("Error formatting Scala files", e);
Expand Down
21 changes: 14 additions & 7 deletions src/main/scala/org/antipathy/mvn_scalafmt/ScalaFormatter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.scalafmt.cli.{Cli, CliOptions}
import org.apache.maven.plugin.logging.Log
import org.scalafmt.util.AbsoluteFile
import scala.collection.JavaConverters._
import java.io.File
import java.nio.file.{Files, Paths}
import java.util.{List => JList}

Expand All @@ -26,20 +27,19 @@ object ScalaFormatter {
configLocation: String,
configRequired: Boolean,
parameters: String,
sourceRoots: JList[Any],
testSourceRoots: JList[Any],
sourceRoots: JList[File],
testSourceRoots: JList[File],
log: Log
): Unit = {

val config = parseConfigLocation(configLocation, configRequired, log)
val params = parseParametersString(parameters, log)

val sources: Seq[String] = getSourcePaths(sourceRoots.asScala) ++
getSourcePaths(testSourceRoots.asScala)
val sources: Seq[String] = getSourcePaths(sourceRoots.asScala, log) ++
getSourcePaths(testSourceRoots.asScala, log)

if (sources.nonEmpty) {
val cliOptions = getCLiOptions(sources, config, params)
log.info(sources.toString())
log.info(s"Formatting ${sources.mkString(",")}")
Cli.run(cliOptions)
} else {
Expand Down Expand Up @@ -72,11 +72,18 @@ object ScalaFormatter {
* @param paths the paths to filter
* @return a sequence of valid paths
*/
private[mvn_scalafmt] def getSourcePaths(paths: Seq[Any]): Seq[String] =
private[mvn_scalafmt] def getSourcePaths(paths: Seq[File], log: Log): Seq[String] =
if (paths == null) {
Seq[String]()
} else {
paths.map(_.toString).filter(p => Files.exists(Paths.get(p)))
paths.map(_.getCanonicalPath).flatMap { p =>
if (Files.exists(Paths.get(p))) {
Some(p)
} else {
log.error(s"Could not locate Scala source at $p")
None
}
}
}

/** Parses the config string and returns a sequence used for formatting
Expand Down
13 changes: 10 additions & 3 deletions src/test/scala/org/antipathy/mvn_scalafmt/ScalaFormatterSpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.antipathy.mvn_scalafmt

import java.io.File

import org.scalatest.{FlatSpec, GivenWhenThen, Matchers}
import org.apache.maven.plugin.logging.SystemStreamLog

Expand Down Expand Up @@ -58,16 +60,21 @@ class ScalaFormatterSpec extends FlatSpec with GivenWhenThen with Matchers {
}

it should "Create a sequence of valid source paths" in {
val sources = Seq(new File("src/main/scala"), new File("src/test/scala"))
val expectedResult = Seq("src/main/scala", "src/test/scala")
ScalaFormatter.getSourcePaths(expectedResult) should be(expectedResult)
canonical(ScalaFormatter.getSourcePaths(sources, new SystemStreamLog)) should be(canonical(expectedResult))
}

it should "Create an empty sequence when given invalid paths" in {
ScalaFormatter.getSourcePaths(Seq("src/main1/scala", "src/test1/scala")) should be(Seq())
ScalaFormatter.getSourcePaths(Seq(new File("src/main1/scala"), new File("src/test1/scala")), new SystemStreamLog) should be(
Seq())
}

it should "Create an empty sequence when given null values" in {
ScalaFormatter.getSourcePaths(null) should be(Seq())
ScalaFormatter.getSourcePaths(null, new SystemStreamLog) should be(Seq())
}

private def canonical(paths: Seq[String]) =
paths.map(new File(_).getCanonicalFile)

}

0 comments on commit 9be3bc3

Please sign in to comment.