build_podspecs.py 5.6 KB

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