[NEW] Makefile: Allow system-wide installation of helpers and scripts

This commit is contained in:
Andrew Rogers 2023-10-16 22:00:19 -05:00
commit ad56bebb81
3 changed files with 117 additions and 0 deletions

5
.gitignore vendored
View file

@ -1,4 +1,9 @@
dist
README.html README.html
pub.css
crud
*.swp *.swp
*.swo *.swo

60
Makefile Normal file
View file

@ -0,0 +1,60 @@
PREFIX ?= /usr/local
INSTALL ?= install
HELPERS := $(wildcard *.bash)
DIST_HELPERS := $(foreach HELPER,$(HELPERS),dist/lib/bash-util/$(HELPER))
DIST_HELPER_LINKS := $(foreach HELPER,$(HELPERS),dist/bin/$(HELPER))
SCRIPTS := $(shell for fsname in $(wildcard scripts/*); \
do [ ! -d $$fsname ] && echo $$fsname || :; done)
DIST_SCRIPTS := $(foreach SCRIPT,$(SCRIPTS),dist/bin/$(shell basename $(SCRIPT)))
dist: dist/bin dist/lib/bash-util $(DIST_HELPERS) $(DIST_HELPER_LINKS) $(DIST_SCRIPTS)
dist/bin:
mkdir -pv $@
dist/bin/%.bash: %.bash
ln -s ../lib/bash-util/$*.bash $@
dist/bin/%: scripts/% dist/bin
sed 's/^\(.\s\+\)\("\)*.\+\/util\//\1\2$(shell \
sed 's/\([$\/]\)/\\\1/g' <<<$(PREFIX))\/lib\/bash-util\//' < $< > $@
chmod 755 $@
dist/lib/bash-util:
mkdir -pv $@
dist/lib/bash-util/%.bash: %.bash
cp $< $@
chmod 644 $@
install: dist
cd dist && \
for each in *; \
do cp -rv "$$each" "$(PREFIX)/$$each"; \
done
clean:
rm -rf dist
.PHONY: dist install
### README #####################################################################
UNAME := $(shell uname)
ifeq ($(UNAME),Linux)
OPEN=xdg-open
else ifeq ($(UNAME),Darwin)
OPEN=open
endif
pub.css:
wget https://github.com/manuelp/pandoc-stylesheet/raw/acac36b976966f76544176161ba826d519b6f40c/pub.css
# Requires Pandoc to be installed
README.html: README.md pub.css
pandoc $< -s -c pub.css -o README.html
$(OPEN) README.html
README: README.html

View file

@ -1,5 +1,57 @@
# Bash Helpers # Bash Helpers
Arcane incantations that we're tired of remembering how to do in Bash will end Arcane incantations that we're tired of remembering how to do in Bash will end
up here. Include me as a submodule in whenever you're writing shell scripts. up here. Include me as a submodule in whenever you're writing shell scripts.
Patches welcome! Patches welcome!
### Installing
Simply install with `make`!
```
# make install
```
If you'd like to install it to a different `PREFIX` simply specify one on the
`make` command-line (or in your environment):
```
$ PREFIX="$HOME/.local" make install
```
### Using in your scripts!
You can use the bash helpers in two ways:
1. As a Git submodule
2. From Bash's `$PATH` environment variable.
#### As a Git submodule
```
$ git submodule add https://github.com/targetdisk/bash-util.git
```
From your scripts you can now source the .bash files relative to the `bash-util`
directory in your tree.
#### From Bash's PATH
If you followed along with the **Installing** section you should have installed
the helpers somewhere in your environment's `$PATH`. If so, you should now be
able to write scripts like:
```bash
#!/usr/bin/env bash
. logging.bash
die "I am dead now!"
echo "I never execute!"
```
Or even run this in your Bash shell:
```
$ . env.bash && add_path "$HOME/.local/bin"
```