Skip to content

Commit bc33307

Browse files
committed
update-script command added. snippets completed. README updated.
1 parent 6826f51 commit bc33307

File tree

3 files changed

+178
-38
lines changed

3 files changed

+178
-38
lines changed

README.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ get-elixir is a shell script which aims to help people download and unpack
77
the Elixir programming language from source, downloading it from the
88
[released version](https://github.com/elixir-lang/elixir/releases).
99

10+
All you have to do is tell the script, whether you want to **download** or **download and unpack** the latest
11+
(or any other release if you wish) release of Elixir, and the script will get it for you.
12+
13+
It is your dury, though, to [set the PATH environmental variable](http://elixir-lang.org/install.html#setting-path-environment-variable) to the directory where Elixir was extracted to.
14+
1015
## Installation
1116

1217
You can simply run from your command line
1318
```sh
14-
curl -fL -o get-elixir.sh https://raw.githubusercontent.com/eksperimental/get-elixir/master/get-elixir.sh
19+
curl -fLO get-elixir.sh https://github.com/eksperimental/get-elixir/raw/master/get-elixir.sh
1520
chmod +x get-elixir.sh
1621
```
1722
or clone it
@@ -27,18 +32,34 @@ cd get-elixir
2732
./get-elixir.sh <command> <package_type> [<version_number>] [<dest_dir>]
2833
```
2934

30-
Where you can download and unpack the latest source codes released by doing:
31-
`./get-elixir.sh unpack source`
35+
Where you can **download** and **unpack** the latest source codes released by doing:
36+
37+
To dowload and unpack the sources...
3238

33-
or the latest precompiled
34-
`./get-elixir.sh unpack precompiled`
39+
```sh
40+
./get-elixir.sh unpack source
41+
```
42+
43+
...the latest precompiled version
44+
45+
```sh
46+
./get-elixir.sh unpack precompiled
47+
```
3548

3649
You can also just download [any version that has been released](https://github.com/elixir-lang/elixir/releases).
37-
`./get-elixir.sh unpack precompiled 1.0.0 elixir-1.0.0`
38-
will unpack the precompiled files of v1.0.0 in the directory `elixir-1.0.0`
3950

40-
## License
51+
```sh
52+
# unpack the precompiled binaries of v1.0.0 to dir `./elixir-1.0.0/`
53+
./get-elixir.sh unpack precompiled 1.0.0 elixir-1.0.0
54+
```
55+
56+
And you can update this very same script from your command line:
4157

42-
Please read [LICENSE.txt] file.
43-
The works is unlicensed, so their are in the public domain.
58+
```sh
59+
./get-elixir.sh update-script
60+
```
61+
62+
## License
4463

64+
Please read [LICENSE.txt](LICENSE.txt) file.
65+
The works is unlicensed, so their are in the public domain.

get-elixir.sh

Lines changed: 99 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
#!/bin/sh
22

3-
APP_NAME="elixir-get"
4-
APP_VERSION="0.0.2-dev"
3+
trap "exit 1" TERM
4+
export TOP_PID=$$
5+
6+
APP_NAME="get-elixir"
7+
APP_VERSION="0.0.2"
8+
#DEV_MODE="true" # true/false
59
APP_COMMAND="./get-elixir.sh"
6-
ELIXIR_CSV_URL="https://raw.githubusercontent.com/elixir-lang/elixir-lang.github.com/master/elixir.csv"
10+
APP_REPO_USER="eksperimental"
11+
APP_URL="https://github.com/${APP_REPO_USER}/${APP_NAME}"
12+
APP_GIT_URL="${APP_URL}.git"
13+
APP_SCRIPT_URL="${APP_URL}/raw/master/get-elixir.sh"
14+
APP_SCRIPT=$(basename "${APP_SCRIPT_URL}")
15+
APP_RELEASES_URL="https://github.com/${APP_REPO_USER}/${APP_NAME}/releases"
16+
ELIXIR_CSV_URL="https://github.com/elixir-lang/elixir-lang.github.com/raw/master/elixir.csv"
717
ELIXIR_RELEASES_URL="https://github.com/elixir-lang/elixir/releases"
818
ELIXIR_RELEASE_TAG_URL=""
919
ELIXIR_TREE_URL=""
20+
SCRIPT_PATH="$(dirname "$0")/${APP_SCRIPT}"
1021

1122
#DEFAULT
1223
DEFAULT_DEST_DIR="elixir"
@@ -47,7 +58,7 @@ help() {
4758
4859
Package Types:
4960
source Source files
50-
precompiled Precompiled files
61+
precompiled Precompiled binaries
5162
5263
Version Number:
5364
'latest' is the default option, and it's not required to specify it
@@ -58,8 +69,9 @@ help() {
5869
Where you want to unpack Elixir. Default value: '${DEFAULT_DEST_DIR}'.
5970
6071
Options:
61-
--version Prints version
62-
--help Prints help menu
72+
--help Prints help menu
73+
--update-script Replace this script by downloading the latest version
74+
--version Prints version
6375
6476
Usage Examples:
6577
@@ -77,8 +89,15 @@ help() {
7789
${ELIXIR_RELEASES_URL}"
7890
}
7991

92+
exit_script() {
93+
# http://stackoverflow.com/questions/9893667/is-there-a-way-to-write-a-bash-function-which-aborts-the-whole-execution-no-mat
94+
kill -s TERM $TOP_PID
95+
}
96+
8097
sanitize_version() {
81-
printf '%s' "$1" | sed -e 's/^v//g'
98+
# remove v from version,
99+
# and after that, any "../"
100+
printf '%s' "$1" | sed -e 's/^v//g;s@^\.\./@@g;'
82101
}
83102

84103
sanitize_dest_dir() {
@@ -87,42 +106,54 @@ sanitize_dest_dir() {
87106

88107
get_latest_version() {
89108
local version
90-
version=$(curl -s -fL "${ELIXIR_CSV_URL}" | sed '2q;d' | cut -d , -f1)
109+
version=$(curl -sfL "${ELIXIR_CSV_URL}" | sed '2q;d' | cut -d , -f1)
91110
if [ "${version}" = "" ]; then
92111
echo "* [ERROR] Latest Elixir version number couldn't be retrieved from ${ELIXIR_CSV_URL}" >&2
93-
exit 1
112+
exit_script
94113
else
95114
echo "${version}"
96115
fi
97116
}
98117

118+
get_latest_script_version() {
119+
#version=$(curl -s -fL "${APP_RELEASES_URL}" | grep browser_download_url | head -n 1 | cut -d '"' -f 4)
120+
local version=$(curl -sfI "${APP_RELEASES_URL}/latest" | grep "Location: " | tr '\r' '\0' | tr '\n' '\0' | rev | cut -d"/" -f1 | rev)
121+
if [ "${version}" = "" ]; then
122+
echo "* [ERROR] Latest ${APP_NAME} version number couldn't be retrieved from ${APP_RELEASES_URL}" >&2
123+
exit_script
124+
else
125+
echo "$(sanitize_version "${version}")"
126+
fi
127+
}
128+
129+
99130
download_source() {
100131
local version="$1"
101132
local url="https://github.com/elixir-lang/elixir/archive/v${version}.tar.gz"
102133
echo "* Downloading ${url}"
103-
curl -fL -o "v${version}.tar.gz" "${url}"
134+
curl -fL -O "${url}"
104135
if [ ! -f "v${version}.tar.gz" ]; then
105136
echo "* [ERROR] Elixir v${VERSION} could not be downloaded from ${url}" >&2
106137
if [ "${VERSION}" != "${DEFAULT_VERSION}" ]; then
107138
echo " Please make sure version number is a valid one, by checking:" >&2
108139
echo " ${ELIXIR_RELEASES_URL}" >&2
109140
fi
110-
exit 1
141+
exit_script
111142
fi
112143
}
113144

114145
download_precompiled() {
115146
local version="$1"
116147
local url="https://github.com/elixir-lang/elixir/releases/download/v${version}/Precompiled.zip"
117148
echo "* Downloading ${url}"
118-
curl -fL -o Precompiled.zip "${url}"
149+
curl -fL -O "${url}"
119150
if [ ! -f Precompiled.zip ]; then
120151
echo "* [ERROR] Elixir v${VERSION} could not be downloaded from ${url}" >&2
121152
if [ "${VERSION}" != "${DEFAULT_VERSION}" ]; then
122153
echo " Please make sure version number is a valid one, by finding it in:" >&2
123154
echo " ${ELIXIR_RELEASES_URL}" >&2
124155
fi
125-
exit 1
156+
exit_script
126157
fi
127158
}
128159

@@ -135,7 +166,7 @@ unpack_source() {
135166
rm -rf elixir-${version}/ || (
136167
echo "* [ERROR] \"v${version}.tar.gz\" could not be unpacked to ${dest_dir}" >&2
137168
echo " Check the file permissions." >&2
138-
exit 1
169+
exit_script
139170
)
140171
}
141172

@@ -145,28 +176,76 @@ unpack_precompiled() {
145176
unzip -o -q -d ${dest_dir} Precompiled.zip || (
146177
echo "* [ERROR] \"Precompiled.zip\" could not be unpacked to ${dest_dir}" >&2
147178
echo " Check the file permissions." >&2
148-
exit 1
179+
exit_script
149180
)
150181
}
151182

183+
update_script() {
184+
if [ "${DEV_MODE}" = "true" ]; then
185+
echo "* [ERROR] ${APP_NAME} cannot be updated when \`DEV_MODE=\"true\"\`" >&2
186+
exit_script
187+
fi
188+
189+
echo "* Retrieving version number of latest ${APP_NAME} release..."
190+
local latest_script_version=$(get_latest_script_version)
191+
local remote_script_url="${APP_URL}/raw/v${latest_script_version}/get-elixir.sh"
192+
193+
if [ "${latest_script_version}" != "${APP_VERSION}" ]; then
194+
confirm "* You are about to replace '${SCRIPT_PATH}'.
195+
Do you confirm?" && (
196+
curl -fL -o "${SCRIPT_PATH}" "${remote_script_url}" && (
197+
chmod +x "${SCRIPT_PATH}"
198+
echo "* [OK] ${APP_NAME} succesfully updated"
199+
return 0
200+
) || (
201+
echo "* [ERROR] ${APP_NAME} could not be downloaded from" >&2
202+
echo " ${remote_script_url}" >&2
203+
exit_script
204+
)
205+
) || (
206+
echo "* Updating script has been cancelled."
207+
return 1
208+
)
209+
else
210+
echo "* [OK] ${APP_COMMAND} is already the newest version."
211+
return 0
212+
fi
213+
}
214+
215+
confirm() {
216+
local response=""
217+
read -p "${1} [Y/N]: " response
218+
#echo ${response}
219+
if printf "%s\n" "${response}" | grep -Eq "^[yY].*"; then
220+
return 0
221+
else
222+
return 1
223+
fi
224+
}
225+
152226
do_main() {
153227
# check for help and version
154228
case "$1" in
155229
"help" | "--help" | "-h")
156230
help
157-
exit 0
231+
return 0
232+
;;
233+
234+
"update-script" | "--update-script")
235+
update_script
236+
return 0
158237
;;
159238

160239
"version" | "--version" | "-v")
161240
echo "${APP_NAME} – version ${APP_VERSION}"
162-
exit 0
241+
return 0
163242
;;
164243
esac
165244

166245
# check for minimun number of args
167246
if [ "$#" -lt 2 ]; then
168247
short_help >&2
169-
exit 1
248+
exit_script
170249
fi
171250

172251
# Get Variables from ARGS
@@ -188,12 +267,12 @@ do_main() {
188267
# Check for unrecognized options
189268
if [ "${COMMAND}" != "unpack" ] && [ "${COMMAND}" != "download" ]; then
190269
echo "* [ERROR] Unrecognized <action> \"${COMMAND}\". Try 'unpack' or 'download'." >&2
191-
exit 1
270+
exit_script
192271
fi
193272

194273
if [ "${PACKAGE_TYPE}" != "source" ] && [ "${PACKAGE_TYPE}" != "precompiled" ]; then
195274
echo "* [ERROR] Unrecognized <package_type> \"${PACKAGE_TYPE}\". Try 'source' or 'precompiled'." >&2
196-
exit 1
275+
exit_script
197276
fi
198277

199278
# Define variables based on $VERSION

snippet.get-elixir.sh

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,54 @@
11
#!/bin/sh
22

3-
# Download latest stable Elixir's source-code.
4-
elixir_version=$(wget -qO- \
5-
https://raw.githubusercontent.com/elixir-lang/elixir-lang.github.com/master/elixir.csv | \
3+
# Download latest stable Elixir source-code.
4+
# curl version
5+
version=$(curl -sfL \
6+
https://github.com/elixir-lang/elixir-lang.github.com/raw/master/elixir.csv | \
7+
sed '2q;d' | cut -d , -f1);
8+
curl -fL -O \
9+
https://github.com/elixir-lang/elixir/archive/v${version}.tar.gz &&
10+
tar -xzf v${version}.tar.gz &&
11+
mkdir -p elixir &&
12+
cp -rf elixir-${version}/* elixir &&
13+
rm -rf elixir-${version}/ &&
14+
echo "* [OK] Elixir v{$version} sources can be found in ./elixir/" ||
15+
echo "* [ERROR] Elixir v{$version} couldn't not be either downloaded or unpacked"
16+
17+
# wget version
18+
version=$(wget -qO- \
19+
https://github.com/elixir-lang/elixir-lang.github.com/raw/master/elixir.csv | \
620
sed '2q;d' | cut -d , -f1);
721
wget -O v${version}.tar.gz \
8-
https://github.com/elixir-lang/elixir/archive/v${elixir_version}.tar.gz &&
22+
https://github.com/elixir-lang/elixir/archive/v${version}.tar.gz &&
923
tar -xzf v${version}.tar.gz &&
1024
mkdir -p elixir &&
11-
cp -rf elixir-${elixir_version}/* elixir &&
12-
rm -rf elixir-${elixir_version}/ &&
13-
echo "* Elixir's sources can be found in ./elixir/" ||
14-
echo "* [ERROR] Elixir couldn't not be either downloaded or unpacked"
25+
cp -rf elixir-${version}/* elixir &&
26+
rm -rf elixir-${version}/ &&
27+
echo "* [OK] Elixir v{$version} sources can be found in ./elixir/" ||
28+
echo "* [ERROR] Elixir v{$version} couldn't not be either downloaded or unpacked"
29+
30+
##########
31+
32+
# Download latest stable Elixir precompiled binaries.
33+
# curl version
34+
version=$(curl -sfL \
35+
https://github.com/elixir-lang/elixir-lang.github.com/raw/master/elixir.csv | \
36+
sed '2q;d' | cut -d , -f1);
37+
curl -fL -O \
38+
https://github.com/elixir-lang/elixir/releases/download/v${version}/Precompiled.zip &&
39+
mkdir -p elixir &&
40+
unzip -o -q -d elixir Precompiled.zip &&
41+
echo "* [OK] Elixir v{$version} sources can be found in ./elixir/" ||
42+
echo "* [ERROR] Elixir v{$version} couldn't not be either downloaded or unpacked"
43+
44+
# wget version
45+
version=$(wget -qO- \
46+
https://github.com/elixir-lang/elixir-lang.github.com/raw/master/elixir.csv | \
47+
sed '2q;d' | cut -d , -f1);
48+
wget -O v${version}.tar.gz \
49+
https://github.com/elixir-lang/elixir/releases/download/v${version}/Precompiled.zip &&
50+
mkdir -p elixir &&
51+
unzip -o -q -d elixir Precompiled.zip &&
52+
echo "* [OK] Elixir's v{$version} sources can be found in ./elixir/" ||
53+
echo "* [ERROR] Elixir v{$version} couldn't not be either downloaded or unpacked"
54+

0 commit comments

Comments
 (0)