Class: BaselineTools

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-mPaaS/baselineTools.rb

Constant Summary collapse

@@baseline_hash_all =
{}
@@libs_array_all =
[]
@@component_hash =
{}

Class Method Summary collapse

Class Method Details

.addDependencies(array, name, module_name, component_hash_) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 463

def self.addDependencies(array, name, module_name, component_hash_)
  component_hash_["frameworks"].each do |framework|
    if framework["name"] == name
      dependencies_array = []

      # 先处理下 differences
      component_hash_["differences"] && component_hash_["differences"].each do |framework|
        if framework["name"] == name
          framework["info"].each do |info|
            if info["module"] == module_name
              if info["framework"]["dependencies"]
                dependencies_array = info["framework"]["dependencies"]
              end
            end
          end
        end
      end

      dependencies_array = framework["dependencies"] if dependencies_array.empty?

      dependencies_array && dependencies_array.each do |dependency|
        unless array.include?(dependency)
          array << dependency

          addDependencies(array, dependency, module_name, component_hash_) unless getDependencies(dependency, component_hash_).empty?
        end
      end
    end
  end
end

.buildAndWriteContentJson(component_hash_, version, content_output_path, only4Show) ⇒ Object



312
313
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
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
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 312

def self.buildAndWriteContentJson(component_hash_, version, content_output_path, only4Show)

  # 先取哪些是 option module
  options = []

  # 写入本地基线
  options_hash = {}

  component_hash_["modules"].each do |module_|
    if module_["options"]
      options = options | module_["options"].map { |option| option["name"] }

      # 保存 module 与 option 的对应关系(全部,给 remove_pod 用)
      options_hash[module_["name"]] = module_["options"].map { |option| option["name"] }
    end
  end
  # p options
  content_hash = {}

  # 记录每个可选模块的直接 frameworks(展开依赖前)
  option_direct_frameworks = {}

  component_hash_["modules"].each do |module_|
    module_name = module_["name"]
    module_frameworks = module_["frameworks"]
    content_hash[module_name] = []

    if options.include?(module_name)
      option_direct_frameworks[module_name] = module_frameworks.dup
    end

    module_frameworks.each do |framework|
      content_hash[module_name] << framework
      addDependencies(content_hash[module_name], framework, module_name, component_hash_)
    end
  end

  final_content_hash = {}
  content_hash.each do |key, value|
    hash_ = {}
    value.each do |framework|
      hash_[framework] = getVersion(framework, key, component_hash_)
    end
    final_content_hash[key] = hash_
  end

  # 构建可选模块的直接 frameworks 映射,只记录直接声明的 frameworks(不含间接依赖)
  optional_frameworks_hash = {}
  options.each do |opt_module_name|
    if option_direct_frameworks[opt_module_name]
      optional_frameworks_hash[opt_module_name] = option_direct_frameworks[opt_module_name]
    end
  end

  # 输出下component更新时间
  LogTools.p_yellow "updateTime : " + Time.at(component_hash_["updateTime"]).to_s unless only4Show

  begin
    # 先输出下产品集版本号
    content_hash_for_version = getContentFileByVersion(version)
    version_code_local = content_hash_for_version["version_code"]
    version_code_online = component_hash_["version_code"]
    LogTools.p_yellow "The current verison is #{version}.#{version_code_local}" if version_code_local && only4Show
    LogTools.p_yellow "The newest verison is #{version}.#{version_code_online}" if version_code_online && only4Show

    old_content_hash = getBaselineByVersion(version)
    diffBaselineFile(old_content_hash, final_content_hash, only4Show)

    # update命令时输出
    LogTools.p_red "The current verison is updated to #{version}.#{version_code_online}" if version_code_online && !only4Show

    # 检查更新时输出
    LogTools.p_red "updateTime : " + Time.at(component_hash_["updateTime"]).to_s if only4Show
  rescue => exception
    LogTools.p_red exception.to_s
    LogTools.p_yellow "#{version} will create ifneeded"
  end

  # 只是给更新提示用,不真正更新文件
  return if only4Show

  # 可能的创建目录
  safe_create_directory_tt(File.dirname(content_output_path))

  # 保存文件
  # 新版数据结构
  final_content_hash_new = {}
  final_content_hash_new["baseline"] = component_hash_["baseline"]
  final_content_hash_new["updateTime"] = component_hash_["updateTime"]
  final_content_hash_new["version_code"] = component_hash_["version_code"]
  final_content_hash_new["modules"] = final_content_hash
  final_content_hash_new["options"] = options_hash
  final_content_hash_new["optional_frameworks"] = optional_frameworks_hash

  # 保存每个模块的可移除子模块(所有 options)
  removable_options_hash = {}
  component_hash_["modules"].each do |module_|
    if module_["options"] && !module_["options"].empty?
      removable_options_hash[module_["name"]] = module_["options"].map { |opt| opt["name"] }
    end
  end
  final_content_hash_new["removable_options"] = removable_options_hash

  module_info_hash = {}
  component_hash_["modules"].each do |module_|
    module_info_hash[module_["name"]] = {
      "title" => module_["title"],
      "description" => module_["description"],
      "releaseNote" => module_["releaseNote"]
    }
  end
  final_content_hash_new["module_info"] = module_info_hash

  File::open(content_output_path, "w") do |f|
    f.write(final_content_hash_new.to_json)
  end
