We are writing a DSL in Kotlin, and we have added a few restrictions in that DSL.
For eg,
table {
tr {
td {
}
}
}
Here, we restrict td should not be called inside another td.
How do we test this? Is there any dsl-testkit or matcher library in kotlin that lets us do something like "shouldNot compile" does in scalatest
One solution is to use the Kotlin scripting engine, make it run a script with your DSL constructs that should be rejected and assert that the script didn't compile.
Links that you may find useful:
Run Kotlin Scripts (kts) from regular Kotlin programs by Kotlin Expertise Blog
s1monw1/KtsRunner, a library that encapsulates running Kotlin scripts programmatically
A newer and more powerful, though more complex scripting support implementation on the Kotlin side is described in KEEP-75
Related
I'm having hard time converting the following groovy script to a kotlin script (build.gradle -> build.gradle.kts).
model {
components {
main(NativeExecutableSpec) {
sources {
// ...
}
}
}
}
All the docs I found Build native software, Setup JNI development in Gradle project all have groovy syntax, but none of them have kotlin syntax. Tried finding on other OS projects but found nothing.
I was trying to find some of the type info to get some clue:
And trying out the stuff in kts:
val model by tasks.getting(ModelReport::class) {
components {
// ...
}
}
But seems like there's no property called components in that object.
Basically can't figure out anything on my own now, if somebody can help it will be greatly appreciated!
The old native plugin that you try to use (c) usees the rule-based software model.
Those plugins are (or will be) deprecated and instead the replacements like cpp-library should be used.
The tutorial link you posted also has a link to an example of how to use the replacement plugins in Groovy DSL that you should be able to port to Kotlin DSL easily.
Besides that, https://docs.gradle.org/current/userguide/kotlin_dsl.html states in the section "Limitations":
The Kotlin DSL will not support the model {} block, which is part of the discontinued Gradle Software Model. However, you can apply model rules from scripts — see the model rules sample for more information.
Is there a way to add a piece of top level KDoc for a Kotlin file?
Since Kotlin supports multiple variables, functions, classes, etc. in a single file, it makes sense to document the file as a whole. However, Documenting Kotlin Code - Kotlin Programming Language seems not to have any instructions on this.
There's no such feature; however, packages and modules can be documented like in Java
In Dokka, additional documentation files are added with include property (e.g. Gradle configuration).
I'm trying to write a maven plugin with kotlin DSL that creates a configuration object. The plugin will depend on that configuration during its operation.
I think I need to use the DSL like following: in runtime load a separate file with kotlin script, execute it and assign result to a variable in the plugin code.
Is there any good way to do it?
It turns out there is a simple answer:
https://github.com/JetBrains/kotlin/blob/master/libraries/examples/kotlin-jsr223-local-example/
I've started looking into Kotlin and I'd like to use it instead of TypeScript for my front end language. Both compile down to JavaScript so I'd like to set it up so that I make a Kotlin file, lets call it myFile.kt, then when I run the compiler it makes a myFile.js file.
Just like how TypeScript takes a myFile.ts and compiles it to a myFile.js.
I'm using the latest version of IntelliJ 15 with release candidate 1 of Kotlin.
I've been searching all over the internet for the way to do this but so far all I've found is setting up IntelliJ so that Kotlin creates a JavaScript library from your code in a JAR file. I also haven't been able to get that to compile any of the kt files in my project (they were previously js files).
Is what I'd like to do currently possible or am I thinking about this in the wrong way?
I think this should help:
https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/kotlin2JsProject/libraryProject/build.gradle
There are sample of library project. JS sources can be founded in buildDir.
So yes, you can get js files from Kotlin sources.
Kotlin language a very different from JavaScript (even ES6), so you can't just rename js to kt, this will not work. You need rewrite javascript source files to Kotlin.
For example:
console.log('Hello, World!');
Should be rewritten as:
fun main(args: Array<String>) {
println("Hello, World!")
}
I've been searching for a way to dynamically compile AIR apps on the go.
Specifically, I'd need the title of the program and a couple variables changed on compile. I'll be creating hundreds, if not thousands, of versions of what is essentially the same program so I'd like to avoid doing it by hand :)
Is this possible with AIR? If not, could I do it with something like Java?
Yes, this is possible using the mxml compiler in the Flex sdk. It requires some knowledge of a build tool like ant or maven.
Automate the build process with apache ant or maven.
Use Actionscript compiler directives to define place holders for your variable data in your code.
Modify the build scripts to compile the build, and pass in the variable data as compiler arguments
I've done this with ant build scripts to include a timestamp and other info in the app's version string. Here's example with a timestamp...
1. Define variables in the compiler argument options for your project
The syntax is:
-define=NAMESPACE::VARIABLE_NAME,'variableValue'
For example:
-define=APPDATA::TIMESTAMP,"xxx"
This is what a developer would put in their IDE. When they compile their apps, the version string will have "xxx" in it. When the automated build script compiles the app, it passes in an actual timestamp using a similar argument.
2. Reference the variable in your code.
I'm using it as a String, but you can do Boolean and I assume the other primitive types.
public var appVersion:String = "MyApp " + APPDATA::TIMESTAMP;
You can conditionally compile code into the SWF with booleans:
-define=CONFIG::DEBUG,true
Now wrap code blocks with this compiler directive, and if true it will be compiled into the swf:
public function something():int
{
var a:int = 1;
CONFIG::DEBUG
{
a=2;
}
return a;
}
While this is pretty handy, I recommend only using compiler directives for things you really need to do at compile time, and not for general configuration :)
Resources:
Using conditional compilation
mxmlc compiler options
flex ant tasks
flex mojos maven plugins