-
Notifications
You must be signed in to change notification settings - Fork 2
/
version-tools.jam
97 lines (88 loc) · 3.01 KB
/
version-tools.jam
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
# owlcpp/version-tools.jam
# part of owlcpp project.
# Distributed under the Boost Software License, Version 1.0; see doc/license.txt.
# Copyright Mikhail K Levin 2010-6
# return file content as string (os specific)
rule read_file ( file ) {
import path ;
local f = [ path.native $(file) ] ;
switch [ modules.peek : OS ] {
case NT : e = [ SHELL "TYPE $(f)" ] ;
case * : e = [ SHELL "cat $(f)" ] ;
}
return $(e) ;
}
# trim whitespace from ends
rule trim_whitespace ( str ) {
import string ;
local re = "^[ \n\t]*([^\n\t]*[^ \n\t])[ \n\t]*$" ;
local match = [ MATCH "$(re)" : $(str) ] ;
if $(match) { return $(match[1]) ; }
else { return $(str) ; }
}
IMPORT $(__name__) #source module
: #source rules
read_file trim_whitespace
: #target module
: #target rules
read_file trim_whitespace
;
# construct build version from date and time numbers; n=[1-6]
rule build_version ( n ? ) {
n ?= 3 ;
local re = "([0-9]+)-([0-9]+)-([0-9]+) ([0-9]+):([0-9]+):([0-9]+)" ;
local b = [ MATCH $(re) : [ modules.peek : JAMDATE ] ] ;
b = $(b[1-$(n)]) ;
return $(b:J) ;
}
# return version based on a git tag
rule git_describe ( path : git_path ? ) {
git_path ?= git ;
# use '~' dirty marker, not '*', to be able to use version string as directory name
local git_command =
"cd $(path) && \"$(git_path)\" describe --always --dirty=~"
;
local res = [ SHELL $(git_command) : exit-status ] ;
if $(res[2]) = "0" { return [ trim_whitespace $(res[1]) ] ; }
else {
echo "" ;
echo " WARNING: Error running git" ;
echo " Please try setting GIT_PATH variable in user-config.jam" ;
echo "" ;
return "" ;
}
}
# split tag "v1.2.5-8-gda17203~" into 1, 2, 5, "8-gda17203", 1
rule process_version_string ( v_str : v_def * ) {
v_def ?= 0 0 0 "???" 0 ;
local re = "v([0-9]*)[ \-\.]([0-9]*)[ \-\.]([0-9]*)(-[0-9a-g\-]+)?([~]?)" ;
local v = [ MATCH $(re) : $(v_str) ] ;
if $(v[1]) = "" || $(v[2]) = "" || $(v[3]) = "" {
echo " WARNING: Could not parse code revision string" \"$(v_str)\". ;
echo " Using default values." ;
return $(v_def) ;
} else {
local v4 = "" ;
if $(v[4]) != "" { v4 = [ MATCH -(.*) : $(v[4]) ] ; }
local v5 = 0 ;
if $(v[5]) = "~" { v5 = 1 ; }
return $(v[1]) $(v[2]) $(v[3]) $(v4) $(v5) ;
}
}
rule __test__ {
import assert ;
assert.result 1 2 5 "" 0 : process_version_string "v1.2.5" ;
assert.result 1 2 5 "" 1 : process_version_string "v1.2.5~" ;
assert.result 0 0 0 "???" 1 : process_version_string "blah" ;
assert.result 0 0 0 "???" 1 : process_version_string "" ;
assert.result 1 2 5 8-gda17203 0 : process_version_string "v1.2.5-8-gda17203" ;
assert.result 1 2 5 8-gda17203 1 : process_version_string "v1.2.5-8-gda17203~" ;
}
# __test__ ;
IMPORT $(__name__) #source module
: #source rules
build_version git_describe process_version_string
: #target module
: #target rules
build_version git_describe process_version_string
;