class Dongjia::Router

Public Instance Methods

route_items_from_file(path) click to toggle source
# File lib/dongjia_router.rb, line 70
def route_items_from_file(path)
  route_items = []
  item = nil
  File.open(path, 'r') do |file|
    pre_line = nil
    file.each_line do |line|
      is_start = false
      if line.start_with?('@Redirect') || line.start_with?('@redirect')
        # 路由定义开始
        item = RouterItem.new(line)
        item.pre_line = pre_line
        is_start = true
      elsif item != nil && line.start_with?('}')
        # 路由定义结束
        item.body << line
        route_items << item
        item = nil
        pre_line = nil
      end
      if item != nil
        if !is_start
          item.body << line
        end
      end
      pre_line = line
    end
  end
  route_items
end
scrape_routers(sandbox_root, config) click to toggle source

外部入口

# File lib/dongjia_router.rb, line 142
def scrape_routers(sandbox_root, config)

  if config == nil
    return
  end

  begin

    result_file_path = ''
    path = config[:path]
    if path && path.length > 0
      result_file_path = File.expand_path(File.join(sandbox_root, '..', path))
    end
    
    if !File.exist?(result_file_path)
      Pod::UI.warn("Router scrapy: path not found.")
      return
    end

    key = 'latest_scrapy_router_tm'
    content = File.read(result_file_path)
    if content.length < 10
      Config.check do |cfg, save| 
        start_scrapy(sandbox_root, result_file_path)
        cfg[key] = Time.now.to_i
        save.call
      end
    else
      Config.is_expired?(key, 24 * 60 * 60) do |_, update|
        start_scrapy(sandbox_root, result_file_path)
        update.call
      end
    end

  rescue => e
    puts "Error: #{e}"
  end

end
start_scrapy(sandbox_root, result_file_path) click to toggle source

开始抓取

# File lib/dongjia_router.rb, line 101
def start_scrapy(sandbox_root, result_file_path)
  
  route_items = []

  Pod::UI.puts("Scraping routers")

  Dir.foreach(sandbox_root).select{|f| f.end_with?('xcodeproj')}.each do |name|
    proj = Xcodeproj::Project.open(File.join(sandbox_root, name))
    if name != 'Pods.xcodeproj'
      proj.targets.each do |target|
        next unless target.name.start_with?('DJ')
        next unless target.is_a?(Xcodeproj::Project::Object::PBXNativeTarget)
        target.source_build_phase.files_references.each do |file|
          route_items += route_items_from_file(file.real_path)
        end
      end
    end
  end

  # 排序
  route_items.sort! do |a, b|
    a.redirect_type.to_i <=> b.redirect_type.to_i
  end

  # 输出所有结果
  result = HEADER_CONTENT
  route_items.each do |item|
    result += item.output
  end

  FileUtils.chmod('u+w', result_file_path)

  File.open(result_file_path, 'w') do |f|
    f.write(result)
  end

  # 将文件只读
  FileUtils.chmod('ugo-w', result_file_path)
end