Skip to content

Commit 19875c4

Browse files
committed
Initial commit
1 parent 1045fd8 commit 19875c4

File tree

4 files changed

+228
-99
lines changed

4 files changed

+228
-99
lines changed

LICENSE.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2024 Kilna, Anthony
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.
20+

README.md

+63-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# git-on
1+
# gnu-on
22

33
Run GNU tools by default on Mac in an easily switchable way
44

@@ -14,11 +14,9 @@ shell session back into a default MacOS environment on the fly.
1414
## Tools Installed
1515

1616
MacOS tools:
17-
1817
* brew
1918

2019
GNU tools:
21-
2220
* coreutils (many CLI utils)
2321
* bash
2422
* findutils (find, locate, updatedb, xargs)
@@ -30,6 +28,67 @@ GNU tools:
3028

3129
## Install
3230

31+
This will install all of the above, and make symlinks into `/usr/local/gnu`
32+
or `/opt/gnu` for all gnu tools, so you have one path to add.
33+
34+
```
35+
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/kilna/gnu-on/main/install.sh)"
36+
```
37+
38+
## Usage
39+
40+
Load into shell as an extension (don't need to source after)
41+
42+
```
43+
$ source /usr/local/bin/gnu
44+
$ gnu on # Turns on gnu utils in path
45+
$ gnu off # Turns off gnu utils in path
46+
```
47+
48+
Turn on via eval
49+
50+
```
51+
$ eval "$(gnu eval-on)"
52+
```
53+
54+
Or alternately source without loading as extension:
55+
56+
```
57+
$ . /usr/local/bin/gnu on
58+
```
59+
60+
Disabling of gnu tools (and restore to MacOS default tools) is done with
61+
either `gnu off` or `gnu eval-off` using the same syntax as "on":
62+
63+
```
64+
$ eval "$(gnu eval-off)"
65+
```
66+
67+
Or:
68+
69+
```
70+
$ . /usr/local/bin/gnu off
71+
```
72+
73+
## Manual enabling
74+
75+
You can optionally run the install and never use `gnu` itself; you can
76+
enable gnu tools in your shell by adding the following to your path on an
77+
Intel-based Mac:
78+
79+
```
80+
export PATH="/usr/local/gnu/bin:$PATH"
81+
export MANPATH="/usr/local/gnu/share/man:$MANPATH"
3382
```
34-
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/kilna/gnu/HEAD/gnu)" install
83+
84+
Or for M-series processor Macs:
85+
3586
```
87+
export PATH="/opt/gnu/bin:$PATH"
88+
export MANPATH="/opt/gnu/share/man:$MANPATH"
89+
```
90+
91+
## Author
92+
93+
Kilna, Anthony <[email protected]>
94+

gnu

+96-95
Original file line numberDiff line numberDiff line change
@@ -5,127 +5,128 @@ if [[ "$OSTYPE" != darwin* ]]; then
55
return 1
66
fi
77

