about summary refs log tree commit diff stats
path: root/ok1
blob: 9d7971cafa5b99b3732928b855f7f43a5632d585 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh
## ok: a minimal command runner and build tool
# by Case Duckworth <acdw@acdw.net> -- released to the public domain

QUIET=false
NORUN=false
FBUILD=false
OKFILE=./ok
FRFILE=.fr

_usage() {
	exec >&2
	cat<<EOF
Usage: ok [OPTIONS] [TARGET...]
Options:
 -h		Show this help and exit
 -q		Don't output diagnostic messages
 -x		Run with set -x
 -B		Treat all targets as 'new' (force running)
 -n		Don't run commands
 -f FILE	Use FILE as \$OKFILE (default: 'ok')
 -C DIRECTORY	Change to DIRECTORY before doing anything
Targets defined in $OKFILE:
$(sed -n \
      -e 's/^alias *\(.*\)/*\1/p' \
      -e 's/^\([a-z][a-z]*\)().*##* *\(.*\)/ \1       	# \2/p' \
      -e 's/^\([a-z][a-z]*\)().*/ \1/p' \
      "$OKFILE")
EOF
	exit $1
}

buildp() { # buildp target dependency...
	$FBUILD && return
	target="$1"; shift
	test -e "$target" || return
	for f
	do
		test x-- = "x$f" && continue
		test "$f" -nt "$target" && return
	done
	return 1
}

ok() { # ok command...
	$QUIET || printf >&2 '* %s\n' "$*"
	$NORUN || "$@" || exit $((100 + $?))
}

quietly() { "$@" >/dev/null 2>&1; }

frun() {
	cut -f2- <"$FRFILE" |
		while read -r line
		do eval "$line"
		done
}

fr() { # fr < GOAL JOB DEPS...
	# recommended: use a heredoc
	:>"$FRFILE"
	while read -r goal job deps
	do
		eval set -- "$deps"
		if buildp "$goal" "$@"
		then
			printf '%s\t%s %s\n' \
			       "$goal" \
			       "$job" "$(echo "$deps"|sed 's/--.*//')" \
			       >>"$FRFILE"
		fi
	done
}

while getopts hqnxBf:C: OPT
do
	case "$OPT" in
		(h) _usage ;;
		(q) QUIET=true ;;
		(x) set -x ;;
		(n) NORUN=true ;;
		(B) FBUILD=true ;;
		(f) OKFILE="$OPTARG" ;;
		(C) cd "$OPTARG" || exit 2 ;;
		(*) _usage 1 ;;
	esac
done
shift $((OPTIND - 1))

. "$OKFILE" || exit 3

test -z "$1" && default
for target
do
	if grep -q "$target" "$FRFILE"
	then eval "$(grep "$target" "$FRFILE" | cut -f2-)"
	else "$target"
	fi
done