Class: BB::SourceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-bb-PodAssistant/config/source_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(verify_stable_env = true) ⇒ SourceManager

Returns a new instance of SourceManager.



10
11
12
13
14
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 10

def initialize(verify_stable_env=true)
    @env = BB::StableEnv.new(verify_stable_env)
    @businessSpecName = @env.business_stable
    @stableMgr = BB::StableManager.new(@env, verify_stable_env)
end

Instance Method Details

#business_stable_datasObject

业务线-公共lock数据



44
45
46
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 44

def business_stable_datas
    return get_stable_datas(business_stable_yaml)
end

#business_stable_yamlObject

业务源路径(远端)



26
27
28
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 26

def business_stable_yaml
    return @stableMgr.business_stable_yaml(@businessSpecName)
end

#cache_pathObject



16
17
18
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 16

def cache_path
  return @env.cache_path
end

#common_stable_datasObject

通用-公共lock数据



40
41
42
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 40

def common_stable_datas
    return get_stable_datas(public_stable_yaml)
end

#compare_specs(local_specs, common_specs, update_pods = []) ⇒ Object

compare specs 参数1:本地,参数2:远端,参数3:指定更新pod库



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
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 194

def compare_specs(local_specs, common_specs, update_pods=[])
    added_projects = []
    updated_projects = []
    rollbacked_projects = []
    deleted_projects = []
    new_specs = local_specs

    listdata = common_specs[YAML_CONFIG_LIST_KEY]
    removedata = common_specs[YAML_CONFIG_REMOVE_KEY]
    dependenciesdata = common_specs[YAML_CONFIG_DEPENDENCIES_KEY]

    # puts "local_specs:#{local_specs}".send(:green)
    # puts "common_specs:#{common_specs}".send(:green)
    need_update_pod_lists={}
    # step.1 匹配组件版本信息
    listdata.each do |name, version|
        name = name.to_s
        local_version = local_specs[name]
        if local_version.nil?
            # 本地不存在这个数据
        elsif local_version != version
            # puts "merge name:#{name} local_version:#{local_version} route_version:#{version}".red
            # 版本不一致
            podCoreName = subspec_podname(name)
            if (update_pods.length == 0) || (update_pods.include?(podCoreName))
                if versionGreat(version, local_version)
                    updated_projects << "#{name}】 (#{local_version}) -> (#{version.to_s.send(:yellow)})"
                else
                    rollbacked_projects << "#{name}】 (#{version.to_s.send(:red)}) <- (#{local_version})"
                end
                need_update_pod_lists[podCoreName] = version
            end
        end
    end
    # 解决subspec组件同步更新版本信息
    need_update_pod_lists.each do |podCoreName, version|
        new_specs.keys.each do |podName|
            if (podName == podCoreName) || has_pod_subspec(podName, podCoreName)
                new_specs[podName] = version
            end
        end
    end
    # step.2 匹配组件新增
    dependenciesdata.each do |name, array|
        name = name.to_s
        version = listdata[name]
        unless version
            puts "公共库缺少[#{name}]版本依赖 cls:#{listdata.class} listdata:#{listdata}".send(:red)
            exit
        end
        local_exist_ver = new_specs[name]
        if local_exist_ver.nil?
            if (update_pods.length == 0) || (update_pods.include?(name))
                new_specs[name] = version
                added_projects << "#{name}】 (#{version.to_s.send(:green)})"
            end
        end
        if array.is_a?(Array)
            array.each do |name|
                name = name.to_s
                local_exist_ver = new_specs[name]
                if local_exist_ver.nil?
                    if (update_pods.length == 0) || (update_pods.include?(name))
                        new_specs[name] = version
                        added_projects << "#{name}】 (#{version.to_s.send(:green)})"
                    end
                end
            end
        end
    end
    # step.3 匹配组件移除
    removedata.each do |name|
        name = name.to_s
        version = listdata[name]
        if version
            deleted_projects << "#{name}】 (#{"delete".send(:red)}) <- (#{version})"
        end
        new_specs.delete(name)
    end
    showMergeLog(added_projects, updated_projects, rollbacked_projects, deleted_projects)
    # puts "new_specs:#{new_specs}".send(:red)
    return new_specs
end

#fetch_local_stable_datasObject

产品线本地lock数据



48
49
50
51
52
53
54
55
56
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 48

def fetch_local_stable_datas
    localSpec = BB::StableSpecs.new()
    yml = local_stable_yaml
    if !File.file?(yml)
        puts "yml配置文件不存在,执行自动初始化项目yml配置文件".green
        generate_localStable
    end
    return get_stable_datas(yml)
end

#fetch_origin_stable_datasObject

远端公共lock数据



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
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 59

