Skip to content

Commit

Permalink
🛠️ mommy's makefile is now much cleaner~
Browse files Browse the repository at this point in the history
  • Loading branch information
FWDekker committed Aug 22, 2023
1 parent 81a3226 commit 3911dab
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 39 deletions.
75 changes: 49 additions & 26 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
# Target locations
prefix := /usr/
bin_prefix := $(prefix)/bin/
man_prefix := $(prefix)/share/man/
fish_prefix := $(prefix)/share/fish/vendor_completions.d/
zsh_prefix := $(prefix)/share/zsh/site-functions/

# Extracted values
version := $(shell head -n 1 version)
date := $(shell tail -n 1 version)

comment := $(shell grep -- "--description" .fpm | tr -d "\"" | cut -d " " -f 2-)
maintainer := $(shell grep -- "--maintainer" .fpm | tr -d "\"" | cut -d " " -f 2-)

install uninstall fpm: prefix ?= /usr/
install uninstall fpm: bin_prefix ?= $(prefix)/bin/
install uninstall fpm: man_prefix ?= $(prefix)/share/man/
install uninstall fpm: fish_prefix ?= $(prefix)/share/fish/vendor_completions.d/
install uninstall fpm: zsh_prefix ?= $(prefix)/share/zsh/site-functions/


## Meta
# Output list of targets
.PHONY: list
list:
@# Taken from https://stackoverflow.com/a/26339924/
@LC_ALL=C $(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/(^|\n)# Files(\n|$$)/,/(^|\n)# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$'
@LC_ALL=C $(MAKE) -pRrq -f "$(lastword $(MAKEFILE_LIST))" : 2>/dev/null | \
awk -v RS= -F: '/(^|\n)# Files(\n|$$)/,/(^|\n)# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | \
sort | \
egrep -v -e '^[^[:alnum:]]' -e '^$@$$'

# Clean up previous builds
.PHONY: clean
clean:
@rm -rf build/ dist/


# Run tests
## Tests
.PHONY: test
test: test/unit test/integration

.PHONY: test/%
test/%: system ?= 0
test/%:
@MOMMY_SYSTEM=$(system) MOMMY_MAKE=$(MAKE) shellspec "src/test/sh/$(@:test/%=%)_spec.sh"
@MOMMY_SYSTEM="$(system)" MOMMY_MAKE="$(MAKE)" shellspec "src/test/sh/$(@:test/%=%)_spec.sh"


## Compilation
# "Compile" files into `build/`
## "Compile" files into `build/`
.PHONY: build
build:
@# Copy relevant files
Expand All @@ -45,13 +49,16 @@ build:
@mkdir -p build/completions/zsh/; cp src/main/completions/zsh/_mommy build/completions/zsh/

@# Insert version information
@sed -i".bak" "s/%%VERSION_NUMBER%%/$(version)/g;s/%%VERSION_DATE%%/$(date)/g" build/bin/mommy build/man/man1/mommy.1
@sed -i".bak" "s/%%VERSION_NUMBER%%/$(version)/g;s/%%VERSION_DATE%%/$(date)/g" \
build/bin/mommy \
build/man/man1/mommy.1
@rm -f build/bin/mommy.bak build/man/man1/mommy.1.bak

@# Compress
@gzip -f build/man/man1/mommy.1


## Installation
# Copy built files into appropriate directories
.PHONY: install
install: build
Expand All @@ -64,13 +71,28 @@ install: build
@install -m 644 build/completions/fish/mommy.fish "$(fish_prefix)"
@install -m 644 build/completions/zsh/_mommy "$(zsh_prefix)"

# Install with preset overrides, as specified later
.PHONY: install/%
install/%: install
@#

# Remove installed files
.PHONY: uninstall
uninstall:
@rm "$(bin_prefix)/mommy"
@rm "$(man_prefix)/man1/mommy.1.gz"
@rm "$(fish_prefix)/mommy.fish"
@rm "$(zsh_prefix)/_mommy"

# Uninstall with preset overrides, as specified later
.PHONY: uninstall/%
uninstall/%: uninstall
@#


## Build packages
.PHONY: dist/%

# Invoke fpm on built files to create `fpm_target` type output
# For valid `fpm_target`s, see https://fpm.readthedocs.io/en/latest/packaging-types.html
.PHONY: fpm
Expand All @@ -89,42 +111,42 @@ endif
"build/completions/zsh/_mommy=$(zsh_prefix)/_mommy"

# Build generic extractable package
.PHONY: dist/generic
dist/generic: build
dist/generic: prefix := ./build/generic/mommy/usr/
dist/generic:
@rm -rf build/generic/

@$(MAKE) prefix="./build/generic/mommy/usr/" install
@$(MAKE) install

@mkdir -p dist/
@tar -C build/generic/ -czf "dist/mommy-$(version)+generic.tar.gz" ./

# Build Debian package with fpm
.PHONY: dist/deb
%/deb: zsh_prefix := $(prefix)/share/zsh/vendor-completions/
dist/deb:
@$(MAKE) fpm_target="deb" zsh_prefix='$$(prefix)/share/zsh/vendor-completions/' fpm
@$(MAKE) fpm_target=deb fpm

# Build AlpineLinux / Debian / ArchLinux / RedHat package with fpm
.PHONY: dist/apk dist/pacman dist/rpm
dist/apk dist/pacman dist/rpm:
@$(MAKE) fpm_target="$(@:dist/%=%)" fpm

# Build macOS package with fpm
.PHONE: dist/osxpkg
%/osxpkg: prefix := /usr/local/
dist/osxpkg:
@$(MAKE) fpm_target="osxpkg" prefix="/usr/local/" fpm
@$(MAKE) fpm_target=osxpkg fpm

@# `installer` program requires `pkg` extension
@mv dist/*.osxpkg "dist/mommy-$(version)+osx.pkg"

# Build FreeBSD package with fpm
.PHONY: dist/freebsd
%/freebsd: prefix := /usr/local/
dist/freebsd:
@$(MAKE) fpm_target="freebsd" prefix="/usr/local/" fpm
@$(MAKE) fpm_target=freebsd fpm

# Build NetBSD package manually
.PHONY: dist/netbsd
%/netbsd: prefix := build/netbsd/usr/pkg/
%/netbsd: man_prefix := $(prefix)/man/
dist/netbsd:
@$(MAKE) prefix='build/netbsd/usr/pkg/' man_prefix='$$(prefix)/man/' install
@$(MAKE) install

@cd build/netbsd; find . -type f | sed -e "s/^/.\//" > +CONTENTS

Expand Down Expand Up @@ -154,9 +176,10 @@ dist/netbsd:
@mv build/netbsd/mommy*.tgz dist/

# Build OpenBSD package manually
.PHONY: dist/openbsd
%/openbsd: prefix := build/openbsd/usr/local/
%/openbsd: man_prefix := $(prefix)/man/
dist/openbsd:
@$(MAKE) prefix='build/openbsd/usr/local/' man_prefix='$$(prefix)/man/' install
@$(MAKE) install

@cd build/openbsd; find . -type f | sed -e "s/^/.\//" > +CONTENTS

Expand Down
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,40 +230,47 @@ if you want to customise where and how mommy installs, you can just compile her
> ℹ️ check the [makefile](https://github.com/FWDekker/mommy/blob/main/GNUmakefile) for a list of all prefix variables
> you can override~
* _debian/ubuntu/apt-based_
```shell
sudo make zsh_prefix='$(prefix)/share/zsh/vendor-completions/' install
sudo make install/deb
```
* _all other linux / windows_
* _freebsd_
```shell
sudo make install
sudo gmake install/freebsd
```
* _macos/freebsd_
* _macos_
```shell
sudo gmake prefix='/usr/local/' install
sudo gmake install/osxpkg
```
* _netbsd_
```shell
sudo gmake prefix='/usr/pkg/' man_prefix='$(prefix)/man/'
sudo gmake install/netbsd
```
* _openbsd_
```shell
sudo gmake prefix='/usr/local/' man_prefix='$(prefix)/man/'
sudo gmake install/openbsd
```
* _windows_
```shell
sudo make install
```
* _all other unix systems_
```shell
sudo make install
```
4. **test** (optional)
if you want to make sure installation was successful, you can run tests using
[shellspec](https://github.com/shellspec/shellspec).
run the following from inside the cloned mommy repository
```shell
git clone https://github.com/shellspec/shellspec.git
PATH="$(pwd)/shellspec/:$PATH" make test
PATH="$(pwd)/shellspec/:$PATH" make system=1 test
```
some tests will be skipped, depending on which other programs you have installed~
5. **uninstall** (optional)
if you want to uninstall after running `make install`, just run the same command as in step 3, except you replace
`install` with `uninstall`.
so on debian, you'd run `sudo make zsh_prefix='$(prefix)/share/zsh/vendor-completions/' uninstall`~

uninstall might not work completely if you installed a different version than the one you're uninstalling.
for the best results, run `mommy -v`, check the version number, run `git checkout <the version>`, and then perform
Expand All @@ -275,7 +282,7 @@ if you want to customise where and how mommy installs, you can just compile her
if you don't want to use a package manager but also don't want to bother with `make`ing mommy, you can download a
universal build of mommy, and play around with that.
this will not install any files into your system.
this will not install any files onto your system.
if you're here because you want to install mommy only for a specific user, the "build from source and install" option
is probably a better approach, though~

Expand Down Expand Up @@ -403,6 +410,9 @@ outputs `your mommy loves you`~
### ✍️ renaming the mommy executable
if you want to write `daddy npm test` instead of `mommy npm test`, you can just create a symlink.
> ℹ️ if you [integrate mommy with your shell](#-shell-integration) you won't have to write `mommy` in the first place~

mommy is installed in slightly different locations on different systems, but you can easily find where mommy is
installed with `whereis mommy`:
```shell
Expand All @@ -420,6 +430,8 @@ sudo ln -fs /usr/bin/mommy /usr/bin/daddy
sudo ln -fs /usr/share/man/man1/mommy.1.gz /usr/share/man/man1/daddy.1.gz
```
> ℹ️ uninstalling mommy will not remove the manually created symlinks~
## 🐚 shell integration
instead of calling mommy for each command, you can also fully integrate mommy with your shell to get mommy's output each
Expand Down
4 changes: 2 additions & 2 deletions src/test/sh/integration_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ Describe "integration of mommy with other programs"
}

It "uninstalls all files that are installed"
"$MOMMY_MAKE" -C ../../../ prefix="$MOMMY_TMP_DIR/" install >/dev/null
"$MOMMY_MAKE" -C ../../../ prefix="$MOMMY_TMP_DIR/" uninstall >/dev/null
$MOMMY_MAKE -C ../../../ prefix="$MOMMY_TMP_DIR/" install >/dev/null
$MOMMY_MAKE -C ../../../ prefix="$MOMMY_TMP_DIR/" uninstall >/dev/null

Assert is_empty "$MOMMY_TMP_DIR/"
End
Expand Down

0 comments on commit 3911dab

Please sign in to comment.