-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
155 lines (133 loc) · 5.16 KB
/
Form1.cs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CheckTools.Rules;
namespace CheckTools
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//窗体展示的时候,要加载所有的规则
ruleList = new List<RuleBase>()
{
//扣分项
new SubClassComment(),
new SubForComment(),
new SubFunctionComment(),
new SubEnumComment(),
//加分项
new AddCodeBlock(),
new AddCodeBeautiful(),
new AddSwitchFormat(),
new AddForFormat(),
new AddIFFormat()
};
}
/// <summary>
/// 所有审核规则的列表
/// </summary>
private List<RuleBase> ruleList;
/// <summary>
/// 开始处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStart_Click(object sender, EventArgs e)
{
//1.让使用者选择文件夹
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
folderBrowser.ShowDialog();
if (string.IsNullOrEmpty(folderBrowser.SelectedPath)) return;
string projectFolderPos = folderBrowser.SelectedPath;
outputfile = projectFolderPos + ".txt";
absolutePath = Path.GetDirectoryName(projectFolderPos);
OutPutFileWrite("**********************************************");
OutPutFileWrite("项目名称:" + Path.GetFileName(projectFolderPos));
//2.从文件夹中遍历所有的C#文件,得到的是文件名
List<string> filesArray = Directory.GetFiles(projectFolderPos, "*.cs", SearchOption.AllDirectories).ToList();
//3.以文件为单元,开始做代码审核
float projectTotalGrade = 0;
foreach (var filePath in filesArray)
{
projectTotalGrade += ProcessContent(filePath);
}
OutPutFileWrite("************************************");
OutPutFileWrite("");
#region 4.文件数大于5,才有加分资格
int intTemp = 0;
if (filesArray.Count > 5)
{
if (rdoFirst.Checked)
{
OutPutFileWrite("项目发布当天提交审核,加分:" + 2);
intTemp = 2;
}
else if (rdoSecond.Checked)
{
OutPutFileWrite("项目发布次日提交审核,加分:" + 1);
intTemp = 1;
}
}
else
{
OutPutFileWrite("项目审核文件数量小于5,不加分");
}
#endregion
OutPutFileWrite("审核文件数:" + filesArray.Count);
OutPutFileWrite("项目总得分:" + (projectTotalGrade + intTemp));
MessageBox.Show("操作完成");
}
/// <summary>
/// 记录运行程序时,文件夹在本地的路径
/// </summary>
private string absolutePath;
/// <summary>
/// 以文件为单位,做每个规则的审核
/// </summary>
private float ProcessContent(string filePath)
{
List<string> fileContent = File.ReadAllLines(filePath).ToList();
//记录项目文件的总得分
float fileContentGrade = 0;
OutPutFileWrite("------------------------------------------------");
OutPutFileWrite("文件名:" + filePath.Replace(absolutePath, ""));
int rowNum = 0;
//1.用当前拿到的内容,去执行每一个规则
foreach (var rule in ruleList)
{
//若满足规则所指条件,那么就追加记录规则名,以及分数
if (rule.RuleResule(fileContent, out rowNum))
{
if (rowNum != 0) OutPutFileWrite(string.Format("规则:{0} 分数:{1} 位置:{2}", rule.GetRuleName(), rule.GetRuleGrade(), rowNum + 1));
else OutPutFileWrite(string.Format("规则:{0} 分数:{1}", rule.GetRuleName(), rule.GetRuleGrade()));
fileContentGrade += rule.GetRuleGrade();
}
}
OutPutFileWrite("文件总得分:" + fileContentGrade);
return fileContentGrade;
}
#region 输出结果相关
/// <summary>
/// 输出文件的全名称
/// </summary>
private string outputfile;
/// <summary>
/// 向输出文件追加内容
/// </summary>
/// <param name="text">要向文件中追加的内容</param>
private void OutPutFileWrite(string text)
{
File.AppendAllText(outputfile, text + Environment.NewLine);
}
#endregion
}
}