def fetch_origin_stable_datas
    # 策略:公共数据包含通用数据 + 业务线数据
    pubSpec = BB::StableSpecs.new()
    common_data = pubSpec.readData(public_stable_yaml)
    # 业务线数据
    business_config_file = business_stable_yaml
    if business_config_file
        if File.exist?(business_config_file)
            busimessSpec = BB::StableSpecs.new()
            busimess_data = busimessSpec.readData(business_config_file)
        else
            puts "业务线公共配置文件#{business_config_file}不存在,请确认!!!". send(:yellow)
            exit
        end
    end
    if busimess_data
        # 数据合并操作(策略:业务线公共盖通用数据,理论不存在该情况)
        newData = common_data
        listdata = newData[YAML_CONFIG_LIST_KEY]
        removedata = newData[YAML_CONFIG_REMOVE_KEY]
        dependenciesdata = newData[YAML_CONFIG_DEPENDENCIES_KEY]
        busimess_data.each do | key, val|
            if key == YAML_CONFIG_LIST_KEY
                if val.is_a?(Hash)
                    val.each do |list_name,list_ver|
                        listdata[list_name] = list_ver
                    end
                end
            elsif key == YAML_CONFIG_REMOVE_KEY
                if val.is_a?(Array)
                    val.each do |remove_name|
                        removedata.push(remove_name)
                    end
                end
            elsif key == YAML_CONFIG_DEPENDENCIES_KEY
                if val.is_a?(Hash)
                    val.each do |dependencies_name,dependencies_val|
                        dependenciesdata[dependencies_name] = dependencies_val
                    end
                end
            end
        end
        return newData
    end
    return common_data
end

#generate_localStableObject

生成项目stable配置文件 策略:根据podfile.lock dependencies依赖记录pod core标签/分支数据,不存储subspec数据



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 314

def generate_localStable
    lockfile = podfile_lock
    local_specs = {}
    dependencies = lockfile.dependencies
    pod_names = lockfile.pod_names
    # dependencies依赖数据
    dependencies.each do | dependency |
        pod_name = dependency.name
        if has_include_subspec(pod_name)
            podCoreName = subspec_podname(pod_name)
        else
            podCoreName = pod_name
        end
        # puts "podCoreName:#{podCoreName} pod_name:#{pod_name}".red
        if dependency.external_source.is_a? Hash
            # 指向分支
            external_source = dependency.external_source
            # checkout_options = lockfile.checkout_options_for_pod_named(podCoreName)
            # if checkout_options.is_a? Hash
            #     commit = checkout_options[:commit]
            #     external_source[:commit] = commit
            #     puts "name:#{podCoreName} checkout_options:#{checkout_options} commit:#{commit}"
            # end
            if !local_specs.has_key?(podCoreName)
                local_specs[podCoreName] = external_source
                # puts "[分支] #{podCoreName} => (#{external_source})".yellow
            end
        else
            # 指向标签版本
            version = lockfile.version(pod_name).to_s
            if !version.empty? && !local_specs.has_key?(podCoreName)
                local_specs[podCoreName] = version
                # puts "[标签] #{podCoreName} => (#{version})".yellow
            end
        end
    end
    # 被依赖数据
    pod_names.each do | pod_name |
        if has_include_subspec(pod_name)
            podCoreName = subspec_podname(pod_name)
        else
            podCoreName = pod_name
        end
        pod = local_specs[podCoreName]
        # puts "podCoreName:#{podCoreName} pod:#{pod}".green
        if pod.nil?
            # 指向标签版本
            version = lockfile.version(pod_name).to_s
            if !version.empty? && !local_specs.has_key?(podCoreName)
                local_specs[podCoreName] = version
                # puts "[标签] #{podCoreName} => (#{version})".yellow
            end
        end
    end

    # Pod::UI.puts "yml配置数据:#{local_specs}"
    if local_specs.length > 0
        update_localstable_datas(local_specs)
        Pod::UI.puts "Generating yml配置文件=> #{local_stable_yaml} 更新成功".green
    end
end

#get_stable_datas(yml) ⇒ Object



35
36
37
38
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 35

def get_stable_datas(yml)
    spec = BB::StableSpecs.new()
    return spec.readData(yml)
end

#has_include_core_subspec(pod_name) ⇒ Object

pod库是否包含core subspec



376
377
378
379
380
381
382
383
384
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 376

def has_include_core_subspec(pod_name)
    if has_include_subspec(pod_name)
        subPodName = pod_name.include?('/') ? pod_name.split('/').last.strip : pod_name
        if (subPodName == "Core") || (subPodName == "Main") # 过滤core数据
            return true
        end
    end
    return false
end

#has_include_subspec(pod_name) ⇒ Object

pod库是否包含subspec



386
387
388
389
390
391
392
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 386

