LibraryVersions.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. def Fail(message):
  20. sys.stderr.write('Error: %s\n' % message)
  21. sys.exit(1)
  22. def ExtractVersion(s):
  23. match = _VERSION_RE.match(s)
  24. return (match.group('major'), match.group('minor'), match.group('revision') or '0')
  25. def ValidateFiles():
  26. # Extra from SwiftProtobuf.podspec
  27. pod_content = open(_PODSPEC_PATH).read()
  28. match = re.search(r'version = \'(\d+.\d+.\d+)\'', pod_content)
  29. if not match:
  30. Fail('Failed to extract a version from SwiftProtobuf.podspec')
  31. (major, minor, revision) = ExtractVersion(match.group(1))
  32. # Test Sources/SwiftProtobuf/Version.swift
  33. version_swift_content = open(_VERSION_SWIFT_PATH).read()
  34. major_line = 'public static let major = %s\n' % major
  35. minor_line = 'public static let minor = %s\n' % minor
  36. revision_line = 'public static let revision = %s\n' % revision
  37. had_major = major_line in version_swift_content
  38. had_minor = minor_line in version_swift_content
  39. had_revision = revision_line in version_swift_content
  40. if not had_major or not had_minor or not had_revision:
  41. Fail('Version in Sources/SwiftProtobuf/Version.swift did not match SwiftProtobuf.podspec')
  42. def UpdateFiles(version_string):
  43. (major, minor, revision) = ExtractVersion(version_string)
  44. # Update SwiftProtobuf.podspec
  45. pod_content = open(_PODSPEC_PATH).read()
  46. pod_content = re.sub(r'version = \'(\d+\.\d+\.\d+)\'',
  47. 'version = \'%s.%s.%s\'' % (major, minor, revision),
  48. pod_content)
  49. open(_PODSPEC_PATH, 'w').write(pod_content)
  50. # Update Sources/SwiftProtobuf/Version.swift
  51. version_swift_content = open(_VERSION_SWIFT_PATH).read()
  52. version_swift_content = re.sub(r'public static let major = \d+\n',
  53. 'public static let major = %s\n' % major,
  54. version_swift_content)
  55. version_swift_content = re.sub(r'public static let minor = \d+\n',
  56. 'public static let minor = %s\n' % minor,
  57. version_swift_content)
  58. version_swift_content = re.sub(r'public static let revision = \d+\n',
  59. 'public static let revision = %s\n' % revision,
  60. version_swift_content)
  61. open(_VERSION_SWIFT_PATH, 'w').write(version_swift_content)
  62. def main(args):
  63. usage = '%prog [OPTIONS] [VERSION]'
  64. description = (
  65. 'Helper for the version numbers in the project sources.'
  66. )
  67. parser = optparse.OptionParser(usage=usage, description=description)
  68. parser.add_option('--validate',
  69. default=False, action='store_true',
  70. help='Check if the versions in all the files match.')
  71. opts, extra_args = parser.parse_args(args)
  72. if opts.validate:
  73. if extra_args:
  74. parser.error('No version can be given when using --validate.')
  75. else:
  76. if len(extra_args) != 1:
  77. parser.error('Expected one argument, the version number to ensure is in the sources.')
  78. if not _VERSION_RE.match(extra_args[0]):
  79. parser.error('Version does not appear to be in the form of x.y.z.')
  80. # Always validate, just use the flag to tell if we're expected to also have set something.
  81. if not opts.validate:
  82. UpdateFiles(extra_args[0])
  83. ValidateFiles()
  84. return 0
  85. if __name__ == '__main__':
  86. sys.exit(main(sys.argv[1:]))