end

.checkBaseline?(baseline, name, version) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
114
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 106

def self.checkBaseline?(baseline, name, version)
  baseline_hash = getBaselineByVersion(baseline)
  baseline_hash.values.each do |eachHash|
    if eachHash[name] == version
      return true
    end
  end
  return false
end

.diffBaselineFile(old_content_hash, new_content_hash, only4Show) ⇒ Object



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
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 229

def self.diffBaselineFile(old_content_hash, new_content_hash, only4Show)
  hasChanged = false
  frameworks_changed = []

  old_content_hash.each do |module_name, old_libs_hash|
    new_libs_hash = new_content_hash[module_name]
    # 判断 module 删除
    if new_libs_hash
      old_libs_hash.each do |lib_name, lib_version|
        new_lib_version = new_libs_hash[lib_name]
        # 判断 framework 删除
        if new_lib_version
          # 判断版本号变化
          if lib_version != new_lib_version
            LogTools.p_yellow "#{module_name} : #{lib_name} changed ( from #{lib_version} to #{new_lib_version} )" unless only4Show
            hasChanged = true
            frameworks_changed << "#{lib_name} : #{new_lib_version}"
          end
        else
          LogTools.p_yellow "#{module_name} : #{lib_name} deleted" unless only4Show
          hasChanged = true
          frameworks_changed << "#{lib_name} deleted"
        end
      end
    else
      LogTools.p_yellow "#{module_name} deleted" unless only4Show
      hasChanged = true
      frameworks_changed << "#{module_name} deleted"
    end
  end

  # 判断新增
  new_content_hash.each do |module_name, new_libs_hash|
    old_libs_hash = old_content_hash[module_name]
    # 判断 module 新增
    if old_libs_hash
      new_libs_hash.each do |lib_name, lib_version|
        old_lib_version = old_libs_hash[lib_name]
        # 判断 framework 新增
        unless old_lib_version
          LogTools.p_yellow "#{module_name} : #{lib_name} added" unless only4Show
          hasChanged = true
          frameworks_changed << "#{lib_name} : #{lib_version} added"
        end
      end
    else
      LogTools.p_yellow "#{module_name} : new" unless only4Show
      hasChanged = true
      frameworks_changed << "#{module_name} : new"
    end
  end

  LogTools.p_yellow "The baseline has no change ..." unless hasChanged unless only4Show

  frameworks_changed.uniq!
  LogTools.p_red frameworks_changed.to_s if only4Show && !frameworks_changed.empty?
end

.filterVerison(version) ⇒ Object



223
224
225
226
227
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 223

def self.filterVerison(version)
  s = version
  s.gsub!("-beta", "")
  return s
end

