summaryrefslogtreecommitdiff
path: root/japh
blob: b9b8af14e555a333f0a8a1e6186d5bcb6f264d45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash

# Jackson's Awesome Project Helper
# Jackson Taylor - 12/2/2020
# See README for more information

JAPH_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
CMD_FILE="$JAPH_DIR/cmds"

EDITOR="vim"

function japh() {
    case $1 in
        "add")
            dir=$(pwd)
            printf "function cd_$2() { cd \"$dir\"; }\n" >> $JAPH_DIR/cmds
            echo "project added!"
            ;;
        "-a")
            # Show the currently added commands
            cat $CMD_FILE
            ;;
        "n")
            if [ -z $2 ]; then
                echo "Command must not be empty!"
                return
            fi

            cmd_name=$2

            cmd_tmp_file=/tmp/japh_$cmd_name

            $EDITOR $cmd_tmp_file

            if [ ! -s $cmd_tmp_file ]; then
                echo "You must have a command!"
            else
                printf "function cmd_$cmd_name() {\n" >> $JAPH_DIR/cmds
                cat $cmd_tmp_file >> $JAPH_DIR/cmds
                printf "\n}\n" >> $JAPH_DIR/cmds
                rm $cmd_tmp_file
            fi
            ;;
        "r")
            if [ -z $2 ]; then
                echo "Command must not be empty!"
                japh_usage
                return
            fi

            source $JAPH_DIR/cmds
            type cmd_$2 &> /dev/null
            status=$?

            if [ $status -eq 0 ];
            then
                cmd_$2
            else
                echo "Command was not found!"
                japh_usage
            fi
            ;;
        "help" | "-h")
            japh_usage
            ;;
        *)
            if [ ! -z $1 ];
            then
                source $JAPH_DIR/cmds
                type cd_$1 &> /dev/null
                status=$?

                if [ $status -eq 0 ];
                then
                    cd_$1
                    [ -d .git ] && git status;
                else
                    echo "Command was not found!"
                    japh_usage
                fi
            else
                japh_usage
            fi
            ;;
   esac
}

function japh_usage() {
    printf "JAPH - Jackson's Awesome Project Helper\n" 
    printf "japh add \$projectName - Add current directory to japh's list as \$projectName\n" 
    printf "japh \$projectName - cd to project directory\n"
    printf "japh n \$commandName - Open new editor so you can add series of steps in a command.\n"
    printf "japh r \$commandName - Run \$commandName in current session.\n"
}