build_podspecs.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. """
  16. Script for generating the gRPC-Swift and CGRPCZlib Podspec files.
  17. Usage:
  18. usage: build_podspecs.py [-h] [-p PATH] [-u] version
  19. Build Podspec files for SwiftGRPC
  20. positional arguments:
  21. version
  22. optional arguments:
  23. -h, --help show this help message and exit
  24. -p PATH, --path PATH The directory where generated podspec files will be
  25. saved. If not passed, defaults to place in the current
  26. working directory.
  27. -u, --upload Determines if the newly built Podspec files should be
  28. pushed.
  29. Example:
  30. 'python scripts/build_podspecs.py -u 1.0.0-alpha.11'
  31. """
  32. import os
  33. import json
  34. import random
  35. import string
  36. import argparse
  37. class Dependency:
  38. def __init__(self, name, version='s.version.to_s', use_verbatim_version=True):
  39. self.name = name
  40. self.version = version
  41. self.use_verbatim_version = use_verbatim_version
  42. def as_podspec(self):
  43. indent=' '
  44. if self.use_verbatim_version:
  45. return indent + "s.dependency '%s', %s\n" % (self.name, self.version)
  46. return indent + "s.dependency '%s', '%s'\n" % (self.name, self.version)
  47. class Pod:
  48. def __init__(self, name, module_name, version, description, dependencies=None, is_plugins_pod=False):
  49. self.name = name
  50. self.module_name = module_name
  51. self.version = version
  52. self.is_plugins_pod = is_plugins_pod
  53. if dependencies is None:
  54. dependencies = []
  55. self.dependencies = dependencies
  56. self.description = description
  57. def add_dependency(self, dependency):
  58. self.dependencies.append(dependency)
  59. def as_podspec(self):
  60. print('\n')
  61. print('Building Podspec for %s' % self.name)
  62. print('-----------------------------------------------------------')
  63. indent=' '
  64. podspec = "Pod::Spec.new do |s|\n\n"
  65. podspec += indent + "s.name = '%s'\n" % self.name
  66. podspec += indent + "s.version = '%s'\n" % self.version
  67. podspec += indent + "s.license = { :type => 'Apache 2.0', :file => 'LICENSE' }\n"
  68. podspec += indent + "s.summary = '%s'\n" % self.description
  69. podspec += indent + "s.homepage = 'https://www.grpc.io'\n"
  70. podspec += indent + "s.authors = { 'The gRPC contributors' => \'grpc-packages@google.com' }\n\n"
  71. podspec += indent + "s.swift_version = '5.0'\n"
  72. podspec += indent + "s.ios.deployment_target = '10.0'\n"
  73. podspec += indent + "s.osx.deployment_target = '10.12'\n"
  74. podspec += indent + "s.tvos.deployment_target = '10.0'\n"
  75. if self.is_plugins_pod:
  76. podspec += indent + "s.source = { :http => \"https://github.com/grpc/grpc-swift/releases/download/#{s.version}/protoc-grpc-swift-plugins-#{s.version}.zip\"}\n\n"
  77. podspec += indent + "s.preserve_paths = '*'\n"
  78. else:
  79. podspec += indent + "s.source = { :git => \"https://github.com/grpc/grpc-swift.git\", :tag => s.version }\n\n"
  80. podspec += indent + "s.source_files = 'Sources/%s/**/*.{swift,c,h}'\n" % (self.module_name)
  81. podspec += "\n" if len(self.dependencies) > 0 else ""
  82. for dep in self.dependencies:
  83. podspec += dep.as_podspec()
  84. podspec += "\nend"
  85. return podspec
  86. class PodManager:
  87. pods = []
  88. def __init__(self, directory, version, should_publish):
  89. self.directory = directory
  90. self.version = version
  91. self.should_publish = should_publish
  92. def write(self, pod, contents):
  93. print(' Writing to %s/%s.podspec ' % (self.directory, pod))
  94. with open('%s/%s.podspec' % (self.directory, pod), 'w') as podspec_file:
  95. podspec_file.write(contents)
  96. def publish(self, pod_name):
  97. os.system('pod repo update')
  98. print(' Publishing %s.podspec' % (pod_name))
  99. os.system('pod trunk push --synchronous %s/%s.podspec' % (self.directory, pod_name))
  100. def build_pods(self):
  101. cgrpczlib_pod = Pod(
  102. 'CGRPCZlib',
  103. 'CGRPCZlib',
  104. self.version,
  105. 'Compression library that provides in-memory compression and decompression functions'
  106. )
  107. grpc_pod = Pod(
  108. 'gRPC-Swift',
  109. 'GRPC',
  110. self.version,
  111. 'Swift gRPC code generator plugin and runtime library',
  112. get_grpc_deps()
  113. )
  114. grpc_plugins_pod = Pod(
  115. 'gRPC-Swift-Plugins',
  116. '',
  117. self.version,
  118. 'Swift gRPC code generator plugin binaries',
  119. [],
  120. is_plugins_pod=True
  121. )
  122. grpc_pod.add_dependency(Dependency(cgrpczlib_pod.name))
  123. grpc_plugins_pod.add_dependency(Dependency(grpc_pod.name))
  124. self.pods += [cgrpczlib_pod, grpc_pod, grpc_plugins_pod]
  125. def go(self):
  126. self.build_pods()
  127. # Create .podspec files and publish
  128. for target in self.pods:
  129. self.write(target.name, target.as_podspec())
  130. if self.should_publish:
  131. self.publish(target.name)
  132. else:
  133. print(' Skipping Publishing...')
  134. def process_package(string):
  135. pod_mappings = {
  136. 'swift-log': 'Logging',
  137. 'swift-nio': 'SwiftNIO',
  138. 'swift-nio-http2': 'SwiftNIOHTTP2',
  139. 'swift-nio-ssl': 'SwiftNIOSSL',
  140. 'swift-nio-transport-services': 'SwiftNIOTransportServices',
  141. 'SwiftProtobuf': 'SwiftProtobuf'
  142. }
  143. return pod_mappings[string]
  144. def get_grpc_deps():
  145. with open('Package.resolved') as package:
  146. data = json.load(package)
  147. deps = []
  148. for obj in data['object']['pins']:
  149. package = process_package(obj['package'])
  150. version = obj['state']['version']
  151. next_major_version = int(version.split('.')[0]) + 1
  152. deps.append(Dependency(package, '\'>= {}\', \'< {}\''.format(version, next_major_version)))
  153. return deps
  154. def dir_path(path):
  155. if os.path.isdir(path):
  156. return path
  157. raise NotADirectoryError(path)
  158. def main():
  159. # Setup
  160. parser = argparse.ArgumentParser(description='Build Podspec files for SwiftGRPC')
  161. parser.add_argument(
  162. '-p',
  163. '--path',
  164. type=dir_path,
  165. help='The directory where generated podspec files will be saved. If not passed, defaults to place in the current working directory.'
  166. )
  167. parser.add_argument(
  168. '-u',
  169. '--upload',
  170. action='store_true',
  171. help='Determines if the newly built Podspec files should be pushed.'
  172. )
  173. parser.add_argument('version')
  174. args = parser.parse_args()
  175. should_publish = args.upload
  176. version = args.version
  177. path = args.path
  178. if not path:
  179. path = os.getcwd()
  180. pod_manager = PodManager(path, version, should_publish)
  181. pod_manager.go()
  182. return 0
  183. if __name__ == "__main__":
  184. main()