1from __future__ import print_function 2 3import os 4import sys 5import re 6import string 7 8from scriptCommon import catchPath 9 10versionParser = re.compile( r'(\s*static\sVersion\sversion)\s*\(\s*(.*)\s*,\s*(.*)\s*,\s*(.*)\s*,\s*\"(.*)\"\s*,\s*(.*)\s*\).*' ) 11rootPath = os.path.join( catchPath, 'include/' ) 12versionPath = os.path.join( rootPath, "internal/catch_version.cpp" ) 13definePath = os.path.join(rootPath, 'catch.hpp') 14readmePath = os.path.join( catchPath, "README.md" ) 15cmakePath = os.path.join(catchPath, 'CMakeLists.txt') 16 17class Version: 18 def __init__(self): 19 f = open( versionPath, 'r' ) 20 for line in f: 21 m = versionParser.match( line ) 22 if m: 23 self.variableDecl = m.group(1) 24 self.majorVersion = int(m.group(2)) 25 self.minorVersion = int(m.group(3)) 26 self.patchNumber = int(m.group(4)) 27 self.branchName = m.group(5) 28 self.buildNumber = int(m.group(6)) 29 f.close() 30 31 def nonDevelopRelease(self): 32 if self.branchName != "": 33 self.branchName = "" 34 self.buildNumber = 0 35 def developBuild(self): 36 if self.branchName == "": 37 self.branchName = "develop" 38 self.buildNumber = 0 39 40 def incrementBuildNumber(self): 41 self.developBuild() 42 self.buildNumber = self.buildNumber+1 43 44 def incrementPatchNumber(self): 45 self.nonDevelopRelease() 46 self.patchNumber = self.patchNumber+1 47 48 def incrementMinorVersion(self): 49 self.nonDevelopRelease() 50 self.patchNumber = 0 51 self.minorVersion = self.minorVersion+1 52 53 def incrementMajorVersion(self): 54 self.nonDevelopRelease() 55 self.patchNumber = 0 56 self.minorVersion = 0 57 self.majorVersion = self.majorVersion+1 58 59 def getVersionString(self): 60 versionString = '{0}.{1}.{2}'.format( self.majorVersion, self.minorVersion, self.patchNumber ) 61 if self.branchName != "": 62 versionString = versionString + '-{0}.{1}'.format( self.branchName, self.buildNumber ) 63 return versionString 64 65 def updateVersionFile(self): 66 f = open( versionPath, 'r' ) 67 lines = [] 68 for line in f: 69 m = versionParser.match( line ) 70 if m: 71 lines.append( '{0}( {1}, {2}, {3}, "{4}", {5} );'.format( self.variableDecl, self.majorVersion, self.minorVersion, self.patchNumber, self.branchName, self.buildNumber ) ) 72 else: 73 lines.append( line.rstrip() ) 74 f.close() 75 f = open( versionPath, 'w' ) 76 for line in lines: 77 f.write( line + "\n" ) 78 79def updateReadmeFile(version): 80 import updateWandbox 81 82 downloadParser = re.compile( r'<a href=\"https://github.com/catchorg/Catch2/releases/download/v\d+\.\d+\.\d+/catch.hpp\">' ) 83 success, wandboxLink = updateWandbox.uploadFiles() 84 if not success: 85 print('Error when uploading to wandbox: {}'.format(wandboxLink)) 86 exit(1) 87 f = open( readmePath, 'r' ) 88 lines = [] 89 for line in f: 90 lines.append( line.rstrip() ) 91 f.close() 92 f = open( readmePath, 'w' ) 93 for line in lines: 94 line = downloadParser.sub( r'<a href="https://github.com/catchorg/Catch2/releases/download/v{0}/catch.hpp">'.format(version.getVersionString()) , line) 95 if '[![Try online](https://img.shields.io/badge/try-online-blue.svg)]' in line: 96 line = '[![Try online](https://img.shields.io/badge/try-online-blue.svg)]({0})'.format(wandboxLink) 97 f.write( line + "\n" ) 98 99 100def updateCmakeFile(version): 101 with open(cmakePath, 'r') as file: 102 lines = file.readlines() 103 with open(cmakePath, 'w') as file: 104 for line in lines: 105 if 'project(Catch2 LANGUAGES CXX VERSION ' in line: 106 file.write('project(Catch2 LANGUAGES CXX VERSION {0})\n'.format(version.getVersionString())) 107 else: 108 file.write(line) 109 110 111def updateVersionDefine(version): 112 with open(definePath, 'r') as file: 113 lines = file.readlines() 114 with open(definePath, 'w') as file: 115 for line in lines: 116 if '#define CATCH_VERSION_MAJOR' in line: 117 file.write('#define CATCH_VERSION_MAJOR {}\n'.format(version.majorVersion)) 118 elif '#define CATCH_VERSION_MINOR' in line: 119 file.write('#define CATCH_VERSION_MINOR {}\n'.format(version.minorVersion)) 120 elif '#define CATCH_VERSION_PATCH' in line: 121 file.write('#define CATCH_VERSION_PATCH {}\n'.format(version.patchNumber)) 122 else: 123 file.write(line) 124 125 126def performUpdates(version): 127 # First update version file, so we can regenerate single header and 128 # have it ready for upload to wandbox, when updating readme 129 version.updateVersionFile() 130 updateVersionDefine(version) 131 132 import generateSingleHeader 133 generateSingleHeader.generate(version) 134 135 # Then copy the reporters to single include folder to keep them in sync 136 # We probably should have some kind of convention to select which reporters need to be copied automagically, 137 # but this works for now 138 import shutil 139 for rep in ('automake', 'tap', 'teamcity'): 140 sourceFile = os.path.join(catchPath, 'include/reporters/catch_reporter_{}.hpp'.format(rep)) 141 destFile = os.path.join(catchPath, 'single_include', 'catch2', 'catch_reporter_{}.hpp'.format(rep)) 142 shutil.copyfile(sourceFile, destFile) 143 144 updateReadmeFile(version) 145 updateCmakeFile(version) 146