Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1120"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "24934DDE1F0CD64800958090"
BuildableName = "KLGenerateSpamCode"
BlueprintName = "KLGenerateSpamCode"
ReferencedContainer = "container:KLGenerateSpamCode.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "24934DDE1F0CD64800958090"
BuildableName = "KLGenerateSpamCode"
BlueprintName = "KLGenerateSpamCode"
ReferencedContainer = "container:KLGenerateSpamCode.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "/Users/zl/Desktop/testRname/VietnamObject/VietnamObject&#10;"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不用提交 .xcscheme 文件,更别把自己的参数提交上来。

isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "-ignoreDirNames Network,Categry,Configure,MGBaseKit,MGLiveDetection,Pods,ThirdSdk"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "-modifyClassNamePrefix /Users/zl/Desktop/testRname/VietnamObject/VietnamObject.xcodeproj PS&gt;RR"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "-modifyProjectName VietnamObject&gt;NewNameDemo"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "-handleXcassets "
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "-deleteComments"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "-spamCodeOut //Users/zl/Desktop/&#x5783;&#x573e;&#x4ee3;&#x7801; dash"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "24934DDE1F0CD64800958090"
BuildableName = "KLGenerateSpamCode"
BlueprintName = "KLGenerateSpamCode"
ReferencedContainer = "container:KLGenerateSpamCode.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
59 changes: 39 additions & 20 deletions KLGenerateSpamCode/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,7 @@ int main(int argc, const char * argv[]) {
}
printf("删除注释和空行完成\n");
}
if (oldProjectName && newProjectName) {
@autoreleasepool {
NSString *dir = gSourceCodeDir.stringByDeletingLastPathComponent;
modifyProjectName(dir, oldProjectName, newProjectName);
}
printf("修改工程名完成\n");
}

