Repository navigation
chore: format samples from sbt - #871
Conversation
| formatIfNeeded := { | ||
| val srcs = (Compile / sources).value | ||
| val targetDir = (Compile / compile / streams).value.cacheDirectory | ||
| val markerFile = targetDir / "prettier-last-run" |
There was a problem hiding this comment.
I want sbt to detect automatically what needs to be formatted or not, but couldn't make it work.
One drawback is that when compile a new checkout, it will always run a formatting task. It slows down the build a bit.
| if (markerFile.exists) { | ||
| srcs.exists(src => src.lastModified() > markerFile.lastModified()) | ||
| } else { | ||
| srcs.nonEmpty |
There was a problem hiding this comment.
One drawback is that when compile a new checkout, it will always run a formatting task. It slows down the build a bit.
How about something like the following to check a git diff
| srcs.nonEmpty | |
| Process(Seq("git", "diff", "--name-only","."), baseDirectory.value).!!.linesIterator.map(_.trim).exists(_.nonEmpty) |
Maybe even look at --staged
There was a problem hiding this comment.
Interesting.
Using the diff can be interesting to decide when to create the markerFile for the first time. For example, you do a clean compile, if there is not diff, not worth running the formatter at all.
But let's assume you change a file. Now you have a diff and the formatter is applied. After that, you still have a diff, but you don't need to format anymore since last change was made by the formatter.
Maybe a combination of both will be better, but I want to give another try and see if I can have sbt doing this trick for me. Sbt does know when there are files to compile, it's just that I couldn't find yet how to get this info.
There was a problem hiding this comment.
Right, I was thinking on those terms, too. Mixing them both but I see your comment below now.
octonato
left a comment
There was a problem hiding this comment.
@franciscolopezsancho, I changed it a little bit now by removing the creation of a marker file.
scalafmt and javafmt are also on first compilation, so I don't think it's a big deal if we do it for the prettier formatter.
Once formatted and compiled, it will only run again if any java file is changed. That's also how scalafmt and javafmt` works.
9e556a0 to
8b2de43
Compare
franciscolopezsancho
left a comment
There was a problem hiding this comment.
scalafmt and javafmt are also on first compilation, so I don't think it's a big deal if we do it for the prettier formatter.
LGTM
This will run the formatter whenever there are files to compile in the samples.