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