//
// main.m
// 移除无用词条
//
// Created by user on 2021/12/24.
//
#import <Foundation/Foundation.h>
void findFilePath(NSMutableArray *filePath, NSString *path) {
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *directroyArray = [fm contentsOfDirectoryAtPath:path error:&error];
if (error) {
NSLog(@"%@", error);
return;
}
for (NSString *fileName in directroyArray) {
BOOL isDirectroy = NO;
NSString *fullPath = [path stringByAppendingPathComponent:fileName];
if ([fm fileExistsAtPath:fullPath isDirectory:&isDirectroy]) {
if (isDirectroy) {
if ([fileName isEqualToString:@"Pods"]) {
continue;
}
findFilePath(filePath, fullPath);
} else {
if ([fullPath hasSuffix:@".swift"] || [fullPath hasSuffix:@".m"]) {
[filePath addObject:fullPath];
}
}
}
}
}
void lcy_searchFileWithKeys(NSMutableArray * allKeys, NSMutableArray * allContents, NSMutableArray *usedKeys, NSMutableArray *usedContents, NSMutableArray *filesPath) {
for (NSString *path in filesPath) {
NSString *content = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *lines = [content componentsSeparatedByString:@"\n"];
for (NSString *line in lines) {
for (NSInteger i = 0; i < allKeys.count; i ++) {
NSString *key = allKeys[i];
NSString *content = allContents[i];
if ([line containsString:key]) {
[allKeys removeObjectAtIndex:i];
[allContents removeObjectAtIndex:i];
[usedKeys addObject:key];
[usedContents addObject:content];
break;
}
}
}
}
}
void findUnuseKey(void) {
NSString *filePath = @"/Users/.../translations/en.lproj/Localizable.strings";
NSError *error;
NSString *content = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(@"%@", error);
return;
}
// 获取key
NSArray *allLines = [content componentsSeparatedByString:@"\n"];
NSMutableArray *allKeys = [NSMutableArray array];
NSMutableArray *allContents = [NSMutableArray array];
for (NSString *line in allLines) {
NSRange range = [line rangeOfString:@"\" = \""];
if (range.location == NSNotFound) {
continue;
}
NSString *first = [line componentsSeparatedByString:@"\" = \""].firstObject;
NSString *last = [line componentsSeparatedByString:@"\" = \""].lastObject;
[allKeys addObject:[NSString stringWithFormat:@"%@\"", first]];
[allContents addObject:[last substringToIndex:last.length - 2]];
}
NSMutableArray *filesPath = [NSMutableArray array];
// 获取所有文件路径
NSString *projectRoot = @"项目路径xcodeproj所在文件夹位置";
findFilePath(filesPath, projectRoot);
NSMutableArray *usedKeys = [NSMutableArray array];
NSMutableArray *usedContents = [NSMutableArray array];
// 感觉不太准,多调两遍
lcy_searchFileWithKeys(allKeys, allContents, usedKeys, usedContents, filesPath);
lcy_searchFileWithKeys(allKeys, allContents, usedKeys, usedContents, filesPath);
// 未使用的
NSMutableString *unusedKeyString = [NSMutableString string];
for (NSInteger i = 0; i < allKeys.count; i ++) {
NSString *key = allKeys[i];
NSString *content = allContents[i];
unusedKeyString = [[NSString stringWithFormat:@"%@\n\n%@ = \"%@\"", unusedKeyString, key, content] mutableCopy];
}
[unusedKeyString writeToFile:@"/Users/name/Desktop/haha/languageUnused.txt" atomically:YES];
// 用到的
NSMutableString *finalString = [NSMutableString string];
for (NSInteger i = 0; i < usedKeys.count; i ++) {
NSString *key = usedKeys[i];
NSString *content = usedContents[i];
finalString = [[NSString stringWithFormat:@"%@\n\n%@ = \"%@\"", finalString, key, content] mutableCopy];
}
[finalString writeToFile:@"/Users/name/Desktop/haha/language.txt" atomically:YES];
NSLog(@"%@", finalString);
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 找到无用的key
findUnuseKey();
}
return 0;
}
网友评论