Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Roman Janusz
committed
Mar 19, 2023
1 parent
a8a4569
commit f4b45b0
Showing
5 changed files
with
172 additions
and
72 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
39 changes: 39 additions & 0 deletions
39
crossproject/src/main/scala/com/github/ghik/sbt/nosbt/crossproject/CrossProjectSupport.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,39 @@ | ||
package com.github.ghik.sbt.nosbt | ||
package crossproject | ||
|
||
import com.avsystem.commons.misc.OptArg | ||
import sbtcrossproject.{CrossProject, CrossType, Platform} | ||
|
||
trait CrossProjectSupport { this: ProjectGroup => | ||
/** | ||
* A cross-project version of `mkSubProject`. | ||
* Use this method in place of `sbtcrossproject.CrossPlugin.autoImport.crossProject`. | ||
*/ | ||
protected def mkCrossSubProject(platforms: Platform*): CrossProjectBuilder = macro Macros.mkCrossSubProjectImpl | ||
|
||
protected def mkCrossSubProject(name: String)(platforms: Platform*): CrossProjectBuilder = | ||
new CrossProjectBuilder(CrossProject(subProjectId(name), subProjectDir(name))(platforms *)) | ||
|
||
/** Imitates `CrossProject.Builder` */ | ||
protected final class CrossProjectBuilder(wrapped: CrossProject.Builder) { | ||
def withoutSuffixFor(platform: Platform): CrossProjectBuilder = | ||
new CrossProjectBuilder(wrapped.withoutSuffixFor(platform)) | ||
|
||
def crossType(crossType: CrossType): CrossProjectBuilder = | ||
new CrossProjectBuilder(wrapped.crossType(crossType)) | ||
|
||
def build(): CrossProject = | ||
wrapped.build().settings(allSubProjectSettings) | ||
} | ||
protected object CrossProjectBuilder { | ||
implicit def crossProjectFromBuilder(builder: CrossProjectBuilder): CrossProject = | ||
builder.build() | ||
} | ||
} | ||
|
||
abstract class CrossProjectGroup( | ||
groupName: String, | ||
parent: OptArg[ProjectGroup] = OptArg.Empty, | ||
)(implicit | ||
discoverProject: DiscoveredProjects | ||
) extends ProjectGroup(groupName, parent) with CrossProjectSupport |
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
34 changes: 34 additions & 0 deletions
34
src/main/scala/com/github/ghik/sbt/nosbt/SbtMacroUtils.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,34 @@ | ||
package com.github.ghik.sbt.nosbt | ||
|
||
import scala.annotation.tailrec | ||
import scala.reflect.macros.blackbox | ||
|
||
// copied from sbt.std.KeyMacro | ||
private[nosbt] object SbtMacroUtils { | ||
def definingValName(c: blackbox.Context, invalidEnclosingTree: String => String): String = { | ||
import c.universe.{Apply as 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) | ||
} | ||
|
||
private def enclosingTrees(c: blackbox.Context): Seq[c.Tree] = | ||
c.asInstanceOf[reflect.macros.runtime.Context] | ||
.callsiteTyper | ||
.context | ||
.enclosingContextChain | ||
.map(_.tree.asInstanceOf[c.Tree]) | ||
} |