Skip to content

Commit

Permalink
Format all sources
Browse files Browse the repository at this point in the history
Even those not known to the build
  • Loading branch information
alexarchambault committed Sep 11, 2024
1 parent 132063c commit 1768f63
Show file tree
Hide file tree
Showing 22 changed files with 143 additions and 43 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,6 @@ jobs:
uses: ./.github/workflows/run-mill-action.yml
with:
java-version: '11'
buildcmd: ./mill -i mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll __.sources + __.mimaReportBinaryIssues + __.fix --check
buildcmd: |
ci/scalafmt.sh --test
./mill -i __.mimaReportBinaryIssues + __.fix --check
8 changes: 8 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ project.git = true

runner.dialect = scala213

project.excludeFilters = [
# can't get fileOverride to work to specify scala3 as dialect for these
"scalalib/test/resources/hello-dotty/*"
# these have scaladoc comments with a particular shape that we don't want to reformat
"example/depth/large/*"
# these need to have a wrong format, tests check that Mill can reformat them
"scalalib/test/resources/scalafmt/*"
]
75 changes: 75 additions & 0 deletions ci/scalafmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env bash

# This is the launcher script of scalafmt-native-image (https://github.com/VirtusLab/scalafmt-native-image).
# This script downloads and runs scalafmt-native-image version set by SCALAFMT_VERSION below.
#
# Download the latest version of this script at https://github.com/VirtusLab/scalafmt-native-image/raw/main/scalafmt.sh

set -eu

DEFAULT_SCALAFMT_VERSION="3.8.3"

SCALAFMT_VERSION=""
if test -e .scalafmt.conf; then
SCALAFMT_VERSION="$(cat .scalafmt.conf | grep '^version\s*=\s*"[0-9.A-Z-]*"$' | sed 's/^version[ \t]*=[ \t]*"\([0-9.A-Z-]*\)"$/\1/g')"
fi

if [ "$SCALAFMT_VERSION" == "" ]; then
SCALAFMT_VERSION="$DEFAULT_SCALAFMT_VERSION"
if test -e .scalafmt.conf; then
echo "Warning: no scalafmt version found in .scalafmt.conf, using $SCALAFMT_VERSION" 1>&2
else
echo "Warning: no .scalafmt.conf found, using scalafmt version $SCALAFMT_VERSION" 1>&2
fi
fi

GH_ORG="VirtusLab"
GH_NAME="scalafmt-native-image"

TAG="v$SCALAFMT_VERSION"

if [ "$(expr substr $(uname -s) 1 5 2>/dev/null)" == "Linux" ]; then
arch=$(uname -m)
if [[ "$arch" == "aarch64" ]] || [[ "$arch" == "x86_64" ]]; then
SCALAFMT_URL="https://github.com/$GH_ORG/$GH_NAME/releases/download/$TAG/scalafmt-${arch}-pc-linux.gz"
else
echo "scalafmt-native-image is not supported on $arch" 1>&2
exit 2
fi
CACHE_BASE="$HOME/.cache/coursier/v1"
elif [ "$(uname)" == "Darwin" ]; then
arch=$(uname -m)
CACHE_BASE="$HOME/Library/Caches/Coursier/v1"
if [[ "$arch" == "x86_64" ]]; then
SCALAFMT_URL="https://github.com/$GH_ORG/$GH_NAME/releases/download/$TAG/scalafmt-x86_64-apple-darwin.gz"
elif [[ "$arch" == "arm64" ]]; then
SCALAFMT_URL="https://github.com/$GH_ORG/$GH_NAME/releases/download/$TAG/scalafmt-aarch64-apple-darwin.gz"
else
echo "scalafmt-native-image is not supported on $arch" 1>&2
exit 2
fi
else
echo "This standalone scalafmt-native-image launcher is supported only in Linux and macOS." 1>&2
exit 1
fi

CACHE_DEST="$CACHE_BASE/$(echo "$SCALAFMT_URL" | sed 's@://@/@')"
SCALAFMT_BIN_PATH=${CACHE_DEST%.gz}

if [ ! -f "$CACHE_DEST" ]; then
mkdir -p "$(dirname "$CACHE_DEST")"
TMP_DEST="$CACHE_DEST.tmp-setup"
echo "Downloading $SCALAFMT_URL" 1>&2
curl -fLo "$TMP_DEST" "$SCALAFMT_URL"
mv "$TMP_DEST" "$CACHE_DEST"
fi

if [ ! -f "$SCALAFMT_BIN_PATH" ]; then
gunzip -k "$CACHE_DEST"
fi

if [ ! -x "$SCALAFMT_BIN_PATH" ]; then
chmod +x "$SCALAFMT_BIN_PATH"
fi

