-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds multiplex sandbox support and improves argument parsing #52
Changes from all commits
83e8d92
e65caef
1b9afd1
e438833
6c5c350
3faf1c2
5e5b0ee
80843d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
package annex.scalafmt | ||
|
||
import higherkindness.rules_scala.common.args.ArgsUtil | ||
import higherkindness.rules_scala.common.args.ArgsUtil.PathArgumentType | ||
import higherkindness.rules_scala.common.sandbox.SandboxUtil | ||
import higherkindness.rules_scala.common.worker.WorkerMain | ||
import higherkindness.rules_scala.workers.common.Color | ||
import java.io.{File, PrintStream} | ||
import java.nio.file.Files | ||
import java.nio.file.{Files, Path} | ||
import net.sourceforge.argparse4j.ArgumentParsers | ||
import net.sourceforge.argparse4j.impl.Arguments | ||
import net.sourceforge.argparse4j.inf.{ArgumentParser, Namespace} | ||
import org.scalafmt.Scalafmt | ||
import org.scalafmt.config.ScalafmtConfig | ||
import org.scalafmt.sysops.FileOps | ||
|
@@ -14,20 +18,38 @@ import scala.io.Codec | |
|
||
object ScalafmtRunner extends WorkerMain[Unit] { | ||
|
||
protected[this] def init(args: Option[Array[String]]): Unit = {} | ||
private[this] class ScalafmtRequest private ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I think we should avoid using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there's a performance advantage, however small, in Scala 2 to using
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, good to know. This was just a nit, so I'm fine with keeping it either way. |
||
val configFile: Path, | ||
val inputFile: Path, | ||
val outputFile: Path, | ||
) | ||
|
||
protected[this] def work(worker: Unit, args: Array[String], out: PrintStream): Unit = { | ||
private[this] object ScalafmtRequest { | ||
def apply(workDir: Path, namespace: Namespace): ScalafmtRequest = { | ||
new ScalafmtRequest( | ||
configFile = SandboxUtil.getSandboxPath(workDir, namespace.get[Path]("config")), | ||
inputFile = SandboxUtil.getSandboxPath(workDir, namespace.get[Path]("input")), | ||
outputFile = SandboxUtil.getSandboxPath(workDir, namespace.get[Path]("output")), | ||
) | ||
} | ||
} | ||
|
||
private[this] val argParser: ArgumentParser = { | ||
val parser = ArgumentParsers.newFor("scalafmt").addHelp(true).defaultFormatWidth(80).fromFilePrefix("@").build | ||
parser.addArgument("--config").required(true).`type`(Arguments.fileType) | ||
parser.addArgument("input").`type`(Arguments.fileType) | ||
parser.addArgument("output").`type`(Arguments.fileType) | ||
parser.addArgument("--config").required(true).`type`(PathArgumentType.apply()) | ||
parser.addArgument("input").`type`(PathArgumentType.apply()) | ||
parser.addArgument("output").`type`(PathArgumentType.apply()) | ||
parser | ||
} | ||
|
||
protected[this] def init(args: Option[Array[String]]): Unit = {} | ||
|
||
val namespace = parser.parseArgsOrFail(args) | ||
protected[this] def work(worker: Unit, args: Array[String], out: PrintStream, workDir: Path): Unit = { | ||
val workRequest = ScalafmtRequest(workDir, ArgsUtil.parseArgsOrFailSafe(args, argParser, out)) | ||
|
||
val source = FileOps.readFile(namespace.get[File]("input").toPath())(Codec.UTF8) | ||
val source = FileOps.readFile(workRequest.inputFile)(Codec.UTF8) | ||
|
||
val config = ScalafmtConfig.fromHoconFile(namespace.get[File]("config").toPath()).get | ||
val config = ScalafmtConfig.fromHoconFile(workRequest.configFile).get | ||
@tailrec | ||
def format(code: String): String = { | ||
val formatted = Scalafmt.format(code, config).get | ||
|
@@ -50,7 +72,7 @@ object ScalafmtRunner extends WorkerMain[Unit] { | |
} | ||
} | ||
|
||
Files.write(namespace.get[File]("output").toPath, output.getBytes) | ||
Files.write(workRequest.outputFile, output.getBytes) | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,13 +71,9 @@ configure_zinc_scala( | |
# IntelliJ libraries, so we can get a Scala SDK on sync. | ||
scala_library( | ||
name = "scala-sdk", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain a bit more why this is needed? I have a couple questions:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't have a Scala 3 SDK before because I forgot to add it. The IntelliJ Scala + Bazel integration could use some improvements. Happy to chat more about this offline because I've got a fork of it that would work well with your toolchain work. |
||
deps_used_whitelist = [ | ||
"@annex//:org_scala_lang_scala3_library_3", | ||
], | ||
deps_used_whitelist = compiler_classpath_2_13, | ||
scala = ":zinc_3", | ||
deps = [ | ||
"@annex//:org_scala_lang_scala3_library_3", | ||
], | ||
deps = compiler_classpath_2_13, | ||
) | ||
|
||
compiler_classpath_3 = [ | ||
|
@@ -91,6 +87,13 @@ runtime_classpath_3 = [ | |
"@annex//:org_scala_lang_tasty_core_3", | ||
] | ||
|
||
scala_library( | ||
name = "scala-sdk-3", | ||
deps_used_whitelist = compiler_classpath_3, | ||
scala = ":zinc_3", | ||
deps = compiler_classpath_3, | ||
) | ||
|
||
configure_bootstrap_scala( | ||
name = "bootstrap_3", | ||
compiler_classpath = compiler_classpath_3, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package higherkindness.rules_scala | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Can we merge these two There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's done like this everywhere else in the codebase. I don't support it, but this is consistent with everywhere else. If we want to change it, I'm open to it, but let's keep consistency everywhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't realize it was done like this elsewhere. Let's keep it for now, then. |
||
package common.args | ||
|
||
import common.error.AnnexWorkerError | ||
import java.io.{PrintStream, PrintWriter} | ||
import java.nio.file.{Path, Paths} | ||
import net.sourceforge.argparse4j.helper.HelpScreenException | ||
import net.sourceforge.argparse4j.inf.{Argument, ArgumentParser, ArgumentParserException, ArgumentType, Namespace} | ||
import scala.language.reflectiveCalls | ||
|
||
object ArgsUtil { | ||
|
||
/** | ||
* Safely handles expected errors that pop up during parsing arguments. The argparse4j calls System.exit when things | ||
* go wrong, and we don't want the worker to exit. Instead we exit the work request and print to the output stream. | ||
*/ | ||
def parseArgsOrFailSafe(args: Array[String], parser: ArgumentParser, out: PrintStream): Namespace = { | ||
try { | ||
parser.parseArgs(args) | ||
} catch { | ||
case e: HelpScreenException => | ||
parser.handleError(e, new PrintWriter(out)) | ||
throw new AnnexWorkerError(0) | ||
case e: ArgumentParserException => | ||
parser.handleError(e, new PrintWriter(out)) | ||
throw new AnnexWorkerError(1) | ||
} | ||
} | ||
|
||
case class PathArgumentType() extends ArgumentType[Path] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Shouldn't this be an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this needs to be a class and that argparse4j needs a new instance of the argument type. At least that's how the file argument type works https://github.com/argparse4j/argparse4j/blob/6c0f8590f7408025daa5f3b234e914cf805808a6/main/src/main/java/net/sourceforge/argparse4j/impl/Arguments.java#L275 |
||
override def convert(parser: ArgumentParser, arg: Argument, value: String): Path = { | ||
Paths.get(value) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Should this be a
case class
(this applies to the other configuration classes created in this commit)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe so because you get this error message related to Scala 3 migration:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I think you need to remove the
private
constructor modifier too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I want the constructor to be private, though, which is why this is a class and not a case class.
I only want people to be able to construct these objects by passing in a namespace and a working directory, so someone doesn't accidentally construct one incorrectly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. I think this is fine as-is, then.