Skip to content

Commit 8891292

Browse files
authored
Enhancements for lib_gnucompat.sh (#7)
2 parents 60162f0 + 82c3930 commit 8891292

5 files changed

Lines changed: 136 additions & 27 deletions

File tree

.github/workflows/interactive-command.yml

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ on:
66
command:
77
description: "Command to execute"
88
required: true
9-
default: "sed --version"
9+
default: "demo/all-demos.sh"
1010
os:
1111
description: "Operating system to run the command on"
1212
required: true
13-
default: "ubuntu-latest"
13+
default: "macos-latest"
1414
type: choice
1515
options:
1616
- ubuntu-latest
@@ -21,6 +21,9 @@ on:
2121
default: false
2222
type: boolean
2323

24+
run-name: |
25+
ICE: ${{ inputs.command }} (${{ inputs.os }}, GNU? ${{ inputs.install_gnu_tools }})
26+
2427
permissions:
2528
contents: read
2629

@@ -29,30 +32,16 @@ jobs:
2932
runs-on: ${{ inputs.os }}
3033

3134
steps:
35+
- name: Checkout repository
36+
uses: actions/checkout@v4
37+
with:
38+
fetch-depth: 1
39+
submodules: recursive
40+
3241
- name: Install GNU tools on macOS
33-
if: runner.os == 'macos-latest' && inputs.install_gnu_tools == 'true'
42+
if: ${{ inputs.install_gnu_tools }}
3443
run: |
35-
brew install gnu-sed gawk grep findutils coreutils gnu-tar gnu-time gnu-indent gnu-getopt gnu-which
36-
brew install gdate gxargs gcut ghead gtail gtr guniq gwc gdiff
37-
echo 'export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"' >> $GITHUB_ENV
38-
echo 'export PATH="/usr/local/opt/gawk/libexec/gnubin:$PATH"' >> $GITHUB_ENV
39-
echo 'export PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"' >> $GITHUB_ENV
40-
echo 'export PATH="/usr/local/opt/findutils/libexec/gnubin:$PATH"' >> $GITHUB_ENV
41-
echo 'export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"' >> $GITHUB_ENV
42-
echo 'export PATH="/usr/local/opt/gnu-tar/libexec/gnubin:$PATH"' >> $GITHUB_ENV
43-
echo 'export PATH="/usr/local/opt/gnu-time/libexec/gnubin:$PATH"' >> $GITHUB_ENV
44-
echo 'export PATH="/usr/local/opt/gnu-indent/libexec/gnubin:$PATH"' >> $GITHUB_ENV
45-
echo 'export PATH="/usr/local/opt/gnu-getopt/libexec/gnubin:$PATH"' >> $GITHUB_ENV
46-
echo 'export PATH="/usr/local/opt/gnu-which/libexec/gnubin:$PATH"' >> $GITHUB_ENV
47-
echo 'export PATH="/usr/local/opt/gdate/libexec/gnubin:$PATH"' >> $GITHUB_ENV
48-
echo 'export PATH="/usr/local/opt/gxargs/libexec/gnubin:$PATH"' >> $GITHUB_ENV
49-
echo 'export PATH="/usr/local/opt/gcut/libexec/gnubin:$PATH"' >> $GITHUB_ENV
50-
echo 'export PATH="/usr/local/opt/ghead/libexec/gnubin:$PATH"' >> $GITHUB_ENV
51-
echo 'export PATH="/usr/local/opt/gtail/libexec/gnubin:$PATH"' >> $GITHUB_ENV
52-
echo 'export PATH="/usr/local/opt/gtr/libexec/gnubin:$PATH"' >> $GITHUB_ENV
53-
echo 'export PATH="/usr/local/opt/guniq/libexec/gnubin:$PATH"' >> $GITHUB_ENV
54-
echo 'export PATH="/usr/local/opt/gwc/libexec/gnubin:$PATH"' >> $GITHUB_ENV
55-
echo 'export PATH="/usr/local/opt/gdiff/libexec/gnubin:$PATH"' >> $GITHUB_ENV
44+
bin/gnu-install.sh
5645
5746
- name: Execute command
5847
run: ${{ inputs.command }}

bin/gnu-install.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env bash
2+
###
3+
### Script to install GNU tools (typically on macOS).
4+
###
5+
### Requires `brew` to be installed.
6+
###
7+
8+
set -o errexit # abort on nonzero exit status
9+
set -o nounset # abort on unbound variable
10+
set -o pipefail # don't hide errors within pipes
11+
12+
### GNU Tools to install
13+
###
14+
### References:
15+
### - https://gist.github.com/skyzyx/3438280b18e4f7c490db8a2a2ca0b9da
16+
### - https://www.topbug.net/blog/2013/04/14/install-and-use-gnu-command-line-tools-in-mac-os-x/
17+
### https://github.com/asantra1/macOS-gnu-tools/blob/master/gnu-cmd.sh
18+
19+
declare -a GNU_TOOLS=(
20+
"binutils"
21+
"coreutils"
22+
"diffutils"
23+
"ed"
24+
"findutils"
25+
"gawk"
26+
"gnu-indent"
27+
"gnu-sed"
28+
"gnu-tar"
29+
"gnu-which"
30+
"gnutls"
31+
"grep"
32+
"gzip"
33+
"screen"
34+
"watch"
35+
"wdiff"
36+
"wget"
37+
)
38+
39+
### Validate `brew` is installed
40+
if ! command -v brew &>/dev/null; then
41+
echo "Error: 'brew' is required. Please install it first."
42+
exit 1
43+
fi
44+
45+
### Install GNU Tools
46+
for tool in "${GNU_TOOLS[@]}"; do
47+
if brew list --formula | grep -q "^${tool}\$"; then
48+
if brew outdated --formula | grep -q "^${tool}\$"; then
49+
echo "Updating ${tool}..."
50+
brew upgrade "${tool}"
51+
else
52+
echo "${tool} is already installed and up-to-date."
53+
fi
54+
else
55+
echo "Installing ${tool}..."
56+
brew install "${tool}"
57+
fi
58+
done

demo/all-demos.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
###
3+
### Run all demo scripts.
4+
###
5+
6+
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
7+
declare -r SCRIPT_DIR
8+
9+
# Find all -demo.sh files in the demo directory and run them.
10+
for demo_script in "${SCRIPT_DIR}"/*-demo.sh; do
11+
if [[ -f "${demo_script}" ]]; then
12+
echo "Running demo: ${demo_script}"
13+
"${demo_script}"
14+
echo
15+
fi
16+
done

demo/gnucompat-demo.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
###
3+
### Demo for GNU Tools Compatibility Layer
4+
###
5+
6+
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
7+
declare -r SCRIPT_DIR
8+
# shellcheck source=/dev/null
9+
source "${SCRIPT_DIR}/../lib_gnucompat.sh"
10+
11+
# Function to output the path and GNU status of a command
12+
function output_command_info() {
13+
local cmd_var_name="${1}"
14+
local cmd="${!cmd_var_name}"
15+
local cmd_path
16+
cmd_path=$(command -v "${cmd}")
17+
local gnu_status
18+
if is_gnu "${cmd}"; then
19+
gnu_status="Yes"
20+
else
21+
gnu_status="No"
22+
fi
23+
echo "${cmd_var_name}: ${cmd_path} (GNU? ${gnu_status})"
24+
}
25+
26+
# Output information for each command
27+
output_command_info "SED"
28+
output_command_info "AWK"
29+
output_command_info "GREP"
30+
output_command_info "FIND"
31+
output_command_info "SORT"
32+
output_command_info "TAR"
33+
output_command_info "DATE"
34+
output_command_info "XARGS"
35+
output_command_info "CUT"
36+
output_command_info "HEAD"
37+
output_command_info "TAIL"
38+
output_command_info "TR"
39+
output_command_info "UNIQ"
40+
output_command_info "WC"
41+
output_command_info "DIFF"

lib_gnucompat.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,16 @@ export DIFF
156156
function is_gnu() {
157157
local cmd="${1:?Error: No command provided to is_gnu function}"
158158

159-
# Check if the command is a GNU tool by expecting the version output
160-
# to contain "GNU".
159+
# Check if the command is available
160+
if ! command -v "${cmd}" &>/dev/null; then
161+
return 0
162+
fi
163+
164+
("${cmd}" --version &>/dev/null) || return 1
165+
161166
local version_output
162167
version_output="$("${cmd}" --version 2>&1)"
163-
if "${GREP}" -q "GNU" <<<"${version_output}"; then
168+
if [[ "${version_output}" == *"GNU"* ]]; then
164169
return 0
165170
else
166171
return 1

0 commit comments

Comments
 (0)