Menu

[ee7d4c]: / version-tools.jam  Maximize  Restore  History

Download this file

97 lines (88 with data), 3.1 kB

# 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
;
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.