class BD::FetchPodList

支持集成区,从网络获取 pod 列表

Constants

CONFIG_NAME

Private Class Methods

format_additional_parameters(pod_json) click to toggle source
# File lib/bd_pod_extentions/integration_area.rb, line 114
def self.format_additional_parameters(pod_json) # [Array<String>]
  pod_json.map do |key,value|
    next ":#{key} => #{value}" if key != 'name' && key != 'version'
    next nil
  end.reject(&:nil?)
end

Public Instance Methods

convertIdToPodfileContent(id, config_json = nil) click to toggle source
# File lib/bd_pod_extentions/integration_area.rb, line 195
def convertIdToPodfileContent(id, config_json = nil) # [String]
  podfile_content = ""
  podfile_content += "# This file is generated automatically. DO NOT MODIFY MANUALY!\n"
  podfile_content += "# Source ID: #{id} \n\n"
  podfile_content += convertListToPodfileContent(config_json || fetch_pod_list(id))
  podfile_content += "# This file is generated automatically. DO NOT MODIFY MANUALY!\n"
  return podfile_content
end
convertIdToPodsSummary(id, config_json = nil) click to toggle source
# File lib/bd_pod_extentions/integration_area.rb, line 204
def convertIdToPodsSummary(id, config_json = nil) # [String]
  podfile_content = ""
  podfile_content += "# This file is generated automatically. DO NOT MODIFY MANUALY!\n"
  podfile_content += "# This file is just for human reading.\n"
  podfile_content += "#\n"
  podfile_content += "# Source ID: #{id} \n"
  podfile_content += "# These pods are imported by `bd_fetch_pod_list XXXXX` in podfile\n\n"
  podfile_content += convertListToHumanReadableText(config_json||fetch_pod_list(id))
  podfile_content += "# Equivalent Podfile content: \n\n"
  podfile_content += begin 
    text = convertListToPodfileContent(config_json || fetch_pod_list(id))
    text.split("\n").map { |line| "#    " + line }.join("\n")
  end
  return podfile_content
end
main() click to toggle source
# File lib/bd_pod_extentions/integration_area.rb, line 181
def main
  podfile.plugin 'cocoapods-bytedance-newIntegrate'
  pod_list_id = get_id
  config = fetch_pod_list(pod_list_id)
  addToPodfile(config)
  
  # write content to file, for human reading
  genereted_pod_content = convertIdToPodsSummary(pod_list_id, config)
  list_file_path = generated_podlist_file_path
  File.write(list_file_path, genereted_pod_content)
  $BD_GENERATED_POD_LIST_FILE = list_file_path
  
end

Private Instance Methods

adaptPodinfoToHashArray(podInfo) click to toggle source
# File lib/bd_pod_extentions/integration_area.rb, line 156
def adaptPodinfoToHashArray(podInfo)
  podInfo.map do |key,value|
    next Hash[key.to_sym => value] if key != 'name' && key != 'version'
    next nil
  end.reject(&:nil?)
end
addToPodfile(info) click to toggle source

add the pod in list to podfile

# File lib/bd_pod_extentions/integration_area.rb, line 155
def addToPodfile(info)
  def adaptPodinfoToHashArray(podInfo)
    podInfo.map do |key,value|
      next Hash[key.to_sym => value] if key != 'name' && key != 'version'
      next nil
    end.reject(&:nil?)
  end
  
  podfile_content = ""
  
  info['projects'].each do |project|
    # taget TARGET_NAME do
    podfile.target project['name'] do
      project['components'].each do |podInfo|
        value = adaptPodinfoToHashArray(podInfo)
        # pod_binary POD_NAME, VERSION
        podfile.pod_binary podInfo['name'],podInfo['version'], *value
      end
    # end
    end
  end
end
config_path() click to toggle source
# File lib/bd_pod_extentions/integration_area.rb, line 36
def config_path # [Pathname]
  podfile.defined_in_file.dirname + CONFIG_NAME
end
convertListToHumanReadableText(info) click to toggle source

convert the json to human readable content (text)

# File lib/bd_pod_extentions/integration_area.rb, line 138
def convertListToHumanReadableText(info)
  content = ""
  
  info['projects'].each do |project|
    content += "#{project['name']}:\n"
    project['components'].each do |podInfo|
      parameters = [podInfo['name'].ljust(18), "#{podInfo['version']}"]
      parameters += FetchPodList.format_additional_parameters(podInfo)
      content += "    - #{parameters.join(", ")}\n"
    end
    content += "\n"
  end
  content
end
convertListToPodfileContent(info) click to toggle source

convert the json to podfile format content (ruby code)

