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