-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #668 from armanbilge/issue/304
Upstream `ScalafixProject`
- Loading branch information
Showing
4 changed files
with
199 additions
and
0 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
126 changes: 126 additions & 0 deletions
126
scalafix/src/main/scala/org/typelevel/sbt/ScalafixProject.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,126 @@ | ||
/* | ||
* Copyright 2022 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.sbt | ||
|
||
import sbt._ | ||
import scalafix.sbt._ | ||
|
||
import Keys._ | ||
import ScalafixTestkitPlugin.autoImport._ | ||
|
||
final class ScalafixProject private ( | ||
val name: String, | ||
val rules: Project, | ||
val input: Project, | ||
val output: Project, | ||
val tests: Project | ||
) extends CompositeProject { | ||
|
||
lazy val componentProjects = Seq(rules, input, output, tests) | ||
|
||
def componentProjectReferences = componentProjects.map(x => x: ProjectReference) | ||
|
||
def in(dir: File): ScalafixProject = | ||
new ScalafixProject( | ||
name, | ||
rules.in(dir / "rules"), | ||
input.in(dir / "input"), | ||
output.in(dir / "output"), | ||
tests.in(dir / "tests") | ||
) | ||
|
||
def rulesSettings(ss: Def.SettingsDefinition*): ScalafixProject = | ||
rulesConfigure(_.settings(ss: _*)) | ||
|
||
def inputSettings(ss: Def.SettingsDefinition*): ScalafixProject = | ||
inputConfigure(_.settings(ss: _*)) | ||
|
||
def outputSettings(ss: Def.SettingsDefinition*): ScalafixProject = | ||
outputConfigure(_.settings(ss: _*)) | ||
|
||
def testsSettings(ss: Def.SettingsDefinition*): ScalafixProject = | ||
testsConfigure(_.settings(ss: _*)) | ||
|
||
def rulesConfigure(transforms: (Project => Project)*): ScalafixProject = | ||
new ScalafixProject( | ||
name, | ||
rules.configure(transforms: _*), | ||
input, | ||
output, | ||
tests | ||
) | ||
|
||
def inputConfigure(transforms: (Project => Project)*): ScalafixProject = | ||
new ScalafixProject( | ||
name, | ||
rules, | ||
input.configure(transforms: _*), | ||
output, | ||
tests | ||
) | ||
|
||
def outputConfigure(transforms: (Project => Project)*): ScalafixProject = | ||
new ScalafixProject( | ||
name, | ||
rules, | ||
input, | ||
output.configure(transforms: _*), | ||
tests | ||
) | ||
|
||
def testsConfigure(transforms: (Project => Project)*): ScalafixProject = | ||
new ScalafixProject( | ||
name, | ||
rules, | ||
input, | ||
output, | ||
tests.configure(transforms: _*) | ||
) | ||
|
||
} | ||
|
||
object ScalafixProject { | ||
def apply(name: String): ScalafixProject = { | ||
|
||
lazy val rules = Project(s"$name-rules", file(s"$name/rules")).settings( | ||
libraryDependencies += "ch.epfl.scala" %% "scalafix-core" % _root_ | ||
.scalafix | ||
.sbt | ||
.BuildInfo | ||
.scalafixVersion | ||
) | ||
|
||
lazy val input = | ||
Project(s"$name-input", file(s"$name/input")).enablePlugins(NoPublishPlugin) | ||
|
||
lazy val output = | ||
Project(s"$name-output", file(s"$name/output")).enablePlugins(NoPublishPlugin) | ||
|
||
lazy val tests = Project(s"$name-tests", file(s"$name/tests")) | ||
.settings( | ||
scalafixTestkitOutputSourceDirectories := (output / Compile / unmanagedSourceDirectories).value, | ||
scalafixTestkitInputSourceDirectories := (input / Compile / unmanagedSourceDirectories).value, | ||
scalafixTestkitInputClasspath := (input / Compile / fullClasspath).value, | ||
scalafixTestkitInputScalacOptions := (input / Compile / scalacOptions).value, | ||
scalafixTestkitInputScalaVersion := (input / Compile / scalaVersion).value | ||
) | ||
.dependsOn(rules) | ||
.enablePlugins(NoPublishPlugin, ScalafixTestkitPlugin) | ||
|
||
new ScalafixProject(name, rules, input, output, tests) | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
scalafix/src/main/scala/org/typelevel/sbt/ScalafixProjectMacros.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 @@ | ||
/* | ||
* Copyright 2022 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.sbt | ||
|
||
import scala.annotation.tailrec | ||
import scala.reflect.macros.blackbox | ||
|
||
private[sbt] object ScalafixProjectMacros { | ||
|
||
// Copied from sbt.std.KeyMacro | ||
def definingValName(c: blackbox.Context, invalidEnclosingTree: String => String): String = { | ||
import c.universe.{Apply => ApplyTree, _} | ||
val methodName = c.macroApplication.symbol.name | ||
def processName(n: Name): String = | ||
n.decodedName | ||
.toString | ||
.trim // trim is not strictly correct, but macros don't expose the API necessary | ||
@tailrec def enclosingVal(trees: List[c.Tree]): String = { | ||
trees match { | ||
case ValDef(_, name, _, _) :: _ => processName(name) | ||
case (_: ApplyTree | _: Select | _: TypeApply) :: xs => enclosingVal(xs) | ||
// lazy val x: X = <methodName> has this form for some reason (only when the explicit type is present, though) | ||
case Block(_, _) :: DefDef(mods, name, _, _, _, _) :: _ if mods.hasFlag(Flag.LAZY) => | ||
processName(name) | ||
case _ => | ||
c.error(c.enclosingPosition, invalidEnclosingTree(methodName.decodedName.toString)) | ||
"<error>" | ||
} | ||
} | ||
enclosingVal(enclosingTrees(c).toList) | ||
} | ||
|
||
// Copied from sbt.std.KeyMacro | ||
def enclosingTrees(c: blackbox.Context): Seq[c.Tree] = | ||
c.asInstanceOf[reflect.macros.runtime.Context] | ||
.callsiteTyper | ||
.context | ||
.enclosingContextChain | ||
.map(_.tree.asInstanceOf[c.Tree]) | ||
|
||
def scalafixProjectImpl(c: blackbox.Context): c.Expr[ScalafixProject] = { | ||
import c.universe._ | ||
|
||
val enclosingValName = definingValName( | ||
c, | ||
methodName => | ||
s"""$methodName must be directly assigned to a val, such as `val x = $methodName`. Alternatively, you can use `org.typelevel.sbt.ScalafixProject.apply`""" | ||
) | ||
|
||
val name = c.Expr[String](Literal(Constant(enclosingValName))) | ||
|
||
reify { ScalafixProject(name.splice) } | ||
} | ||
} |
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