8-
_gnu_check_install() {
9-
[[ -d /usr/local/gnu/bin ]] && return 0
10-
[[ -d /opt/gnu/bin ]] && return 0
11-
echo "Does not look like gnu utilities are installed via homebrew" >&2
12-
echo "Please run gnu install" >&2
13-
return 1
14-
}
15-
16-
_gnu_check_is_sourced() {
17-
(( _gnu_is_sourced )) && return 0
18-
echo "gnu $_gnu_action must be is_sourced. Run as:" >&2
19-
echo "$ $_gnu_source_run $_gnu_action" >&2
20-
echo "or" >&2
21-
echo "$ $_gnu_source_run load; gnu $_gnu_action" >&2
22-
if [[ "$_gnu_action" == 'on' || "$_gnu_action" == 'off' ]]; then
23-
echo "or you can turn gnu $_gnu_action via eval" >&2
24-
echo "$ eval \"\$($_gnu_run eval-$_gnu_action)\"" >&2
8+
_gnu_check() {
9+
if ! [[ -d /usr/local/gnu/bin || -d /opt/gnu/bin ]]; then
10+
echo "Gnu utilities are not installed properly, reinstall with:" >&2
11+
echo >&2
12+
echo "$ gnu install" >&2
13+
echo >&2
14+
echo "or" >&2
15+
echo >&2
16+
echo /bin/bash -c "$(curl -fsSL $_gnu_url)" >&2
17+
export _gnu_exit=2
18+
return 2
2519
fi
26-
return 1
27-
}
28-
29-
_gnu_install() {
30-
local state="$(set +o)" # Store shell args
31-
set -e -u -o pipefail
32-
if ! $(which brew >/dev/null); then # Install brew
33-
/bin/bash -c "$(curl -fsSL \
34-
https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
20+
if ! (( _gnu_is_sourced )); then
21+
echo "gnu $_gnu_action must be is_sourced. Run as:" >&2
22+
echo >&2
23+
echo "$ source $_gnu_path $_gnu_action" >&2
24+
echo >&2
25+
echo "or" >&2
26+
echo >&2
27+
echo "$ source $_gnu_path load # Use gnu as a shell extension" >&2
28+
echo "$ gnu $_gnu_action" >&2
29+
if [[ "$_gnu_action" == 'on' || "$_gnu_action" == 'off' ]]; then
30+
echo >&2
31+
echo "or you can turn gnu $_gnu_action via eval" >&2
32+
echo >&2
33+
echo "$ eval \"\$(gnu eval-$_gnu_action)\"" >&2
34+
fi
35+
export _gnu_exit=3
36+
return 3
3537
fi
36-
sudo mkdir -p /usr/local/bin
37-
echo "Copying..."
38-
sudo cp -v -f "$_gnu_script" /usr/local/bin/
39-
sudo chmod +x /usr/local/bin/"$_gnu_me"
40-
local base=/usr/local;
41-
if /opt/homebrew/bin/brew --prefix >/dev/null 2>&1; then base=/opt; fi
42-
sudo mkdir -p $base/gnu/bin
43-
sudo mkdir -p $base/gnu/man/man1
44-
local packages=(coreutils findutils grep gawk gnu-sed gnu-tar gnu-which)
45-
local pkg
46-
for pkg in ${packages[@]}; do
47-
brew install "$pkg"
48-
echo "Symlinking..."
49-
local prefix=$(brew --prefix "$pkg")
50-
cd "$prefix/libexec/gnubin/"
51-
local file
52-
for file in *; do
53-
sudo ln -v -f -s -L "$(pwd)/$file" "$base/gnu/bin/$file"
54-
done
55-
cd "$prefix/libexec/gnuman/man1/"
56-
for file in *; do
57-
sudo ln -v -f -s -L "$(pwd)/$file" "$base/gnu/man/man1/$file"
58-
done
59-
done
60-
eval "$state" # Reset shell args
6138
}
6239

40+
export _gnu_url="https://raw.githubusercontent.com/kilna/gnu-on/main/install.sh"
6341
export _gnu_source="${BASH_SOURCE[0]:-${(%):-%x}}"
64-
export _gnu_script="$(realpath "$_gnu_source")"
65-
export _gnu_me="$(basename "$_gnu_script")"
66-
export _gnu_dir="$(dirname $_gnu_script | sed -e "s;^$HOME/;~/;")"
67-
export _gnu_path="$_gnu_dir/$_gnu_me"
68-
export _gnu_is_sourced=0; [[ "$_gnu_source" != "$0" ]] && _gnu_is_sourced=1
69-
[[ "$zsh_eval_context" == *'file'* ]] && _gnu_is_sourced=1
70-
export _gnu_run="$_gnu_path"
71-
export _gnu_source_run="source $_gnu_path"
72-
export _gnu_where="$(whereis -q -b "$_gnu_me")"
73-
if [[ "$_gnu_where" && "$(realpath "$_gnu_where")" == "$_gnu_script" ]]; then
74-
_gnu_run="$_gnu_me"
75-
_gnu_source_run='source "$(whereis -q -b $_gnu_me)"'
76-
fi
77-
export _gnu_action=load
42+
export _gnu_is_sourced=0
43+
if [[ "$_gnu_source" != "$0" ]]; then _gnu_is_sourced=1
44+
elif [[ "$zsh_eval_context" == *'file'* ]]; then _gnu_is_sourced=1; fi
45+
export _gnu_exit=0
46+
export _gnu_script="$(realpath "$_gnu_source")" # Canonical script location
47+
export _gnu_path="$(echo "$_gnu_script" | sed -e "s;^$HOME/;~/;")" # Pretty
48+
export _gnu_base=/usr/local/gnu;
49+
if /opt/homebrew/bin/brew --prefix >/dev/null 2>&1; then _gnu_base=/opt/gnu; fi
7850

79-
while [[ $# -gt 0 ]]; do case "$1" in
80-
install|load|unload|on|off|eval-on|eval-off) _gnu_action="$1"; shift;;
81-
*) echo "Unknown option: $1" >&2; (( _gnu_is_sourced )) && return 1 || exit 1;;
82-
esac; done
51+
export _gnu_action=load
52+
while [[ $# -gt 0 ]]; do
53+
case "$1" in
54+
install|load|''|unload|on|off|eval-on|eval-off) _gnu_action="$1"; shift;;
55+
help|--help|-h) _gnu_action="help"; shift;;
56+
*) echo "Unknown option: $1" >&2; _gnu_action="help"; export _gnu_exit=1;;
57+
esac
58+
done
8359

