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