-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added option to only format changed files (#50)
* feature/1.0.3: added option to only format changes from a git branch * updated maven version in build file * post review changes
- Loading branch information
1 parent
2bd1b47
commit 3260add
Showing
11 changed files
with
157 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/scala/org/antipathy/mvn_scalafmt/builder/ChangedFilesBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.antipathy.mvn_scalafmt.builder | ||
|
||
import java.io.File | ||
import org.apache.maven.plugin.logging.Log | ||
import scala.sys.process.ProcessLogger | ||
import scala.util.{Try, Success, Failure} | ||
|
||
/** | ||
* Class for building a list of files that have changed from a specified git branch | ||
* @param log The maven logger | ||
* @param diff Should only changed files be returned | ||
* @param branch the git branch to compare against | ||
* @param changeFunction Function to identify changed files | ||
*/ | ||
class ChangedFilesBuilder(log: Log, diff: Boolean, branch: String, changeFunction: () => String) | ||
extends Builder[Seq[File], Seq[File]] { | ||
|
||
/** | ||
* Build a list of files that have changed in git from the specified input | ||
* | ||
* @param input The input to build from | ||
* @return A list of changed files. | ||
*/ | ||
override def build(input: Seq[File]): Seq[File] = | ||
if (diff) { | ||
log.info(s"Checking for files changed from $branch") | ||
Try { | ||
val names: Seq[String] = | ||
Predef.augmentString(changeFunction()).linesIterator.toSeq | ||
val changedFiles = names.map(new File(_).getAbsolutePath) | ||
changedFiles.foreach { f => | ||
log.info(s"Changed from $branch: $f") | ||
} | ||
changedFiles.map(new File(_)).filter { f => | ||
val path = f.getAbsolutePath | ||
path.endsWith("scala") || | ||
path.endsWith("sc") || | ||
path.endsWith("sbt") | ||
|
||
} | ||
} match { | ||
case Success(value) => value | ||
case Failure(e) => | ||
log.error("Could not obtain list of changed files", e) | ||
throw e | ||
} | ||
} else { | ||
input | ||
} | ||
|
||
} | ||
|
||
object ChangedFilesBuilder { | ||
|
||
def apply(log: Log, diff: Boolean, branch: String, workingDirectory: File): ChangedFilesBuilder = { | ||
|
||
def command(branch: String): String = s"git diff --name-only --diff-filter=d $branch" | ||
val logger: ProcessLogger = sys.process.ProcessLogger(_ => (), err => log.error(err)) | ||
def processFunction: () => String = () => { | ||
sys.process.Process(command(branch), workingDirectory).!!(logger) | ||
} | ||
new ChangedFilesBuilder(log, diff, branch, processFunction) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/test/scala/org/antipathy/mvn_scalafmt/builder/ChangedFilesBuilderSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.antipathy.mvn_scalafmt.builder | ||
|
||
import org.scalatest.{FlatSpec, GivenWhenThen, Matchers} | ||
import java.io.File | ||
import org.apache.maven.plugin.logging.SystemStreamLog | ||
|
||
class ChangedFilesBuilderSpec extends FlatSpec with GivenWhenThen with Matchers { | ||
|
||
behavior of "ChangedFilesBuilder" | ||
|
||
it should "Identify files that have changed from master" in { | ||
val log = new SystemStreamLog | ||
val sourceDirs = Seq("src/test/scala", "src/main/scala").map(new File(_)) | ||
val sources = new SourceFileSequenceBuilder(log).build(sourceDirs) | ||
val changedFiles = Seq( | ||
"/mvn_scalafmt/src/main/scala/org/antipathy/mvn_scalafmt/builder/ChangedFilesBuilder.scala", | ||
"/mvn_scalafmt/src/main/scala/org/antipathy/mvn_scalafmt/builder/SourceFileSequenceBuilder.scala", | ||
"/mvn_scalafmt/src/test/scala/org/antipathy/mvn_scalafmt/builder/ChangedFilesBuilderSpec.scala", | ||
"/mvn_scalafmt/src/test/scala/org/antipathy/mvn_scalafmt/builder/LocalConfigBuilderSpec.scala" | ||
) | ||
|
||
val changeFunction = () => changedFiles.mkString(System.lineSeparator()) | ||
|
||
val result = new ChangedFilesBuilder(log, true, "master", changeFunction).build(sources) | ||
result should be(changedFiles.map(new File(_))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters