LibraryVersions.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #! /usr/bin/python3
  2. # DevTools/LibraryVersions.py - Helper for the library's version number
  3. #
  4. # Copyright (c) 2014 - 2017 Apple Inc. and the project authors
  5. # Licensed under Apache License v2.0 with Runtime Library Exception
  6. #
  7. # See LICENSE.txt for license information:
  8. # https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
  9. #
  10. """Helper script to for the versions numbers in the project sources."""
  11. import optparse
  12. import os
  13. import re
  14. import sys
  15. _VERSION_RE = re.compile(r'^(?P<major>\d+)\.(?P<minor>\d+)(.(?P<revision>\d+))?$')
  16. _PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  17. _PODSPEC_PATH = os.path.join(_PROJECT_ROOT, 'SwiftProtobuf.podspec')
  18. _VERSION_SWIFT_PATH = os.path.join(_PROJECT_ROOT, 'Sources/SwiftProtobuf/Version.swift')
  19. _XCCONFIG_PATH = os.path.join(_PROJECT_ROOT, 'SwiftProtobuf.xcodeproj/xcconfigs/Base.xcconfig')
  20. def Fail(message):
  21. sys.stderr.write('Error: %s\n' % message)
  22. sys.exit(1)
  23. def ExtractVersion(s):
  24. match = _VERSION_RE.match(s)
  25. return (match.group('major'), match.group('minor'), match.group('revision') or '0')
  26. def ValidateFiles():
  27. # Extra from SwiftProtobuf.podspec
  28. pod_content = open(_PODSPEC_PATH).read()
  29. match = re.search(r'version = \'(\d+.\d+.\d+)\'', pod_content)
  30. if not match:
  31. Fail('Failed to extract a version from SwiftProtobuf.podspec')
  32. (major, minor, revision) = ExtractVersion(match.group(1))
  33. # Test Sources/SwiftProtobuf/Version.swift
  34. version_swift_content = open(_VERSION_SWIFT_PATH).read()
  35. major_line = 'public static let major = %s\n' % major
  36. minor_line = 'public static let minor = %s\n' % minor
  37. revision_line = 'public static let revision = %s\n' % revision
  38. had_major = major_line in version_swift_content
  39. had_minor = minor_line in version_swift_content
  40. had_revision = revision_line in version_swift_content
  41. if not had_major or not had_minor or not had_revision:
  42. Fail('Version in Sources/SwiftProtobuf/Version.swift did not match SwiftProtobuf.podspec')
  43. # Test SwiftProtobuf.xcodeproj/xcconfigs/Base.xcconfig
  44. xcconfig_content = open(_XCCONFIG_PATH).read()
  45. current_version_line = 'CURRENT_PROJECT_VERSION = %s.%s.%s' % (major, minor, revision)
  46. if current_version_line not in xcconfig_content:
  47. Fail('Version in SwiftProtobuf.xcodeproj/xcconfigs/Base.xcconfig did not match SwiftProtobuf.podspec')
  48. def UpdateFiles(version_string):
  49. (major, minor, revision) = ExtractVersion(version_string)
  50. # Update SwiftProtobuf.podspec
  51. pod_content = open(_PODSPEC_PATH).read()
  52. pod_content = re.sub(r'version = \'(\d+\.\d+\.\d+)\'',
  53. 'version = \'%s.%s.%s\'' % (major, minor, revision),
  54. pod_content)
  55. open(_PODSPEC_PATH, 'w').write(pod_content)
  56. # Update Sources/SwiftProtobuf/Version.swift
  57. version_swift_content = open(_VERSION_SWIFT_PATH).read()
  58. version_swift_content = re.sub(r'public static let major = \d+\n',
  59. 'public static let major = %s\n' % major,
  60. version_swift_content)
  61. version_swift_content = re.sub(r'public static let minor = \d+\n',
  62. 'public static let minor = %s\n' % minor,
  63. version_swift_content)
  64. version_swift_content = re.sub(r'public static let revision = \d+\n',
  65. 'public static let revision = %s\n' % revision,
  66. version_swift_content)
  67. open(_VERSION_SWIFT_PATH, 'w').write(version_swift_content)
  68. # Update SwiftProtobuf.xcodeproj/xcconfigs/Base.xcconfig
  69. xcconfig_content = open(_XCCONFIG_PATH).read()
  70. xcconfig_content = re.sub(r'CURRENT_PROJECT_VERSION = \d+\.\d+\.\d+',
  71. 'CURRENT_PROJECT_VERSION = %s.%s.%s' % (major, minor, revision),
  72. xcconfig_content)
  73. open(_XCCONFIG_PATH, 'w').write(xcconfig_content)
  74. def main(args):
  75. usage = '%prog [OPTIONS] [VERSION]'
  76. description = (
  77. 'Helper for the version numbers in the project sources.'
  78. )
  79. parser = optparse.OptionParser(usage=usage, description=description)
  80. parser.add_option('--validate',
  81. default=False, action='store_true',
  82. help='Check if the versions in all the files match.')
  83. opts, extra_args = parser.parse_args(args)
  84. if opts.validate:
  85. if extra_args:
  86. parser.error('No version can be given when using --validate.')
  87. else:
  88. if len(extra_args) != 1:
  89. parser.error('Expected one argument, the version number to ensure is in the sources.')
  90. if not _VERSION_RE.match(extra_args[0]):
  91. parser.error('Version does not appear to be in the form of x.y.z.')
  92. # Always validate, just use the flag to tell if we're expected to also have set something.
  93. if not opts.validate:
  94. UpdateFiles(extra_args[0])
  95. ValidateFiles()
  96. return 0
  97. if __name__ == '__main__':
  98. sys.exit(main(sys.argv[1:]))