exec "$SCALAFMT_BIN_PATH" "$@"
4 changes: 2 additions & 2 deletions example/depth/tasks/11-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
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package millbuild
object ScalaVersion{
object ScalaVersion {
def myScalaVersion = "2.13.10"
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package myplugin
import mill._

/**
* Example Mill plugin trait that adds a `line-count.txt`
* to the resources of your `JavaModule`
*/
trait LineCountJavaModule extends mill.javalib.JavaModule{
trait LineCountJavaModule extends mill.javalib.JavaModule {

/** Name of the file containing the line count that we create in the resource path */
def lineCountResourceFileName: T[String]

/** Total number of lines in module's source files */
def lineCount = T{
def lineCount = T {
allSourceFiles().map(f => os.read.lines(f.path).size).sum
}

/** Generate resources using lineCount of sources */
override def resources = T{
override def resources = T {
os.write(T.dest / lineCountResourceFileName(), "" + lineCount())
super.resources() ++ Seq(PathRef(T.dest))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ object UnitTests extends TestSuite {

val resourceFolder = os.Path(sys.env("MILL_TEST_RESOURCE_FOLDER"))
UnitTester(build, resourceFolder / "unit-test-project").scoped { eval =>

// Evaluating tasks by direct reference
val Right(result) = eval(build.resources)
assert(
Expand Down
6 changes: 4 additions & 2 deletions example/scalalib/basic/3-multi-module/foo/src/Foo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package foo
import mainargs.{main, ParserForMethods, arg}
object Foo {
@main
def main(@arg(name = "foo-text") fooText: String,
@arg(name = "bar-text") barText: String): Unit = {
def main(
@arg(name = "foo-text") fooText: String,
@arg(name = "bar-text") barText: String
): Unit = {
println("Foo.value: " + fooText)
println("Bar.value: " + bar.Bar.generateHtml(barText))
}
Expand Down
6 changes: 4 additions & 2 deletions example/scalalib/basic/4-builtin-commands/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-text") barText: String): Unit = {
def main(
@arg(name = "foo-text") fooText: String,
@arg(name = "bar-text") barText: String
): Unit = {
println("Foo.value: " + Foo.value)
bar.Bar.printText(barText)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ object Foo2 {
if (sys.env.contains("MY_CUSTOM_ENV")) println("MY_CUSTOM_ENV: " + sys.env("MY_CUSTOM_ENV"))
}
}

10 changes: 6 additions & 4 deletions example/scalalib/builds/4-nested-modules/baz/src/Baz.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import mainargs.{main, ParserForMethods, arg}

object Baz {
@main
def main(@arg(name = "bar-text") barText: String,
@arg(name = "qux-text") quxText: String,
@arg(name = "foo-text") fooText: String,
@arg(name = "baz-text") bazText: String): Unit = {
def main(
@arg(name = "bar-text") barText: String,
@arg(name = "qux-text") quxText: String,
@arg(name = "foo-text") fooText: String,
@arg(name = "baz-text") bazText: String
): Unit = {
foo.Foo.main(barText, quxText, fooText)

val value = p(bazText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import mainargs.{main, ParserForMethods, arg}

object Qux {
@main
def main(@arg(name = "bar-text") barText: String,
@arg(name = "qux-text") quxText: String): Unit = {
def main(
@arg(name = "bar-text") barText: String,
@arg(name = "qux-text") quxText: String
): Unit = {
foo.bar.Bar.main(barText)

val value = p(quxText)
Expand Down
8 changes: 5 additions & 3 deletions example/scalalib/builds/4-nested-modules/foo/src/Foo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import mainargs.{main, ParserForMethods, arg}

object Foo {
@main
def main(@arg(name = "bar-text") barText: String,
@arg(name = "qux-text") quxText: String,
@arg(name = "foo-text") fooText: String): Unit = {
def main(
@arg(name = "bar-text") barText: String,
@arg(name = "qux-text") quxText: String,
@arg(name = "foo-text") fooText: String
): Unit = {
foo.qux.Qux.main(barText, quxText)

val value = p(fooText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object FooTests extends TestSuite {
// Use `MILL_TEST_RESOURCE_FOLDER` to list files available in resource folder
assert(
os.list(testFileResourceDir).sorted ==
Seq(testFileResourceDir / "test-file-a.txt", testFileResourceDir / "test-file-b.txt")
Seq(testFileResourceDir / "test-file-a.txt", testFileResourceDir / "test-file-b.txt")
)

// Use the `OTHER_FILES_FOLDER` configured in your build to access the
Expand Down
1 change: 1 addition & 0 deletions example/scalalib/module/7-docjar/bar/src/Bar.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package bar

/**
* My Awesome Docs for class Bar
*/
Expand Down
11 changes: 6 additions & 5 deletions example/scalalib/web/4-webapp-scalajs/client/src/ClientApp.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package client
import org.scalajs.dom
object ClientApp{
object ClientApp {
var state = "all"
var todoApp = dom.document.getElementsByClassName("todoapp")(0)

Expand All @@ -11,7 +11,7 @@ object ClientApp{
method = dom.HttpMethod.POST
}
).then[String](response => response.text())
.then[Unit]{ text =>
.then[Unit] { text =>
todoApp.innerHTML = text
initListeners()
}
Expand Down Expand Up @@ -45,19 +45,20 @@ object ClientApp{
bindEvent("todo-completed", s"/list/completed", Some("completed"))
bindEvent("clear-completed", s"/clear-completed/$state", None)

val newTodoInput = dom.document.getElementsByClassName("new-todo")(0).asInstanceOf[dom.HTMLInputElement]
val newTodoInput =
dom.document.getElementsByClassName("new-todo")(0).asInstanceOf[dom.HTMLInputElement]
newTodoInput.addEventListener(
"keydown",
(evt: dom.KeyboardEvent) => {
if (evt.keyCode == 13){
if (evt.keyCode == 13) {
dom.fetch(
s"/add/$state",
new dom.RequestInit {
method = dom.HttpMethod.POST
body = newTodoInput.value
}
).then[String](response => response.text())
.then[Unit]{text =>
.then[Unit] { text =>
newTodoInput.value = ""
todoApp.innerHTML = text
initListeners()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package client
import org.scalajs.dom
import shared.{Todo, Shared}
object ClientApp{
object ClientApp {
var state = "all"
var todoApp = dom.document.getElementsByClassName("todoapp")(0)

Expand All @@ -12,7 +12,7 @@ object ClientApp{
method = dom.HttpMethod.POST
}
).`then`[String](response => response.text())
.`then`[Unit]{ text =>
.`then`[Unit] { text =>
todoApp.innerHTML = Shared
.renderBody(upickle.default.read[Seq[Todo]](text), state)
.render
Expand Down Expand Up @@ -49,19 +49,20 @@ object ClientApp{
bindEvent("todo-completed", s"/list/completed", Some("completed"))
bindEvent("clear-completed", s"/clear-completed/$state", None)

val newTodoInput = dom.document.getElementsByClassName("new-todo")(0).asInstanceOf[dom.HTMLInputElement]
val newTodoInput =
dom.document.getElementsByClassName("new-todo")(0).asInstanceOf[dom.HTMLInputElement]
newTodoInput.addEventListener(
"keydown",
(evt: dom.KeyboardEvent) => {
if (evt.keyCode == 13){
if (evt.keyCode == 13) {
dom.fetch(
s"/add/$state",
new dom.RequestInit {
method = dom.HttpMethod.POST
body = newTodoInput.value
}
).`then`[String](response => response.text())
.`then`[Unit]{text =>
.`then`[Unit] { text =>
newTodoInput.value = ""

todoApp.innerHTML = Shared
Expand All @@ -75,6 +76,5 @@ object ClientApp{
)
}


def main(args: Array[String]): Unit = initListeners()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object Todo {
implicit def todoRW: upickle.default.ReadWriter[Todo] = upickle.default.macroRW[Todo]
}

object Shared{
object Shared {
def renderBody(todos: Seq[Todo], state: String) = {
val filteredTodos = state match {
case "all" => todos.zipWithIndex
Expand Down
4 changes: 2 additions & 2 deletions scalajslib/test/resources/esModuleRemap/src/app/App.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package app

import scala.scalajs.js
import scala.scalajs.js.annotation._
import scala.scalajs.js.annotation._

object App {
object App {
def main(args: Array[String]): Unit = {
println(linspace(-10.0, 10.0, 10))
}
Expand Down
2 changes: 1 addition & 1 deletion scalalib/test/resources/assembly/src/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ object Main {
}

def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ object MainTests extends TestSuite {
test("resource") {
val expected = new java.util.ArrayList[Path]()
expected.add(Paths.get(sys.env("MILL_TEST_RESOURCE_FOLDER") + "/hello-resource.txt"))
val listed = Files.list(Paths.get(sys.env("MILL_TEST_RESOURCE_FOLDER"))).collect(Collectors.toList())
val listed =
Files.list(Paths.get(sys.env("MILL_TEST_RESOURCE_FOLDER"))).collect(Collectors.toList())
assert(listed == expected)
assert(
Files.readString(Paths.get(sys.env("MILL_TEST_RESOURCE_FOLDER") + "/hello-resource.txt")) ==
"hello world resource text"
Files.readString(
Paths.get(sys.env("MILL_TEST_RESOURCE_FOLDER") + "/hello-resource.txt")
) ==
"hello world resource text"
)
}
}
Expand Down

0 comments on commit 1768f63

Please sign in to comment.