-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resubmit #149 with fixes #182
Changes from all commits
79593b2
a979b38
8165303
719139f
9d61409
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.antipathy.mvn_scalafmt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. compared to #149: moved the bulk of the logic from |
||
|
||
import java.io.File | ||
import java.util.{List => JList} | ||
|
||
import scala.jdk.CollectionConverters._ | ||
|
||
import org.apache.maven.project.MavenProject | ||
|
||
/** Class for building a set of source dirs in the maven project */ | ||
class SourcesBuilder(project: MavenProject) { | ||
|
||
private val sources = Set.newBuilder[File] | ||
private val build = project.getBuild() | ||
private val outpath = getCanonicalFile(build.getDirectory()).toPath() | ||
|
||
private[mvn_scalafmt] def resultSet(): Set[File] = | ||
try sources.result() | ||
finally sources.clear() | ||
|
||
def result(): JList[File] = resultSet().toList.asJava | ||
|
||
def addMain(dirs: JList[File]): Unit = | ||
if (!add(dirs)) { | ||
appendPrimary(build.getSourceDirectory()) | ||
appendAlternative(project.getCompileSourceRoots()) | ||
} | ||
|
||
def addTest(dirs: JList[File]): Unit = | ||
if (!add(dirs)) { | ||
appendPrimary(build.getTestSourceDirectory()) | ||
appendAlternative(project.getTestCompileSourceRoots()) | ||
} | ||
|
||
private def add(dirs: JList[File]): Boolean = { | ||
val ok = dirs != null && !dirs.isEmpty | ||
if (ok) sources ++= dirs.asScala.map(getCanonicalFile) | ||
ok | ||
} | ||
|
||
private def getCanonicalFile(dir: String): File = | ||
project.getBasedir().toPath.resolve(dir).toFile().getCanonicalFile() | ||
|
||
private def getCanonicalFile(dir: File): File = | ||
project.getBasedir().toPath.resolve(dir.toPath).toFile().getCanonicalFile() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed to |
||
|
||
private def appendPrimary(dir: String): Unit = | ||
sources += getCanonicalFile(dir + "/../scala") | ||
|
||
private def appendAlternative(altDirs: JList[String]): Unit = | ||
sources ++= altDirs.asScala.map(getCanonicalFile).filter(!_.toPath().startsWith(outpath)) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package org.antipathy.mvn_scalafmt | ||
|
||
import java.io.File | ||
import java.nio.file.{Path, Paths} | ||
import java.util.{List => JList} | ||
|
||
import scala.jdk.CollectionConverters._ | ||
import scala.language.implicitConversions | ||
|
||
import org.antipathy.mvn_scalafmt.SourcesBuilderSpec._ | ||
import org.apache.maven.model.{Build, Model} | ||
import org.apache.maven.project.MavenProject | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
class SourcesBuilderSpec extends AnyFlatSpec with Matchers { | ||
|
||
behavior of "ProjectSourceCollectionBuilder" | ||
|
||
it should "add nonempty main" in { | ||
val builder = new SourcesBuilder(getMavenProject()) | ||
builder.addMain(Seq("foo", "/bar", "foo")) | ||
builder.resultSet() shouldBe Set[File]("/xyz/foo", "/bar") | ||
builder.resultSet() shouldBe empty | ||
} | ||
|
||
it should "add empty main" in { | ||
val mavenBuild = new Build() { | ||
setSourceDirectory("/foo/src/main/java") | ||
} | ||
|
||
val builder = new SourcesBuilder(getMavenProject(mavenBuild)) | ||
builder.addMain(null) | ||
builder.resultSet() shouldBe Set[File]("/foo/src/main/scala") | ||
builder.resultSet() shouldBe empty | ||
} | ||
|
||
it should "add nonempty test" in { | ||
val builder = new SourcesBuilder(getMavenProject()) | ||
builder.addTest(Seq("foo", "/bar", "/bar")) | ||
builder.resultSet() shouldBe Set[File]("/xyz/foo", "/bar") | ||
builder.resultSet() shouldBe empty | ||
} | ||
|
||
it should "add empty test" in { | ||
val mavenBuild = new Build() { | ||
setTestSourceDirectory("/foo/src/test/scala") | ||
} | ||
|
||
val builder = new SourcesBuilder(getMavenProject(mavenBuild)) | ||
builder.addTest(null) | ||
builder.resultSet() shouldBe Set[File]("/foo/src/test/scala") | ||
builder.resultSet() shouldBe empty | ||
} | ||
|
||
it should "add nonempty both" in { | ||
val builder = new SourcesBuilder(getMavenProject()) | ||
builder.addMain(Seq("foo", "/bar")) | ||
builder.addTest(Seq("foo", "/qux")) | ||
builder.resultSet() shouldBe Set[File]("/xyz/foo", "/bar", "/qux") | ||
builder.resultSet() shouldBe empty | ||
} | ||
|
||
it should "add empty both" in { | ||
val mavenBuild = new Build() { | ||
setSourceDirectory("/foo/src/main/java") | ||
setTestSourceDirectory("/foo/src/test/java") | ||
} | ||
|
||
val builder = new SourcesBuilder(getMavenProject(mavenBuild)) | ||
builder.addMain(null) | ||
builder.addTest(null) | ||
builder.resultSet() shouldBe Set[File]("/foo/src/main/scala", "/foo/src/test/scala") | ||
builder.resultSet() shouldBe empty | ||
} | ||
|
||
it should "add empty both, filtering" in { | ||
val mavenBuild = new Build() { | ||
setSourceDirectory("/foo/src/main/java") | ||
setTestSourceDirectory("/foo/src/test/java") | ||
} | ||
val project = getMavenProject(mavenBuild) | ||
project.addCompileSourceRoot("/foo/src/main/scala") | ||
project.addCompileSourceRoot("/out/src/main/scala") | ||
project.addCompileSourceRoot("/out2/src/main/scala") | ||
project.addTestCompileSourceRoot("/foo/src/test/scala") | ||
project.addTestCompileSourceRoot("/out/src/test/scala") | ||
project.addTestCompileSourceRoot("/out2/src/test/scala") | ||
|
||
val builder = new SourcesBuilder(project) | ||
builder.addMain(null) | ||
builder.addTest(null) | ||
builder.resultSet() shouldBe Set[File]( | ||
"/foo/src/main/scala", | ||
"/out2/src/main/scala", | ||
"/foo/src/test/scala", | ||
"/out2/src/test/scala" | ||
) | ||
builder.resultSet() shouldBe empty | ||
} | ||
|
||
} | ||
|
||
private object SourcesBuilderSpec { | ||
|
||
def getMavenProject(mavenBuild: Build = new Build()) = { | ||
mavenBuild.setDirectory("/out") | ||
new MavenProject(new Model { setBuild(mavenBuild) }) { | ||
setFile(new File("/xyz/pom.xml")) // sets basedir | ||
} | ||
} | ||
|
||
implicit def implicitStringToPath(file: String): Path = Paths.get(file) | ||
|
||
implicit def implicitStringToFile(file: String): File = implicitStringToPath(file).toFile | ||
|
||
implicit def implicitStringsToFiles(files: Seq[String]): JList[File] = | ||
files.map(implicitStringToFile).asJava | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
compared to #149: added
property = "project"
as some online sources insist that it's required with the switch frommaven-project
tomaven-core
.however, i also tested with setting only one of
property = "project"
anddefaultValue = "${project}"
, and all three versions seem to work. looks like this was not the culprit, but decided to keep both just in case.