bump-version.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/env python
  2. import os
  3. import re
  4. import sys
  5. import argparse
  6. from lib.util import execute, get_electron_version, parse_version, scoped_cwd
  7. SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
  8. def main():
  9. parser = argparse.ArgumentParser(
  10. description='Bump version numbers. Must specify at least one of the three'
  11. +' options:\n'
  12. +' --bump=patch to increment patch version, or\n'
  13. +' --stable to promote current beta to stable, or\n'
  14. +' --version={version} to set version number directly\n'
  15. +'Note that you can use both --bump and --stable '
  16. +'simultaneously.',
  17. formatter_class=argparse.RawTextHelpFormatter
  18. )
  19. parser.add_argument(
  20. '--version',
  21. default=None,
  22. dest='new_version',
  23. help='new version number'
  24. )
  25. parser.add_argument(
  26. '--bump',
  27. action='store',
  28. default=None,
  29. dest='bump',
  30. help='increment [major | minor | patch | beta]'
  31. )
  32. parser.add_argument(
  33. '--stable',
  34. action='store_true',
  35. default= False,
  36. dest='stable',
  37. help='promote to stable (i.e. remove `-beta.x` suffix)'
  38. )
  39. parser.add_argument(
  40. '--dry-run',
  41. action='store_true',
  42. default= False,
  43. dest='dry_run',
  44. help='just to check that version number is correct'
  45. )
  46. args = parser.parse_args()
  47. if args.new_version == None and args.bump == None and args.stable == False:
  48. parser.print_help()
  49. return 1
  50. increments = ['major', 'minor', 'patch', 'beta']
  51. curr_version = get_electron_version()
  52. versions = parse_version(re.sub('-beta', '', curr_version))
  53. if args.bump in increments:
  54. versions = increase_version(versions, increments.index(args.bump))
  55. if versions[3] == '0':
  56. # beta starts at 1
  57. versions = increase_version(versions, increments.index('beta'))
  58. if args.stable == True:
  59. versions[3] = '0'
  60. if args.new_version != None:
  61. versions = parse_version(re.sub('-beta', '', args.new_version))
  62. version = '.'.join(versions[:3])
  63. suffix = '' if versions[3] == '0' else '-beta.' + versions[3]
  64. if args.dry_run:
  65. print 'new version number would be: {0}\n'.format(version + suffix)
  66. return 0
  67. with scoped_cwd(SOURCE_ROOT):
  68. update_electron_gyp(version, suffix)
  69. update_win_rc(version, versions)
  70. update_version_h(versions, suffix)
  71. update_info_plist(version)
  72. update_package_json(version, suffix)
  73. tag_version(version, suffix)
  74. print 'Bumped to version: {0}'.format(version + suffix)
  75. def increase_version(versions, index):
  76. for i in range(index + 1, 4):
  77. versions[i] = '0'
  78. versions[index] = str(int(versions[index]) + 1)
  79. return versions
  80. def update_electron_gyp(version, suffix):
  81. pattern = re.compile(" *'version%' *: *'[0-9.]+(-beta[0-9.]*)?'")
  82. with open('electron.gyp', 'r') as f:
  83. lines = f.readlines()
  84. for i in range(0, len(lines)):
  85. if pattern.match(lines[i]):
  86. lines[i] = " 'version%': '{0}',\n".format(version + suffix)
  87. with open('electron.gyp', 'w') as f:
  88. f.write(''.join(lines))
  89. return
  90. def update_win_rc(version, versions):
  91. pattern_fv = re.compile(' FILEVERSION [0-9,]+')
  92. pattern_pv = re.compile(' PRODUCTVERSION [0-9,]+')
  93. pattern_fvs = re.compile(' *VALUE "FileVersion", "[0-9.]+"')
  94. pattern_pvs = re.compile(' *VALUE "ProductVersion", "[0-9.]+"')
  95. win_rc = os.path.join('atom', 'browser', 'resources', 'win', 'atom.rc')
  96. with open(win_rc, 'r') as f:
  97. lines = f.readlines()
  98. versions = [str(v) for v in versions]
  99. for i in range(0, len(lines)):
  100. line = lines[i]
  101. if pattern_fv.match(line):
  102. lines[i] = ' FILEVERSION {0}\r\n'.format(','.join(versions))
  103. elif pattern_pv.match(line):
  104. lines[i] = ' PRODUCTVERSION {0}\r\n'.format(','.join(versions))
  105. elif pattern_fvs.match(line):
  106. lines[i] = ' VALUE "FileVersion", "{0}"\r\n'.format(version)
  107. elif pattern_pvs.match(line):
  108. lines[i] = ' VALUE "ProductVersion", "{0}"\r\n'.format(version)
  109. with open(win_rc, 'w') as f:
  110. f.write(''.join(lines))
  111. def update_version_h(versions, suffix):
  112. version_h = os.path.join('atom', 'common', 'atom_version.h')
  113. with open(version_h, 'r') as f:
  114. lines = f.readlines()
  115. for i in range(0, len(lines)):
  116. line = lines[i]
  117. if 'ATOM_MAJOR_VERSION' in line:
  118. lines[i] = '#define ATOM_MAJOR_VERSION {0}\n'.format(versions[0])
  119. lines[i + 1] = '#define ATOM_MINOR_VERSION {0}\n'.format(versions[1])
  120. lines[i + 2] = '#define ATOM_PATCH_VERSION {0}\n'.format(versions[2])
  121. if (suffix):
  122. lines[i + 3] = '#define ATOM_PRE_RELEASE_VERSION {0}\n'.format(suffix)
  123. else:
  124. lines[i + 3] = '// #define ATOM_PRE_RELEASE_VERSION\n'
  125. with open(version_h, 'w') as f:
  126. f.write(''.join(lines))
  127. return
  128. def update_info_plist(version):
  129. info_plist = os.path.join('atom', 'browser', 'resources', 'mac', 'Info.plist')
  130. with open(info_plist, 'r') as f:
  131. lines = f.readlines()
  132. for i in range(0, len(lines)):
  133. line = lines[i]
  134. if 'CFBundleVersion' in line:
  135. lines[i + 1] = ' <string>{0}</string>\n'.format(version)
  136. if 'CFBundleShortVersionString' in line:
  137. lines[i + 1] = ' <string>{0}</string>\n'.format(version)
  138. with open(info_plist, 'w') as f:
  139. f.write(''.join(lines))
  140. def update_package_json(version, suffix):
  141. package_json = 'package.json'
  142. with open(package_json, 'r') as f:
  143. lines = f.readlines()
  144. for i in range(0, len(lines)):
  145. line = lines[i];
  146. if 'version' in line:
  147. lines[i] = ' "version": "{0}",\n'.format(version + suffix)
  148. break
  149. with open(package_json, 'w') as f:
  150. f.write(''.join(lines))
  151. def tag_version(version, suffix):
  152. execute(['git', 'commit', '-a', '-m', 'Bump v{0}'.format(version + suffix)])
  153. if __name__ == '__main__':
  154. sys.exit(main())