def has_include_subspec(pod_name)
    podCoreName = subspec_podname(pod_name)
    if podCoreName != pod_name
        return true
    end
    return false
end

#has_pod_subspec(podName, podCoreName) ⇒ Object

是否pod subspec库



398
399
400
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 398

def has_pod_subspec(podName, podCoreName)
    return (has_include_subspec(podName) && podName.include?("#{podCoreName}/"))
end

#local_stable_yamlObject

本地源路径



31
32
33
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 31

def local_stable_yaml
    return @stableMgr.local_stable_yaml
end

#merge_stable_data(update_pods = []) ⇒ Object

合并stable数据,参数,支持传入更新某几个数据



124
125
126
127
128
129
130
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 124

def merge_stable_data(update_pods=[])
    #4、show origin_stable_lock diff with local_stable_lock
    new_stable_spec = compare_specs(fetch_local_stable_datas, fetch_origin_stable_datas, update_pods)

    #5、rewirte local_stable_lock with origin_stable_lock
    update_localstable_datas(new_stable_spec)    
end

#public_stable_yamlObject

公共源路径(远端)



21
22
23
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 21

def public_stable_yaml
    return @stableMgr.public_stable_yaml
end

#showMergeLog(added, updated, rollbacked, deleted) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 278

def showMergeLog(added, updated, rollbacked, deleted)
    #31m: 红色 32m:绿色 33m:黄色 34m:蓝色
    #puts "\e[34m#{string}\e[0m"
    if added.any?
        puts "新增了以下项目:".send(:green)
        puts added.join("\n")
    end

    if updated.any?
        puts "更新了以下项目:". send(:yellow)
        puts updated.join("\n")
    end

    if rollbacked.any?
        puts "回滚了以下项目:".send(:red)
        puts rollbacked.join("\n")
    end

    if deleted.any?
        puts "移除了以下项目:".send(:red)
        puts deleted.join("\n")
    end

    unless added.any? || updated.any? || added.any? || deleted.any?
        puts "已经是最新版本数据".send(:green)
    end
end

#subspec_podname(pod_name) ⇒ Object

subspec pod库名称



394
395
396
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 394

def subspec_podname(pod_name)
    return pod_name.include?('/') ? pod_name.split('/').first.strip : pod_name
end

#update_business_dependencies_and_remove_data(dependencies_pods, remove_pods) ⇒ Object



190
191
192
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 190

def update_business_dependencies_and_remove_data(dependencies_pods, remove_pods)
    update_dependencies_and_remove_data(business_stable_yaml, business_stable_datas, dependencies_pods, remove_pods)
end

#update_business_stable_lock(stable_lock) ⇒ Object

podfile 更新配置文件使用(业务线)



115
116
117
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 115

def update_business_stable_lock(stable_lock)
    update_stable_lock(business_stable_yaml, stable_lock)
end

#update_common_dependencies_and_remove_data(dependencies_pods, remove_pods) ⇒ Object



187
188
189
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 187

def update_common_dependencies_and_remove_data(dependencies_pods, remove_pods)
    update_dependencies_and_remove_data(public_stable_yaml, common_stable_datas, dependencies_pods, remove_pods)
end

#update_common_stable_lock(stable_lock) ⇒ Object

podfile 更新配置文件使用(通用)



111
112
113
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 111

def update_common_stable_lock(stable_lock)
    update_stable_lock(public_stable_yaml, stable_lock)
end

#update_dependencies_and_remove_data(stable_yaml, stable_specs, dependencies_pods, remove_pods) ⇒ Object

更新依赖/移除数据



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
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 152

def update_dependencies_and_remove_data(stable_yaml, stable_specs, dependencies_pods, remove_pods)
    if dependencies_pods || remove_pods
        listdata = stable_specs[YAML_CONFIG_LIST_KEY]
        removedata = stable_specs[YAML_CONFIG_REMOVE_KEY]
        dependenciesdata = stable_specs[YAML_CONFIG_DEPENDENCIES_KEY]
        if remove_pods
            remove_pods.each do |name|
                name = name.to_s
                if listdata[name].nil?
                    puts "❌ 移除未知公共组件【#{name}】,请确认名称是否正确!!! 传入参数list:#{remove_pods}".red
                    exit
                end
                if !removedata.include?(name)
                    removedata.push(name)
                end
            end
        end
        if dependencies_pods
            dependencies_pods.each do |name, array|
                if array.length > 0
                    array.each do |pod_name|
                        if listdata[pod_name].nil?
                            puts "❌ 依赖未知公共组件【#{name}】,请确认名称是否正确!!! 传入参数list:#{array}".red
                            exit
                        end
                    end
                    dependenciesdata[name] = array
                end
            end
        end
        stable_specs[YAML_CONFIG_REMOVE_KEY] = removedata
        stable_specs[YAML_CONFIG_DEPENDENCIES_KEY] = dependenciesdata
        update_stable_lock(stable_yaml, stable_specs)
    end
