|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +source ./src/say.sh |
| 4 | +source ./src/file.sh |
| 5 | +source ./src/string.sh |
| 6 | + |
| 7 | +# contains module array that are added to result. |
| 8 | +_usedModuleArray=(); |
| 9 | + |
| 10 | +# _uri.isfile(file) -> bool |
| 11 | +# An extention of path.isfile. |
| 12 | +# Args: |
| 13 | +# file (str) > takes file path. |
| 14 | +_uri.isfile(){ |
| 15 | + # checking. |
| 16 | + if path.isfile "${1}"; then |
| 17 | + return 0; |
| 18 | + else |
| 19 | + say.error "There is no such '${1}' file."; |
| 20 | + exit 1; |
| 21 | + fi |
| 22 | +} |
| 23 | + |
| 24 | +# _uri.hasSource(file) -> bool |
| 25 | +# Checks that source exists in the code or not. |
| 26 | +# Args: |
| 27 | +# file (str) > takes file path. |
| 28 | +_uri.hasSource(){ |
| 29 | + # checking that. |
| 30 | + grep -n '^source.*\.sh$' "${1}" &> /dev/null || |
| 31 | + # taking user file arg. |
| 32 | + #_uri.isfile "${1}"; |
| 33 | + exit 0; |
| 34 | +} |
| 35 | + |
| 36 | +# _uri.hasModuleAlready(module) -> bool |
| 37 | +# Tells you that module is already added or not. |
| 38 | +# Args: |
| 39 | +# module (str) > takes module as arg (eg: ./src/ask.sh). |
| 40 | +_uri.hasModuleAlready(){ |
| 41 | + [[ "${_usedModuleArray[*]}" == *"${1}"* ]] |
| 42 | +} |
| 43 | + |
| 44 | +# _uri.moduleVar(path) -> str |
| 45 | +# Gives you variable of module. |
| 46 | +# Args: |
| 47 | +# path (str) > takes module path. |
| 48 | +_uri.moduleVar(){ |
| 49 | + echo "${1}" | awk -F/ '{print $NF}' | awk -F. '{print $1}' |
| 50 | +} |
| 51 | + |
| 52 | +# _uri.chotuCode(file) |
| 53 | +# This returns you only required code from file. |
| 54 | +# Args: |
| 55 | +# file (str) > takes file path. |
| 56 | +_uri.chotuCode(){ |
| 57 | + # let us return code. |
| 58 | + grep -vE "^\s*#|^\s*$" "${1}" 2> /dev/null || |
| 59 | + # checking file exist or not if any errors. |
| 60 | + _uri.isfile "${1}"; |
| 61 | +} |
| 62 | + |
| 63 | +# _uri.startSourceStrip(file) -> str |
| 64 | +# Gives you first sourced script path. |
| 65 | +# Args: |
| 66 | +# file (str) > takes file path. |
| 67 | +# Returns: |
| 68 | +# path (str) > This returns you first sourced script path (eg: ./src/say.sh). |
| 69 | +_uri.startSourceStrip(){ |
| 70 | + # taking file arg from user. |
| 71 | + local ARGFile="${1}"; |
| 72 | + local Dir="./src"; |
| 73 | + local SourceStrip="$(grep -n '^source.*\.sh$' "${ARGFile}" | awk '{print $2}' | head -n 1)"; |
| 74 | + # grep -n "pattern" returns all source line no.s and lines also (seprated by :) |
| 75 | + # cut -d: -f1 cuts every line within : and provides us only first key using f1. |
| 76 | + # head -n 1 provides us only first line of no. |
| 77 | + # assign module variable. |
| 78 | + if text.startwith "${SourceStrip}" '"${Dir}"' || text.startwith "${SourceStrip}" '${Dir}'; then |
| 79 | + local SourceModule="${Dir}/$(echo "${SourceStrip}" | awk -F/ '{print $NF}')"; |
| 80 | + else |
| 81 | + local SourceModule="${SourceStrip}"; |
| 82 | + fi |
| 83 | + # checking file exist or not. |
| 84 | + _uri.isfile "${SourceModule}" && |
| 85 | + # let us return path. |
| 86 | + echo "${SourceModule}"; |
| 87 | + return 0; |
| 88 | +} |
| 89 | + |
| 90 | +# _uri.startSourceStrip.pos(file) -> int |
| 91 | +# Gives you first source position path of file. |
| 92 | +# Args: |
| 93 | +# file (str) > takes file path. |
| 94 | +# Returns: |
| 95 | +# pos (int) > returns you first source position. |
| 96 | +_uri.startSourceStrip.pos(){ |
| 97 | + # taking file arg from user. |
| 98 | + local ARGFile="${1}"; |
| 99 | + grep -n '^source.*\.sh$' "${ARGFile}" | cut -d: -f1 | head -n 1 || |
| 100 | + _uri.isfile "${ARGFile}"; |
| 101 | + # grep -n "pattern" returns all source line no.s and lines also (seprated by :) |
| 102 | + # cut -d: -f1 cuts every line within : and provides us only first key using f1. |
| 103 | + # head -n 1 provides us only first line of no. |
| 104 | +} |
| 105 | + |
| 106 | +# _uri.addModuleSource(module,file) |
| 107 | +# Adds module to given file. |
| 108 | +# Args: |
| 109 | +# module (str) > takes module path. |
| 110 | +# file (str) > takes file path. |
| 111 | +_uri.addModuleSource(){ |
| 112 | + # taking file arg from user. |
| 113 | + local ARGModule="${1}"; |
| 114 | + # taking result file arg from user. |
| 115 | + local ARGResultFile="${2}"; |
| 116 | + # convert module path to module only. |
| 117 | + local ARGModuleVar="$(_uri.moduleVar "${ARGModule}")"; |
| 118 | + # checking that module already added or not. if added this returns from function. |
| 119 | + _uri.hasModuleAlready "${ARGModuleVar}" && return 0; |
| 120 | + # checking catogories. |
| 121 | + if [[ "${PRIMARY_MODULES[*]}" == *"${ARGModuleVar}"* ]]; then |
| 122 | + # position of catogory hash. |
| 123 | + local PosMap="$(file.search.int "#@PRIMARY_MODULES" "${ARGResultFile}")"; |
| 124 | + elif [[ "${SECONDARY_MODULES[*]}" == *"${ARGModuleVar}"* ]]; then |
| 125 | + # position of catogory hash. |
| 126 | + local PosMap="$(file.search.int "#@SECONDARY_MODULES" "${ARGResultFile}")"; |
| 127 | + elif [[ "${TERTIARY_MODULES[*]}" == *"${ARGModuleVar}"* ]]; then |
| 128 | + # position of catogory hash. |
| 129 | + local PosMap="$(file.search.int "#@TERTIARY_MODULES" "${ARGResultFile}")"; |
| 130 | + else |
| 131 | + # position of catogory hash. |
| 132 | + local PosMap="$(file.search.int "#@OTHER_MODULES" "${ARGResultFile}")"; |
| 133 | + fi |
| 134 | + # installing module. |
| 135 | + file.append.vert "$(_uri.chotuCode "${ARGModule}")" "$(( PosMap+1 ))" "${ARGResultFile}" && |
| 136 | + # adding to array. |
| 137 | + _usedModuleArray+=("${ARGModuleVar}") && |
| 138 | + say.success "Module: '${ARGModuleVar}' is added." && |
| 139 | + # return function. |
| 140 | + return 0; |
| 141 | +} |
0 commit comments