Class: Dongjia::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/dongjia_router.rb

Instance Method Summary collapse

Instance Method Details

#route_items_from_file(path) ⇒ Object



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
# 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) ⇒ Object

外部入口



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
# 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) ⇒ Object

开始抓取



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
# 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