forked from leancloud/objc-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpodspec.rb
296 lines (238 loc) · 9.19 KB
/
podspec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env ruby
# Podspec automation script
#
# Created by Tang Tianyong on 06/28/16.
# Copyright (c) 2016 LeanCloud Inc. All rights reserved.
require 'clactive'
require 'fileutils'
require 'mustache'
require 'xcodeproj'
module Podspec
class Generator
attr_reader :version
attr_reader :project
attr_reader :targets
attr_reader :output_path
PROJECT_PATH = 'AVOS/AVOS.xcodeproj'
def initialize(version, output_path = nil)
@version = version
@project = Xcodeproj::Project.open(PROJECT_PATH)
@output_path = output_path
end
# All targets of project.
def targets
project.targets
end
# Return target for name.
def target(name)
target = targets.find { |target| target.name == name }
abort "The target named #{name} not found." if target.nil?
return target
end
# Return relative path based on current working directory.
def relative_path(pathname)
pwd = Pathname.new('.').realpath
pathname.file_ref.real_path.relative_path_from(pwd)
end
# Return all header files of a target.
def header_files(target_name, &filter)
target = target(target_name)
header_files = target.headers_build_phase.files
header_files = header_files.select(&filter) unless filter.nil?
header_paths = header_files.map { |pathname|
relative_path(pathname)
}
header_paths
end
# Return all public header files of a target.
def public_header_files(target_name)
header_files(target_name) do |file|
settings = file.settings
settings && settings['ATTRIBUTES'].include?('Public')
end
end
# Return all public source files for a target.
def source_files(target_name, &filter)
target = target(target_name)
source_files = target.source_build_phase.files
source_files = source_files.select(&filter) unless filter.nil?
source_paths = source_files.map { |pathname|
relative_path(pathname)
}
source_paths
end
# Check whether a file has compiler flag or not.
def has_compiler_flag(file, flag)
settings = file.settings
return false if settings.nil?
compiler_flags = settings['COMPILER_FLAGS']
return false if compiler_flags.nil?
compiler_flags.include? flag
end
# Return all files that have '-fno-objc-arc' flag of a target.
def no_arc_files(target_name)
source_files(target_name) do |file|
has_compiler_flag(file, '-fno-objc-arc')
end
end
# Return all files that have '-fobjc-arc' flag of a target.
def arc_files(target_name)
source_files(target_name) do |file|
has_compiler_flag(file, '-fobjc-arc')
end
end
# Convert pathnames to file list string.
def file_list_string(pathnames, indent)
spaces = ' ' * indent
unless pathnames.empty?
paths = pathnames.map { |pathname| "'#{pathname.to_s}'" }
paths.join(",\n#{spaces}")
else
spaces += '[]'
end
end
# Write content to file.
# If `output_path` specified, file will be written to that path,
# otherwise, file will be written to `filename`.
def write(filename, content)
abort 'File name not found.' if filename.nil?
path = filename
unless output_path.nil?
abort "Invalid output directory: #{output_path}" unless File.directory?(output_path)
path = File.join(output_path, filename)
end
File.write(path, content)
end
# Generate foundtion podspec.
def generateAVOSCloud()
ios_header_files = header_files('AVOSCloud-iOS')
osx_header_files = header_files('AVOSCloud-macOS')
watchos_header_files = header_files('AVOSCloud-watchOS')
ios_source_files = source_files('AVOSCloud-iOS')
osx_source_files = source_files('AVOSCloud-macOS')
watchos_source_files = source_files('AVOSCloud-watchOS')
public_header_files = public_header_files('AVOSCloud-iOS')
osx_exclude_files = (ios_header_files - osx_header_files) + (ios_source_files - osx_source_files)
watchos_exclude_files = (ios_header_files - watchos_header_files) + (ios_source_files - watchos_source_files)
template = File.read('Podspec/AVOSCloud.podspec.mustache')
podspec = Mustache.render template, {
'version' => version,
'source_files' => "'AVOS/AVOSCloud/**/*.{h,m}'",
'resources' => "'AVOS/AVOSCloud/AVOSCloud_Art.inc'",
'public_header_files' => file_list_string(public_header_files, 4),
'osx_exclude_files' => file_list_string(osx_exclude_files, 4),
'watchos_exclude_files' => file_list_string(watchos_exclude_files, 4),
'xcconfig' => "{'OTHER_LDFLAGS' => '-ObjC'}"
}
write 'AVOSCloud.podspec', podspec
end
# Generate IM podspec.
def generateAVOSCloudIM()
header_files = header_files('AVOSCloudIM-iOS')
source_files = source_files('AVOSCloudIM-iOS')
no_arc_files = [
Pathname.new('AVOS/AVOSCloudIM/Protobuf/*.{h,m}'),
Pathname.new('AVOS/AVOSCloudIM/Protobuf/google/protobuf/*.{h,m}'),
Pathname.new('AVOS/AVOSCloudIM/Commands/MessagesProtoOrig.pbobjc.{h,m}')
]
public_header_files = public_header_files('AVOSCloudIM-iOS')
template = File.read('Podspec/AVOSCloudIM.podspec.mustache')
podspec = Mustache.render template, {
'version' => version,
'_ARC' => {
'source_files' => file_list_string(header_files + source_files, 6),
'public_header_files' => file_list_string(public_header_files, 6),
'exclude_files' => file_list_string(no_arc_files, 6)
}
}
write 'AVOSCloudIM.podspec', podspec
end
# Generate IM Group Chat podspec.
def generateAVOSCloudIMGroupChat()
header_files = header_files('AVOSCloudIMGroupChat')
source_files = source_files('AVOSCloudIMGroupChat')
public_header_files = public_header_files('AVOSCloudIMGroupChat')
template = File.read('Podspec/AVOSCloudIMGroupChat.podspec.mustache')
podspec = Mustache.render template, {
'version' => version,
'source_files' => file_list_string(header_files + source_files, 4),
'public_header_files' => file_list_string(public_header_files, 4),
}
write 'AVOSCloudIMGroupChat.podspec', podspec
end
# Generate crash reporting podspec.
def generateAVOSCloudCrashReporting()
header_files = header_files('AVOSCloudCrashReporting-iOS')
source_files = source_files('AVOSCloudCrashReporting-iOS')
arc_files = arc_files('AVOSCloudCrashReporting-iOS')
non_arc_files = header_files + source_files - arc_files
public_header_files = public_header_files('AVOSCloudCrashReporting-iOS')
header_search_paths = [
'"${PODS_ROOT}/AVOSCloudCrashReporting/Breakpad/src"',
'"${PODS_ROOT}/AVOSCloudCrashReporting/Breakpad/src/client/apple/Framework"',
'"${PODS_ROOT}/AVOSCloudCrashReporting/Breakpad/src/common/mac"'
].join(' ')
template = File.read('Podspec/AVOSCloudCrashReporting.podspec.mustache')
podspec = Mustache.render template, {
'version' => version,
'_ARC' => {
'source_files' => file_list_string(arc_files, 6),
},
'_NOARC' => {
'source_files' => file_list_string(non_arc_files, 6),
'public_header_files' => file_list_string(public_header_files, 6),
'preserve_paths' => "'Breakpad'",
'pod_target_xcconfig' => "{'HEADER_SEARCH_PATHS' => '#{header_search_paths}'}"
}
}
write 'AVOSCloudCrashReporting.podspec', podspec
end
# Generate live query podspec.
def generateAVOSCloudLiveQuery()
header_files = header_files('AVOSCloudLiveQuery-iOS')
source_files = source_files('AVOSCloudLiveQuery-macOS')
public_header_files = public_header_files('AVOSCloudLiveQuery-iOS')
template = File.read('Podspec/AVOSCloudLiveQuery.podspec.mustache')
podspec = Mustache.render template, {
'version' => version,
'source_files' => "'AVOS/AVOSCloudLiveQuery/**/*.{h,m}'",
'public_header_files' => file_list_string(public_header_files, 4)
}
write 'AVOSCloudLiveQuery.podspec', podspec
end
def generate()
generateAVOSCloud
generateAVOSCloudIM
generateAVOSCloudIMGroupChat
generateAVOSCloudCrashReporting
generateAVOSCloudLiveQuery
end
end
end
def execute_command(command, exit_on_error = true)
output = `#{command}`
exitstatus = $?.exitstatus
if exitstatus != 0 && exit_on_error
$stderr.puts "Following command exits with status #{exitstatus}:"
$stderr.puts command
exit 1
end
output
end
CLActive do
subcmd :create do
option :version, '-v v', '--version=version', 'Pod version'
action do |opt|
abort 'Version number not found.' if version?.nil?
abort 'Version number is invalid.' unless Gem::Version.correct? version?
generator = Podspec::Generator.new(version?)
generator.generate
end
end
subcmd :push do
action do |opt|
pusher = Podspec::Pusher.new('.')
pusher.push
end
end
end