forked from mathiasbynens/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.sh
More file actions
executable file
·116 lines (95 loc) · 2.94 KB
/
Copy pathbootstrap.sh
File metadata and controls
executable file
·116 lines (95 loc) · 2.94 KB
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
#!/usr/bin/env bash
rootFolder=$(dirname "${BASH_SOURCE}")
cd "$rootFolder"
git pull origin master
buildFolder="$rootFolder/build"
# files to build with platform dependent sections
declare -a filesToBuild=(".hgrc" ".hgignore" ".gitconfig" ".gitignore")
# running on Mac or Linux (platform dependent sections)
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) machine=Linux;;
Darwin*) machine=OSX;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
*) machine="UNKNOWN:${unameOut}"
esac
# concatenates base and platform specific files
function build() {
[[ -e "$buildFolder" ]] || mkdir "$buildFolder";
for file in "${filesToBuild[@]}"; do
cat "$file (Base)" "$file ($machine)" > "${buildFolder}/$file"
done
}
# wrapper around rsync for easier building --excludes options
function sync() {
local sourceDir="$1"
local destinationDir="$2"
local excludeOptions=
if (( $# == 3 )); then
declare -a argAry=("${!3}")
for file in "${argAry[@]}"; do
if [[ ! -n "$excludeOptions" ]]; then
excludeOptions="--exclude=$file "
else
excludeOptions="$excludeOptions--exclude=$file "
fi
done
fi
# Note --dry-run (or -n) option will make rsync only list files to be synced
eval "rsync $excludeOptions-avh --no-perms $sourceDir $destinationDir"
}
function doIt() {
build
# sync builded files (no excludes)
echo "Syncing build folder..."
echo ""
# TODO: Why is rsync not working here...
#sync "$buildFolder" "$HOME"
cp -a "$buildFolder/." ~/
# excludeOptions=
# for file in "${filesToBuild[@]}"; do
# excludeOptions="$excludeOptions --exclude='/${file} (Base)'"
# done
# http://stackoverflow.com/questions/1527049/bash-join-elements-of-an-array
sep=','
joinedFilesToBuild=$(printf "${sep}%s" "${filesToBuild[@]}")
joinedFilesToBuild=${joinedFilesToBuild:${#sep}}
# Notes about --excludes patterns and wildcards:
# /.gitignore* matches .gitignore, '.gitignore (Base)' etc.
# '.git/' excludes any .git directory in ../dotfiles tree.
# '.DS_Store' excludes any Finder created dot file in ../dotfiles tree.
# '/bootstrap.sh' excludes only this file.
# '/scripts/' excludes ./scripts directory (i.e. only ../dotfiles/scripts/ folder).
# etc....
# Array to build --excludes options for rsync
local -a excludes=(
".git/"
".editorconfig"
"/{$joinedFilesToBuild}*"
".DS_Store"
"/{build,scripts,docs}/"
"bootstrap.{sh,ps1}"
"/README.md"
"/LICENSE-MIT.txt")
echo ""
echo "Syncing root folder..."
echo ""
sync "$rootFolder" "$HOME" excludes[@]
echo ""
echo "Reloading .bash_profile..."
source ~/.bash_profile
}
if [[ "$1" == "--build" || "$1" == "-b" ]]; then
build
elif [[ "$1" == "--force" || "$1" == "-f" ]]; then
doIt
else
read -p "This may overwrite existing files in your home directory. Are you sure? (y/n) " -n 1
echo ""
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
doIt
fi
fi
unset -f doIt;