Skip to main content

bin/bash function

#!/bin/bash

function main() {
    echo "Creating plugin package..."
    local root=$(clean_folder $(dirname $(realpath "$0")))
    local path=$(prepare_package "$root")
    local output="$root/nord-jetbrains.jar"

    local jar=$(which jar)
    if [ ! -z "$jar" ]; then
        local output=$(pack_with_jar "$path" "$output")
        terminate "$output"
    fi

    local zip=$(which zip)
    if [ ! -z "$zip" ]; then
        local output=$(pack_with_zip "$path" "$output")
        terminate "$output"
    fi

    echo "Could not package plugin. Either zip or jar must be available, none found."
    exit 1
}

# signature: clean($root)
function clean_folder() {
    local root="$1"

    rm $root/nord-jetbrains.jar 2>/dev/null
    rm -rf $root/out 2>/dev/null

    echo $root
}

# signature: prepare_package($root)
function prepare_package() {
    local root="$1"
    local path="$root/out/production/nord-jetbrains"

    mkdir -p "$path"
    cp -r $root/resources/* $path
    cp -r $root/src/* $path

    echo $path
}

# signature: pack_with_jar($path, $output)
function pack_with_jar() {
    local path="$1"
    local output="$2"

    jar cMf $output -C $path . > /dev/null 2>&1

    echo $output
}

# signature: pack_with_zip($path, $output)
function pack_with_zip() {
    local path="$1"
    local output="$2"

    local current=$(pwd)
    cd $path
    zip -r $output . > /dev/null 2>&1
    cd $current

    echo $output
}

# signature terminate($path)
function terminate() {
    local path="$1"
    echo "Plugin package created: $path"
    exit 0
}

main