This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rpg-package-list.sh
97 lines (87 loc) · 2.53 KB
/
rpg-package-list.sh
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
#!/bin/sh
# Parses a package list given in argv or piped in standard input (or both)
# and writes a formatted package list to standard output. This is used by
# most commands that accept multiple package arguments to get consistent
# behavior.
#
# A variety of argument styles are supported:
#
# $ rpg-package-list rdiscount '>=1.8.7' sinatra/1.0 rails \> 3
# rdiscount >= 1.8.7
# sinatra = 1.0
# rails > 3
#
# The output format is:
#
# <package> <verspec> <version>
#
set -e
. rpg-sh-setup
ARGV="$@"
USAGE '${PROGNAME} <package> [<version>] ...
Parse list of packages as args or on standard input and output
in standard package list format.'
# These variables are used to keep the current package and version.
package=
verspec=
vers=
# Parse argv
parse_packages () {
# Run over arguments adding packages as needed.
for arg in $(cat)
do
case "$arg" in
-v|--version) :;;
-*) warn "invalid argument: '$arg'";
exit 2;;
[\>\<=~]*) verspec="$arg";;
*.gem) write_package; package="$arg";;
*.*) vers="$arg";;
[0-9]|[0-9][0-9]|[0-9][0-9][0-9]) vers="$arg";;
[A-Za-z0-9_]*) write_package; package="$arg";;
*) warn "invalid argument: '$arg'";
exit 2;;
esac
done
write_package
}
# Write a `<package> <version>` pair to standard output and reset the
# `package`, `verspec`, and `vers` variables.
write_package () {
test -n "$package" || return 0
# Use `>=0` if no version was given
test -n "$vers" || {
verspec='>='
vers='0'
}
# Write single package list line to standard output.
echo "$package ${verspec:-=} ${vers:-0}"
# Reset variables and start over.
package=
verspec=
vers=
}
# Massage input to make option parsing a bit easier. Substitutions are:
#
# * `foo -v1.2.3` turns into `foo -v 1.2.3`
# * `>=0.3.1` turns into `>= 0.3.1`
# * `rails/2.3.4` turns into `rails 2.3.4`
#
preformat () {
sed -e "s/[ ]\{1,\}/$ENEWLINE/g" |
sed -e "s/^-\([a-z]\)\([^ ]\)/-\1$ENEWLINE\2/g" \
-e "s@^\([a-z][a-z]*\)/\([0-9.]\)@\1$ENEWLINE\2@g" \
-e "s/\([><=~]\)\([0-9]\)/\1$ENEWLINE\2/g"
}
# Read package list from stdin if - given
test "$1" = - && {
shift
notice "parsing package list items on stdin"
preformat |
parse_packages
}
# Now format arguments
notice "parsing package list items in $# arguments"
echo "$@" |
preformat |
parse_packages