Skip to content

Commit

Permalink
continue
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed May 16, 2021
1 parent 0dbcfac commit d00885e
Show file tree
Hide file tree
Showing 6 changed files with 4,643 additions and 1,348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ object ImportScalaDefinitions extends App {
val projectRoot: os.Path = {
var current = os.pwd

def containtsBuildSbt =
def containsBuildSbt =
os.list(current).exists(_.last === "build.sbt")

while (!containtsBuildSbt) {
while (!containsBuildSbt) {
if (os.root == current) sys.error("Couldn't find project root folder")
current = current / os.up
}
Expand All @@ -65,46 +65,67 @@ object ImportScalaDefinitions extends App {

type MyTree = Tree with HasCodePath

private val all: IArray[Tree] = {
private val all: IArray[MyTree] = {
implicit val wd: os.Path = tempDir

All.foreach { jar =>
%("unzip", "-o", jar.toString)
}

IArray.fromTraversable(os.walk(tempDir)).flatMap {
case file if file.isFile && file.ext == "class" && !file.last.contains("$$") =>
val byteCode = ByteCode(os.read.bytes(file))
val classFile = ClassFileParser.parse(byteCode)

ScalaSigParser.parse(classFile) match {
case Some(scalaSig) =>
IArray.fromTraversable(scalaSig.symbols).mapNotNone(toTree)
IArray
.fromTraversable(os.walk(tempDir))
.flatMap {
case file if file.isFile && file.ext == "class" && !file.last.contains("$") =>
val byteCode = ByteCode(os.read.bytes(file))
val classFile = ClassFileParser.parse(byteCode)

ScalaSigParser.parse(classFile) match {
case Some(scalaSig) =>
IArray
.fromTraversable(scalaSig.symbols)
.filter(ScalaSigEntryParsers.isTopLevel)
.mapNotNone(toTree)
.flatMap {
case x: ContainerTree if x.name.unescaped === "package" => x.members.collect { case x: MyTree => x }
case x: MyTree => IArray(x)
case _ => Empty
}
case None =>
System.err.println(s"Couldnt parse $file")
Empty
}

case None =>
System.err.println(s"Couldnt parse $file")
Empty
}
case _ => Empty
}
}

def flatten(tree: IArray[Tree]): IArray[MyTree] =
tree.flatMap {
case x: ContainerTree => flatten(x.members)
case x: MyTree => IArray(x)
case _ => Empty
}
}

val allJs: IArray[MyTree] = {
val inheritanceByName: Map[QualifiedName, MyTree] =
all.collect {
flatten(all).collect {
case x: ClassTree => x.codePath -> x
case x: TypeAliasTree => x.codePath -> x
}.toMap

def isJs(tree: Tree): Option[MyTree] = {
def isJs(tree: MyTree): Option[MyTree] = {
def extendsJs(ref: TypeRef): Boolean =
ref match {
case TypeRef.Object => true
case TypeRef.Union(types, _) => types.forall(extendsJs)
case TypeRef.Any => true
case TypeRef.Object => true
case other =>
inheritanceByName.get(other.typeName) match {
case Some(found) if found.name =/= tree.name => isJs(found).isDefined // recursive type aliases and stuff
case _ => false
case Some(found) if found.codePath =/= tree.codePath =>
isJs(found).isDefined // recursive type aliases and stuff
case _ =>
println(s"didnt find parent ${Printer.formatQN(other.typeName)}")
false
}
}

Expand All @@ -113,7 +134,7 @@ object ImportScalaDefinitions extends App {

tree match {
case cls: ClassTree =>
if (extendsJsMany(cls.parents)) Some(cls) else None
if (extendsJsMany(cls.parents)) Some(cls.copy(annotations = cls.annotations :+ Annotation.JsNative)) else None

case obj: ModuleTree =>
val isJsObject = extendsJsMany(obj.parents)
Expand All @@ -126,26 +147,26 @@ object ImportScalaDefinitions extends App {
case _ => None
} match {
case Empty => None
case kept => Some(obj.withMembers(kept))
case kept =>
Some(
obj.copy(
members = kept,
parents = if (isJsObject) obj.parents else Empty,
annotations = if (isJsObject) obj.annotations :+ Annotation.JsNative else obj.annotations,
),
)
}
case ta: TypeAliasTree =>
if (extendsJs(ta.alias)) Some(ta) else None
case _ =>
None
}
}

all
.mapNotNone(isJs)
.flatMap {
case x: ContainerTree => x.members.collect { case x: MyTree => x } // unpack containers
case other => IArray(other)
}
.distinct // some things are duplicated within package objects and so on
all.mapNotNone(isJs)
}

{
val asPackageName = Name("imported")
val scope = new TreeScope.Root(Name.typings, asPackageName, Map.empty, logging.stdout, false)

val inContainers: IArray[Tree] =
allJs.map { one =>
val codePath = one.codePath.parts
Expand Down Expand Up @@ -180,6 +201,9 @@ object ImportScalaDefinitions extends App {
c.withMembers(combinedMembers).asInstanceOf[C]
}

val asPackageName = Name("imported")
val scope = new TreeScope.Root(Name.typings, asPackageName, Map.empty, logging.stdout, false)

val combined = combineContainerTrees(
PackageTree(Empty, asPackageName, inContainers, NoComments, QualifiedName(Empty)),
)
Expand All @@ -189,7 +213,7 @@ object ImportScalaDefinitions extends App {
files.sync(printed, dumpSourceTo, deleteUnknowns = true, soft = true)
}

Json.persist[IArray[Tree]](dumpJsonTo)(allJs)
Json.persist[IArray[Tree]](dumpJsonTo)(flatten(allJs))

%("git", "add", dumpSourceTo, dumpJsonTo)(projectRoot)

Expand Down Expand Up @@ -228,7 +252,7 @@ object ImportScalaDefinitions extends App {

case x: AliasSymbol =>
val name = Name(processName(x.name))
val codePath = QualifiedName(x.symbolInfo.owner.path) + name
val codePath = QualifiedName(processName(x.symbolInfo.owner.path)) + name
val (tparams, alias) = x.infoType match {
case PolyType(typeRef, symbols) =>
(toTypeParameterTrees(symbols), toTypeRefForce(typeRef))
Expand Down Expand Up @@ -545,7 +569,7 @@ object ImportScalaDefinitions extends App {
NameTransformer
.decode(result)
// local adaptations below
.replace(".package.", ".") // we just flatten package objects anyway
.replace(".package", "") // we just flatten package objects anyway
.trim // didnt bother to figure out why this was needed
}
}
Expand Down
Loading

0 comments on commit d00885e

Please sign in to comment.