8460
case "$_gnu_action" in
61+
help)
62+
_gnu_found_usage=0
63+
while read line; do
64+
(( _gnu_found_usage )) && echo "$line"
65+
if [[ "$line" == __USAGE__ ]]; then _gnu_found_usage=1; fi
66+
done < "$_gnu_script"
67+
unset _gnu_found_usage
68+
;;
8569
install)
86-
_gnu_install
70+
/bin/bash -c "$(curl -fsSL $_gnu_url/install.sh)"
8771
;;
8872
load)
89-
_gnu_check_install
90-
_gnu_check_is_sourced
91-
eval "gnu() { source \"$_gnu_script\" \"\$@\"; };"
73+
if _gnu_check; then
74+
eval "gnu() { source \"$_gnu_script\" \"\$@\"; };"
75+
fi
9276
;;
9377
unload)
9478
typeset -f gnu >/dev/null && unset -f gnu
9579
;;
9680
on)
97-
_gnu_check_install
98-
_gnu_check_is_sourced
99-
export PATH="/usr/local/gnu/bin:$PATH"
100-
export MANPATH="/usr/local/gnu/man:$MANPATH"
81+
if _gnu_check; then
82+
export PATH="$_gnu_base/bin:$PATH"
83+
export MANPATH="$_gnu_base/share/man:$MANPATH"
84+
fi
10185
;;
10286
off)
103-
_gnu_check_install
104-
_gnu_check_is_sourced
105-
export PATH="$(echo "$PATH"|tr : '\n'|grep -v -x -F /usr/local/gnu/bin|uniq|awk NF|tr '\n' :|sed -e 's/:$//')"
106-
export MANPATH="$(echo "$MANPATH"|tr : '\n'|grep -v -x -F /usr/local/gnu/man|uniq|awk NF|tr '\n' :|sed -e 's/:$//')"
87+
if _gnu_check; then
88+
# Break-down of how we remove the path from environment
89+
# echo "$PATH" # gets the path in : delimited format
90+
# | tr : '\n' # Turns : into newlines
91+
# | grep -vxF (path) # Removes the path from the newline list
92+
# | uniq # Gets rid of duplicate lines/paths
93+
# | awk NF # Gets rid of blank lines/paths
94+
# | tr '\n' : # Turns it back into a : delimted list
95+
# | sed -e 's/:$//' # Removes trailing :
96+
export PATH="$( echo "$PATH" | tr : '\n' \
97+
| grep -vxF $_gnu_base/bin \
98+
| uniq | awk NF | tr '\n' : | sed -e 's/:$//' )"
99+
export MANPATH="$( echo "$MANPATH" | tr : '\n' \
100+
| grep -vxF $_gnu_base/share/man \
101+
| uniq | awk NF | tr '\n' : | sed -e 's/:$//' )"
102+
fi
107103
;;
108104
eval-on)
109-
echo 'export PATH="/usr/local/gnu/bin:$PATH"'
110-
echo 'export MANPATH="/usr/local/gnu/man:$MANPATH"'
105+
echo 'export PATH="'$_gnu_base'/bin:$PATH"'
106+
echo 'export MANPATH="'$_gnu_base'/man:$MANPATH"'
111107
;;
112108
eval-off)
113-
echo 'export PATH="$(echo "$PATH"|tr : '\''\n'\''|grep -v -x -F /usr/local/gnu/bin|uniq|awk NF|tr '\''\n'\'' :|sed -e '\''s/:$//'\'')"'
114-
echo 'export MANPATH="$(echo "$MANPATH"|tr : '\''\n'\''|grep -v -x -F /usr/local/gnu/man|uniq|awk NF|tr '\''\n'\'' :|sed -e '\''s/:$//'\'')"'
109+
echo -n 'export PATH="$(echo "$PATH"|tr : '\''\n'\'
110+
echo -n '|grep -vxF '$_gnu_base'/bin'
111+
echo '|uniq|awk NF|tr '\''\n'\'' :|sed -e '\''s/:$//'\'')"'
112+
echo -n 'export MANPATH="$(echo "$MANPATH"|tr : '\''\n'\'
113+
echo -n '|grep -vxF '$_gnu_base'/share/man'
114+
echo '|uniq|awk NF|tr '\''\n'\'' :|sed -e '\''s/:$//'\'')"'
115115
;;
116116
esac
117+
118+
(( _gnu_is_sourced )) || exit _gnu_exit
119+
120+
unset _gnu_url
117121
unset _gnu_source
122+
unset _gnu_is_sourced
118123
unset _gnu_script
119-
unset _gnu_me
120-
unset _gnu_dir
121124
unset _gnu_path
122-
unset _gnu_run
123-
unset _gnu_source_run
125+
unset _gnu_base
124126
unset _gnu_action
125-
unset _gnu_quiet
126-
unset _gnu_is_sourced
127-
unset _gnu_where
128-
unset -f _gnu_install
129-
unset -f _gnu_check_install
130-
unset -f _gnu_check_is_sourced
127+
unset -f _gnu_check
128+
129+
return $_gnu_exit
130+
131+
__USAGE__
131132

