给你的 iOS 马甲项目增加一点垃圾代码

  由于公司的一个项目屡次出现 Guideline 4.3 - Design 审核被拒问题,想着能最大程度的解决这种问题出现,就去了解了一下怎么给项目增加垃圾代码以通过审核,这里给大家推荐两种。

一、用 Python 生成垃圾文件

1.第一步:首先生成一个500位的数组 驼峰类型的元素 用作文件名 eg:AsdfdfGsd

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import random

import string

first = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

second = "abcdefghijklmnopqrstuvwxyz"

number = "345"

index = 0

array = []

for i in range(500):

final=(random.choice(first))

index = random.randint(3, 5)

for i in range(index):

final+=(random.choice(second))

final += (random.choice(first))

for i in range(index):

final+=(random.choice(second))

array.append(final)

print (array)

PS.如果是直接复制粘贴,不要忘记 Python 变态的首行缩进。

2.第二步: 用上边生成的数组来创建对应的 .h.m 文件

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# -*- coding: utf-8 -*-

import random

import os

import string

#创建.h文件

def text_createH(fileNmae,msg,msg1,propertyNumber,methodArray,msg3):

full_path = '/Users/zhoujinglin/Desktop/python-H-M/' + fileNmae + '.h'

file = open(full_path, 'w')

file.write('//\n// '+fileNmae+'.h\n// SajiaoShopping\n\n// Created by Kaaaaai bo on 18/06/28.\n// Copyright © 2018年 Kaaaaai. All rights reserved.\n//\n\n')

file.write(msg)

file.write(msg1)

propryNameArray = []

for index in range(1,propertyNumber):

propryNameArray.append(random.choice(array))

propryNameArray = list(set(propryNameArray))

for propertyName in propryNameArray:
file.write('@property(nonatomic,strong)'+random.choice(classArray)+' * '+propertyName+';\n')

file.write('\n\n')

for methodName in methodArray:

file.write('- (void)pushTo'+methodName+'VC:(NSDictionary *)info;\n')

file.write(msg3)

file.close()

print('Done')

#创建.m文件

def text_createM(fileNmae,msg,msg1,methodArray,msg3):

full_path = '/Users/zhoujinglin/Desktop/python-H-M/' + fileNmae + '.m'

file = open(full_path, 'w')

file.write('//\n// '+fileNmae+'.m\n// SajiaoShopping\n\n// Created by Kaaaaai bo on 18/06/28.\n// Copyright © 2017年 Kaaaaai. All rights reserved.\n//\n\n')

file.write(msg)

file.write(msg1)

for methodName in methodArray:

file.write('- (void)pushTo'+methodName+'VC:(NSDictionary *)info\n{\n\n NSMutableArray *array = [NSMutableArray array];\n')

number = random.randint(3, 10)

for i in range(1,number):

file.write(' [array addObject:@"'+random.choice(array)+'"];\n')

file.write('\n}\n\n')

file.write(msg3)

file.close()

print('Done')

classArray = ['NSString','UILabel','NSDictionary','NSData','UIScrollView','UIView']

array = ['HwxrFvrj', 'QnzduQbtdd', 'PvcrwLtqhf', 'UvdhDbjn', 'SuntmyTxvyzg', 'CvlxwBipbp', 'GzrdyzIbimvz', 'CqsjqMmgsp', 'OxaaeuWjhasc', 'NjiardRvwgbi', 'NcculmLtpljq', 'ApoqQrll', 'GkgokDyvjb', 'EblldkVouplj', 'KfdrFvnw', 'SfhyhObftc', 'SmruByoc', 'YzcccvXmpmit', 'OmqvaHpxat', 'XzytsUyvyd', 'MjforNnnyi', 'ZvjhuIdogs', 'BzfrxzSeahxc', 'PycycwFjtpny', 'XvngtoSedljr', 'DktiaCbucd', 'AqbplNuodc', 'MzkvgZuala', 'KdwzIoej', 'AaynatUpqcfd', 'IyvwhZvtjc', 'UmijGmsy', 'AoayndXxghym']

array = list(set(array))

for name in array:

number = random.randint(3, 10)

methodArray = []

for i in range(1,5):

methodArray.append(random.choice(array))

methodArray = list(set(methodArray))#数组去重

text_createH(name+'ViewController', '#import <UIKit/UIKit.h>\n','@interface '+name+ 'ViewController:'+ 'UIViewController\n\n',number,methodArray,'\n\n@end')

