kotlin command line compiler - kotlin

How do I use the js command line compiler?
fun main(args: Array<String>): Unit {
println("Hello world!")
}
E:\kotlinc\bin>kotlinc-js -output test -sourceFiles test.kt
ERROR: E:\kotlinc\bin\test.kt: (4, 5) Unresolved reference: println
exec() finished with COMPILATION_ERROR return code
In the IDE I couldn't get js compilation to work either. Is there an up to date getting started guide for IntelliJ IDEA 12?

You need to specify a path to the library (kotlin-jslib.jar), use -libraryFiles command line option

Related

Why is the kotlin-gradle-plugin failing to create a PSIFile from CodeInsightTestFixture.configureByText?

I created an IntelliJ plugin using the template https://github.com/JetBrains/intellij-platform-plugin-template. The template comes with a test that runs on an XML file. I want to create a similar test for a Kotlin file. Here's the template test file plus my added test (test2):
package org.jetbrains.plugins.template
import com.intellij.ide.highlighter.XmlFileType
import com.intellij.psi.xml.XmlFile
import com.intellij.testFramework.TestDataPath
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import com.intellij.util.PsiErrorElementUtil
#TestDataPath("\$CONTENT_ROOT/src/test/testData")
class MyPluginTest : BasePlatformTestCase() {
fun testXMLFile() {
val psiFile = myFixture.configureByText(XmlFileType.INSTANCE, "<foo>bar</foo>")
val xmlFile = assertInstanceOf(psiFile, XmlFile::class.java)
assertFalse(PsiErrorElementUtil.hasErrors(project, xmlFile.virtualFile))
assertNotNull(xmlFile.rootTag)
xmlFile.rootTag?.let {
assertEquals("foo", it.name)
assertEquals("bar", it.value.text)
}
}
override fun getTestDataPath() = "src/test/testData/rename"
fun testRename() {
myFixture.testRename("foo.xml", "foo_after.xml", "a2")
}
// Here's my test
fun test2() {
val fileText: String = """
package com.loganmay.test
data class MyClass(val myString: String)
""".trimIndent()
val psiFile = myFixture.configureByText("a.kt", fileText)
val xmlFile = assertInstanceOf(psiFile, XmlFile::class.java)
}
}
Without changing the build.gradle file, that test fails with:
Expected instance of: com.intellij.psi.xml.XmlFile actual: com.intellij.psi.impl.source.PsiPlainTextFileImpl
I want it to parse the text as a PsiFile that's also a KtFile. From various sources, I've been led to believe that the fixture is parsing it as a plain text file because the test project doesn't have access to the Kotlin compiler. So, I added:
dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
}
to the build.gradle. Then, when I run the test, configureByText throws an exception with a big trace, the root exception of which is:
Caused by: java.lang.Throwable: 'filetype.archive.display.name' is not found in java.util.PropertyResourceBundle#4ecbb519(messages.CoreBundle)
... 53 more
org.jetbrains.plugins.template.MyPluginTest > test2 FAILED
com.intellij.diagnostic.PluginException at ComponentManagerImpl.kt:511
Caused by: java.util.MissingResourceException at Registry.java:164
Does anyone have any insight into what the issue is or know how to resolve it?
Notes:
I also tried importing the kotlin compiler and casting psiFile as KtFile, which produced the same error, an idea I got from here
This project has a test like this that may be working
This post and this post recommend adding the kotlin gradle plugin, which I did
This question seems similar
Yann Cebron replied on the jetbrains help forum with an answer for Java, which also worked for Kotlin.
The solution is to add a dependency to the IntelliJ gradle plugin. The template comes with these lines in the build.gradle:
intellij {
pluginName.set(properties("pluginName"))
version.set(properties("platformVersion"))
type.set(properties("platformType"))
// Plugin Dependencies. Uses `platformPlugins` property from the gradle.properties file.
plugins.set(properties("platformPlugins").split(',').map(String::trim).filter(String::isNotEmpty))
}
So, didn't need to do anything there. In my gradle.properties, I added
platformPlugins = com.intellij.java, org.jetbrains.kotlin
To my plugin.xml, I added:
<depends>com.intellij.modules.java</depends>
<depends>org.jetbrains.kotlin</depends>
I was able to remove
dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
}
from the build.gradle which I mentioned above.
Now, the test works for Java and Kotlin files.

How to run a command line command with Kotlin DSL in Gradle 6.1.1?

