0

In the tutorial https://scalacenter.github.io/scalafix/docs/developers/tutorial.html I see how a SemanticRule is implemented for scalafix. I would like to call it directly in the source code on a Scala file which I have read from the disk and transform the file.

In scalameta I can do the following:

val f = .... // some File
val path = f.toPath
val bytes = java.nio.file.Files.readAllBytes(path)
val text = new String(bytes, "UTF-8")
val input = Input.VirtualFile(path.toString, text)
val source = input.parse[Source].get
val transformer = new MyTransformer()
val transformedSource = transformer(source)
// The transformed source can be written into an output file

I would like to do the same with scalafix but with a SemanticRule. If I read this: https://scalacenter.github.io/scalafix/docs/developers/tutorial.html#run-the-rule-from-source-code it only shows me how I can start the scalafix program with a file path to the Scala rule file but this is not what I want.

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
Baradé
  • 1,290
  • 1
  • 15
  • 35

1 Answers1

0

The only way I know is to use file path to the rule if it's in a single file and doesn't have external dependencies or publish it otherwise.

Example projects for code generation with Scalafix are

https://github.com/olafurpg/scalafix-codegen

https://github.com/DmytroMitin/scalafix-codegen

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • So if I have multiple files which I want to instrument and they have semanticdb files, I should not transform them separately with scalafix? If the classes in the files implement traits from external libraries such as akka.Actor, can I still get this type information from the semanticdb file of one single compiled source file? – Baradé Aug 08 '19 at 23:01
  • @Baradé I was talking about **rules** (subproject `rules`), not about input sources for code generation (subproject `in`). Everything can be multi-file and have external dependencies. Just for rules in such case they should be published to be used (for example `sbt publishLocal`). Sources in `in` can be multi-file and have dependencies without problems. I guess you will find symbols like `akka/Actor#`, `akka/Actor.`. – Dmytro Mitin Aug 09 '19 at 09:15
  • @Baradé I have a small library where I use Scalameta and Scalafix for code generation https://github.com/DmytroMitin/AUXify – Dmytro Mitin Aug 09 '19 at 09:19