end

#update_local_stable_from_podmodules(pod_lists) ⇒ Object

更新local stable组件



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 434

def update_local_stable_from_podmodules(pod_lists)
    local_stable_data = fetch_local_stable_datas # 本地stable配置
    need_update_pod_lists = podCoreNames_from_podmodules(pod_lists)
    lockfile = podfile_lock
    pod_names = lockfile.pod_names # lock标签数据
    is_resave_stable_data = false
    need_update_pod_lists.each do |podCoreName|
        local_stable_data.keys.each do |podName|
            if (podName == podCoreName) || has_pod_subspec(podName, podCoreName)
                pod = local_stable_data[podName]
                if pod.is_a?(String)
                    version = pod.lstrip # 去除首字母空格
                    initial_str = version[0..0] # 第一个字母
                    is_greaterthan_version = version.include?('>') ? true : false # 限制《最低》组件版本 pod 'A','> x.x.x' 或者 pod 'A','>= x.x.x'
                    if (is_greaterthan_version == true)
                        version = lockfile.version(podCoreName).to_s
                        if !version.empty?
                            local_stable_data[podName] = version
                            is_resave_stable_data = true
                            puts "update pod #{podName} (#{version})".green
                        end
                    end
                end
            end
        end
    end
    if is_resave_stable_data == true
        puts "[PodAssistant] 更新本地stable数据".yellow
        update_localstable_datas(local_stable_data)
    end
end

#update_localstable_datas(stable_lock) ⇒ Object

更新本地lock数据



107
108
109
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 107

def update_localstable_datas(stable_lock)
    update_stable_lock(local_stable_yaml, stable_lock)
end

#update_podmodules(pod_lists) ⇒ Object

更新单个pod组件



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 403

def update_podmodules(pod_lists)
    local_stable_data = fetch_local_stable_datas # 本地stable配置
    need_update_pod_lists = podCoreNames_from_podmodules(pod_lists)
    is_resave_stable_data = false
    need_update_pod_lists.each do |podCoreName|
        local_stable_data.keys.each do |podName|
            if (podName == podCoreName) || has_pod_subspec(podName, podCoreName)
                pod = local_stable_data[podName]
                if pod.is_a?(String)
                    version = pod.lstrip # 去除首字母空格
                    initial_str = version[0..0] # 第一个字母
                    # 项目配置固定版本,以本项目为主
                    is_fixed_version = initial_str.include?('=') ? true : false # 固定版本 pod 'A','= x.x.x'
                    is_lessthan_version = version.include?('<') ? true : false # 限制《最高》组件版本 pod 'A','< x.x.x' 或者 pod 'A','<= x.x.x'
                    is_greaterthan_version = version.include?('>') ? true : false # 限制《最低》组件版本 pod 'A','> x.x.x' 或者 pod 'A','>= x.x.x'
                    is_fuzzy_version = version.include?('~>') ? true : false # 固定版本 pod 'A','~> x.x.x'
                    is_fuzzy_versionV2 = (!is_fixed_version && !is_lessthan_version && !is_greaterthan_version && !is_fuzzy_version) ? true : false # 固定版本 pod 'A','x.x.x'
                    if (is_fuzzy_versionV2 == true)
                        local_stable_data[podName] = ">= #{version}"
                        is_resave_stable_data = true
                    end
                end
            end
        end
    end
    if is_resave_stable_data == true
        puts "[PodAssistant] 更新本地stable数据".yellow
        update_localstable_datas(local_stable_data)
    end
end

#update_stable_lock(stable_yaml, stable_lock) ⇒ Object



118
119
120
121
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 118

def update_stable_lock(stable_yaml, stable_lock)
    stableSpec = BB::StableSpecs.new()
    stableSpec.writeData(stable_yaml, stable_lock)
end

#versionGreat(tag1, tag2) ⇒ Object

compare tags (>)



144
145
146
147
148
149
150
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 144

def versionGreat(tag1, tag2)
    result = versionGreatOrEqual(tag1,tag2)
    if result == true && tag1 == tag2
        return false
    end
    return result
end

#versionGreatOrEqual(tag1, tag2) ⇒ Object

Help ######################################## compare tags (>=)



134
135
136
137
138
139
140
141
# File 'lib/cocoapods-bb-PodAssistant/config/source_manager.rb', line 134

def versionGreatOrEqual(tag1, tag2)
    if (tag1.is_a? String) && (tag2.is_a? String) && tag1.to_i >= tag2.to_i
        return true
    else
        return false
    end
    return true
end