if (oldClassNamePrefix && newClassNamePrefix) {
printf("开始修改类名前缀...\n");
@autoreleasepool {
Expand All @@ -303,6 +297,14 @@ int main(int argc, const char * argv[]) {
}
printf("修改类名前缀完成\n");
}

if (oldProjectName && newProjectName) {
@autoreleasepool {
NSString *dir = gSourceCodeDir.stringByDeletingLastPathComponent;
modifyProjectName(dir, oldProjectName, newProjectName);
}
printf("修改工程名完成\n");
}
if (outDirString) {
NSMutableString *categoryCallImportString = [NSMutableString string];
NSMutableString *categoryCallFuncString = [NSMutableString string];
Expand Down Expand Up @@ -442,7 +444,7 @@ void generateSpamCodeFile(NSString *outDirectory, NSString *mFilePath, GSCSource
return;
}
}

// 查找方法
NSString *implementation = [mFileContent substringWithRange:impResult.range];
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"^ *([-+])[^)]+\\)([^;{]+)" options:NSRegularExpressionAnchorsMatchLines|NSRegularExpressionUseUnicodeWordBoundaries error:nil];
Expand Down Expand Up @@ -561,9 +563,9 @@ void generateSpamCodeFile(NSString *outDirectory, NSString *mFilePath, GSCSource
extension %@ {\n%@\
}\n";
static NSString *const kSwiftMethodTemplate = @"\
func %@%@(_ %@: String%@) {\n\
print(%@)\n\
}\n";
func %@%@(_ %@: String%@) {\n\
print(%@)\n\
}\n";
void generateSwiftSpamCodeFile(NSString *outDirectory, NSString *swiftFilePath) {
NSString *swiftFileContent = [NSString stringWithContentsOfFile:swiftFilePath encoding:NSUTF8StringEncoding error:nil];

Expand Down Expand Up @@ -697,7 +699,7 @@ void deleteComments(NSString *directory, NSArray<NSString *> *ignoreDirNames) {
deleteComments(filePath, ignoreDirNames);
continue;
}
if (![fileName hasSuffix:@".h"] && ![fileName hasSuffix:@".m"] && ![fileName hasSuffix:@".mm"] && ![fileName hasSuffix:@".swift"]) continue;
if (![fileName hasSuffix:@".h"] && ![fileName hasSuffix:@".m"] && ![fileName hasSuffix:@".mm"] && ![fileName hasSuffix:@".mm"] && ![fileName hasSuffix:@".swift"]) continue;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里重复添加 [fileName hasSuffix:@".mm"],得去掉。

NSMutableString *fileContent = [NSMutableString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
regularReplacement(fileContent, @"([^:/])//.*", @"\\1");
regularReplacement(fileContent, @"^//.*", @"");
Expand Down Expand Up @@ -857,7 +859,7 @@ void modifyFilesClassName(NSString *sourceCodeDir, NSString *oldClassName, NSStr
}

NSString *fileName = filePath.lastPathComponent;
if ([fileName hasSuffix:@".h"] || [fileName hasSuffix:@".m"] || [fileName hasSuffix:@".pch"] || [fileName hasSuffix:@".swift"] || [fileName hasSuffix:@".xib"] || [fileName hasSuffix:@".storyboard"]) {
if ([fileName hasSuffix:@".h"] || [fileName hasSuffix:@".m"] || [fileName hasSuffix:@".mm"] || [fileName hasSuffix:@".pch"] || [fileName hasSuffix:@".swift"] || [fileName hasSuffix:@".xib"] || [fileName hasSuffix:@".storyboard"]) {

NSError *error = nil;
NSMutableString *fileContent = [NSMutableString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
Expand Down Expand Up @@ -886,6 +888,8 @@ void modifyClassNamePrefix(NSMutableString *projectContent, NSString *sourceCode
NSArray<NSString *> *files = [fm contentsOfDirectoryAtPath:sourceCodeDir error:nil];
BOOL isDirectory;
for (NSString *filePath in files) {


NSString *path = [sourceCodeDir stringByAppendingPathComponent:filePath];
if ([fm fileExistsAtPath:path isDirectory:&isDirectory] && isDirectory) {
if (![ignoreDirNames containsObject:filePath]) {
Expand All @@ -899,27 +903,42 @@ void modifyClassNamePrefix(NSMutableString *projectContent, NSString *sourceCode
NSString *newClassName;
if ([fileName hasPrefix:oldName]) {
newClassName = [newName stringByAppendingString:[fileName substringFromIndex:oldName.length]];
} else {
//处理是category的情况。当是category时,修改+号后面的类名前缀
}else if([fileName containsString:@"+"]){
// category类型
NSString *oldNamePlus = [NSString stringWithFormat:@"+%@",oldName];
if ([fileName containsString:oldNamePlus]) {
NSMutableString *fileNameStr = [[NSMutableString alloc] initWithString:fileName];
[fileNameStr replaceCharactersInRange:[fileName rangeOfString:oldNamePlus] withString:[NSString stringWithFormat:@"+%@",newName]];
newClassName = fileNameStr;
}else{
newClassName = [newName stringByAppendingString:fileName];

newClassName = fileName;

}

}else {
newClassName = fileName;

}

// 文件名 Const.ext > DDConst.ext
if ([fileExtension isEqualToString:@"h"]) {
NSString *mFileName = [fileName stringByAppendingPathExtension:@"m"];
if ([files containsObject:mFileName]) {
NSString *mmFileName = [fileName stringByAppendingPathExtension:@"mm"];

if ([files containsObject:mFileName] || [files containsObject:mmFileName]) {
NSString *oldFilePath = [[sourceCodeDir stringByAppendingPathComponent:fileName] stringByAppendingPathExtension:@"h"];
NSString *newFilePath = [[sourceCodeDir stringByAppendingPathComponent:newClassName] stringByAppendingPathExtension:@"h"];
renameFile(oldFilePath, newFilePath);
oldFilePath = [[sourceCodeDir stringByAppendingPathComponent:fileName] stringByAppendingPathExtension:@"m"];
newFilePath = [[sourceCodeDir stringByAppendingPathComponent:newClassName] stringByAppendingPathExtension:@"m"];
if ([files containsObject:mFileName]) {
oldFilePath = [[sourceCodeDir stringByAppendingPathComponent:fileName] stringByAppendingPathExtension:@"m"];
newFilePath = [[sourceCodeDir stringByAppendingPathComponent:newClassName] stringByAppendingPathExtension:@"m"];
}else{
oldFilePath = [[sourceCodeDir stringByAppendingPathComponent:fileName] stringByAppendingPathExtension:@"mm"];
newFilePath = [[sourceCodeDir stringByAppendingPathComponent:newClassName] stringByAppendingPathExtension:@"mm"];

}

renameFile(oldFilePath, newFilePath);
oldFilePath = [[sourceCodeDir stringByAppendingPathComponent:fileName] stringByAppendingPathExtension:@"xib"];
if ([fm fileExistsAtPath:oldFilePath]) {
Expand All @@ -930,7 +949,7 @@ void modifyClassNamePrefix(NSMutableString *projectContent, NSString *sourceCode
@autoreleasepool {
modifyFilesClassName(gSourceCodeDir, fileName, newClassName);
}
} else {
} else {
continue;
}
} else if ([fileExtension isEqualToString:@"swift"]) {
Expand Down