前言
由于Xcode的習(xí)慣欣范,很多OC代碼中.h
文件引用都是只寫文件的,因?yàn)閄code會自動查找該文件的真實(shí)位置再傳遞給編譯器編譯,但這種做法有個限制,就是必須把.h
文件引用到項目中,不然會找到,筆者的問題也就源于此,出于個人習(xí)慣,我不喜歡把不需要修改的文件引用到項目中,特別是編譯好的庫,一般情況下都是只引用.a
文件,然后包含一個頭文件全部搞掂,即方便后期維護(hù)第三方庫,也方便文件管理,鑒于手動修改的文件實(shí)在太多了,這種沒含量的浪費(fèi)時間行為,還是交給CPU吧,于是便有了此文.
(特別說明,此工具的作用是把#import "header.h"
,擴(kuò)展為#import "../../include/header.h"
的形式,如果沒這個需求就不用向下看了)
正文
假設(shè)源引用是#import "header.h"
,然后該文件放在上兩層的include
目錄中(也就是../../include/header.h
),先整理一下思路,要找到.h
文件,第一種是暴力法,枚舉整個項目目錄,把找到的文件填充進(jìn)去,但感覺不是很好(感覺會有隱性BUG),第二種是先列舉所有可用文件列表,再從列表中匹配,這種方法可自定義程度高,本文采用此法.
進(jìn)行項目目錄,用命令行find . -name "*.h">~/Desktop/1.txt && find . -name "*.m">>~/Desktop/1.txt
把所有可能的文件生成列表(若有其它格式,繼續(xù)添加,如.mm
),打開.txt
把./
替換為完整的路徑,然后把文本拆解為單行的數(shù)組,也就是完整的文件路徑,對每個文件進(jìn)行讀取,查找引用,替換引用,重新保存,過程如下:
- 1.查找源文件目錄中是否存在引用文件,若有,則優(yōu)先使用.
- 2.查找子目錄中是否存在引用文件,若有,則優(yōu)先使用.
- 3.如源文件目錄和子目錄都找不到,則搜索切換到父目錄重新執(zhí)行,若枚舉完所有路徑都找不到,那就寫入出錯日志(代碼中未實(shí)現(xiàn),可自行添加).
代碼如下,有什么疑問歡迎和我交流.
@interface SCCodeHelper : NSObject
+ (void)fixHeaderWithSourceFiles: (NSArray *)sourceFiles; // 參數(shù)為可用的文件列表
@end
// SCModel為一個簡易模型轉(zhuǎn)換對象,可在我的github找到
@interface SCCoderFixImportHeaderInfo : SCModel
@property (nonatomic, strong) NSString *codeFile;
@property (nonatomic, strong) NSMutableString *codeString;
@property (nonatomic, strong) NSArray *validHeaders;
@property (nonatomic, strong) NSString *originImportLine;
@property (nonatomic, strong, readonly) NSString *headerFileName;
@property (nonatomic, strong, readonly) NSString *codeFileDir;
@property (nonatomic, strong, readonly) NSString *codeFileName;
@end
@implementation SCCoderFixImportHeaderInfo
- (void)setValidHeaders:(NSArray *)validHeaders
{
_validHeaders = validHeaders;
_headerFileName = [_validHeaders.firstObject lastPathComponent];
}
- (void)setCodeFile:(NSString *)codeFile
{
_codeFile = codeFile;
_codeFileDir = [_codeFile regexpReplace:@"/[^/]+$" replace:@""];
_codeFileName = [_codeFile lastPathComponent];
}
@end
@implementation SCCodeHelper
+ (void)fixHeaderWithSourceFiles: (NSArray *)sourceFiles
{
NSArray *ebx = sourceFiles;
SCCoderFixImportHeaderInfo *info = NewClass(SCCoderFixImportHeaderInfo);
for (NSString *ecx in ebx)
{
if (0 == ecx.length) continue;
NSMutableString *code = [NSMutableString stringWithContentsOfFile:ecx encoding:NSUTF8StringEncoding error:NULL];
if (0 == code.length) continue;
NSMutableDictionary *imports = [self getImports:code];
if (0 == imports.count) continue;
for (NSString *file in imports.allKeys)
{
NSArray *finds = [self find:file map:sourceFiles];
if (0 == finds.count) continue;
info.codeFile = ecx;
info.codeString = code;
info.validHeaders = finds;
info.originImportLine = imports[file];
BOOL state = [self fixCodeWithModel:info fbackLevel:0];
if (state) [code writeToFile:ecx atomically:YES encoding:NSUTF8StringEncoding error:NULL];
BreakPointHere;
}
BreakPointHere;
}
}
+ (BOOL)fixCodeWithModel: (SCCoderFixImportHeaderInfo *)info fbackLevel: (NSUInteger)fbackLevel
{
NSString *codeFileName = info.codeFileName;
NSString *headerFileName = info.headerFileName;
NSString *originImportLine = info.originImportLine;
NSString *codeFileDir = info.codeFileDir;
NSMutableString *codeString = info.codeString;
NSArray *validHeaders = info.validHeaders;
// 跳過絕對路徑
if (StringHasSubstring(originImportLine, @"/")) return NO;
BOOL codeChanged = NO;
BOOL result = NO;
if (fbackLevel)
{
for (NSUInteger a = 0; a < fbackLevel; ++a)
{
codeFileDir = [codeFileDir regexpReplace:@"/[^/]+$" replace:@""];
}
BreakPointHere;
}
{
// 看看同目錄
NSString *ebx = [NSString stringWithFormat:@"%@/%@", codeFileDir, headerFileName];
for (NSString *eax in validHeaders)
{
if (NO == IsSameString(eax, ebx)) continue;
NSString *ecx = @"";
if (fbackLevel)
{
for (NSUInteger a = 0; a < fbackLevel; ++a)
{
ecx = [NSString stringWithFormat:@"../%@", ecx];
}
}
NSString *line = [NSString stringWithFormat:@"#import \"%@%@\"", ecx, headerFileName];
if (NO == StringHasSubstring(originImportLine, line))
{
MLog(@"src: %@", codeFileName);
MLog(@"fix: %@", originImportLine);
MLog(@"to : %@", line);
MLog(@"=========================================================");
[codeString replaceOccurrencesOfString:originImportLine withString:line options:0 range:NSMakeRange(0, codeString.length)];
codeChanged = YES;
}
result = YES;
break;
}
if (result) return codeChanged;
}
// 看看子目錄
{
for (NSString *eax in validHeaders)
{
if (NO == StringHasSubstring(eax, codeFileDir)) continue;
NSString *buffer = [eax stringByReplacingOccurrencesOfString:codeFileDir withString:@""];
buffer = [buffer regexpReplace:@"^/" replace:@""];
NSString *ecx = @"";
if (fbackLevel)
{
for (NSUInteger a = 0; a < fbackLevel; ++a)
{
ecx = [NSString stringWithFormat:@"../%@", ecx];
}
}
NSString *line = [NSString stringWithFormat:@"#import \"%@%@\"", ecx, buffer];
if (NO == StringHasSubstring(originImportLine, line))
{
MLog(@"src: %@", codeFileName);
MLog(@"fix: %@", originImportLine);
MLog(@"to : %@", line);
MLog(@"=========================================================");
[codeString replaceOccurrencesOfString:originImportLine withString:line options:0 range:NSMakeRange(0, codeString.length)];
codeChanged = YES;
}
result = YES;
break;
}
if (result) return codeChanged;
}
// 看看上層目錄
{
if (NO == [codeFileDir isEqualToString:@"/"])
{
codeChanged = [self fixCodeWithModel:info fbackLevel:fbackLevel+1];
return codeChanged;
}
}
LogAnything(codeFileDir);
LogAnything(info.validHeaders);
BreakPointHere;
return NO;
}
+ (NSMutableArray *)find: (NSString *)file map: (NSArray *)map
{
NSMutableArray *result = NewMutableArray();
for (NSString *name in map)
{
NSString *eax = [NSString stringWithFormat:@"/%@", file];
if ([name hasSuffix:eax])
{
[result addObject:name];
}
}
if (0 == result.count) return nil;
return result;
}
+ (NSMutableDictionary *)getImports: (NSString *)code
{
NSArray *imps = [code regexpMatch:@"[\\r\\n]\\s*#import\\s+\\x22[^\\x22]+\\x22"];
NSMutableDictionary *result = nil;
if (imps.count) result = NewMutableDictionary();
for (NSString *eax in imps)
{
NSString *file = [eax regexpMatch:@"\\x22[^\\x22]+\\x22"][0];
file = [file stringByReplacingOccurrencesOfString:@"\"" withString:@""];
result[file] = [eax regexpReplace:@"[\\r\\n]+" replace:@""];
}
return result;
}
@end