Skip to content

Commit de4a130

Browse files
committed
Initial Project
0 parents  commit de4a130

16 files changed

+1480
-0
lines changed

.github/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
A bash library to create standalone scripts.
2+
3+
There are some features of bash-sdk are mentioned here.
4+
* OOPS like code 💎.
5+
* Module based code 🗂️.
6+
* Similar functions to python 🐍.
7+
* Standalone script creation 📔.
8+
9+
You should to visit site to read docs and updates
10+
[here](https://ourcodebase.gitlab.io/bashsdk-docs/).

_uri.sh

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
}

builder.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/bash
2+
3+
source src/file.sh
4+
source src/say.sh
5+
source _uri.sh
6+
7+
PRIMARY_MODULES=(
8+
'cursor'
9+
'db'
10+
'file'
11+
'os'
12+
'say'
13+
'spinner'
14+
'string'
15+
'screen'
16+
)
17+
18+
SECONDARY_MODULES=(
19+
'inspect'
20+
'ask'
21+
)
22+
23+
TERTIARY_MODULES=(
24+
'repo'
25+
'package'
26+
'url'
27+
)
28+
29+
# handle args & assign source & result vars.
30+
while [[ ${#} -gt 0 ]]; do
31+
case "${1}" in
32+
-i)
33+
buildSourceFile="${2}";
34+
_uri.isfile "${buildSourceFile}";
35+
shift 2;
36+
;;
37+
-o)
38+
buildResultFile="${2}";
39+
if [[ -z "${buildResultFile}" ]]; then
40+
say.error "No such output variable.";
41+
exit 1;
42+
fi
43+
shift 2;
44+
;;
45+
*|?)
46+
builderDocs="Options:\n\t\t-i <file>\n\t\t-o <file>";
47+
say.error "Unknown Option: ${1}";
48+
say.warn "${builderDocs}";
49+
exit 1;
50+
;;
51+
esac
52+
done
53+
54+
# adding chotu source code to result.
55+
echo "$(_uri.chotuCode "${buildSourceFile}")" > "${buildResultFile}" &&
56+
say.success "Module: 'code' is added.";
57+
58+
# variable to store hash code.
59+
HashCode=$'#@PRIMARY_MODULES\n#@SECONDARY_MODULES\n#@TERTIARY_MODULES\n#@OTHER_MODULES';
60+
# variable to store hash code line.
61+
HashCodePos=1;
62+
# adding hash to file.
63+
file.append.vert "${HashCode}" "${HashCodePos}" "${buildResultFile}" &&
64+
say.success "Module: 'hash' is added.";
65+
66+
# This add shebang to output.
67+
file.append.vert $'#!/bin/bash\n' "1" "${buildResultFile}" &&
68+
say.success "Module: 'shebang' is added.";
69+
70+
# This is to adding main process.
71+
while (( 1<2 )); do
72+
# checking source.
73+
_uri.hasSource "${buildResultFile}";
74+
# first used source.
75+
StartStrip="$(_uri.startSourceStrip "${buildResultFile}")" &&
76+
# position of first used source.
77+
StartStripPos="$(_uri.startSourceStrip.pos "${buildResultFile}")" &&
78+
# pop that source position.
79+
file.pop "${StartStripPos}" "${buildResultFile}";
80+
# add that source module.
81+
_uri.addModuleSource "${StartStrip}" "${buildResultFile}";
82+
done

src/ask.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/bash
2+
3+
if (( 1<2 )); then
4+
# dir of current file.
5+
Dir="$(dirname "${BASH_SOURCE[0]}")";
6+
fi
7+
8+
source "${Dir}"/os.sh
9+
source "${Dir}"/say.sh
10+
source "${Dir}"/string.sh
11+
12+
# source os.sh
13+
# source say.sh
14+
# source string.sh
15+
16+
# Stores object that you selected.
17+
askChoice='';
18+
# Stores position of object that you selected.
19+
askReply='';
20+
21+
# ask.case(title) -> bool
22+
# This takes case ( yes or no ) for any work.
23+
# Args:
24+
# title (str) > takes title (eg: You're Agree).
25+
ask.case(){
26+
echo -ne "\n ${1}";
27+
read -p " ? [Y/n] " ARGS;
28+
echo;
29+
case "${ARGS}" in
30+
y|Y)
31+
return 0;
32+
;;
33+
n|N)
34+
say.error "Process Aborted.\n" &&
35+
exit 1;
36+
;;
37+
*)
38+
say.error "You have to enter only\n \t\t'Y' for Yes &\n \t\t'n' for No.\n" &&
39+
exit 1;
40+
;;
41+
esac
42+
}
43+
44+
# ask.choice(title,list) -> var
45+
# This creates a simple menu.
46+
# Args:
47+
# title (str) > takes title (eg: Choose One).
48+
# list (array) > takes array as arg.
49+
# Returns:
50+
# object (str) > result in 'askChoice' & 'askReply' variables.
51+
ask.choice(){
52+
PS3="
53+
${1} > ";
54+
local ARGs=("${@}");
55+
local ARGs2=("${ARGs[@]:1}");
56+
echo;
57+
select ARG in "${ARGs2[@]}"
58+
do
59+
if ! text.isdigit "${REPLY}"; then
60+
say.error "You can only input 'digits'.\n";
61+
exit 1;
62+
fi
63+
if [[ "${REPLY}" -gt "${#ARGs2[@]}" ]]; then
64+
say.error "You should input correct digits.\n";
65+
exit 1;
66+
fi
67+
askChoice="${ARG}";
68+
askReply="${REPLY}";
69+
break;
70+
done
71+
return 0;
72+
}
73+
74+
# ask.storagePermission() -> bool
75+
# Ask for storage permission in diffrent os.
76+
ask.storagePermission(){
77+
if os.is_termux; then
78+
say.warn "Asking for storage permission";
79+
termux-setup-storage;
80+
say.checkStatus "${?}";
81+
fi
82+
}

src/cursor.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
# setCursor(on~off)
4+
# Switch terminal cursor easily.
5+
setCursor(){
6+
setterm -cursor "${1}";
7+
}

0 commit comments

Comments
 (0)