forked from scalacenter/scalafix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Squashed commits: scalafix-reflect3 cross compiled for scala3
Original commit messages follow: configured build for reflect3, isolated RuleCompiler in scala2 and scala3 versions, scala3 has an empty impl, fixed some compilation errors in other classes convert quasiquotes in RuleInstrumentation with scalameta Trees removed wrong line forgotten during development improved term apply quasiquotes q... wip Rulecompiler with dotty removed unused lambdarule object in ruleInstrumentation ruleInstrumentation: fix Init tree for Rewrite with Nil instead of _ Co-authored-by: Tomasz Godzik <tgodzik@users.noreply.github.com> first draft of reflect3 ruleCompiler, applied scalafixAll and scalafmt manually removed/organized imports made reporter private better error handling messages refactored RuleCompiler companion obj into own file for reusability fixed usage of RuleCompilerClasspath fix scalaversion in build.sbt and import dottyAbstractFile alias added settings and ctx to rule compiler, changed error handling, removed custom exception, fixed class import for ruleCompilerClasspath explicit using params, fixed imports manually added scala3-library in reflect3 This is the commit message scalacenter#2 rebased and squashed afterwards: fix RuleCompiler initialization by propertly setting classpath and outputDir This is the commit message scalacenter#2: build sbt reflect This is the commit message scalacenter#3: rule compiler remove position for confError, and notOk/apply fixes
- Loading branch information
Showing
13 changed files
with
178 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
scalafix-reflect/src/main/scala-3/scalafix/internal/reflect/RuleCompiler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package scalafix.internal.reflect | ||
|
||
import dotty.tools.dotc.Compiler | ||
import dotty.tools.dotc.Run | ||
import dotty.tools.dotc.core.Contexts.FreshContext | ||
import dotty.tools.dotc.util.SourceFile | ||
import dotty.tools.dotc.reporting.StoreReporter | ||
import dotty.tools.dotc.interactive.InteractiveDriver | ||
import dotty.tools.io.AbstractFile as DottyAbstractFile | ||
import dotty.tools.io.VirtualDirectory as DottyVirtualDirectory | ||
|
||
import scala.reflect.io.VirtualDirectory | ||
import scala.reflect.io.AbstractFile | ||
import scala.reflect.internal.util.AbstractFileClassLoader | ||
|
||
import metaconfig.Configured | ||
import metaconfig.Input | ||
import metaconfig.ConfError | ||
import metaconfig.Position | ||
|
||
class RuleCompiler( | ||
classpath: String, | ||
target: AbstractFile = new VirtualDirectory("(memory)", None) | ||
) { | ||
private val settings = "-unchecked" :: "-deprecation" :: "-classpath" :: classpath :: Nil | ||
private val driver = new InteractiveDriver(settings) | ||
private val reporter: StoreReporter = new StoreReporter() | ||
private var ctx: FreshContext = driver.currentCtx.fresh | ||
private val dottyVirtualDirectory = new DottyVirtualDirectory(target.name, None) | ||
|
||
ctx = ctx | ||
.setReporter(reporter) | ||
.setSetting(ctx.settings.outputDir, dottyVirtualDirectory) | ||
|
||
private val compiler: Compiler = new Compiler() | ||
private val classLoader: AbstractFileClassLoader = | ||
new AbstractFileClassLoader(target, this.getClass.getClassLoader) | ||
|
||
def compile(input: Input): Configured[ClassLoader] = { | ||
reporter.removeBufferedMessages(using ctx) | ||
val run: Run = compiler.newRun(using ctx) | ||
run.compileSources( | ||
List( | ||
new SourceFile( | ||
DottyAbstractFile.getFile(input.path), | ||
input.chars | ||
) | ||
) | ||
) | ||
|
||
val errors = reporter.allErrors.map(error => | ||
ConfError | ||
.message(error.getMessage) | ||
) | ||
if (!errors.isEmpty) | ||
errors :+ ConfError.message( | ||
"Error compiling rule(s) from source using Scala 3 compiler; " + | ||
"to use the Scala 2.x compiler instead, use the corresponding " + | ||
"scalafix-cli artifact or force scalafixScalaBinaryVersion " + | ||
"to 2.x in your build tool" | ||
) | ||
|
||
ConfError | ||
.apply(errors) | ||
.map(_.notOk) | ||
.getOrElse(Configured.Ok(classLoader)) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
scalafix-reflect/src/main/scala/scalafix/internal/reflect/RuleCompilerClasspath.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package scalafix.internal.reflect | ||
|
||
import java.io.File | ||
import java.nio.file.Paths | ||
|
||
import scala.meta.io.AbsolutePath | ||
|
||
object RuleCompilerClasspath { | ||
|
||
def defaultClasspath: String = { | ||
defaultClasspathPaths.mkString(File.pathSeparator) | ||
} | ||
|
||
def defaultClasspathPaths: List[AbsolutePath] = { | ||
val classLoader = ClasspathOps.thisClassLoader | ||
val paths = classLoader.getURLs.iterator.map { u => | ||
if (u.getProtocol.startsWith("bootstrap")) { | ||
import java.nio.file._ | ||
val stream = u.openStream | ||
val tmp = Files.createTempFile("bootstrap-" + u.getPath, ".jar") | ||
try { | ||
Files.copy(stream, tmp, StandardCopyOption.REPLACE_EXISTING) | ||
} finally { | ||
stream.close() | ||
} | ||
AbsolutePath(tmp) | ||
} else { | ||
AbsolutePath(Paths.get(u.toURI)) | ||
} | ||
} | ||
paths.toList | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters