Skip to content

Commit 1503992

Browse files
committed
Makefile: add install target
Add an install-from-source target to the 'Makefile' to allow installation on any Unix-based system, rather than only those for which packages are built (i.e., Darwin and Debian). This is done by repurposing the 'layout-unix.sh' script into an 'install.sh' script. Like the 'uninstall.sh' update from the previous commit, the new script contains only a few major differences from 'layout-unix.sh': - The '--payload' option is renamed to '--install-root'. - Like 'uninstall.sh', commands that may require root privileges (such as when '--install-root=/') are wrapped in a 'retry_root' function that first runs the operation as the calling user, then as root if it fails. However, unlike 'uninstall.sh', this behavior is gated by an '--allow-root' option, ensuring we only do this escalation if we're running the 'install' target. - 'INSTALL_TO' is renamed to 'APP_ROOT' to avoid confusion with 'INSTALL_ROOT'. This is also done in the pkg 'postinstall' script. Signed-off-by: Victoria Dye <[email protected]>
1 parent c7dde5d commit 1503992

File tree

2 files changed

+54
-26
lines changed

2 files changed

+54
-26
lines changed

Makefile

+20-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ NAME := git-bundle-server
66
VERSION :=
77
PACKAGE_REVISION := 1
88

9+
# Installation information
10+
INSTALL_ROOT := /
11+
912
# Helpful paths
1013
BINDIR := $(CURDIR)/bin
1114
DISTDIR := $(CURDIR)/_dist
@@ -25,6 +28,17 @@ build:
2528
@mkdir -p $(BINDIR)
2629
GOOS="$(GOOS)" GOARCH="$(GOARCH)" go build -o $(BINDIR) ./...
2730

31+
# Installation targets
32+
.PHONY: install
33+
install: build
34+
@echo
35+
@echo "======== Installing to $(INSTALL_ROOT) ========"
36+
@scripts/install.sh --bindir="$(BINDIR)" \
37+
--uninstaller="$(CURDIR)/scripts/uninstall.sh" \
38+
--allow-root \
39+
--include-symlinks \
40+
--install-root="$(INSTALL_ROOT)"
41+
2842
# Packaging targets
2943
.PHONY: check-arch
3044
check-arch:
@@ -52,9 +66,9 @@ DEB_FILENAME := $(DISTDIR)/$(NAME)_$(VERSION)-$(PACKAGE_REVISION)_$(PACKAGE_ARCH
5266
$(DEBDIR)/root: check-arch build
5367
@echo
5468
@echo "======== Formatting package contents ========"
55-
@build/package/layout-unix.sh --bindir="$(BINDIR)" \
56-
--include-symlinks \
57-
--output="$(DEBDIR)/root"
69+
@scripts/install.sh --bindir="$(BINDIR)" \
70+
--include-symlinks \
71+
--install-root="$(DEBDIR)/root"
5872

5973
$(DEB_FILENAME): check-version $(DEBDIR)/root
6074
@echo
@@ -83,9 +97,9 @@ PKG_FILENAME := $(DISTDIR)/$(NAME)_$(VERSION)-$(PACKAGE_REVISION)_$(PACKAGE_ARCH
8397
$(PKGDIR)/payload: check-arch build
8498
@echo
8599
@echo "======== Formatting package contents ========"
86-
@build/package/layout-unix.sh --bindir="$(BINDIR)" \
87-
--uninstaller="$(CURDIR)/scripts/uninstall.sh" \
88-
--output="$(PKGDIR)/payload"
100+
@scripts/install.sh --bindir="$(BINDIR)" \
101+
--uninstaller="$(CURDIR)/scripts/uninstall.sh" \
102+
--install-root="$(PKGDIR)/payload"
89103

90104
$(PKG_FILENAME): check-version $(PKGDIR)/payload
91105
@echo

build/package/layout-unix.sh renamed to scripts/install.sh

+34-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ die () {
44
exit 1
55
}
66

7+
# Setup root escalation operation
8+
SUDO=""
9+
retry_root () {
10+
if [ -n "$SUDO" ]
11+
then
12+
# run as user, then with 'sudo'
13+
$@ 2>/dev/null || "$SUDO" $@
14+
else
15+
# passthrough
16+
$@
17+
fi
18+
}
19+
720
# Parse script arguments
821
for i in "$@"
922
do
@@ -16,12 +29,16 @@ case "$i" in
1629
UNINSTALLER="${i#*=}"
1730
shift # past argument=value
1831
;;
32+
--allow-root)
33+
SUDO=$(if command -v sudo >/dev/null 2>&1; then echo sudo; fi)
34+
shift # past argument
35+
;;
1936
--include-symlinks)
2037
INCLUDE_SYMLINKS=1
2138
shift # past argument
2239
;;
23-
--output=*)
24-
PAYLOAD="${i#*=}"
40+
--install-root=*)
41+
INSTALL_ROOT="${i#*=}"
2542
shift # past argument=value
2643
;;
2744
*)
@@ -34,46 +51,43 @@ done
3451
if [ -z "$BINDIR" ]; then
3552
die "--bindir was not set"
3653
fi
37-
if [ -z "$PAYLOAD" ]; then
38-
die "--output was not set"
54+
if [ -z "$INSTALL_ROOT" ]; then
55+
die "--install-root was not set"
56+
fi
57+
58+
if [ "$INSTALL_ROOT" == "/" ]; then
59+
# Reset $INSTALLDIR to empty string to avoid double leading slash
60+
INSTALL_ROOT=""
3961
fi
4062

4163
# Exit as soon as any line fails
4264
set -e
4365

44-
# Cleanup any old payload directory
45-
if [ -d "$PAYLOAD" ]; then
46-
echo "Cleaning old output directory '$PAYLOAD'..."
47-
rm -rf "$PAYLOAD"
48-
fi
49-
5066
# Ensure payload directory exists
51-
APP_ROOT="$PAYLOAD/usr/local/git-bundle-server"
52-
mkdir -p "$APP_ROOT"
67+
APP_ROOT="$INSTALL_ROOT/usr/local/git-bundle-server"
68+
retry_root mkdir -p "$APP_ROOT"
5369

5470
# Copy built binaries
5571
echo "Copying binaries..."
56-
cp -R "$BINDIR/." "$APP_ROOT/bin"
72+
retry_root cp -R "$BINDIR/." "$APP_ROOT/bin"
5773

5874
# Copy uninstaller script
5975
if [ -n "$UNINSTALLER" ]; then
6076
echo "Copying uninstall script..."
61-
cp "$UNINSTALLER" "$APP_ROOT"
77+
retry_root cp "$UNINSTALLER" "$APP_ROOT"
6278
fi
6379

6480
# Create symlinks
6581
if [ -n "$INCLUDE_SYMLINKS" ]; then
66-
LINK_TO="$PAYLOAD/usr/local/bin"
82+
LINK_TO="$INSTALL_ROOT/usr/local/bin"
6783
RELATIVE_LINK_TO_BIN="../git-bundle-server/bin"
68-
mkdir -p "$LINK_TO"
84+
retry_root mkdir -p "$LINK_TO"
6985

7086
echo "Creating binary symlinks..."
7187
for program in "$APP_ROOT"/bin/*
7288
do
7389
p=$(basename "$program")
74-
rm -f "$LINK_TO/$p"
75-
ln -s "$RELATIVE_LINK_TO_BIN/$p" "$LINK_TO/$p"
90+
retry_root rm -f "$LINK_TO/$p"
91+
retry_root ln -s "$RELATIVE_LINK_TO_BIN/$p" "$LINK_TO/$p"
7692
done
7793
fi
78-
79-
echo "Layout complete."

0 commit comments

Comments
 (0)