From c78dfcca63df1b1eaba457417bdd19abc634fccf Mon Sep 17 00:00:00 2001 From: Rake Yang Date: Sat, 11 Jan 2020 17:19:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=AE=9E=E4=BE=8B=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Example/Podfile.lock | 4 +- Peregrine.podspec | 2 +- Peregrine/NSObject+Peregrine.h | 28 ++++++++++++++ Peregrine/NSObject+Peregrine.m | 71 ++++++++++++++++++++++++++++++++++ Peregrine/PGGenerator.rb | 2 +- Peregrine/PGRouter-Generate.h | 31 +++++++++++++-- Peregrine/PGRouter-Generate.m | 33 +++++++++------- Peregrine/PGRouterManager.h | 5 +++ Peregrine/Peregrine.h | 2 + 9 files changed, 158 insertions(+), 20 deletions(-) create mode 100644 Peregrine/NSObject+Peregrine.h create mode 100644 Peregrine/NSObject+Peregrine.m diff --git a/Example/Podfile.lock b/Example/Podfile.lock index bc08efe..f4a8640 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,6 +1,6 @@ PODS: - Expecta (1.0.6) - - Peregrine (0.6.3) + - Peregrine (0.6.4) - Specta (1.0.7) DEPENDENCIES: @@ -19,7 +19,7 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: Expecta: 3b6bd90a64b9a1dcb0b70aa0e10a7f8f631667d5 - Peregrine: d339ebce32d3ec21f46b15a9c23161427b6dce12 + Peregrine: e1b035ddde8115a44344dd3eb2bc96645cbd6b3b Specta: 3e1bd89c3517421982dc4d1c992503e48bd5fe66 PODFILE CHECKSUM: d76d71e8befa29d376a0d577864c2209ab26ac39 diff --git a/Peregrine.podspec b/Peregrine.podspec index 35b6f92..51181d3 100644 --- a/Peregrine.podspec +++ b/Peregrine.podspec @@ -7,7 +7,7 @@ # Pod::Spec.new do |s| s.name = 'Peregrine' - s.version = '0.6.3' + s.version = '0.6.4' s.summary = 'A short description of Peregrine.' # This description is used to generate tags and improve search results. diff --git a/Peregrine/NSObject+Peregrine.h b/Peregrine/NSObject+Peregrine.h new file mode 100644 index 0000000..63e98cd --- /dev/null +++ b/Peregrine/NSObject+Peregrine.h @@ -0,0 +1,28 @@ +// +// NSObject+Peregrine.h +// Peregrine +// +// Created by Rake Yang on 2020/1/11. +// + +#import + +/// 示例的路由实现 +@interface NSObject (Peregrine) + +/// 打开路由 +/// @param URLString 路由地址 +/// @param completion 回调 +- (void)pg_openURL:(NSString *)URLString completion:(void (^)(BOOL ret, id object))completion; + +/// 打开路由 +/// @param URLString 路由地址 +/// @param object 参数对象 +/// @param completion 回调 +- (void)pg_openURL:(NSString *)URLString object:(id)object completion:(void (^)(BOOL ret, id object))completion; + +/// 校验路由是否生效 +/// @param URLString 路由地址 +- (BOOL)pg_dryRun:(NSString *)URLString; + +@end diff --git a/Peregrine/NSObject+Peregrine.m b/Peregrine/NSObject+Peregrine.m new file mode 100644 index 0000000..f0b53c9 --- /dev/null +++ b/Peregrine/NSObject+Peregrine.m @@ -0,0 +1,71 @@ +// +// NSObject+Peregrine.m +// Peregrine +// +// Created by Rake Yang on 2020/1/11. +// + +#import "NSObject+Peregrine.h" +#import "PGRouterManager.h" +#import "PGRouterContext.h" + +@implementation NSObject (Peregrine) + +- (void)pg_openURL:(NSString *)URLString completion:(void (^)(BOOL, id))completion { + [self pg_openURL:URLString object:nil completion:completion]; +} + +- (void)pg_openURL:(NSString *)URLString object:(id)object completion:(void (^)(BOOL, id))completion { + NSURL *patternURL = [NSURL pg_SafeURLWithString:URLString]; + PGRouterNode *node = [PGRouterManager routerMap][patternURL.host]; + NSMutableArray *componets = [patternURL.pathComponents mutableCopy]; + if (componets.firstObject) { + [componets removeObject:componets.firstObject]; + } + __block PGRouterNode *curNode = node; + [componets enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + curNode = [curNode nodeForName:obj]; + if (idx == componets.count - 1) { + PGRouterConfig *config = curNode.config; + if (config) { + [self pg_openWithRouter:config context:[PGRouterContext contextWithURL:patternURL object:object callback:completion]]; + } else { + if (completion) { + completion(NO, [NSString stringWithFormat:@"router: %@ no match.", URLString]); + } + } + } + }]; +} + +- (void)pg_openWithRouter:(PGRouterConfig *)router context:(PGRouterContext *)context { + if ([self respondsToSelector:router.selector]) { + context.config = router; + IMP imp = [router.targetClass instanceMethodForSelector:router.selector]; + void (*targetMethod)(id, SEL, PGRouterContext *) = (void *)imp; + targetMethod(self, router.selector, context); + } +} + +- (BOOL)pg_dryRun:(NSString *)URLString { + NSURL *patternURL = [NSURL pg_SafeURLWithString:URLString]; + PGRouterNode *node = [PGRouterManager routerMap][patternURL.host]; + NSMutableArray *componets = [patternURL.pathComponents mutableCopy]; + if (componets.firstObject) { + [componets removeObject:componets.firstObject]; + } + __block BOOL valid = NO; + __block PGRouterNode *context = node; + [componets enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + context = [context nodeForName:obj]; + if (idx == componets.count - 1) { + PGRouterConfig *config = context.config; + if (config) { + valid = YES; + } + } + }]; + return valid; +} + +@end diff --git a/Peregrine/PGGenerator.rb b/Peregrine/PGGenerator.rb index 58d4188..86e0c11 100755 --- a/Peregrine/PGGenerator.rb +++ b/Peregrine/PGGenerator.rb @@ -131,7 +131,7 @@ def mapRouter(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] - class_content.scan(/PGMethod\((\b\w+\b),\s*\"([\s\S]+?)\"\);/) do |match1| + class_content.scan(/PG\w*Method\((\b\w+\b),\s*\"([\s\S]+?)\"\);/) do |match1| @routers.push({ 'class' => class_name, 'selector' => match1[0] + ':', 'url' => match1[1] }) end end diff --git a/Peregrine/PGRouter-Generate.h b/Peregrine/PGRouter-Generate.h index 1db9c8b..b4abe38 100755 --- a/Peregrine/PGRouter-Generate.h +++ b/Peregrine/PGRouter-Generate.h @@ -6,11 +6,36 @@ // Copyright © 2019 BinaryParadise. All rights reserved. /** - Generated automatic by Peregrine version 0.6.2 + Generated automatic by Peregrine version 0.6.3 Don't modify manual ⚠️ */ - -/// Build Time 2020-01-01 10:40:45 +0800 + +/// Build Time 2020-01-11 17:14:04 +0800 typedef NSString *PGRouterURLKey; +#pragma - mark xincheng + +FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_address_list; +FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_darwinkit_open; +FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_item_list; +FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_itemdetail_home; +FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_itemdetail_showparam; +FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_itemdetail_showprotection; +FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_itemdetail_showsku; +FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_order_confirm; +FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_order_detail; +FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_order_list; +FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_webview; + +#pragma - mark shop + +FOUNDATION_EXPORT PGRouterURLKey const vip8_shop_cart; + +#pragma - mark address + +FOUNDATION_EXPORT PGRouterURLKey const vip8_address_card; + +#pragma - mark xincheng + +FOUNDATION_EXPORT PGRouterURLKey const vip_xincheng_order_logistics; diff --git a/Peregrine/PGRouter-Generate.m b/Peregrine/PGRouter-Generate.m index e546ee3..668f2b8 100755 --- a/Peregrine/PGRouter-Generate.m +++ b/Peregrine/PGRouter-Generate.m @@ -7,21 +7,28 @@ #import "PGRouter-Generate.h" -#pragma - mark tlbb +#pragma - mark xincheng -PGRouterURLKey const ap_tlbb_most_like_wangyuyan = @"ap://tlbb/most/like/wangyuyan"; -PGRouterURLKey const ap_tlbb_most_like_wangzuxian = @"ap://tlbb/most/like/wangzuxian"; -PGRouterURLKey const ap_tlbb_wyy = @"ap://tlbb/wyy"; -PGRouterURLKey const ap_tlbb_xlv = @"ap://tlbb/xlv"; -PGRouterURLKey const ap_tlbb_xxlv = @"ap://tlbb/xxlv"; -PGRouterURLKey const ap_tlbb_ym = @"ap://tlbb/ym"; +PGRouterURLKey const vip8_xincheng_address_list = @"vip8://xincheng/address/list"; +PGRouterURLKey const vip8_xincheng_darwinkit_open = @"vip8://xincheng/darwinkit/open"; +PGRouterURLKey const vip8_xincheng_item_list = @"vip8://xincheng/item/list"; +PGRouterURLKey const vip8_xincheng_itemdetail_home = @"vip8://xincheng/itemdetail/home"; +PGRouterURLKey const vip8_xincheng_itemdetail_showparam = @"vip8://xincheng/itemdetail/showparam"; +PGRouterURLKey const vip8_xincheng_itemdetail_showprotection = @"vip8://xincheng/itemdetail/showprotection"; +PGRouterURLKey const vip8_xincheng_itemdetail_showsku = @"vip8://xincheng/itemdetail/showsku"; +PGRouterURLKey const vip8_xincheng_order_confirm = @"vip8://xincheng/order/confirm"; +PGRouterURLKey const vip8_xincheng_order_detail = @"vip8://xincheng/order/detail"; +PGRouterURLKey const vip8_xincheng_order_list = @"vip8://xincheng/order/list"; +PGRouterURLKey const vip8_xincheng_webview = @"vip8://xincheng/webview"; -#pragma - mark webview +#pragma - mark shop -PGRouterURLKey const ap_webview_UIWebView = @"ap://webview/UIWebView"; -PGRouterURLKey const ap_webview_WKWebView = @"ap://webview/WKWebView"; -PGRouterURLKey const ap_webview_calloc = @"ap://webview/calloc"; +PGRouterURLKey const vip8_shop_cart = @"vip8://shop/cart"; -#pragma - mark : +#pragma - mark address -PGRouterURLKey const _invalidurl_haha = @"://invalidurl/haha"; +PGRouterURLKey const vip8_address_card = @"vip8://address/card"; + +#pragma - mark xincheng + +PGRouterURLKey const vip_xincheng_order_logistics = @"vip://xincheng/order/logistics"; diff --git a/Peregrine/PGRouterManager.h b/Peregrine/PGRouterManager.h index 1a1e05b..9910b3a 100644 --- a/Peregrine/PGRouterManager.h +++ b/Peregrine/PGRouterManager.h @@ -10,9 +10,14 @@ #import "PGRouterNode.h" #import "PGRouter-Generate.h" +//类方法 #define PGMethod(_name, _url) \ + (void)_name:(PGRouterContext *)context; +//实例方法 +#define PGInstanceMethod(_name, _url) \ +- (void)_name:(PGRouterContext *)context; + //使用__attribute__ #define PGMethodA(_name, _url) \ + (void)_name:(PGRouterContext *)context __attribute__((pe_routed(_router))); diff --git a/Peregrine/Peregrine.h b/Peregrine/Peregrine.h index 6d5fcb4..11dd95b 100644 --- a/Peregrine/Peregrine.h +++ b/Peregrine/Peregrine.h @@ -8,3 +8,5 @@ #import #import +#import +#import