diff --git a/Example/Peregrine.xcodeproj/project.pbxproj b/Example/Peregrine.xcodeproj/project.pbxproj index 3068edf..374140f 100644 --- a/Example/Peregrine.xcodeproj/project.pbxproj +++ b/Example/Peregrine.xcodeproj/project.pbxproj @@ -395,7 +395,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "export LANG=en_US.UTF-8\nexport LANGUAGE=en_US.UTF-8\nexport LC_ALL=en_US.UTF-8\nruby /Users/rakeyang/Github/Peregrine/Peregrine/PGGenerator.rb 0\n"; + shellScript = "export LANG=en_US.UTF-8\nexport LANGUAGE=en_US.UTF-8\nexport LC_ALL=en_US.UTF-8\nruby /Users/rakeyang/Github/Peregrine/Peregrine/PGGenerator.rb 1\n"; }; 842D5995DC0ECC9C1951B7AE /* [CP] Check Pods Manifest.lock */ = { isa = PBXShellScriptBuildPhase; diff --git a/Example/Podfile b/Example/Podfile index 9ea38c9..90fffba 100644 --- a/Example/Podfile +++ b/Example/Podfile @@ -21,7 +21,7 @@ target 'Peregrine_Example' do post_install do |installer| require_relative '../Peregrine/PGGenerator.rb' callback = proc { |name| name.index("Peregrine") == 0 } - PGGenerator::configure_project(installer, false, callback) + PGGenerator::configure_project(installer, true, callback) end end diff --git a/Example/Podfile.lock b/Example/Podfile.lock index fb22b0d..19586e2 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -22,6 +22,6 @@ SPEC CHECKSUMS: Peregrine: 87cfcb0d9ef7dcf38ebb21ca6c7b5a93a76fbaa0 Specta: 3e1bd89c3517421982dc4d1c992503e48bd5fe66 -PODFILE CHECKSUM: 07d40e365e8bb20a745ff17e5a4023edb04c9635 +PODFILE CHECKSUM: d76d71e8befa29d376a0d577864c2209ab26ac39 COCOAPODS: 1.8.4 diff --git a/Peregrine/PGGenerator.rb b/Peregrine/PGGenerator.rb index 94b1ce2..56b7b6b 100755 --- a/Peregrine/PGGenerator.rb +++ b/Peregrine/PGGenerator.rb @@ -6,20 +6,21 @@ CLANG_TOOL_PATH = '/usr/local/bin/clang-peregrine' class PGGenerator - # 生成路由表 - def self.generator(args) - if !ENV["ACTION"].eql?("build") + attr_accessor:routers + def initialize(args) + if args[0].eql?('install') return end - if args[0] == 1 || args[0].nil? - if !File::exist?(CLANG_TOOL_PATH) - raise "Peregrine not install -要安装请执行以下命令: -brew tap binaryparadise/formula -brew install peregrine" - return - end + if args[0].eql?('1') + @routers = Array.new + buildWithRegularExpression(args) + else + buildWithClang(args) end + end + + # 通过Clang生成路由表 + def buildWithClang(args) project = Xcodeproj::Project.open(ENV['PROJECT_FILE_PATH']) current_target = (project.targets.select { |target| target.name == ENV['TARGET_NAME'] }).first @@ -67,8 +68,69 @@ def self.generator(args) puts "#{shell}" `#{shell}` end - # required=true表示未安装peregrine的clang插件时会编译失败(防止路由表未生成) - def self.configure_project(installer, required=true, condition=nil) + + # 通过正则表达式匹配生成路由表 + def buildWithRegularExpression(args) + srcroot = ENV['SRCROOT'] + # srcroot = "/Users/rakeyang/Github/Peregrine/" + + collectPath(srcroot) + destination_file = ENV["BUILT_PRODUCTS_DIR"] + if !ENV["PODS_CONFIGURATION_BUILD_DIR"].nil? + destination_file = ENV["PODS_CONFIGURATION_BUILD_DIR"] + end + + Dir::entries(destination_file).each{|item|( + if File.extname(item).eql?(".app") + destination_file = ENV["PODS_CONFIGURATION_BUILD_DIR"] + "/"+item+"/Peregrine.bundle" + end + )} + if !File::exist?(destination_file) + `mkdir #{destination_file}` + end + + router_json_file = File.new("#{destination_file}/routers.json", 'w+') + router_json_file.write(JSON.pretty_generate(@routers)) + router_json_file.close + end + + # 收集所有.h中声明的路由 + def collectPath(path) + Dir::entries(path).each {|item| ( + subPath = path+"/"+item + if File.directory?(subPath) + if isDirectory(item) + collectPath(subPath) + end + else + if File.extname(item).eql?('.h') + mapRouter(subPath) + end + end + )} + end + + def isDirectory(path) + if path.eql?('.') || path.eql?('..') || path.eql?('.git') || File.extname(path).length > 0 + return false + end + return true + end + + def mapRouter(file_path) + file_content = File.read(file_path) + file_content.scan(/@interface\s+(\w+)\s*[\s\S]+?\n([\s\S]+?)@end/) do |match| + class_name = match[0] + class_content = match[1] + # puts match + class_content.scan(/PGMethod\((\b\w+\b),\s*\"([\s\S]+?)\"\);/) do |match1| + @routers.push({ 'class' => class_name, 'selector' => match1[0] + ':', 'url' => match1[1] }) + end + end + end + + # expression=true表示使用正则匹配模式 + def self.configure_project(installer, expression=true, condition=nil) path = installer.sandbox.development_pods['Peregrine'] @dev_path = path ? path.dirname.to_s : nil @@ -76,7 +138,7 @@ def self.configure_project(installer, required=true, condition=nil) if target.user_project_path.exist? && target.user_target_uuids.any? project = Xcodeproj::Project.open(target.user_project_path) project_targets = self.project_targets(project, target) - self.add_shell_script(project_targets, project, required) + self.add_shell_script(project_targets, project, expression) end end @@ -91,7 +153,7 @@ def self.configure_project(installer, required=true, condition=nil) end - def self.add_shell_script(project_targets, project, required=true) + def self.add_shell_script(project_targets, project, expression=true) install_targets = project_targets.select { |target| ['com.apple.product-type.application','com.apple.product-type.framework'].include?(target.product_type) } install_targets.each do |project_target| rubypath = (@dev_path == nil ? "${PODS_ROOT}/Peregrine" : @dev_path) + "/Peregrine/PGGenerator.rb" @@ -105,16 +167,16 @@ def self.add_shell_script(project_targets, project, required=true) end end - clang_reqired = 0 - if required - clang_reqired = 1 + expr = 0 + if expression + expr = 1 end phase.run_only_for_deployment_postprocessing = "0" phase.shell_script = "export LANG=en_US.UTF-8 export LANGUAGE=en_US.UTF-8 export LC_ALL=en_US.UTF-8 -ruby #{rubypath} #{clang_reqired}" +ruby #{rubypath} #{expr}" project.save() end @@ -147,4 +209,4 @@ def self.fetch_exist_phase(phase_name, project_target) end -PGGenerator::generator(ARGV) +PGGenerator::new(ARGV) diff --git a/README.md b/README.md index e4e0356..7ef8078 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ post_install do |installer| require_relative 'Pods/Peregrine/Peregrine/PGGenerator.rb' # 依赖库判断条件 callback = proc { |name| name.index("Peregrine") == 0 } - # true表示未安装clang插件的编译错误 + # true表示使用正则匹配模式 PGGenerator::configure_project(installer, true, callback) end ```