Created
          May 10, 2023 10:19 
        
      - 
      
- 
        Save JoeBlakeB/be4423dc2915923b7450c73e531be29c to your computer and use it in GitHub Desktop. 
    A simple bash script to build and run a single kotlin file with build cache to only re compile if source file changes.
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | #!/bin/bash | |
| # | |
| # A simple bash script to build and run a single kotlin file | |
| # with build cache to only re compile if source file changes | |
| # | |
| # Copyright (C) 2023 Joe Baker (JoeBlakeB) | |
| # This program is free software under the GPLv3 license. | |
| # | |
| if [[ $1 == "-h" || $1 == "--help" ]]; then | |
| echo "Usage: kotlinRun filename [buildpath]" | |
| echo "filename The kotlin file you want to run" | |
| echo "buildpath Where you want the build files to be stored" | |
| exit 0 | |
| fi | |
| if [ -z "$1" ]; then | |
| echo "Error: filename argument is required." | |
| exit 1 | |
| fi | |
| filename="$1" | |
| buildpath="${2:-./build/}" | |
| if [ ! -f "$filename" ]; then | |
| echo "Error: $filename not found" | |
| exit 1 | |
| fi | |
| if [[ ! "$buildpath" == */ ]]; then | |
| buildpath="$buildpath/" | |
| fi | |
| mkdir -p "$buildpath" | |
| # Check the files hash against last time it was built | |
| currentHash=$(sha256sum "$filename") | |
| if [ -f "$buildpath$filename".txt ] && [ -f "$buildpath$filename".jar ]; then | |
| previousHash=$(cat "$buildpath$filename".txt) | |
| fi | |
| # Recompile kotlin file if needed, then run | |
| if [ ! "$currentHash" == "$previousHash" ]; then | |
| kotlinc "$filename" -include-runtime -d "$buildpath$filename".jar | |
| exitCode=$? | |
| if [ ! $exitCode -eq 0 ]; then | |
| echo "Error: kotlinc failed to compile $filename" | |
| rm "$buildpath$filename".jar | |
| exit $exitCode | |
| fi | |
| echo -n "$currentHash" > "$buildpath$filename".txt | |
| fi | |
| java -jar "$buildpath$filename".jar | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment