Script to Speed Up Bash Dev

This script grabs the last modified file in your defined directory, makes it executable, and then runs it in a new terminal window.

…Not sure this was the best way to solve the Qubes Vscode bottleneck… but hey, it works.

Save as: run_last_modified.sh.

Cheers

#!/bin/bash
# Runs last modified script in new Terminal window tab in dev qube.
# Requires xdotools to be installed in the dev qube.
# (debian) sudo apt install xdotool / (fedora) sudo dnf install xdotool

# Add hotkey to custom shortcuts.  Change 'dev' to match your development qube name.
# qvm-run dev '/home/user/Documents/run_last_modified.sh' 

# Global vars
qube_name='dev' # Change 'dev' to match your development qube name.
code_directory='/home/user/Documents/' # change to match your working code directory.
last_modified='' # Last modified file in code directory.
file_path='' # Full file path of last modified file.

# Saves current focused window.
save_file(){
    # Simulates ctr+s keystrokes in window with focus.
    # Requires xdotools installed.
    xdotool key ctrl+s
}

# Stops script if there is no code directory.
no_code_directory_error(){
    msg="There was a problem accessing $code_directory in $qube_name qube.\
    Check your code directory setting and try again."
    notify-send "$msg";echo "$msg"
    exit
}

# Gets the last modified file in the code directory.
get_filename(){
    cd "$code_directory" || no_code_directory_error # Stop script if directory doesn't exist.
    last_modified=$(ls -t | head -n1)
    echo "$last_modified"
}

# Returns full file path.
get_full_path(){
    # $1=filename
    file_path="$code_directory$1"
    echo "$file_path"
}

# Stops script if last modified file is this one.
check_filename(){
    # $1=file name
    # Guard clause to prevent recursive call.
    if [ "$1" == 'run_last_modified.sh' ]; then
        msg='Last modified file is run_last_modified.sh.
            Stopping execution...'
        notify-send "$msg";echo "$msg"
        exit
    fi
    echo "$1"
}

# Makes file executable if it isn't already.
make_executable(){
    #$1=file_path
    msg="Making $file_path executable..."
    # notify-send "$msg";echo "$msg"
    echo "$msg"
    sudo chmod +x "$1"
    echo "$1"
}

# Opens a new terminal window and runs the file.
run_in_terminal(){
    # $1=file_path
    msg="Running $1 
        in $qube_name Terminal..."
    notify-send "$msg";echo "$msg"

    sudo gnome-terminal --tab --wait --active --geometry=120X60 \
    --title="$1" --working-directory="$code_directory" \
    -- bash -c "$1; exec bash"
}

run(){
    # Pipeline
    save_file # Emulates ctrl+s in the current active window.
    last_modified=$(get_filename)
    file_path=$(get_full_path "$last_modified")
    check_filename "$last_modified"
    make_executable "$file_path" 
    run_in_terminal "$file_path"
}

run

################## TESTS #################
# Note to self: Comment out run ^^^ before running tests.

tests(){
    set -x # for debugging.

    # Save current file in active window test.
    # save_file

    # get filename test.
    last_modified=$(get_filename)
    msg="Last modified: $last_modified"
    notify-send "$msg";echo "$msg"

    # get filepath test.
    file_path=$(get_full_path "$last_modified")
    msg="File path: $file_path"
    notify-send "$msg";echo "$msg"

    # Make executable tests.
    make_executable "$file_path"

    # Run in Terminal test.
    # Note.  Sample script called test.sh
    # Must be placed in code directory before running test.
    last_modified='test.sh'
    file_path=$(get_full_path "$last_modified")
    make_executable "$file_path"
    run_in_terminal "$file_path"

    # Check filename test.
    # Comment out when running other tests.
    last_modified='run_last_modified.sh'
    # check_filename "$last_modified" # Should stop execution.

}

# tests