.getBaselineByVersion(version, raise_if_missing = false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 62

def self.getBaselineByVersion(version, raise_if_missing = false)
  content_hash = getContentFileByVersion(version, raise_if_missing)

  if content_hash["modules"]
    return content_hash["modules"]
  elsif content_hash
    return content_hash
  else
    return {}
  end
end

.getBaselineMatched(podfile_path) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 200

def self.getBaselineMatched(podfile_path)
  mPaaSPodfile = MPaaSPodfile.new
  mPaaSPodfile.run(podfile_path)

  if mPaaSPodfile.mPaaSBaseline
    return filterVerison(mPaaSPodfile.mPaaSBaseline)
  elsif mPaaSPodfile.mPaaSPodVersion
    return filterVerison(mPaaSPodfile.mPaaSPodVersion)
  end

  return ""
end

.getComponentHashByBaseline(baseline, isPre, isDev = false) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 154

def self.getComponentHashByBaseline(baseline, isPre,  isDev = false)
  final_baseline = baseline
  unless @@component_hash[baseline]
    final_baseline = requestComponentOnline(baseline, isPre, isDev)
  end
  return @@component_hash[final_baseline]
end

.getContentFileByVersion(version, raise_if_missing = false) ⇒ Object



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
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 9

def self.getContentFileByVersion(version, raise_if_missing = false)
  unless @@baseline_hash_all[version]
    content_json_path, allow_fallback = getLocalContentJsonConfig
    if content_json_path
      abs_path = File.expand_path(content_json_path, Dir::pwd)
      if File::exist?(abs_path)
        baseline_hash = JSON.parse(File.read(abs_path))
        @@baseline_hash_all[version] = baseline_hash
        return baseline_hash
      end
      unless allow_fallback
        if raise_if_missing
          info = "content.json not found at: #{abs_path}\n" \
                 "Please run 'pod mpaas update' first to generate it.\n" \
                 "Or set fallback: mPaaS_local_content_json '#{content_json_path}', true"
          raise Pod::Informative, info
        end
        return {}
      end
    end

    baseline_file_path = File.join(CocoapodsmPaaS::MPAAS_LOCAL_PATH, "baseline", version, "content.json")
    if File::exist?(baseline_file_path)
      baseline_hash = JSON.parse(File.read(baseline_file_path))
      @@baseline_hash_all[version] = baseline_hash
      return baseline_hash
    else
      if raise_if_missing
        info = "content.json not found for baseline #{version} !\n" \
               "Please run 'pod mpaas update #{version}' first to generate it.\n" \
               "Or configure in Podfile: mPaaS_local_content_json 'path/to/content.json'"
        raise Pod::Informative, info
      end
      return {}
    end
  else
    return @@baseline_hash_all[version]
  end
end

.getCurrentBaselineOnlineObject



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 141

def self.getCurrentBaselineOnline
  baseline_array = []
  begin
    url = "#{CocoapodsmPaaS::MPAAS_IOS_PATH_PREFIX}/mPaaS-Baseline/manifest.json"
    text = URI.open(url).read
    baseline_array = JSON.parse(text)["versions2"]
  rescue => exception
    LogTools.p_red exception.to_s
  end

  return baseline_array
end

.getDependencies(name, component_hash_) ⇒ Object



430
431
432
433
434
435
436
437
438
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 430

def self.getDependencies(name, component_hash_)
  component_hash_["frameworks"].each do |framework|
    if framework["name"] == name
      return framework["dependencies"]
    end
  end

  return []
end

.getLocalContentJsonConfigObject



49
50
51
52
53
54
55
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 49

def self.getLocalContentJsonConfig
  podfile_path = File.join(Dir::pwd, "Podfile")
  return [nil, true] unless File::exist?(podfile_path)
  mpaas_podfile = MPaaSPodfile.new
  mpaas_podfile.run(podfile_path)
  [mpaas_podfile.localContentJsonPath, mpaas_podfile.localContentJsonFallback]
end

.getLocalContentJsonPathObject



57
58
59
60
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 57

def self.getLocalContentJsonPath
  config = getLocalContentJsonConfig
  config[0]
end

.getModuleInfoByName(name, baseline) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 162

def self.getModuleInfoByName(name, baseline)
  content_hash = getContentFileByVersion(baseline)
  if content_hash["module_info"] && content_hash["module_info"][name]
    info = content_hash["module_info"][name]
    return "#{info["title"]};#{info["description"]};#{info["releaseNote"]}"
  end

  unless @@component_hash[baseline]
    requestComponentOnline(baseline, false)
  end

  if @@component_hash[baseline]
    @@component_hash[baseline]["modules"].each do |module_|
      if module_["name"] == name
        return "#{module_["title"]};#{module_["description"]};#{module_["releaseNote"]}"
      end
    end
  end

  return "N/A"
end

.getmPaaSlibsObject



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-mPaaS/baselineTools.rb', line 74

def self.getmPaaSlibs
  if @@libs_array_all.empty?
    baseline_dir_path = File.join(CocoapodsmPaaS::MPAAS_LOCAL_PATH, "baseline")
    Dir::foreach(baseline_dir_path) do |dir_name|
      baseline_file_path = File.join(baseline_dir_path, dir_name, "content.json")
      if File::exist?(baseline_file_path)
        baseline_hash = JSON.parse(File.read(baseline_file_path))

        # 兼容历史结构
        modules_hash = baseline_hash["modules"] ? baseline_hash["modules"] : baseline_hash

        modules_hash && modules_hash.each do |name, libs_hash|
          libs_array = libs_hash.keys
          if libs_array
            libs_array.each do |lib|
              unless @@libs_array_all.include?(lib)
                @@libs_array_all << lib
              end
            end
          end
        end
      else
        LogTools.v "No baseline file found ... version : #{dir_name}"
      end
    end

    return @@libs_array_all
  else
    return @@libs_array_all
  end
end

.getOptionsInLocalByBaseline(baseline) ⇒ Object



218
219
220
221
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 218

def self.getOptionsInLocalByBaseline(baseline)
  content_hash = getContentFileByVersion(baseline)
  return content_hash["options"]
end

.getPodVersionOnlineByName(name, baseline) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 184

def self.getPodVersionOnlineByName(name, baseline)
  unless @@component_hash[baseline]
    requestComponentOnline(baseline, false)
  end

  if @@component_hash[baseline]
    @@component_hash[baseline]["frameworks"].each do |framework|
      if framework["name"] == name
        return framework["version"]
      end
    end
  end

  return ""
end

.getVersion(name, module_name, component_hash_) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 440

def self.getVersion(name, module_name, component_hash_)
  # 先匹配 differences
  component_hash_["differences"] && component_hash_["differences"].each do |framework|
    if framework["name"] == name
      framework["info"].each do |info|
        if info["module"] == module_name
          if info["framework"]["version"]
            return info["framework"]["version"]
          end
        end
      end
    end
  end

  component_hash_["frameworks"].each do |framework|
    if framework["name"] == name
      return framework["version"]
    end
  end

  return ""
end

.getVersionCodeInLocalByBaseline(baseline) ⇒ Object



213
214
215
216
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 213

def self.getVersionCodeInLocalByBaseline(baseline)
  content_hash = getContentFileByVersion(baseline)
  return content_hash["version_code"]
end

.requestComponentOnline(baseline, isPre, isDev = false) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 116

def self.requestComponentOnline(baseline, isPre,  isDev = false)
  begin
    # 判断输入的 baseline 是否是个url
    isUrl = baseline.start_with?("http")

    if isUrl
      url = baseline
    else
      if isDev
        url = "#{CocoapodsmPaaS::MPAAS_IOS_TEST_PATH_PREFIX_DEV}/mPaaS-Baseline/#{baseline}/Component.json"
      elsif isPre
        url = "#{CocoapodsmPaaS::MPAAS_IOS_TEST_PATH_PREFIX}/mPaaS-Baseline/#{baseline}/Component.json"
      else
        url = "#{CocoapodsmPaaS::MPAAS_IOS_PATH_PREFIX}/mPaaS-Baseline/#{baseline}/Component.json"
      end
    end
    text = URI.open(url).read
    final_baseline = isUrl ? text["baseline"] : baseline
    @@component_hash[final_baseline] = JSON.parse(text)
    return final_baseline
  rescue => exception
    LogTools.p_red exception.to_s
  end
end

.updateContentFileByComponent(version, only4Show, isPre, isDev = false, content_output_path = nil) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 287

def self.updateContentFileByComponent(version, only4Show, isPre, isDev = false, content_output_path = nil)
  component_hash_ = getComponentHashByBaseline(version, isPre, isDev)
  version = component_hash_["baseline"]

  unless component_hash_
    LogTools.p_red "#{version} baseline file unfound !"
    return
  end

  output_path = content_output_path || File.join(CocoapodsmPaaS::MPAAS_LOCAL_PATH, "baseline", version, "content.json")
  buildAndWriteContentJson(component_hash_, version, output_path, only4Show)
end

.updateContentFileFromLocal(component_json_path, content_json_path, version) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
# File 'lib/cocoapods-mPaaS/baselineTools.rb', line 300

def self.updateContentFileFromLocal(component_json_path, content_json_path, version)
  unless File::exist?(component_json_path)
    LogTools.p_red "Component JSON not found: #{component_json_path}"
    return
  end

  component_hash_ = JSON.parse(File.read(component_json_path))
  version = component_hash_["baseline"] if component_hash_["baseline"]

  buildAndWriteContentJson(component_hash_, version, content_json_path, false)
end