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
83    # Wandbox no longer accepts the single-header upload, skip
84    # import updateWandbox
85
86    downloadParser = re.compile( r'<a href=\"https://github.com/catchorg/Catch2/releases/download/v\d+\.\d+\.\d+/catch.hpp\">' )
87    # success, wandboxLink = updateWandbox.uploadFiles()
88    # if not success:
89    #     print('Error when uploading to wandbox: {}'.format(wandboxLink))
90    #     exit(1)
91    f = open( readmePath, 'r' )
92    lines = []
93    for line in f:
94        lines.append( line.rstrip() )
95    f.close()
96    f = open( readmePath, 'w' )
97    for line in lines:
98        line = downloadParser.sub( r'<a href="https://github.com/catchorg/Catch2/releases/download/v{0}/catch.hpp">'.format(version.getVersionString()) , line)
99#        if '[![Try online](https://img.shields.io/badge/try-online-blue.svg)]' in line:
100#            line = '[![Try online](https://img.shields.io/badge/try-online-blue.svg)]({0})'.format(wandboxLink)
101        f.write( line + "\n" )
102
103
104def updateCmakeFile(version):
105    with open(cmakePath, 'rb') as file:
106        lines = file.readlines()
107    replacementRegex = re.compile(b'project\\(Catch2 LANGUAGES CXX VERSION \\d+\\.\\d+\\.\\d+\\)')
108    replacement = 'project(Catch2 LANGUAGES CXX VERSION {0})'.format(version.getVersionString()).encode('ascii')
109    with open(cmakePath, 'wb') as file:
110        for line in lines:
111            file.write(replacementRegex.sub(replacement, line))
112
113
114def updateVersionDefine(version):
115    # First member of the tuple is the compiled regex object, the second is replacement if it matches
116    replacementRegexes = [(re.compile(b'#define CATCH_VERSION_MAJOR \\d+'),'#define CATCH_VERSION_MAJOR {}'.format(version.majorVersion).encode('ascii')),
117                          (re.compile(b'#define CATCH_VERSION_MINOR \\d+'),'#define CATCH_VERSION_MINOR {}'.format(version.minorVersion).encode('ascii')),
118                          (re.compile(b'#define CATCH_VERSION_PATCH \\d+'),'#define CATCH_VERSION_PATCH {}'.format(version.patchNumber).encode('ascii')),
119                         ]
120    with open(definePath, 'rb') as file:
121        lines = file.readlines()
122    with open(definePath, 'wb') as file:
123        for line in lines:
124            for replacement in replacementRegexes:
125                line = replacement[0].sub(replacement[1], line)
126            file.write(line)
127
128
129def updateVersionPlaceholder(filename, version):
130    with open(filename, 'rb') as file:
131        lines = file.readlines()
132    placeholderRegex = re.compile(b'in Catch X.Y.Z')
133    replacement = 'in Catch {}.{}.{}'.format(version.majorVersion, version.minorVersion, version.patchNumber).encode('ascii')
134    with open(filename, 'wb') as file:
135        for line in lines:
136            file.write(placeholderRegex.sub(replacement, line))
137
138
139def updateDocumentationVersionPlaceholders(version):
140    print('Updating version placeholder in documentation')
141    docsPath = os.path.join(catchPath, 'docs/')
142    for basePath, _, files in os.walk(docsPath):
143        for file in files:
144            if fnmatch.fnmatch(file, "*.md") and "contributing.md" != file:
145                updateVersionPlaceholder(os.path.join(basePath, file), version)
146
147
148def performUpdates(version):
149    # First update version file, so we can regenerate single header and
150    # have it ready for upload to wandbox, when updating readme
151    version.updateVersionFile()
152    updateVersionDefine(version)
153
154    import generateSingleHeader
155    generateSingleHeader.generate(version)
156
157    # Then copy the reporters to single include folder to keep them in sync
158    # We probably should have some kind of convention to select which reporters need to be copied automagically,
159    # but this works for now
160    import shutil
161    for rep in ('automake', 'tap', 'teamcity', 'sonarqube'):
162        sourceFile = os.path.join(catchPath, 'include/reporters/catch_reporter_{}.hpp'.format(rep))
163        destFile = os.path.join(catchPath, 'single_include', 'catch2', 'catch_reporter_{}.hpp'.format(rep))
164        shutil.copyfile(sourceFile, destFile)
165
166    updateReadmeFile(version)
167    updateCmakeFile(version)
168    updateDocumentationVersionPlaceholders(version)
169