install.sh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bash
2+
3+
set -e -u -o pipefail
4+
5+
gh_url=https://raw.githubusercontent.com
6+
7+
if ! $(which brew >/dev/null); then # Install brew
8+
echo "Installing brew"
9+
/bin/bash -c "$(curl -fsSL $gh_url/Homebrew/install/HEAD/install.sh)"
10+
fi
11+
12+
echo "Copying gnu-on script..."
13+
curl -o "$TMPDIR/gnu" -fsSL $gh_url/kilna/gnu-on/main/gnu
14+
sudo mkdir -p /usr/local/bin
15+
sudo mv -v -f "$TMPDIR/gnu" /usr/local/bin/
16+
sudo chmod +x /usr/local/bin/gnu
17+
curl -o "$TMPDIR/README.md" -fsSL $gh_url/kilna/gnu-on/main/README.md
18+
cat "$TMPDIR/README.md" >>/usr/local/bin/gnu
19+
rm -f "$TMPDIR/README.md"
20+
21+
base=/usr/local;
22+
if /opt/homebrew/bin/brew --prefix >/dev/null 2>&1; then
23+
base=/opt
24+
fi
25+
26+
sudo mkdir -p $base/gnu/bin
27+
sudo mkdir -p $base/gnu/share/man/man1
28+
29+
packages=(coreutils findutils grep gawk gnu-sed gnu-tar gnu-which)
30+
31+
for pkg in ${packages[@]}; do
32+
33+
brew install "$pkg"
34+
35+
echo "Symlinking..."
36+
prefix=$(brew --prefix "$pkg")
37+
38+
cd "$prefix/libexec/gnubin/"
39+
for file in *; do
40+
sudo ln -v -f -s -L "$(pwd)/$file" "$base/gnu/bin/$file"
41+
done
42+
43+
cd "$prefix/libexec/gnuman/man1/"
44+
for file in *; do
45+
sudo ln -v -f -s -L "$(pwd)/$file" "$base/gnu/share/man/man1/$file"
46+
done
47+
48+
done
49+

0 commit comments

Comments
 (0)