-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Rafael Raposo <[email protected]>
- Loading branch information
Showing
10 changed files
with
290 additions
and
148 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"sbt","version":"1.10.1","bspVersion":"2.1.0-M1","languages":["scala"],"argv":["/Users/rafaelraposo/.sdkman/candidates/java/11.0.22-amzn/bin/java","-Xms100m","-Xmx100m","-classpath","/Users/rafaelraposo/Library/Application Support/JetBrains/IntelliJIdea2024.1/plugins/Scala/launcher/sbt-launch.jar","-Dsbt.script=/Users/rafaelraposo/.sdkman/candidates/sbt/current/bin/sbt","xsbt.boot.Boot","-bsp"]} |
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,10 @@ | ||
import org.flyte.flytekitscala.FlytekitScalaPlugin | ||
|
||
ThisBuild / version := "0.4.60-SNAPSHOT" | ||
|
||
ThisBuild / scalaVersion := "2.13.14" | ||
|
||
lazy val root = (project in file(".")) | ||
.settings( | ||
name := "flytekit-examples-scala_2.13", | ||
).enablePlugins(FlytekitScalaPlugin) |
This file was deleted.
Oops, something went wrong.
136 changes: 136 additions & 0 deletions
136
flytekit-examples-scala/project/FlytekitScalaPlugin.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,136 @@ | ||
package org.flyte.flytekitscala | ||
|
||
import io.github.classgraph.{ClassGraph, ClassInfo, ClassInfoList, ScanResult} | ||
import sbt.Keys._ | ||
import sbt._ | ||
|
||
import scala.collection.JavaConverters.* | ||
|
||
object FlytekitScalaPlugin extends AutoPlugin { | ||
val autoImport = FlytekitJavaKeys | ||
import autoImport._ | ||
|
||
private val MetaInfoServiceFileNames = Seq( | ||
"org.flyte.flytekit.SdkRunnableTask", | ||
"org.flyte.flytekit.SdkDynamicWorkflowTask", | ||
"org.flyte.flytekit.SdkPluginTask", | ||
"org.flyte.flytekit.SdkContainerTask", | ||
"org.flyte.flytekit.SdkWorkflow", | ||
"org.flyte.flytekit.SdkLaunchPlanRegistry" | ||
) | ||
|
||
override def trigger: PluginTrigger = noTrigger | ||
|
||
|
||
|
||
override lazy val projectSettings: Seq[Def.Setting[_]] = Seq( | ||
flyteVersion := "0.4.58", | ||
libraryDependencies ++= | ||
Seq( | ||
"org.flyte" % "flytekit-api" % flyteVersion.value, | ||
"org.flyte" %% "flytekit-scala" % flyteVersion.value, | ||
"org.flyte" % "flytekit-testing" % flyteVersion.value % Test | ||
), | ||
// add flyte generated services after compilation as a jar resource | ||
// note that we first have to remove potentially duplicated META-INF/services | ||
// files to address a failure path like: | ||
// $ sbt clean pack | ||
// And then build project in IntelliJ (where generated files are copied to target/classes folder) | ||
// $ sbt pack # this will result duplicated files and fail the build | ||
Compile / packageBin / mappings := | ||
(Compile / packageBin / mappings).value | ||
.filterNot(v => MetaInfoServiceFileNames.contains(v._1.getName)) ++ | ||
flyteGenerateServicesTask(Compile) | ||
.map(_.map(f => (f, s"META-INF/services/${f.getName}"))) | ||
.value, | ||
// add flyte generated services after compilation as a test resource | ||
Test / resourceGenerators += flyteGenerateServicesTask(Test) | ||
) | ||
|
||
private def flyteGenerateServicesTask(configKey: ConfigKey) = Def.task { | ||
val log = (configKey / streams).value.log | ||
val classPath = (Runtime / fullClasspath).value.map(_.data.getAbsolutePath) | ||
val classGraph = new ClassGraph().overrideClasspath(classPath: _*) | ||
val result = classGraph.enableMethodInfo().scan() | ||
try { | ||
MetaInfoServiceFileNames | ||
.filter(fileName => result.getClassInfo(fileName) != null) // in case old version of flytekit-java | ||
.map { fileName => | ||
val impls = getClassesImplementingOrExtending(result, fileName, log) | ||
impls.foreach(x => log.info(s"Discovered $fileName: $x")) | ||
val services = impls.mkString("\n") | ||
val file = (configKey / classDirectory).value / "META-INF" / "services" / fileName | ||
IO.write(file, services) | ||
file | ||
} | ||
} finally { | ||
result.close() | ||
} | ||
} | ||
|
||
private def getClassesImplementingOrExtending( | ||
result: ScanResult, | ||
className: String, | ||
log: Logger | ||
): List[String] = { | ||
val classesOrInterfaces = | ||
if (result.getClassInfo(className).isInterface) { | ||
result.getClassesImplementing(className) | ||
} else { | ||
result.getSubclasses(className) | ||
} | ||
|
||
warnAnonymousClasses(classesOrInterfaces, log) | ||
|
||
val subClasses = | ||
classesOrInterfaces | ||
.filter(x => !x.isAbstract && !x.isAnonymousInnerClass) | ||
|
||
failIfMissingDefaultConstructor(subClasses, log) | ||
|
||
val subInterfaces = classesOrInterfaces.getInterfaces.getNames.asScala.toList | ||
val subAbstractClasses = classesOrInterfaces.filter(_.isAbstract).getNames.asScala.toList | ||
|
||
val all = subClasses.getNames.asScala.toList ++ | ||
subInterfaces.flatMap(getClassesImplementingOrExtending(result, _, log)) ++ | ||
subAbstractClasses.flatMap(getClassesImplementingOrExtending(result, _, log)) | ||
|
||
all.distinct | ||
} | ||
|
||
private def warnAnonymousClasses( | ||
classesOrInterfaces: ClassInfoList, | ||
log: Logger | ||
): Unit = { | ||
classesOrInterfaces | ||
.filter(_.isAnonymousInnerClass) | ||
.forEach( | ||
x => | ||
log.warn( | ||
s"Anonymous class ${x.getName} cannot be used to implement Flyte entities" | ||
) | ||
) | ||
} | ||
|
||
private def failIfMissingDefaultConstructor(classes: ClassInfoList, log: Logger): Unit = { | ||
val classesMissingDefaultConstructor = classes.filter(hasNoDefaultConstructor) | ||
|
||
if (!classesMissingDefaultConstructor.isEmpty) { | ||
classesMissingDefaultConstructor.forEach( | ||
x => log.error(s"Class ${x.getName} has no default constructor defined") | ||
) | ||
|
||
throw new MessageOnlyException( | ||
"One or more classes implementing Flyte entity have no default constructor defined" | ||
) | ||
} | ||
} | ||
|
||
private def hasNoDefaultConstructor(clazz: ClassInfo): Boolean = | ||
clazz.getDeclaredConstructorInfo.filter(_.getParameterInfo.isEmpty).isEmpty | ||
} | ||
|
||
object FlytekitJavaKeys { | ||
// don't override defaults for these settings unless you want to use unstable version | ||
lazy val flyteVersion = settingKey[String]("Flyte version") | ||
} |
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 @@ | ||
sbt.version = 1.10.1 |
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,6 @@ | ||
import scala.collection.immutable.Seq | ||
|
||
|
||
libraryDependencies ++=Seq( | ||
"io.github.classgraph" % "classgraph" % "4.8.87" | ||
) |
1 change: 0 additions & 1 deletion
1
...mples-scala/src/main/resources/META-INF/services/org.flyte.flytekit.SdkLaunchPlanRegistry
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
...it-examples-scala/src/main/resources/META-INF/services/org.flyte.flytekit.SdkRunnableTask
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
flytekit-examples-scala/src/main/resources/META-INF/services/org.flyte.flytekit.SdkWorkflow
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.