/[gentoo-x86]/eclass/versionator.eclass
Gentoo

Contents of /eclass/versionator.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations) (download)
Fri Sep 10 18:45:01 2004 UTC (9 years, 7 months ago) by ciaranm
Branch: MAIN
new eclassy goodness

1 # Copyright 1999-2004 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: $
4 #
5 # Original Author: Ciaran McCreesh <ciaranm@gentoo.org>
6 #
7 # This eclass provides functions which simplify manipulating $PV and similar
8 # variables. Most functions default to working with $PV, although other
9 # values can be used.
10 #
11 # Simple Example 1: $PV is 1.2.3b, we want 1_2.3b:
12 # MY_PV=$(replace_version_separator 1 '_' )
13 #
14 # Simple Example 2: $PV is 1.4.5, we want 1:
15 # MY_MAJORV=$(get_major_version )
16 #
17 # Full list of user usable functions provided by this eclass (see the functions
18 # themselves for documentation):
19 # get_all_version_components ver_str
20 # get_version_components ver_str
21 # get_major_version ver_str
22 # get_version_component_range range ver_str
23 # get_after_major_version ver_str
24 # replace_version_separator index newvalue ver_str
25 # replace_all_version_separators newvalue ver_str
26
27 ECLASS=versionator
28 INHERITED="$INHERITED $ECLASS"
29
30 shopt -s extglob
31
32 # Split up a version string into its component parts. If no parameter is
33 # supplied, defaults to $PV.
34 # 0.8.3 -> 0 . 8 . 3
35 # 7c -> 7 c
36 # 3.0_p2 -> 3 . 0 _ p2
37 # 20040905 -> 20040905
38 # 3.0c-r1 -> 3 . 0 c - r1
39 get_all_version_components() {
40 local ver_str=${1:-${PV}} result result_idx=0
41 result=( )
42
43 # sneaky cache trick cache to avoid having to parse the same thing several
44 # times.
45 if [[ "${VERSIONATOR_CACHE_VER_STR}" == "${ver_str}" ]] ; then
46 echo ${VERSIONATOR_CACHE_RESULT}
47 return
48 fi
49 export VERSIONATOR_CACHE_VER_STR="${ver_str}"
50
51 while [[ -n "$ver_str" ]] ; do
52 case "${ver_str:0:1}" in
53 # number: parse whilst we have a number
54 [[:digit:]])
55 result[$result_idx]="${ver_str%%[^[:digit:]]*}"
56 ver_str="${ver_str##+([[:digit:]])}"
57 result_idx=$(($result_idx + 1))
58 ;;
59
60 # separator: single character
61 [-_.])
62 result[$result_idx]="${ver_str:0:1}"
63 ver_str="${ver_str:1}"
64 result_idx=$(($result_idx + 1))
65 ;;
66
67 # letter: grab the letters plus any following numbers
68 [[:alpha:]])
69 local not_match="${ver_str##+([[:alpha:]])*([[:digit:]])}"
70 result[$result_idx]=${ver_str:0:$((${#ver_str} - ${#not_match}))}
71 ver_str="${not_match}"
72 result_idx=$(($result_idx + 1))
73 ;;
74
75 # huh?
76 *)
77 result[$result_idx]="${ver_str:0:1}"
78 ver_str="${ver_str:1}"
79 result_idx=$(($result_idx + 1))
80 ;;
81 esac
82 done
83
84 export VERSIONATOR_CACHE_RESULT="${result[@]}"
85 echo ${result[@]}
86 }
87
88 # Get the important version components, excluding '.', '-' and '_'. Defaults to
89 # $PV if no parameter is supplied.
90 # 0.8.3 -> 0 8 3
91 # 7c -> 7 c
92 # 3.0_p2 -> 3 0 p2
93 # 20040905 -> 20040905
94 # 3.0c-r1 -> 3 0 c r1
95 get_version_components() {
96 local c="$(get_all_version_components "${1:-${PV}}")"
97 c=( ${c[@]//[-._]/ } )
98 echo ${c[@]}
99 }
100
101 # Get the major version of a value. Defaults to $PV if no parameter is supplied.
102 # 0.8.3 -> 0
103 # 7c -> 7
104 # 3.0_p2 -> 3
105 # 20040905 -> 20040905
106 # 3.0c-r1 -> 3
107 get_major_version() {
108 local c
109 c=( $(get_all_version_components "${1:-${PV}}" ) )
110 echo ${c[0]}
111 }
112
113 # Get a particular component or range of components from the version. If no
114 # version parameter is supplied, defaults to $PV.
115 # 1 1.2.3 -> 1
116 # 1-2 1.2.3 -> 1.2
117 # 2- 1.2.3 -> 2.3
118 get_version_component_range() {
119 local c v="${2:-${PV}}" range="${1}" range_start range_end i=-1 j=0
120 c=( $(get_all_version_components ${v} ) )
121 range_start="${range%-*}" ; range_start="${range_start:-1}"
122 range_end="${range#*-}" ; range_end="${range_end:-${#c[@]}}"
123
124 while (( j < ${range_start} )) ; do
125 i=$(($i + 1))
126 [[ $i -gt ${#c[@]} ]] && return
127 [[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1))
128 done
129
130 while (( j <= ${range_end} )) ; do
131 echo -n ${c[$i]}
132 [[ $i -gt ${#c[@]} ]] && return
133 [[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1))
134 i=$(($i + 1))
135 done
136 }
137
138 # Get everything after the major version and its separator (if present) of a
139 # value. Defaults to $PV if no parameter is supplied.
140 # 0.8.3 -> 8.3
141 # 7c -> c
142 # 3.0_p2 -> 0_p2
143 # 20040905 -> (empty string)
144 # 3.0c-r1 -> 0c-r1
145 get_after_major_version() {
146 echo $(get_version_component_range 2- "${1:-PV}" )
147 }
148
149 # Replace the $1th separator with $2 in $3 (defaults to $PV if $3 is not
150 # supplied). If there are fewer than $1 separators, don't change anything.
151 # 1 '_' 1.2.3 -> 1_2.3
152 # 2 '_' 1.2.3 -> 1.2_3
153 # 1 '_' 1b-2.3 -> 1b_2.3
154 replace_version_separator() {
155 local i c found=0 v="${3:-${PV}}"
156 c=( $(get_all_version_components ${v} ) )
157 for (( i = 0 ; i < ${#c[@]} ; i = $i + 1 )) ; do
158 if [[ -n "${c[${i}]//[^-._]}" ]] ; then
159 found=$(($found + 1))
160 if [[ "$found" == "${1:-1}" ]] ; then
161 c[${i}]="${2}"
162 break
163 fi
164 fi
165 done
166 c=${c[@]}
167 echo ${c// }
168 }
169
170 # Replace all version separators in $2 (defaults to $PV) with $1.
171 # '_' 1b.2.3 -> 1b_2_3
172 replace_all_version_separators() {
173 local c
174 c=( $(get_all_version_components "${2:-${PV}}" ) )
175 c="${c[@]//[-._]/$1}"
176 echo ${c// }
177 }

  ViewVC Help
Powered by ViewVC 1.1.20