-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain
executable file
·141 lines (123 loc) · 4.33 KB
/
main
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
if [[ "$BASHRC_LOADED" != "yes" ]]; then
if command -v gdate &> /dev/null; then
function unix_nanos {
gdate +%s.%N
}
else
function unix_nanos {
date +%s.%N
}
fi
# Record this directory for later reference
if [ -n "$BASH_VERSION" ]; then
export BASHRC_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
elif [ -n "$ZSH_VERSION" ]; then
export BASHRC_DIR="$( cd "$( dirname "$0" )" && pwd )"
setopt SH_WORD_SPLIT
fi
load_all_scripts_in_dir () {
local SCRIPT_D_DIR
local SCRIPT_D_SCRIPT
local SCRIPT_BASE_SORT=""
TOTAL_SOURCE_START=`unix_nanos`
for SCRIPT_D_DIR in "$@"; do
if [[ -d "$SCRIPT_D_DIR" ]]; then
# Apply additional profile scripts if they exist
for SCRIPT_D_SCRIPT in "$SCRIPT_D_DIR"/*.sh; do
# If the file is readable
if [ -r "$SCRIPT_D_SCRIPT" ]; then
SCRIPT_BASE_SORT="$SCRIPT_BASE_SORT"$'\n'"$(basename $SCRIPT_D_SCRIPT):$SCRIPT_D_SCRIPT"
fi
done
fi
done
SCRIPT_BASE_SORT="$(echo "$SCRIPT_BASE_SORT" | LC_COLLATE=C sort)"
# echo "Sorted scripts:"
# echo "$SCRIPT_BASE_SORT"
for SCRIPT_D_SCRIPT in `echo "$SCRIPT_BASE_SORT" | cut -d: -f2`; do
if [[ "$MEASURE_SCRIPT_LOAD_TIME" == "yes" ]]; then
UNIX_START_TIME=`unix_nanos`
echo "Sourcing: $SCRIPT_D_SCRIPT"
fi
source "$SCRIPT_D_SCRIPT"
if [[ "$MEASURE_SCRIPT_LOAD_TIME" == "yes" ]]; then
echo "Done: $SCRIPT_D_SCRIPT"
UNIX_END_TIME=`unix_nanos`
SLOW_TO_SOURCE=`echo $UNIX_END_TIME $UNIX_START_TIME | awk '{print ((($1-$2)) > 0.2)}'`
TOTAL_TIME_IN_MS=`echo $UNIX_END_TIME $UNIX_START_TIME | awk '{ printf("%0.2f",$1-$2) }'`
if [ -z "$SUPPRESS_SLOW_SOURCE_WARNING" ]; then
if [[ "$SLOW_TO_SOURCE" == "1" ]]; then
msg-error "It took ${TOTAL_TIME_IN_MS}s to load $SCRIPT_D_SCRIPT"
else
msg-info "It took ${TOTAL_TIME_IN_MS}s to load $SCRIPT_D_SCRIPT"
fi
fi
fi
done
TOTAL_SOURCE_END=`unix_nanos`
SLOW_TO_SOURCE=`echo $TOTAL_SOURCE_END $TOTAL_SOURCE_START | awk '{print ((($1-$2)) > 2)}'`
TOTAL_TIME_IN_MS=`echo $TOTAL_SOURCE_END $TOTAL_SOURCE_START | awk '{ printf("%0.2f",$1-$2) }'`
if [ -z "$SUPPRESS_SLOW_SOURCE_WARNING" ]; then
if [[ "$SLOW_TO_SOURCE" == "1" ]]; then
msg-error "
It took ${TOTAL_TIME_IN_MS}s to load all scripts in these directories: $@
If you want to find the scripts that are slow, add the following to the top of your ~/.bashrc or ~/.zshrc or ~/.bash_profile file:
MEASURE_SCRIPT_LOAD_TIME=yes
To suppress this message, add this line instead:
SUPPRESS_SLOW_SOURCE_WARNING=yes
"
fi
fi
}
load_all_scripts_in_dir "$BASHRC_DIR/scripts/src"
# Bash aliases
if [ -f $BASHRC_DIR/aliases ]; then
source $BASHRC_DIR/aliases
fi
# Default variables
if [[ "$HOST_ALIAS" == "" ]]; then
if [ -n "$BASH_VERSION" ]; then
HOST_ALIAS='\H'
elif [ -n "$ZSH_VERSION" ]; then
HOST_ALIAS="%m"
fi
fi
if [[ "$USER" == "" ]]; then
USER="${USERNAME}"
fi
function assert-path {
if [ -d "$1" ]; then
if ! echo ":${PATH}:" | grep -q ":${1}:"; then PATH="$PATH:$1" ; fi
fi
export PATH
}
assert-path "${HOME}/bin"
assert-path "/bin"
assert-path "/usr/bin"
assert-path "/usr/local/bin"
assert-path "/sbin"
assert-path "/usr/sbin"
assert-path "/usr/local/sbin"
assert-path "/opt/python2.7/bin"
assert-path "$BASHRC_DIR/bin"
if [ -n "$BASH_VERSION" ]; then
PS1='$(ps1_msg)'"\[${CUSER}\]\u@\[${CYELLOW}\]${HOST_ALIAS}:\[${CGREEN}\]\w\[${CDEFAULT}\] "$'\n$ '
elif [ -n "$ZSH_VERSION" ]; then
precmd() { eval "$PROMPT_COMMAND"; }
PS1='$(ps1_msg)'"${CUSER}%n@${CYELLOW}${HOST_ALIAS}:${CGREEN}%~${CDEFAULT}"$'\n$ '
# PS1="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%~ %{$reset_color%}%% "
fi
# Bash history improvements
if [ -f $BASHRC_DIR/history ]; then
. $BASHRC_DIR/history
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
. $BASHRC_DIR/secrecy.sh
# You can extend your bashrc with an entry in your local ~/profile.d
load_all_scripts_in_dir "$HOME/profile.d"
BASHRC_LOADED="yes"
fi