Is there a way to set a base .kts file as implemen...
# announcements
a
Is there a way to set a base .kts file as implementing an interface? I’m hacking around with deploying a .kts to Google Cloud Functions (for fun/the challenge). I can do it (define
class Operator : HttpFunction
in Main.kts, compile to .jar, then in GCF set the target to
main.Main$Operator
), but I don’t like that
$
. So if the .kts could implement the interface, then my minor annoyance would be cured. Or if it’s not possible, at least my curiousity eased :)
Main.kts
- defines a class that implements
HttpFunction
Copy code
package main

import com.google.cloud.functions.*

class Operator : HttpFunction {
  override fun service(request: HttpRequest, response: HttpResponse) {
    TODO("Not yet implemented")
  }
}
Main.kts
is compiled to
main.Main.class
, which extends
ScriptTemplateWithArgs
.
Copy code
➜ javap -cp build/libs/experiment-all.jar main.Main          
Compiled from "Main.kts"
public class main.Main extends kotlin.script.templates.standard.ScriptTemplateWithArgs {
  public static final void main(java.lang.String[]);
  public main.Main(java.lang.String[]);
}
class Operator
is compiled to
main.Main$Operator
.
Copy code
➜ javap -cp build/libs/experiment-all.jar main.Main\$Operator
Compiled from "Main.kts"
public final class main.Main$Operator implements com.google.cloud.functions.HttpFunction {
  public void service(com.google.cloud.functions.HttpRequest, com.google.cloud.functions.HttpResponse);
  public main.Main$Operator();
}
u
Unfamiliar with google cloud functions and kotlin scripting but id say your best bet is to create your own custom script definition. The script would extend the custom definition class which I assume you can just implement http function there. That being said not sure if that uh works with what you're doing. Take a look at https://github.com/Kotlin/KEEP/blob/master/proposals/scripting-support.md
a
thanks, that does look like the answer
so I can make a definition like this
Copy code
@KotlinScript(fileExtension = "gcf.kts")
interface GcfScript : HttpFunction
then a file called
Main.gcf.kts
should be made to implement HttpFunction
u
From my understanding yes that might do it. Also GcfScript probably need to be an abstract class instead of interface.
a
I will give both a go 👍
I’m just trying to figure out how to make gradle scan the srcDir for script definitions
u
If you look in that KEEP you'll find a way to make IDE aware of custom definition, referred to as discovery. One way is to make a folder in resources called META-INF/kotlin/script/template or something and inside create a file with the fully qualified name of the custom definition class
Also possible to do it via command line apparently but not really helpful for gradle/IDE. Might even have to restart your IDE and pray. Kotlin scripting is really wonky at times. I believe custom definitions is broken in 2021.1
a
mmmm yeah
I was thinking one of these options might be the cli equivalent
u
Edited previous message but yeah just creating the folder and file in resources and implementing the project that has the definition class in the project that will have the scripts should work. Also not sure if this is relevant but worth checking out https://github.com/Kotlin/KEEP/issues/75#issuecomment-572519766
`kotlinScriptDef`… but I want to have the current project, not a different one
u
Hmm you most likely won't need to touch gradle file then, just the resource file. If it's still problematic then give the KEEP a read. Also might be able to get better help at #C0BT46EL8 :p
a
I can’t get the script def picked up, but that’s okay, it’s not important. I’m going to move on to another task. Thanks for your help :) I’ll take it over to #C0BT46EL8 if I pick it up again
u
Not a surprise, I'm also having issues with it. Worth noting that latest IJ (2021.1) wont pick it up. Anyways, good luck 😛
🙏 1