build_podspecs.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/usr/bin/env python3
  2. # Copyright 2020, gRPC Authors All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. import json
  17. import random
  18. import string
  19. import argparse
  20. class Dependency:
  21. def __init__(self, name, version='s.version.to_s', use_verbatim_version=True):
  22. self.name = name
  23. self.version = version
  24. self.use_verbatim_version = use_verbatim_version
  25. def as_podspec(self):
  26. indent=' '
  27. if self.use_verbatim_version:
  28. return indent + "s.dependency '%s', %s\n" % (self.name, self.version)
  29. return indent + "s.dependency '%s', '%s'\n" % (self.name, self.version)
  30. class Pod:
  31. def __init__(self, name, module_name, version, description, dependencies=None, is_plugins_pod=False):
  32. self.name = name
  33. self.module_name = module_name
  34. self.version = version
  35. self.is_plugins_pod = is_plugins_pod
  36. if dependencies is None:
  37. dependencies = []
  38. self.dependencies = dependencies
  39. self.description = description
  40. def add_dependency(self, dependency):
  41. self.dependencies.append(dependency)
  42. def as_podspec(self):
  43. print('\n')
  44. print('Building Podspec for %s' % self.name)
  45. print('-' * 80)
  46. indent=' ' * 4
  47. podspec = "Pod::Spec.new do |s|\n\n"
  48. podspec += indent + "s.name = '%s'\n" % self.name
  49. if not self.is_plugins_pod:
  50. podspec += indent + "s.module_name = '%s'\n" % self.module_name
  51. podspec += indent + "s.version = '%s'\n" % self.version
  52. podspec += indent + "s.license = { :type => 'Apache 2.0', :file => 'LICENSE' }\n"
  53. podspec += indent + "s.summary = '%s'\n" % self.description
  54. podspec += indent + "s.homepage = 'https://www.grpc.io'\n"
  55. podspec += indent + "s.authors = { 'The gRPC contributors' => \'grpc-packages@google.com' }\n\n"
  56. podspec += indent + "s.swift_version = '5.0'\n"
  57. podspec += indent + "s.ios.deployment_target = '10.0'\n"
  58. podspec += indent + "s.osx.deployment_target = '10.12'\n"
  59. podspec += indent + "s.tvos.deployment_target = '10.0'\n"
  60. if self.is_plugins_pod:
  61. podspec += indent + "s.source = { :http => \"https://github.com/grpc/grpc-swift/releases/download/#{s.version}/protoc-grpc-swift-plugins-#{s.version}.zip\"}\n\n"
  62. podspec += indent + "s.preserve_paths = '*'\n"
  63. else:
  64. podspec += indent + "s.source = { :git => \"https://github.com/grpc/grpc-swift.git\", :tag => s.version }\n\n"
  65. podspec += indent + "s.source_files = 'Sources/%s/**/*.{swift,c,h}'\n" % (self.module_name)
  66. podspec += "\n" if len(self.dependencies) > 0 else ""
  67. for dep in self.dependencies:
  68. podspec += dep.as_podspec()
  69. podspec += "\nend"
  70. return podspec
  71. class PodManager:
  72. def __init__(self, directory, version, should_publish):
  73. self.directory = directory
  74. self.version = version
  75. self.should_publish = should_publish
  76. def write(self, pod, contents):
  77. print(' Writing to %s/%s.podspec ' % (self.directory, pod))
  78. with open('%s/%s.podspec' % (self.directory, pod), 'w') as podspec_file:
  79. podspec_file.write(contents)
  80. def publish(self, pod_name):
  81. os.system('pod repo update')
  82. print(' Publishing %s.podspec' % (pod_name))
  83. os.system('pod trunk push --synchronous %s/%s.podspec' % (self.directory, pod_name))
  84. def build_pods(self):
  85. cgrpczlib_pod = Pod(
  86. 'CGRPCZlib',
  87. 'CGRPCZlib',
  88. self.version,
  89. 'Compression library that provides in-memory compression and decompression functions'
  90. )
  91. grpc_pod = Pod(
  92. 'gRPC-Swift',
  93. 'GRPC',
  94. self.version,
  95. 'Swift gRPC code generator plugin and runtime library',
  96. get_grpc_deps()
  97. )
  98. grpc_plugins_pod = Pod(
  99. 'gRPC-Swift-Plugins',
  100. '',
  101. self.version,
  102. 'Swift gRPC code generator plugin binaries',
  103. [],
  104. is_plugins_pod=True
  105. )
  106. grpc_pod.add_dependency(Dependency(cgrpczlib_pod.name))
  107. grpc_plugins_pod.add_dependency(Dependency(grpc_pod.name))
  108. return [cgrpczlib_pod, grpc_pod, grpc_plugins_pod]
  109. def go(self, start_from):
  110. pods = self.build_pods()
  111. if start_from:
  112. pods = pods[list(pod.name for pod in pods).index(start_from):]
  113. # Create .podspec files and publish
  114. for target in pods:
  115. self.write(target.name, target.as_podspec())
  116. if self.should_publish:
  117. self.publish(target.name)
  118. else:
  119. print(' Skipping Publishing...')
  120. def process_package(string):
  121. pod_mappings = {
  122. 'swift-log': 'Logging',
  123. 'swift-nio': 'SwiftNIO',
  124. 'swift-nio-http2': 'SwiftNIOHTTP2',
  125. 'swift-nio-ssl': 'SwiftNIOSSL',
  126. 'swift-nio-transport-services': 'SwiftNIOTransportServices',
  127. 'SwiftProtobuf': 'SwiftProtobuf'
  128. }
  129. return pod_mappings[string]
  130. def get_grpc_deps():
  131. with open('Package.resolved') as package:
  132. data = json.load(package)
  133. deps = []
  134. for obj in data['object']['pins']:
  135. package = process_package(obj['package'])
  136. version = obj['state']['version']
  137. next_major_version = int(version.split('.')[0]) + 1
  138. deps.append(Dependency(package, '\'>= {}\', \'< {}\''.format(version, next_major_version)))
  139. return deps
  140. def dir_path(path):
  141. if os.path.isdir(path):
  142. return path
  143. raise NotADirectoryError(path)
  144. def main():
  145. parser = argparse.ArgumentParser(description='Build Podspec files for gRPC Swift')
  146. parser.add_argument(
  147. '-p',
  148. '--path',
  149. type=dir_path,
  150. help='The directory where generated podspec files will be saved. If not passed, defaults to the current working directory.'
  151. )
  152. parser.add_argument(
  153. '-u',
  154. '--upload',
  155. action='store_true',
  156. help='Determines if the newly built Podspec files should be pushed.'
  157. )
  158. parser.add_argument(
  159. '-f',
  160. '--start-from',
  161. help='The name of the Podspec to start from.'
  162. )
  163. parser.add_argument('version')
  164. args = parser.parse_args()
  165. should_publish = args.upload
  166. version = args.version
  167. path = args.path
  168. start_from = args.start_from
  169. if not path:
  170. path = os.getcwd()
  171. pod_manager = PodManager(path, version, should_publish)
  172. pod_manager.go(start_from)
  173. return 0
  174. if __name__ == "__main__":
  175. main()