build_podspecs.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. if self.use_verbatim_version:
  44. return " s.dependency '%s', %s\n" % (self.name, self.version)
  45. return " s.dependency '%s', '%s'\n" % (self.name, self.version)
  46. class Pod:
  47. def __init__(self, name, module_name, version, description, dependencies=None):
  48. self.name = name
  49. self.module_name = module_name
  50. self.version = version
  51. if dependencies is None:
  52. dependencies = []
  53. self.dependencies = dependencies
  54. self.description = description
  55. def add_dependency(self, dependency):
  56. self.dependencies.append(dependency)
  57. def as_podspec(self):
  58. print('\n')
  59. print('Building Podspec for %s' % self.name)
  60. print('-----------------------------------------------------------')
  61. podspec = "Pod::Spec.new do |s|\n\n"
  62. podspec += " s.name = '%s'\n" % self.name
  63. podspec += " s.module_name = '%s'\n" % self.module_name
  64. podspec += " s.version = '%s'\n" % self.version
  65. podspec += " s.license = { :type => 'Apache 2.0', :file => 'LICENSE' }\n"
  66. podspec += " s.summary = '%s'\n" % self.description
  67. podspec += " s.homepage = 'https://www.grpc.io'\n"
  68. podspec += " s.authors = { 'The gRPC contributors' => \'grpc-packages@google.com' }\n\n"
  69. podspec += " s.source = { :git => 'https://github.com/grpc/grpc-swift.git', :tag => s.version }\n\n"
  70. podspec += " s.swift_version = '5.0'\n"
  71. podspec += " s.ios.deployment_target = '10.0'\n"
  72. podspec += " s.osx.deployment_target = '10.10'\n"
  73. podspec += " s.tvos.deployment_target = '10.0'\n"
  74. podspec += " s.source_files = 'Sources/%s/**/*.{swift,c,h}'\n" % (self.module_name)
  75. podspec += "\n" if len(self.dependencies) > 0 else ""
  76. for dep in self.dependencies:
  77. podspec += dep.as_podspec()
  78. podspec += "\nend"
  79. return podspec
  80. class PodManager:
  81. pods = []
  82. def __init__(self, directory, version, should_publish):
  83. self.directory = directory
  84. self.version = version
  85. self.should_publish = should_publish
  86. def write(self, pod, contents):
  87. print(' Writing to %s/%s.podspec ' % (self.directory, pod))
  88. with open('%s/%s.podspec' % (self.directory, pod), 'w') as podspec_file:
  89. podspec_file.write(contents)
  90. def publish(self, pod_name):
  91. os.system('pod repo update')
  92. print(' Publishing %s.podspec' % (pod_name))
  93. os.system('pod trunk push %s/%s.podspec' % (self.directory, pod_name))
  94. def build_pods(self):
  95. cgrpczlib_pod = Pod(
  96. 'CGRPCZlib',
  97. 'CGRPCZlib',
  98. self.version,
  99. 'Compression library that provides in-memory compression and decompression functions'
  100. )
  101. grpc_pod = Pod(
  102. 'gRPC-Swift',
  103. 'GRPC',
  104. self.version,
  105. 'Swift gRPC code generator plugin and runtime library',
  106. get_grpc_deps()
  107. )
  108. grpc_pod.add_dependency(Dependency('CGRPCZlib'))
  109. self.pods += [cgrpczlib_pod, grpc_pod]
  110. def go(self):
  111. self.build_pods()
  112. # Create .podspec files and publish
  113. for target in self.pods:
  114. self.write(target.name, target.as_podspec())
  115. if self.should_publish:
  116. self.publish(target.name)
  117. else:
  118. print(' Skipping Publishing...')
  119. def process_package(string):
  120. pod_mappings = {
  121. 'swift-log': 'Logging',
  122. 'swift-nio': 'SwiftNIO',
  123. 'swift-nio-http2': 'SwiftNIOHTTP2',
  124. 'swift-nio-ssl': 'SwiftNIOSSL',
  125. 'swift-nio-transport-services': 'SwiftNIOTransportServices',
  126. 'SwiftProtobuf': 'SwiftProtobuf'
  127. }
  128. return pod_mappings[string]
  129. def get_grpc_deps():
  130. with open('Package.resolved') as package:
  131. data = json.load(package)
  132. deps = []
  133. for obj in data['object']['pins']:
  134. package = process_package(obj['package'])
  135. version = obj['state']['version']
  136. deps.append(Dependency(package, version, False))
  137. return deps
  138. def dir_path(path):
  139. if os.path.isdir(path):
  140. return path
  141. raise NotADirectoryError(path)
  142. def main():
  143. # Setup
  144. parser = argparse.ArgumentParser(description='Build Podspec files for SwiftGRPC')
  145. parser.add_argument(
  146. '-p',
  147. '--path',
  148. type=dir_path,
  149. help='The directory where generated podspec files will be saved. If not passed, defaults to place in the current working directory.'
  150. )
  151. parser.add_argument(
  152. '-u',
  153. '--upload',
  154. action='store_true',
  155. help='Determines if the newly built Podspec files should be pushed.'
  156. )
  157. parser.add_argument('version')
  158. args = parser.parse_args()
  159. should_publish = args.upload
  160. version = args.version
  161. path = args.path
  162. if not path:
  163. path = os.getcwd()
  164. pod_manager = PodManager(path, version, should_publish)
  165. pod_manager.go()
  166. return 0
  167. if __name__ == "__main__":
  168. main()