Skip to content

Instantly share code, notes, and snippets.

@jose-almir
Last active December 1, 2020 17:48
Show Gist options
  • Save jose-almir/0ce474a34b6b537bdbfca7884df610d7 to your computer and use it in GitHub Desktop.
Save jose-almir/0ce474a34b6b537bdbfca7884df610d7 to your computer and use it in GitHub Desktop.
ktexec - compile and optionally executes Kotlin file.
#!/bin/bash
###############################################################
# Script Name : ktexec #
# Description : Compile and optionally executes Kotlin File #
# Args : KOTLIN-FILE [-x] #
# Author : José Almir #
# Email : jrcodev@gmail.com #
###############################################################
function quit() {
echo -e "\033[1;0m"
[[ -z $1 ]] && exit 0
exit $1
}
function shwerr(){
echo -e "\033[1;31m[ERROR] $1"
}
FILENAME="${1%.*}"
EXTENSION="${1##*.}"
[[ ! $(which kotlinc 2>/dev/null) ]] && shwerr "Install Kotlin-compiler to compile kt code." && quit 1
[[ $EXTENSION != 'kt' || -z $1 ]] && shwerr "Pass a Kotlin file as parameter" && quit 1
[[ ! -e $1 ]] && shwerr "File not found" && quit 1
echo -e "\033[1;32m[COMPILING] $1 ..."
kotlinc $1 -include-runtime -d $FILENAME.'jar'
[[ $? -ne 0 ]] && shwerr "A error has occurred when compiling" && quit 1
echo -e "\033[1;32m[DONE] Kotlin file compiled."
[[ $2 == '-x' ]] && echo -e "\033[1;32m[RUNNING] $FILENAME.jar ...\n\033[1;0m" && java -jar $FILENAME.'jar'
quit
@jose-almir
Copy link
Author

A sample:
ktexec.sh MyFile.kt -x

@jose-almir
Copy link
Author

Using with vim

Copy the script to /usr/bin:
sudo cp ktexec.sh /usr/bin

Add this to your .vimrc:
:command KtExec :! ktexec.sh %:p -x

On vim normal mode, use:
:KtExec

Then the file will be compiled and executed.

😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment