Skip to content

Commit eb7fb90

Browse files
committed
enh(install): Detect suitable branch to download
1 parent 0bfe760 commit eb7fb90

File tree

4 files changed

+67
-57
lines changed

4 files changed

+67
-57
lines changed

install.sh

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
#!/bin/sh
1+
#!/bin/bash
2+
set -e
23

34
# check for unzip before we continue
45
if [ ! "$(command -v unzip)" ]; then
56
echo 'unzip is required but was not found. Install unzip first and then run this script again.' >&2
67
exit 1
78
fi
89

9-
_fetch_sources(){
10-
wget -O /tmp/nanorc.zip https://github.com/galenguyer/nano-syntax-highlighting/archive/master.zip
10+
_fetch_sources() {
11+
br=$(_find_suitable_branch)
1112
mkdir -p ~/.nano/
13+
cd ~/.nano/
1214

13-
cd ~/.nano/ || exit
15+
wget -O "/tmp/nanorc.zip" "https://github.com/galenguyer/nano-syntax-highlighting/archive/${br}.zip"
1416
unzip -o "/tmp/nanorc.zip"
15-
mv nano-syntax-highlighting-master/* ./
16-
rm -rf nano-syntax-highlighting-master
17-
rm /tmp/nanorc.zip
17+
mv "nano-syntax-highlighting-${br}"/* ./
18+
rm -rf "nano-syntax-highlighting-${br}"
19+
rm -f "/tmp/nanorc.zip"
1820
}
1921

20-
_update_nanorc(){
22+
_update_nanorc() {
2123
touch $NANORC_FILE
22-
2324
# add all includes from ~/.nano/nanorc if they're not already there
2425
while read -r inc; do
2526
if ! grep -q "$inc" "${NANORC_FILE}"; then
@@ -28,25 +29,68 @@ _update_nanorc(){
2829
done < ~/.nano/nanorc
2930
}
3031

31-
_update_nanorc_lite(){
32+
_update_nanorc_lite() {
3233
sed -i '/include "\/usr\/share\/nano\/\*\.nanorc"/i include "~\/.nano\/*.nanorc"' "${NANORC_FILE}"
3334
}
3435

36+
_version_str_to_num() {
37+
if [ -z "$1" ]; then
38+
return
39+
fi
40+
printf "$1" | awk -F . '{printf("%d%02d%02d", $1, $2, $3)}'
41+
}
42+
43+
_find_suitable_branch() {
44+
# find the branch that is suitable for local nano
45+
verstr=$(nano --version 2>/dev/null | awk '/GNU nano/ {print ($3=="version")? $4: substr($5,2)}')
46+
ver=$(_version_str_to_num "$verstr")
47+
if [ -z "$ver" ]; then
48+
echo "Cannot determine nano's version, fallback to master" >&2
49+
echo "master"
50+
return
51+
fi
52+
branches=(
53+
pre-6.0
54+
pre-5.0
55+
pre-4.5
56+
pre-2.9.5
57+
pre-2.6.0
58+
pre-2.3.3
59+
pre-2.1.6
60+
)
61+
target="master"
62+
# find smallest branch that is larger than ver
63+
for b in "${branches[@]}"; do
64+
num=$(_version_str_to_num "${b#*pre-}")
65+
if (( ver < num )); then
66+
target="${b}"
67+
else
68+
break
69+
fi
70+
done
71+
echo "$target"
72+
}
73+
74+
3575
NANORC_FILE=~/.nanorc
3676

3777
case "$1" in
3878
-l|--lite)
39-
UPDATE_LITE=1;;
79+
UPDATE_LITE=1
80+
;;
81+
--find_suitable_branch)
82+
_find_suitable_branch
83+
exit 0
84+
;;
4085
-h|--help)
4186
echo "Install script for nanorc syntax highlights"
4287
echo "Call with -l or --lite to update .nanorc with secondary precedence to existing .nanorc includes"
4388
exit 0
4489
;;
4590
esac
4691

47-
_fetch_sources;
48-
if [ "$UPDATE_LITE" ];
49-
then
92+
_fetch_sources
93+
if [ "$UPDATE_LITE" ]; then
5094
_update_nanorc_lite
5195
else
5296
_update_nanorc

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ There are three ways to install this repo.
1313
Copy the following code to download and run the installer script:
1414

1515
```sh
16-
curl https://raw.githubusercontent.com/galenguyer/nano-syntax-highlighting/master/install.sh | sh
16+
curl https://raw.githubusercontent.com/galenguyer/nano-syntax-highlighting/master/install.sh | bash
1717
```
1818

1919
If your machine doesn't have `curl` command, use this code:
2020

2121
```sh
22-
wget https://raw.githubusercontent.com/galenguyer/nano-syntax-highlighting/master/install.sh -O- | sh
22+
wget https://raw.githubusercontent.com/galenguyer/nano-syntax-highlighting/master/install.sh -O- | bash
2323
```
2424

2525
This automatically unpacks all the `.nanorc` files to `~/.nano`.

tool/create-branches.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then
1616
exit 1
1717
fi
1818

19-
# to ensure this list enumerates all created branches
19+
# NOTE: remember to also sync with install.sh
2020
declare -A branches=(
2121
[pre-6.0]=pre-6.0
2222
[pre-5.0]=pre-5.0

tool/switch-branch.sh

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,9 @@
11
#! /usr/bin/env bash
22

3-
function version_to_num() {
4-
if [[ -z "$1" ]]; then
5-
return
6-
fi
7-
awk -F . '{printf("%d%02d%02d", $1, $2, $3)}' <<<"$1"
8-
}
3+
base="$(dirname "$0")/../"
4+
branch=$("$base/install.sh" --find_suitable_branch)
5+
verstr=$(nano --version 2>/dev/null | awk '/GNU nano/ {print ($3=="version")? $4: substr($5,2)}')
96

10-
11-
NANO_VER=$(version_to_num "$(nano --version 2>/dev/null | awk '/version/ {print $4}')")
12-
13-
if [[ -z "${NANO_VER}" ]]; then
14-
printf "Cannot determine nano's version\n" >&2
15-
exit 1
16-
fi
17-
18-
echo "Found nano version ${NANO_VER} ($(nano --version | awk '/version/ {print $4}'))"
19-
20-
# fetch the available "pre*" branches
21-
git fetch --prune origin
22-
branches=()
23-
24-
#mapfile -t branches <<<"$(git branch -l -r --format='%(refname:short)' "origin/pre-*")"
25-
while IFS= read -r line; do
26-
branches+=("$line")
27-
done < <(git for-each-ref --format='%(refname:short)' 'refs/remotes/origin/pre-*')
28-
29-
# choose the suitable branch
30-
target="master"
31-
32-
for b in "${branches[@]}"; do
33-
num=$(version_to_num "${b#*/pre-}")
34-
35-
if ((NANO_VER < num)); then
36-
target="${b}"
37-
break
38-
fi
39-
done
40-
41-
echo "Switching to branch ${target}"
42-
43-
git checkout "${target}"
7+
echo "Current nano version: ${verstr}"
8+
echo "Switching to ${branch}"
9+
git checkout "${branch}"

0 commit comments

Comments
 (0)