1
+ # Copyright 2020, gRPC Authors All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import os
16
+ import json
17
+ import random
18
+ import string
19
+ import argparse
20
+
21
+ class Dependency :
22
+ def __init__ (self , name , version = "s.version.to_s" , internal = True ):
23
+ self .name = name
24
+ self .version = version
25
+ self .internal = internal
26
+
27
+ def as_podspec (self ):
28
+ if self .internal :
29
+ return " s.dependency '%s', %s\n " % (self .name , self .version )
30
+ else :
31
+ return " s.dependency '%s', '%s'\n " % (self .name , self .version )
32
+
33
+ class Pod :
34
+ def __init__ (self , name , module_name , version , dependencies = []):
35
+ self .name = name
36
+ self .module_name = module_name
37
+ self .version = version
38
+ self .dependencies = dependencies
39
+
40
+ def add_dependency (self , dependency ):
41
+ self .dependencies .append (dependency )
42
+
43
+ def as_podspec (self ):
44
+ print ('\n ' )
45
+ print ('Building Podspec for %s' % self .name )
46
+ print ('-----------------------------------------------------------' )
47
+
48
+ podspec = "Pod::Spec.new do |s|\n \n "
49
+ podspec += " s.name = '%s'\n " % self .name
50
+ podspec += " s.module_name = '%s'\n " % self .module_name
51
+ podspec += " s.version = '%s'\n " % self .version
52
+ podspec += " s.license = { :type => 'Apache 2.0', :file => 'LICENSE' }\n "
53
+ podspec += " s.summary = 'Swift gRPC code generator plugin and runtime library'\n "
54
+ podspec += " s.homepage = 'https://www.grpc.io'\n "
55
+ podspec += " s.authors = { 'The gRPC contributors' => 'grpc-packages@google.com' }\n \n "
56
+
57
+ podspec += " s.source = { :git => 'https://github.com/grpc/grpc-swift.git', :tag => s.version }\n \n "
58
+
59
+ podspec += " s.swift_version = '5.0'\n "
60
+
61
+ podspec += " s.ios.deployment_target = '10.0'\n "
62
+ podspec += " s.osx.deployment_target = '10.12'\n "
63
+ podspec += " s.tvos.deployment_target = '10.0'\n "
64
+
65
+ podspec += " s.source_files = 'Sources/%s/**/*.{swift,c,h}'\n " % (self .module_name )
66
+
67
+ podspec += "\n " if len (self .dependencies ) > 0 else ""
68
+
69
+ for dep in self .dependencies :
70
+ podspec += dep .as_podspec ()
71
+
72
+ podspec += "\n end"
73
+ return podspec
74
+
75
+ class PodManager :
76
+ pods = []
77
+
78
+ def __init__ (self , directory , version , should_publish ):
79
+ self .directory = directory
80
+ self .version = version
81
+ self .should_publish = should_publish
82
+
83
+ def write (self , pod , contents ):
84
+ print (" Writing to %s/%s.podspec " % (self .directory , pod ))
85
+ f = open ("%s/%s.podspec" % (self .directory , pod ), "w" )
86
+ f .write (contents )
87
+ f .close
88
+
89
+ def publish (self , pod_name ):
90
+ os .system ('pod repo update' )
91
+ print (" Publishing %s.podspec" % (pod_name ))
92
+ os .system ('pod repo push %s/%s.podspec' % (self .directory , pod_name ))
93
+
94
+ def build_pods (self ):
95
+ CGRPCZlibPod = Pod ('CGRPCZlib' , 'CGRPCZlib' , self .version )
96
+
97
+ GRPCPod = Pod ('gRPC-Swift' , 'GRPC' , self .version , get_grpc_deps ())
98
+ GRPCPod .add_dependency (Dependency ('CGRPCZlib' ))
99
+
100
+ self .pods .append (CGRPCZlibPod )
101
+ self .pods .append (GRPCPod )
102
+
103
+ def go (self ):
104
+ self .build_pods ()
105
+ # Create .podspec files and publish
106
+ for target in self .pods :
107
+ self .write (target .name , target .as_podspec ())
108
+ if self .should_publish :
109
+ self .publish (target .name )
110
+ else :
111
+ print (" Skipping Publishing..." )
112
+
113
+ def process_package (string ):
114
+ if string == "swift-log" :
115
+ return "Logging"
116
+
117
+ if len (string .split ('-' )) > 1 :
118
+ tempList = string .split ('-' )
119
+ returnStr = ""
120
+ for item in tempList :
121
+ if item == 'nio' or item == 'ssl' or item == 'http2' :
122
+ returnStr += item .upper ()
123
+ else :
124
+ returnStr += item .title ()
125
+ return returnStr
126
+
127
+ return string
128
+
129
+ def get_grpc_deps ():
130
+ with open ('Package.resolved' ) as f :
131
+ data = json .load (f )
132
+
133
+ deps = []
134
+
135
+ for obj in data ["object" ]["pins" ]:
136
+ package = process_package (obj ["package" ])
137
+ version = obj ["state" ]["version" ]
138
+
139
+ deps .append (Dependency (package , version , False ))
140
+
141
+ return deps
142
+
143
+ def main ():
144
+
145
+ # Setup
146
+
147
+ parser = argparse .ArgumentParser (description = 'Build Podspec files for SwiftGRPC' )
148
+
149
+ parser .add_argument (
150
+ '-u' ,
151
+ '--upload' ,
152
+ action = 'store_true' ,
153
+ help = 'Determines if the newly built Podspec files should be pushed.'
154
+ )
155
+
156
+ parser .add_argument ('version' )
157
+
158
+ args = parser .parse_args ()
159
+
160
+ should_publish = args .upload
161
+ version = args .version
162
+
163
+ # Creates temp folder /tmp/.argonaut_podspecsXXXXXX for storage
164
+ # XXXXXX is a random alpha numeric string to ensure we don't have duplicates
165
+ path = "/tmp/.build_podspecs" + '' .join (random .SystemRandom ().choice (string .ascii_uppercase + string .digits ) for _ in range (6 ))
166
+ os .mkdir (path )
167
+
168
+ pod_manager = PodManager (path , version , should_publish )
169
+ pod_manager .go ()
170
+
171
+ if __name__ == "__main__" :
172
+ main ()
0 commit comments