set_version.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. '''
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. '''
  7. import sys, os, re, subprocess, io
  8. def finish(code):
  9. global executePath
  10. os.chdir(executePath)
  11. sys.exit(code)
  12. if sys.platform == 'win32' and not 'COMSPEC' in os.environ:
  13. print('[ERROR] COMSPEC environment variable is not set.')
  14. finish(1)
  15. executePath = os.getcwd()
  16. scriptPath = os.path.dirname(os.path.realpath(__file__))
  17. inputVersion = ''
  18. versionOriginal = ''
  19. versionMajor = ''
  20. versionMinor = ''
  21. versionPatch = ''
  22. versionAlpha = '0'
  23. versionBeta = False
  24. for arg in sys.argv:
  25. match = re.match(r'^\s*(\d+)\.(\d+)(\.(\d+)(\.(\d+|beta))?)?\s*$', arg)
  26. if match:
  27. inputVersion = arg
  28. versionOriginal = inputVersion
  29. versionMajor = match.group(1)
  30. versionMinor = match.group(2)
  31. versionPatch = match.group(4) if match.group(4) else '0'
  32. versionAlphaBeta = match.group(5) if match.group(5) else ''
  33. if len(versionAlphaBeta) > 0:
  34. if match.group(6) == 'beta':
  35. versionBeta = True
  36. else:
  37. versionAlpha = match.group(6)
  38. if not len(versionMajor):
  39. print("Wrong version parameter")
  40. finish(1)
  41. def checkVersionPart(part):
  42. cleared = int(part) % 1000 if len(part) > 0 else 0
  43. if str(cleared) != part:
  44. print("Bad version part: " + part)
  45. finish(1)
  46. checkVersionPart(versionMajor)
  47. checkVersionPart(versionMinor)
  48. checkVersionPart(versionPatch)
  49. checkVersionPart(versionAlpha)
  50. versionFull = str(int(versionMajor) * 1000000 + int(versionMinor) * 1000 + int(versionPatch))
  51. versionFullAlpha = '0'
  52. if versionAlpha != '0':
  53. versionFullAlpha = str(int(versionFull) * 1000 + int(versionAlpha))
  54. versionStr = versionMajor + '.' + versionMinor + '.' + versionPatch
  55. versionStrSmall = versionStr if versionPatch != '0' else versionMajor + '.' + versionMinor
  56. if versionBeta:
  57. print('Setting version: ' + versionStr + ' beta')
  58. elif versionAlpha != '0':
  59. print('Setting version: ' + versionStr + '.' + versionAlpha + ' closed alpha')
  60. else:
  61. print('Setting version: ' + versionStr + ' stable')
  62. #def replaceInFile(path, replaces):
  63. def checkChangelog():
  64. global scriptPath, versionStr, versionStrSmall
  65. count = 0
  66. with io.open(scriptPath + '/../../changelog.txt', encoding='utf-8') as f:
  67. for line in f:
  68. if line.startswith(versionStr + ' ') or line.startswith(versionStrSmall + ' '):
  69. count = count + 1
  70. if count == 0:
  71. print('Changelog entry not found!')
  72. finish(1)
  73. elif count != 1:
  74. print('Wrong changelog entries count found: ' + count)
  75. finish(1)
  76. checkChangelog()
  77. def replaceInFile(path, replacements):
  78. content = ''
  79. foundReplacements = {}
  80. updated = False
  81. with open(path, 'r') as f:
  82. for line in f:
  83. for replacement in replacements:
  84. if re.search(replacement[0], line):
  85. changed = re.sub(replacement[0], replacement[1], line)
  86. if changed != line:
  87. line = changed
  88. updated = True
  89. foundReplacements[replacement[0]] = True
  90. content = content + line
  91. for replacement in replacements:
  92. if not replacement[0] in foundReplacements:
  93. print('Could not find "' + replacement[0] + '" in "' + path + '".')
  94. finish(1)
  95. if updated:
  96. with open(path, 'w') as f:
  97. f.write(content)
  98. print('Patching build/version...')
  99. replaceInFile(scriptPath + '/version', [
  100. [ r'(AppVersion\s+)\d+', r'\g<1>' + versionFull ],
  101. [ r'(AppVersionStrMajor\s+)\d[\d\.]*', r'\g<1>' + versionMajor + '.' + versionMinor ],
  102. [ r'(AppVersionStrSmall\s+)\d[\d\.]*', r'\g<1>' + versionStrSmall ],
  103. [ r'(AppVersionStr\s+)\d[\d\.]*', r'\g<1>' + versionStr ],
  104. [ r'(BetaChannel\s+)\d', r'\g<1>' + ('1' if versionBeta else '0') ],
  105. [ r'(AlphaVersion\s+)\d+', r'\g<1>' + versionFullAlpha ],
  106. [ r'(AppVersionOriginal\s+)\d[\d\.beta]*', r'\g<1>' + versionOriginal ],
  107. ])
  108. print('Patching core/version.h...')
  109. replaceInFile(scriptPath + '/../SourceFiles/core/version.h', [
  110. [ r'(TDESKTOP_REQUESTED_ALPHA_VERSION\s+)\(\d+ULL\)', r'\g<1>(' + versionFullAlpha + 'ULL)' ],
  111. [ r'(AppVersion\s+=\s+)\d+', r'\g<1>' + versionFull ],
  112. [ r'(AppVersionStr\s+=\s+)[^;]+', r'\g<1>"' + versionStrSmall + '"' ],
  113. [ r'(AppBetaVersion\s+=\s+)[a-z]+', r'\g<1>' + ('true' if versionBeta else 'false') ],
  114. ])
  115. parts = [versionMajor, versionMinor, versionPatch, versionAlpha]
  116. withcomma = ','.join(parts)
  117. withdot = '.'.join(parts)
  118. rcReplaces = [
  119. [ r'(FILEVERSION\s+)\d+,\d+,\d+,\d+', r'\g<1>' + withcomma ],
  120. [ r'(PRODUCTVERSION\s+)\d+,\d+,\d+,\d+', r'\g<1>' + withcomma ],
  121. [ r'("FileVersion",\s+)"\d+\.\d+\.\d+\.\d+"', r'\g<1>"' + withdot + '"' ],
  122. [ r'("ProductVersion",\s+)"\d+\.\d+\.\d+\.\d+"', r'\g<1>"' + withdot + '"' ],
  123. ]
  124. print('Patching Telegram.rc...')
  125. replaceInFile(scriptPath + '/../Resources/winrc/Telegram.rc', rcReplaces)
  126. print('Patching Updater.rc...')
  127. replaceInFile(scriptPath + '/../Resources/winrc/Updater.rc', rcReplaces)
  128. print('Patching appxmanifest.xml...')
  129. replaceInFile(scriptPath + '/../Resources/uwp/AppX/AppxManifest.xml', [
  130. [ r'( Version=)"\d+\.\d+\.\d+\.\d+"', r'\g<1>"' + withdot + '"' ],
  131. ])