-
Notifications
You must be signed in to change notification settings - Fork 2
/
env
137 lines (114 loc) · 3.31 KB
/
env
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
#!/usr/bin/env bash
# env -- envvars & standard library for dotfiles; don't symlink me!
# Can be sourced by zsh/bash scripts
export XDG_CACHE_HOME=~/.cache
export XDG_CONFIG_HOME=~/.config
export XDG_DATA_HOME=~/.local/share
export XDG_BIN_HOME=~/.local/bin
DOTFILES="$(cd $(dirname "${BASH_SOURCE:-${(%):-%x}}") && pwd -P)"
export DOTFILES
export DOTFILES_DATA="$XDG_DATA_HOME/dotfiles"
export DOTFILES_ASSETS="$DOTFILES/assets"
for dir in "$XDG_CACHE_HOME" "$XDG_CONFIG_HOME" "$XDG_DATA_HOME" "$XDG_BIN_HOME" "$DOTFILES_DATA"; do
[[ -d $dir ]] || mkdir -p "$dir"
done
alias dot="${DOTFILES}/deploy"
## Library
function is_backports_configured {
grep -q "^deb .*backports" /etc/apt/sources.list /etc/apt/sources.list.d/*.list >/dev/null 2>&1
}
function _is_interactive { [[ $- == *i* || -n $EMACS ]]; }
function _is_running {
for prc in "$@"; do
pgrep -x "$prc" >/dev/null || return 1
done
}
function _is_callable {
for cmd in "$@"; do
command -v "$cmd" >/dev/null || return 1
done
}
function _source {
[[ -f $1 ]] && source "$1"
}
function _load {
case $1 in
/*) source "$1" ;;
*) source "$DOTFILES/$1" ;;
esac
}
_load_all() {
setopt NULL_GLOB # Prevent "no matches found" error
for file in "$DOTFILES_DATA"/*.topic/"$1"; do
[ -e "$file" ] && source "$file"
done
unsetopt NULL_GLOB
}
function _load_repo {
_ensure_repo "$1" "$2" && source "$2/$3" || >&2 echo "Failed to load $1"
}
function _ensure_repo {
local target=$1
local dest=$2
local branch=$3
if [[ ! -d $dest ]]; then
if [[ $target =~ "^[^/]+/[^/]+$" ]]; then
url=https://github.com/$target.git
elif [[ $target =~ "^[^/]+$" ]]; then
url=https://github.com/$USER/$target.git
fi
[[ -n ${dest%/*} ]] && mkdir -p ${dest%/*}
echo "clone --recursive -b $branch $url $dest"
if [[ -z $branch ]]; then
echo "git clone --recursive $url $dest"
git clone --recursive "$url" "$dest" || return 1
else
git clone --recursive -b "$branch" "$url" "$dest" || return 1
fi
fi
}
function _os {
case $OSTYPE in
linux*) if [[ -f /etc/arch-release ]]; then echo arch
elif [[ -f /etc/debian_version ]]; then echo debian
elif [[ -f /etc/fedora-release ]]; then echo fedora
fi ;;
darwin*) echo macos ;;
cygwin*) echo cygwin ;;
esac
}
function _cache {
local cache_dir="$XDG_CACHE_HOME/${SHELL##*/}"
local cache="$cache_dir/$1"
if _is_callable "$1" && [[ ! -f "$cache" || ! -s "$cache" ]]; then
echo "Caching $1"
"$@" >| $cache
fi
_source $cache
}
function _cache_clear {
command rm -rfv $XDG_CACHE_HOME/${SHELL##*/}/*;
}
_backup() {
delete_after_backup=false
BASE_BACKUP_DIR="${TMPDIR}/dotfiles"
# Check if the first argument is the delete flag
if [[ "$1" == "-d" || "$1" == "--delete" ]]; then
delete_after_backup=true
shift # Shift arguments to remove the flag
fi
mkdir -p "$BASE_BACKUP_DIR"
for file in "$@"; do
# Check if the file exists and is not a symlink
if [[ -e $file && ! -L $file ]]; then
dest="$BASE_BACKUP_DIR/$(basename "$file").$(date +%Y%m%d%H%M%S)"
cp -r "$file" "$dest"
if $delete_after_backup; then
rm -rf "$file"
echo "$file deleted, you can find a backup in $dest"
else
echo "Backup of $file made in $dest"
fi
fi
done
}