-
Notifications
You must be signed in to change notification settings - Fork 497
/
Copy pathinstall.sh
executable file
·152 lines (126 loc) · 3.56 KB
/
install.sh
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
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
set -e
OS=$(uname -s)
if [ "$OS" == "Darwin" ]; then
FILENAME="fnm-macos"
elif [ "$OS" == "Linux" ]; then
FILENAME="fnm-linux"
else
echo "OS $OS is not supported."
echo "If you think that's a bug - please file an issue to https://github.com/Schniz/fnm/issues"
exit 1
fi
INSTALL_DIR="$HOME/.fnm"
# Parse Flags
parse_args() {
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-d|--install-dir)
INSTALL_DIR="$2"
shift # past argument
shift # past value
;;
-s|--skip-shell)
SKIP_SHELL="true"
shift # past argument
;;
*)
echo "Unrecognized argument $key"
exit 1
;;
esac
done
}
parse_args "$@"
get_latest_release() {
# Taken from https://gist.github.com/lukechilds/a83e1d7127b78fef38c2914c4ececc3c
curl --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api
grep '"tag_name":' | # Get tag line
sed -E 's/.*"([^"]+)".*/\1/' # Pluck JSON value
}
download_fnm() {
LATEST_RELEASE=$(get_latest_release Schniz/fnm)
URL=https://github.com/Schniz/fnm/releases/download/$LATEST_RELEASE/$FILENAME.zip
DOWNLOAD_DIR=$(mktemp -d)
echo "Downloading $URL..."
mkdir -p $INSTALL_DIR &> /dev/null
curl --progress-bar -L $URL -o $DOWNLOAD_DIR/$FILENAME.zip
unzip -q $DOWNLOAD_DIR/$FILENAME.zip -d $DOWNLOAD_DIR
mv $DOWNLOAD_DIR/$FILENAME/fnm $INSTALL_DIR/fnm
chmod u+x $INSTALL_DIR/fnm
}
check_dependencies() {
echo "Checking dependencies for the installation script..."
echo -n "Checking availablity of curl... "
if hash curl 2>/dev/null; then
echo "OK!"
else
echo "Missing!"
SHOULD_EXIT="true"
fi
echo -n "Checking availablity of unzip... "
if hash unzip 2>/dev/null; then
echo "OK!"
else
echo "Missing!"
SHOULD_EXIT="true"
fi
if [ "$SHOULD_EXIT" = "true" ]; then
exit 1
fi
}
setup_shell() {
CURRENT_SHELL=$(basename $SHELL)
if [ "$CURRENT_SHELL" == "zsh" ]; then
CONF_FILE=$HOME/.zshrc
echo "Installing for Zsh. Appending the following to $CONF_FILE:"
echo ""
echo ' # fnm'
echo ' export PATH=$HOME/.fnm:$PATH'
echo ' eval "`fnm env --multi`"'
echo '' >> $CONF_FILE
echo '# fnm' >> $CONF_FILE
echo 'export PATH=$HOME/.fnm:$PATH' >> $CONF_FILE
echo 'eval "`fnm env --multi`"' >> $CONF_FILE
elif [ "$CURRENT_SHELL" == "fish" ]; then
CONF_FILE=$HOME/.config/fish/config.fish
echo "Installing for Fish. Appending the following to $CONF_FILE:"
echo ""
echo ' # fnm'
echo ' set PATH $HOME/.fnm $PATH'
echo ' fnm env --multi | source'
echo '' >> $CONF_FILE
echo '# fnm' >> $CONF_FILE
echo 'set PATH $HOME/.fnm $PATH' >> $CONF_FILE
echo 'fnm env --multi | source"' >> $CONF_FILE
elif [ "$CURRENT_SHELL" == "bash" ]; then
if [ "$OS" == "Darwin" ]; then
CONF_FILE=$HOME/.profile
else
CONF_FILE=$HOME/.bashrc
fi
echo "Installing for Bash. Appending the following to $CONF_FILE:"
echo ""
echo ' # fnm'
echo ' export PATH=$HOME/.fnm:$PATH'
echo ' eval "`fnm env --multi`"'
echo '' >> $CONF_FILE
echo '# fnm' >> $CONF_FILE
echo 'export PATH=$HOME/.fnm:$PATH' >> $CONF_FILE
echo 'eval "`fnm env --multi`"' >> $CONF_FILE
else
echo "Could not infer shell type. Please set up manually."
exit 1
fi
echo ""
echo "In order to apply the changes, open a new terminal or run the following command:"
echo ""
echo " source $CONF_FILE"
}
check_dependencies
download_fnm
if [ "$SKIP_SHELL" != "true" ]; then
setup_shell
fi