Skip to content

Commit

Permalink
Apply Scalafmt to examples/ in CI (#3903)
Browse files Browse the repository at this point in the history
Partially completes: #3829

Tested locally by breaking format of
`example/scalalib/basic/1-simple/foo/src/Foo.scala` by just adding a
bunch of spaces.

* Observe break with `./mill
mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll __.sources`
failure.
* Observe fix with `./mill
mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll __.sources`

Used `// format: off` rather than changing [docstrings.style =
keep](https://scalameta.org/scalafmt/docs/configuration.html#docstringsstyle--keep)
to our `.scalafmt.conf` since that would affect all files. There doesn't
seem to be a built in way in ScalaFmt to have folder level configs or
something like this. I thought it might be better to not have `example`
be an exception cases if we can avoid it. I'm not sure if the `//
format: off` comments break something else though.
  • Loading branch information
myyk authored Nov 8, 2024
1 parent 07df8c3 commit 4f70fd6
Show file tree
Hide file tree
Showing 135 changed files with 327 additions and 372 deletions.
18 changes: 17 additions & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "3.8.3"
version = "3.8.4-RC1"

align.openParenCallSite = false
align.preset = none
Expand All @@ -21,3 +21,19 @@ project.git = true

runner.dialect = scala213

project {
excludePaths = [
"glob:**/example/scalalib/linting/1-scalafmt/src/Foo.scala",
"glob:**/mill/out/**"
]
}

fileOverride {
"glob:**/example/**/build.mill*" {
docstrings.style = keep
}
"glob:**/example/**/build.sc" {
docstrings.style = keep
}
"glob:**/example/scalalib/native/**/*.scala" = scala3
}
11 changes: 6 additions & 5 deletions contrib/jmh/test/src/mill/contrib/jmh/JmhModuleTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ object JmhModuleTest extends TestSuite {
val paths = EvaluatorPaths.resolveDestPaths(eval.outPath, jmh.listJmhBenchmarks())
val outFile = paths.dest / "benchmarks.out"
val Right(result) = eval(jmh.listJmhBenchmarks("-o", outFile.toString))
val expected = """Benchmarks:
|mill.contrib.jmh.Bench2.log
|mill.contrib.jmh.Bench2.sqrt
|mill.contrib.jmh.Bench1.measureShared
|mill.contrib.jmh.Bench1.measureUnshared""".stripMargin
val expected =
"""Benchmarks:
|mill.contrib.jmh.Bench2.log
|mill.contrib.jmh.Bench2.sqrt
|mill.contrib.jmh.Bench1.measureShared
|mill.contrib.jmh.Bench1.measureUnshared""".stripMargin
val out = os.read.lines(outFile).map(_.trim).mkString(System.lineSeparator())
assert(out == expected)
}
Expand Down
2 changes: 1 addition & 1 deletion example/depth/large/10-multi-file-builds/bar/package.mill
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package build.bar
package build.bar
2 changes: 1 addition & 1 deletion example/depth/large/10-multi-file-builds/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ BarQux.value: <p>world</p>
// `package.mill` files are only discovered in direct subfolders of the root `build.mill` or
// subfolders of another folder containing a `package.mill`; Hence in this example, we need
// an `bar/package.mill` to be present for `bar/qux/package.mill` to be discovered, even
// though `bar/package.mill` is empty
// though `bar/package.mill` is empty
6 changes: 4 additions & 2 deletions example/depth/large/10-multi-file-builds/foo/src/Foo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ object Foo {
val value = "hello"

@main
def main(@arg(name = "foo-text") fooText: String,
@arg(name = "bar-qux-text") barQuxText: String): Unit = {
def main(
@arg(name = "foo-text") fooText: String,
@arg(name = "bar-qux-text") barQuxText: String
): Unit = {
println("Foo.value: " + Foo.value)
bar.qux.BarQux.printText(barQuxText)
}
Expand Down
6 changes: 2 additions & 4 deletions example/depth/large/11-helper-files/build.mill
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// Apart from having `package` files in subfolders to define modules, Mill
// also allows you to have helper code in any `*.mill` file in the same folder
// as your `build.mill` or a `package.mill`.
Expand All @@ -9,18 +8,17 @@ import mill._, scalalib._
import $file.foo.versions
import $file.util.MyModule

object `package` extends RootModule with MyModule{
object `package` extends RootModule with MyModule {
def forkEnv = Map(
"MY_SCALA_VERSION" -> build.scalaVersion(),
"MY_PROJECT_VERSION" -> versions.myProjectVersion,
"MY_PROJECT_VERSION" -> versions.myProjectVersion
)
}

/** See Also: util.mill */
/** See Also: foo/package.mill */
/** See Also: foo/versions.mill */


//
// Different helper scripts and ``build.mill``/``package`` files can all refer to
// each other using the `build` object, which marks the root object of your build.
Expand Down
2 changes: 1 addition & 1 deletion example/depth/large/11-helper-files/foo/versions.mill
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package build.foo

def myProjectVersion = "0.0.1"
def myProjectVersion = "0.0.1"
10 changes: 4 additions & 6 deletions example/depth/large/12-helper-files-sc/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,24 @@
// current `.mill` extension with `package` declaration, so you should use `.mill` whenever
// possible


import mill._, scalalib._
import $packages._
import $file.foo.versions
import $file.util, util.MyModule

object `package` extends RootModule with MyModule{
def forkEnv = T{
object `package` extends RootModule with MyModule {
def forkEnv = T {
Map(
"MY_SCALA_VERSION" -> build.scalaVersion(),
"MY_PROJECT_VERSION" -> versions.myProjectVersion,
"MY_PROJECT_VERSION" -> versions.myProjectVersion
)
}
}

/** See Also: util.sc */
/** See Also: foo/package.sc */
/** See Also: foo/versions.sc */



/** Usage
> ./mill run
Expand Down
2 changes: 1 addition & 1 deletion example/depth/large/12-helper-files-sc/foo/versions.sc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
def myProjectVersion = "0.0.1"
def myProjectVersion = "0.0.1"
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import mill._, scalalib._
import $file.foo.versions
import $file.util.MyModule

object `package` extends RootModule with MyModule{
object `package` extends RootModule with MyModule {
def forkEnv = Map(
"MY_SCALA_VERSION" -> build.scalaVersion(),
"MY_PROJECT_VERSION" -> versions.myProjectVersion,
"MY_PROJECT_VERSION" -> versions.myProjectVersion
)
}
///** See Also: util.mill.scala */
///** See Also: foo/package.mill.scala */
///** See Also: foo/versions.mill.scala */


// Apart from having `package` files in subfolders to define modules, Mill
// also allows you to have helper code in any `*.mill` file in the same folder
// as your `build.mill` or a `package.mill`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package build.foo

def myProjectVersion = "0.0.1"
def myProjectVersion = "0.0.1"
6 changes: 2 additions & 4 deletions example/depth/sandbox/1-task/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package build
import mill._

object foo extends Module{
object foo extends Module {
def tDestTask = Task { println(Task.dest.toString) }
}

Expand All @@ -23,7 +23,6 @@ object foo extends Module{
.../out/foo/tDestTask.dest
*/


// === Task `os.pwd` redirection
// Mill also redirects the `os.pwd` property from https://github.com/com-lihaoyi/os-lib[OS-Lib],
// such that that also points towards a running task's own `.dest/` folder
Expand Down Expand Up @@ -62,7 +61,6 @@ def externalPwdTask = Task { println(externalPwd.toString) }
.../out/mill-server/.../sandbox
*/


// === Limitations of Mill's Sandboxing
//
// Mill's approach to filesystem sandboxing is designed to avoid accidental interference
Expand All @@ -72,4 +70,4 @@ def externalPwdTask = Task { println(externalPwd.toString) }
// outside of Mill's control.
//
// However, by setting `os.pwd` to safe sandbox folders, we hope to minimize the cases where
// someone accidentally causes issues with their build by doing the wrong thing.
// someone accidentally causes issues with their build by doing the wrong thing.
6 changes: 3 additions & 3 deletions example/depth/sandbox/2-test/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
package build
import mill._, javalib._

trait MyModule extends JavaModule{
trait MyModule extends JavaModule {
object test extends JavaTests with TestModule.Junit4
}

object foo extends MyModule{
object foo extends MyModule {
def moduleDeps = Seq(bar)
}

Expand Down Expand Up @@ -68,4 +68,4 @@ object bar extends MyModule
// sandbox folder via `..` or from the root folder `/` or home folder `~/`.
// Nevertheless, it should add some simple guardrails to prevent many common
// causes of inter-test interference, letting your test suite run in parallel both
// quickly and reliably
// quickly and reliably
2 changes: 1 addition & 1 deletion example/depth/sandbox/3-breaking/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def myTask = Task { println(Task.workspace) }
// Whereas `MILL_WORKSPACE_ROOT` as well as in tests, which can access the
// workspace root via the `MILL_WORKSPACE_ROOT` environment variable

object foo extends JavaModule{
object foo extends JavaModule {
object test extends JavaTests with TestModule.Junit4
}

Expand Down
3 changes: 1 addition & 2 deletions example/extending/imports/1-import-ivy/build.mill
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// The following example shows how to import the library `org.thymeleaf:thymeleaf:3.1.1.RELEASE`
// into your build, so you can use it at build-time to safely generate escaped HTML snippets
// in your resource path for your application to use.
Expand All @@ -20,7 +19,7 @@ object foo extends JavaModule {
)
}

def resources = Task.Sources{
def resources = Task.Sources {
os.write(Task.dest / "snippet.txt", htmlSnippet())
super.resources() ++ Seq(PathRef(Task.dest))
}
Expand Down
3 changes: 1 addition & 2 deletions example/extending/imports/2-import-ivy-scala/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object bar extends ScalaModule {

def ivyDeps = Agg(ivy"com.lihaoyi::os-lib:0.10.7")
def htmlSnippet = Task { div(h1("hello"), p("world")).toString }
def resources = Task.Sources{
def resources = Task.Sources {
os.write(Task.dest / "snippet.txt", htmlSnippet())
super.resources() ++ Seq(PathRef(Task.dest))
}
Expand All @@ -22,7 +22,6 @@ object bar extends ScalaModule {
// Note that using `import $ivy` on Scala libraries requires a double colon `::` between
// the organization name and artifact name.


/** Usage

> mill bar.compile
Expand Down
5 changes: 2 additions & 3 deletions example/extending/imports/3-contrib-import/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ object foo extends ScalaModule with BuildInfo {
def scalaVersion = "2.13.10"
def buildInfoPackageName = "foo"
def buildInfoMembers = Seq(
BuildInfo.Value("scalaVersion", scalaVersion()),
BuildInfo.Value("scalaVersion", scalaVersion())
)
}


// This example illustrates usage of Mill `contrib` plugins. These are Mill
// plugins contributed by Mill user that are maintained within the Mill
// repo, published under `mill-contrib-*`.
Expand All @@ -33,4 +32,4 @@ foo.BuildInfo.scalaVersion: 2.13.10
//
// * Like third-party plugins, contrib modules are submitted by third-parties,
// and do now maintain the same binary compatibility guarantees of Mill's
// builtin comdules
// builtin comdules
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package foo
object Foo{
object Foo {
def main(args: Array[String]): Unit = {
println("foo.BuildInfo.scalaVersion: " + foo.BuildInfo.scalaVersion)
}
Expand Down
6 changes: 3 additions & 3 deletions example/extending/jvmcode/1-subprocess/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import mill._, javalib._
import mill.util.Jvm

object foo extends JavaModule {
def groovyClasspath: Task[Agg[PathRef]] = Task{
def groovyClasspath: Task[Agg[PathRef]] = Task {
defaultResolver().resolveDeps(Agg(ivy"org.codehaus.groovy:groovy:3.0.9"))
}

def groovyScript = Task.Source(millSourcePath / "generate.groovy")

def groovyGeneratedResources = Task{
def groovyGeneratedResources = Task {
Jvm.runSubprocess(
mainClass = "groovy.ui.GroovyMain",
classPath = groovyClasspath().map(_.path),
Expand Down Expand Up @@ -62,4 +62,4 @@ Contents of groovy-generated.html is <html><body><h1>Hello!</h1><p>Groovy!</p></
// As mentioned above, `defaultResolver().resolveDeps` and `Jvm.runSubprocess` are an
// alternative to `import $ivy`, providing you more flexibility to resolve dependencies
// on-demand as part of your task graph only when necessary, and keeping it isolated from
// the build in a subprocess preventing classpath collisions.
// the build in a subprocess preventing classpath collisions.
8 changes: 4 additions & 4 deletions example/extending/jvmcode/2-classloader/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import mill._, javalib._
import mill.util.Jvm

object foo extends JavaModule {
def groovyClasspath: Task[Agg[PathRef]] = Task{
def groovyClasspath: Task[Agg[PathRef]] = Task {
defaultResolver().resolveDeps(Agg(ivy"org.codehaus.groovy:groovy:3.0.9"))
}

def groovyScript = Task.Source(millSourcePath / "generate.groovy")

def groovyGeneratedResources = Task{
Jvm.runClassloader(classPath = groovyClasspath().map(_.path)){ classLoader =>
def groovyGeneratedResources = Task {
Jvm.runClassloader(classPath = groovyClasspath().map(_.path)) { classLoader =>
classLoader
.loadClass("groovy.ui.GroovyMain")
.getMethod("main", classOf[Array[String]])
Expand Down Expand Up @@ -52,4 +52,4 @@ Contents of groovy-generated.html is <html><body><h1>Hello!</h1><p>Groovy!</p></
// of wall-clock time and in terms of memory footprint. However, it does have somewhat less
// isolation, as the code is running inside your JVM and cannot be configured to have a separate
// working directory, environment variables, and other process-global configs. Which one is
// better to use differs on a case-by-case basis.
// better to use differs on a case-by-case basis.
12 changes: 5 additions & 7 deletions example/extending/jvmcode/3-worker/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ import mill.util.Jvm

object coursierModule extends CoursierModule

def groovyClasspath: Task[Agg[PathRef]] = Task{
def groovyClasspath: Task[Agg[PathRef]] = Task {
coursierModule.defaultResolver().resolveDeps(Agg(ivy"org.codehaus.groovy:groovy:3.0.9"))
}

def groovyWorker: Worker[java.net.URLClassLoader] = Task.Worker{
def groovyWorker: Worker[java.net.URLClassLoader] = Task.Worker {
mill.api.ClassLoader.create(groovyClasspath().map(_.path.toIO.toURL).toSeq, parent = null)
}

trait GroovyGenerateJavaModule extends JavaModule {
def groovyScript = Task.Source(millSourcePath / "generate.groovy")

def groovyGeneratedResources = Task{
def groovyGeneratedResources = Task {
val oldCl = Thread.currentThread().getContextClassLoader
Thread.currentThread().setContextClassLoader(groovyWorker())
try {
Expand All @@ -49,10 +49,10 @@ trait GroovyGenerateJavaModule extends JavaModule {
def resources = super.resources() ++ Seq(groovyGeneratedResources())
}

object foo extends GroovyGenerateJavaModule{
object foo extends GroovyGenerateJavaModule {
def groovyGenerateArg = "Foo Groovy!"
}
object bar extends GroovyGenerateJavaModule{
object bar extends GroovyGenerateJavaModule {
def groovyGenerateArg = "Bar Groovy!"
}

Expand All @@ -61,7 +61,6 @@ object bar extends GroovyGenerateJavaModule{
// method of `groovy.ui.GroovyMain`, which also happens to require us to set the
// `ContextClassLoader` to work.


/** Usage

> ./mill foo.run
Expand All @@ -71,7 +70,6 @@ Contents of groovy-generated.html is <html><body><h1>Hello!</h1><p>Foo Groovy!</
Contents of groovy-generated.html is <html><body><h1>Hello!</h1><p>Bar Groovy!</p></body></html>
*/


// Because the `URLClassLoader` within `groovyWorker` is long-lived, the code within the
// classloader can be optimized by the JVM runtime, and would have less overhead than if
// run in separate classloaders via `Jvm.runClassloader`. And because `URLClassLoader`
Expand Down
4 changes: 2 additions & 2 deletions example/extending/jvmcode/4-module-run-task/bar/src/Bar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package bar
object Bar {
def main(args: Array[String]) = {
val dest = os.pwd
for(sourceStr <- args){
for (sourceStr <- args) {
val sourcePath = os.Path(sourceStr)
for(p <- os.walk(sourcePath) if p.ext == "scala"){
for (p <- os.walk(sourcePath) if p.ext == "scala") {
val text = os.read(p)
val mangledText = text.replace("hello", "HELLO")
val fileDest = dest / (p.subRelativeTo(sourcePath))
Expand Down
Loading

0 comments on commit 4f70fd6

Please sign in to comment.