I am trying to run the code block below, after reading multiple posts on the topic and the Gradle manual. I run the below and get the following error: execCommand == null!
Any ideas on what I am doing wrong with the below code block?
open class BuildDataClassFromAvro : org.gradle.api.tasks.Exec() {
#TaskAction
fun build() {
println("Building data classes.....")
commandLine("date")
}
}
tasks.register<BuildDataClassFromAvro>("buildFromAvro") {
description = "Do stuff"
}
To define a Gradle task that runs a command-line using the Gradle Kotlin DSL do something like this in your build file:
task<Exec>("buildFromAvro") {
commandLine("echo", "test")
}
In the example above the commandLine will simply run echo, outputting the value test. So replace that with whatever you want to actually do.
You can then run that with gradle buildFromAvro
More info here: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html
If adding to an existing task:
exec {
commandLine("echo", "hi")
}
Another approach is to use the Java ProcessBuilder API:
tasks.create("MyTask") {
val command = "echo Hello"
doLast {
val process = ProcessBuilder()
.command(command.split(" "))
.directory(rootProject.projectDir)
.redirectOutput(Redirect.INHERIT)
.redirectError(Redirect.INHERIT)
.start()
.waitFor(60, TimeUnit.SECONDS)
val result = process.inputStream.bufferedReader().readText()
println(result) // Prints Hello
}
}

readLine() doesn't wait for user input in Kotlin/Native

Here is a simple script
fun main() {
print("ready> ")
val input = readLine()
println("User input: $input")
}
When I run this program with gradle runReleaseExecutableMacos I expect that I'll see a ready> prompt and will have a possibility to type some chars. But this program finishes immediately with User input: null as a result.
Am I missing something?
To achieve the behavior you desire, you can run the executable file produced by the Gradle. It will have an extension *.kexe.
Also, you can extend your build.gradle file with additional parameter. you got to add something like this:
macosX64("macos") {
binaries {
executable {
runTask.standardInput = System.in
}
}
}

Kotlin/Native Tensorflow

I created a tensorflow library and trying get tensorflow version in my kotlin script like that
import tensorflow.TF_Version
fun main(args: Array<String>) {
TF_Version()
}
idea can link TF_Version() method to library with Ctrl+click
and there(in library) i have method
fun TF_Version(): CPointer<ByteVar>? {
return interpretCPointer<ByteVar>(kniBridge0())
}
#SymbolName("tensorflow_kniBridge0")
private external fun kniBridge0(): NativePtr
but when i try to build and start i see this error
/tmp/konan_temp8584442034275821874/combined.o:ld-temp.o:function Konan_start: error: undefined reference to 'tensorflow_kniBridge0'
error: /home/fnasibov/.konan/dependencies/target-gcc-toolchain-3-linux-x86-64/x86_64-unknown-linux-gnu/bin/ld.gold invocation reported errors
Please help me fix that
Found solution:
my .def file was
headers = /home/user/.konan/third--arty/tensorflow/include/tensorflow/c/c_api.h
And after i add Opts to def file:
headers = /home/fnasibov/.konan/third-party/tensorflow/include/tensorflow/c/c_api.h
compilerOpts.linux = -I/usr/include -I/usr/include/x86_64-linux-gnu
linkerOpts.linux = -L/home/user/.konan/third-party/tensorflow/lib -ltensorflow
and regenerate klib - thats worked!

How to get rid of the compile time error (despite using the import) for assertThrows? (Java, Eclipse Oxygen)

I am testing out the following code snippet that I found here.
Eclipse Oxygen Version: Oxygen.2 Release (4.7.2) - if that matters
import org.junit.jupiter.api.Assertions;
....
#Test
void exceptionTesting() {
Executable closureContainingCodeToTest = () -> {throw new IllegalArgumentException("a message");};
Assertions.assertThrows(IllegalArgumentException.class, closureContainingCodeToTest, "a message");
}
However, the code doesn't compile.
I am getting the error below:
The method assertThrows(Class, Executable, String) in the type Assertions is not applicable for the arguments (Class, Executable, String) DbHandlerTest.java line 96 Java Problem
Of course my goal is not just to test the above snippet but to write a test for my code. Please help.
I figured out the problem ...
Thanks somuras for the right question.
Following import was wrong
import org.junit.jupiter.api.Executable;
It should have been this:
import org.junit.jupiter.api.function.Executable;