From 53024829e6f5cac7ac764a5d94021a247c1715b1 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Wed, 4 Dec 2024 16:02:47 +0100 Subject: [PATCH] wip --- .../src/mill/pythonlib/PythonModule.scala | 57 ------------------- pythonlib/src/mill/pythonlib/RuffModule.scala | 54 ++++++++++++++++++ 2 files changed, 54 insertions(+), 57 deletions(-) create mode 100644 pythonlib/src/mill/pythonlib/RuffModule.scala diff --git a/pythonlib/src/mill/pythonlib/PythonModule.scala b/pythonlib/src/mill/pythonlib/PythonModule.scala index 04795c2f2bf..67915f7c2da 100644 --- a/pythonlib/src/mill/pythonlib/PythonModule.scala +++ b/pythonlib/src/mill/pythonlib/PythonModule.scala @@ -180,63 +180,6 @@ trait PythonModule extends PipModule with TaskModule { outer => override def moduleDeps: Seq[PythonModule] = Seq(outer) } - // def getRuff(): PathRef = Task { - // import coursier.cache.FileCache - // import coursier.util.Artifact - - // val cache = FileCache() - // val file = cache - // .file(Artifact("")) - // .run - // .unsafeRun()(cache.ec) - // .fold(ex => throw new Exception(ex), identity) - // os.unzip(os.Path(file), T.dest) - // PathRef(T.dest) - // } - - // Move these into a RuffModule, and consolidate all tasks into one. - /** Command line options to pass the the black code formatter. - * - * This is the way to configure black in mill, since mill doesn't use a - * pyproject.toml file. - */ - def blackOptions: T[Seq[String]] = Task { Seq.empty[String] } - - /** Reformat all source files of this module. */ - def reformat(): Command[Unit] = Task.Command { - runner().run( - // format: off - ( - "-m", "ruff", - "format", - blackOptions(), - sources().map(_.path) - ), - // format: on - workingDir = T.dest - ) - } - - /** Check the format of all source files of this module. */ - def checkFormat(): Command[Unit] = Task.Command { - runner().run( - // format: off - ( - "-m", "ruff", - "format", - "--check", - blackOptions(), - sources().map(_.path) - ), - // format: on - workingDir = T.dest - ) - } - - def fix(): Command[Unit] = ??? - - def check(): Command[Unit] = ??? - } object PythonModule { diff --git a/pythonlib/src/mill/pythonlib/RuffModule.scala b/pythonlib/src/mill/pythonlib/RuffModule.scala new file mode 100644 index 00000000000..80d779d3205 --- /dev/null +++ b/pythonlib/src/mill/pythonlib/RuffModule.scala @@ -0,0 +1,54 @@ +package mill.pythonlib + +import mill._ +import mill.define.ExternalModule +import mill.define.Discover +import mill.TaskModule + +trait RuffModule extends PythonModule { + + override def pythonToolDeps = Task { + super.pythonToolDeps() ++ Seq("ruff>=0.8.1") + } + + /** Ad-hoc command to invoke ruff on the sources of this module. + * + * You'll need to supply any subcommand that ruff understands. For example: + * + * - format sources: ruff format + * - check format of sources: ruff format --check + * - see format diff: ruff format --diff + * - find linting errors: ruff check + * - automatically fix linting errors: ruff check --fix + * + */ + def ruff(args: String*) = Task.Command { + runner().run( + // format: off + ( + "-m", "ruff", + args, + sources().map(_.path) + ), + // format: on + workingDir = Task.dest + ) + } + +} + +/** + * Includes some common tasks to reformat and check all sources. This mimics ScalafmtModule, so that + * +*/ +object RuffModule extends ExternalModule with TaskModule { + + def defaultCommandName() = "reformatAll" + + lazy val millDiscover: Discover = Discover[this.type] + + def reformatAll + + +} +