text_createM(name+'ViewController', '#import "'+name+'ViewController.h"\n\n' '@interface '+ name+'ViewController()\n\n @end\n\n','@implementation '+name+'ViewController\n\n- (void)viewDidLoad { \n\n [super viewDidLoad];\n\n}\n\n',methodArray,'\n\n@end')

二、用造好的轮子直接生成

KLGenerateSpamCode 垃圾代码生成器

这个项目不仅可以生成垃圾代码,还可以自动化修改类名前缀,详细包含功能如下:

主要功能:
修改工程名
修改类名前缀
扫描工程中的代码,生成同等数量的 Category 文件,文件中及是同等方法数量的垃圾代码。
修改 xxx.xcassets 文件夹中的 png 资源文件名。
删除代码中的所有注释和空行。

使用时值得注意的有两点,一个是,他生成垃圾代码会遍历你所有的类名,然后生成无用的打印类,而实际使用时,我们并不需要增加如此多的代码,可以选择性的导入相关的 .m.h 文件;另一个是在配置文件中的这个属性

  • ignoreDirNames [忽略文件夹名称字符串] 忽略这些文件夹,对-modifyClassNamePrefix 和 - spamCodeOut 参数有效。目前只会忽略源码文件夹绝对路径下一级的这些目录。如:/p/s - ignoreDirNames categorys,那么 /p/s/categorys 会被忽略,但 /p/s/viewControllers/categorys 不会忽略。

因为他设定逻辑的原因,所以你可能要重复添加文件夹路径。这里我们需要做的是,到项目内部的main 方法里,添加判断,比如在这个方法可以添加判断,如果文件路径包含你想跳过的字符,直接 return ,就不用那么费劲了。

Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
void modifyClassNamePrefix(NSMutableString *projectContent, NSString *sourceCodeDir, NSArray<NSString *> *ignoreDirNames, NSString *oldName, NSString *newName) {
NSFileManager *fm = [NSFileManager defaultManager];

// 遍历源代码文件 h 与 m 配对,swift
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]) {
modifyClassNamePrefix(projectContent, path, ignoreDirNames, oldName, newName);
}
continue;
}

NSString *fileName = filePath.lastPathComponent.stringByDeletingPathExtension;
NSString *fileExtension = filePath.pathExtension;
NSString *newClassName;
if ([fileName hasPrefix:oldName]) {
newClassName = [newName stringByAppendingString:[fileName substringFromIndex:oldName.length]];
} else {
newClassName = [newName stringByAppendingString:fileName];
}

// 文件名 Const.ext > DDConst.ext
if ([fileExtension isEqualToString:@"h"]) {
NSString *mFileName = [fileName stringByAppendingPathExtension:@"m"];
if ([files containsObject:mFileName]) {
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"];
renameFile(oldFilePath, newFilePath);
oldFilePath = [[sourceCodeDir stringByAppendingPathComponent:fileName] stringByAppendingPathExtension:@"xib"];
if ([fm fileExistsAtPath:oldFilePath]) {
newFilePath = [[sourceCodeDir stringByAppendingPathComponent:newClassName] stringByAppendingPathExtension:@"xib"];
renameFile(oldFilePath, newFilePath);
}

@autoreleasepool {
modifyFilesClassName(gSourceCodeDir, fileName, newClassName);
}
} else {
continue;
}
} else if ([fileExtension isEqualToString:@"swift"]) {
NSString *oldFilePath = [[sourceCodeDir stringByAppendingPathComponent:fileName] stringByAppendingPathExtension:@"swift"];
NSString *newFilePath = [[sourceCodeDir stringByAppendingPathComponent:newClassName] stringByAppendingPathExtension:@"swift"];
renameFile(oldFilePath, newFilePath);
oldFilePath = [[sourceCodeDir stringByAppendingPathComponent:fileName] stringByAppendingPathExtension:@"xib"];
if ([fm fileExistsAtPath:oldFilePath]) {
newFilePath = [[sourceCodeDir stringByAppendingPathComponent:newClassName] stringByAppendingPathExtension:@"xib"];
renameFile(oldFilePath, newFilePath);
}

@autoreleasepool {
modifyFilesClassName(gSourceCodeDir, fileName.stringByDeletingPathExtension, newClassName);
}
} else {
continue;
}

// 修改工程文件中的文件名
NSString *regularExpression = [NSString stringWithFormat:@"\\b%@\\b", fileName];
regularReplacement(projectContent, regularExpression, newClassName);
}
}
------本文结束 感谢阅读------

本文地址:http://kaaaaai.cn/articles/028.html
本文基于 知识共享署名-相同方式共享 4.0 国际许可协议发布
转载请注明出处,谢谢!

众筹项目:拯救世界!
0%