# File lib/bd_pod_extentions/integration_area.rb, line 122
def convertListToPodfileContent(info)
  podfile_content = ""
  
  info['projects'].each do |project|
    podfile_content += "target '#{project['name']}' do\n"
    project['components'].each do |podInfo|
      parameters = ["'#{podInfo['name']}'", "'#{podInfo['version']}'"]
      parameters += FetchPodList.format_additional_parameters(podInfo)
      podfile_content += "    pod_binary #{parameters.join(", ")}\n"
    end
    podfile_content += "end\n\n"
  end
  podfile_content
end
convertToPodfileFormat(response) click to toggle source
# File lib/bd_pod_extentions/integration_area.rb, line 71
def convertToPodfileFormat(response) # [Hash]
  targetNameArray = Array.new
  targetHash = Hash.new
  response["data"]["components"].each do |pod|
    #先判断是否有新增target,因为平台中组件不会重复
    pod["app_struct"].each_key do |targetName|
      unless targetNameArray.include?(targetName)
        targetNameArray << targetName
        # 创建对应的target
        targetDef = Hash.new
        targetDef["name"] = targetName
        targetDef["components"] = Array.new
        # 添加到target中
        targetHash[targetName] = targetDef
      end
      # 创建pod的定义
      pod_def = Hash.new
      pod_def["name"] = pod["name"]
      pod_def["version"] = pod["version"]
      if pod["app_struct"][targetName].size != 0
        pod_def["subspecs"] = pod["app_struct"][targetName]
      end
      targetHash[targetName]["components"] << pod_def
    end
  end
    
  project_adapt = targetHash.values
  completeData = Hash.new
  completeData["projects"] = project_adapt
  completeData
end
fetch(url) click to toggle source
# File lib/bd_pod_extentions/integration_area.rb, line 61
def fetch(url)
  begin
    url_str = URI.parse(url)
    return Net::HTTP.get_response(url_str)
  rescue Exception => ex
    puts "[!] Pod 列表请求失败. (#{url})".red
    raise ex
  end
end
fetch_pod_list(ids) click to toggle source
# File lib/bd_pod_extentions/integration_area.rb, line 59
def fetch_pod_list(ids) # [Hash]
  puts "Fetching pod list: #{ids} ..."
  def fetch(url)
    begin
      url_str = URI.parse(url)
      return Net::HTTP.get_response(url_str)
    rescue Exception => ex
      puts "[!] Pod 列表请求失败. (#{url})".red
      raise ex
    end
  end
  
  def convertToPodfileFormat(response) # [Hash]
    targetNameArray = Array.new
    targetHash = Hash.new
    response["data"]["components"].each do |pod|
      #先判断是否有新增target,因为平台中组件不会重复
      pod["app_struct"].each_key do |targetName|
        unless targetNameArray.include?(targetName)
          targetNameArray << targetName
          # 创建对应的target
          targetDef = Hash.new
          targetDef["name"] = targetName
          targetDef["components"] = Array.new
          # 添加到target中
          targetHash[targetName] = targetDef
        end
        # 创建pod的定义
        pod_def = Hash.new
        pod_def["name"] = pod["name"]
        pod_def["version"] = pod["version"]
        if pod["app_struct"][targetName].size != 0
          pod_def["subspecs"] = pod["app_struct"][targetName]
        end
        targetHash[targetName]["components"] << pod_def
      end
    end
    
    project_adapt = targetHash.values
    completeData = Hash.new
    completeData["projects"] = project_adapt
    completeData
  end
  
  config_json = begin
    url = generate_url_for_id(ids)
    respose = fetch(url)
    json = JSON.parse(respose.body)
    if json["errno"] != 200
      raise "[!] Pod 列表请求错误:#{json["errmsg"]}"
    end
    convertToPodfileFormat(json)
  end
end
generate_url_for_id(ids) click to toggle source
# File lib/bd_pod_extentions/integration_area.rb, line 54
def generate_url_for_id(ids) # [string]
  url = "https://ios.bytedance.net/api/v1/mergeRequest/components_list"
  url += "?mrUpdateId=#{ids}"
end
generated_podlist_file_path() click to toggle source
# File lib/bd_pod_extentions/integration_area.rb, line 49
def generated_podlist_file_path # [String]
  @podlist_path ||= Tool::Path.config.sandbox_root + "PodsSummary"
  @podlist_path
end
get_id() click to toggle source
# File lib/bd_pod_extentions/integration_area.rb, line 40
def get_id # [String]
  raise "Cannot find #{CONFIG_NAME} file in the same directory with Podfile" unless config_path.file?
  text = File.read(config_path)
  hash = Pod::YAMLHelper.load_string(text)
  id = hash["history_id"]
  raise "No history_id found in #{CONFIG_NAME}" if id.nil?
  id
end