main.cs

来自「SharpDevelop2.0.0 c#开发免费工具」· CS 代码 · 共 167 行

CS
167
字号
// <file>
//     <copyright see="prj:///doc/copyright.txt">2002-2005 AlphaSierraPapa</copyright>
//     <license see="prj:///doc/license.txt">GNU General Public License</license>
//     <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
//     <version>$Revision: 1162 $</version>
// </file>

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace CheckFileHeaders
{
	class MainClass
	{
		static int Main(string[] args)
		{
			int count;
			try {
				MainClass m = new MainClass();
				if (args.Length == 0) {
					Console.WriteLine("Checking files in {0}  ...", Path.GetFullPath(@"..\..\..\..\"));
					count = m.Run(@"..\..\..\..\");
				} else {
					Console.WriteLine("Checking files in {0}  ...", Path.GetFullPath(args[0]));
					count = m.Run(args[0]);
				}
				Console.WriteLine("Finished! (checked {0} files, changed {1} files, ignored {2} files)", count, m.changeCount, m.ignoreCount);
			} catch (Exception ex) {
				Console.WriteLine(ex);
			}
			Console.Write("Press any key to continue...");
			Console.ReadKey(true);
			return 0;
		}
		int Run(string dir)
		{
			int count = 0;
			foreach (string file in Directory.GetFiles(dir, "*.cs")) {
				if (file.EndsWith(".Designer.cs"))
					continue;
				count++;
				ProcessFile(file);
			}
			foreach (string subdir in Directory.GetDirectories(dir)) {
				if (subdir.EndsWith("\\.svn"))
					continue;
				if (subdir.EndsWith("Libraries\\DockPanel_Src"))
					continue;
				if (subdir.EndsWith("Libraries\\log4net"))
					continue;
				if (subdir.EndsWith("Libraries\\NUnit.Framework"))
					continue;
				if (Path.GetFullPath(subdir).EndsWith("src\\Tools"))
					continue;
				// Disabled addins:
				if (subdir.EndsWith("AddIns\\BackendBindings\\CPPNetBinding"))
					continue;
				if (subdir.EndsWith("AddIns\\BackendBindings\\ILAsmBinding"))
					continue;
				if (subdir.EndsWith("AddIns\\BackendBindings\\WixBinding"))
					continue;
				if (subdir.EndsWith("AddIns\\DisplayBindings\\AssemblyScout"))
					continue;
				if (subdir.EndsWith("AddIns\\Misc\\Debugger\\TreeListView\\Project"))
					continue;
				count += Run(subdir);
			}
			return count;
		}
		
		// must be splitted because this file is under version control, too
		Regex resetVersionRegex = new Regex(@"//     <version>\$Revi" + @"sion: \d+ \$</version>", RegexOptions.Compiled);
		
		int changeCount, ignoreCount;
		
		void ProcessFile(string file)
		{
			string content = GetFileContent(file);
			string author, email;
			int lastLine;
			int headerType = AnalyzeHeader(content, out author, out email, out lastLine);
			if (headerType == 5) {
				//Console.WriteLine("unknown file: " + file);
				ignoreCount++;
				return;
			}
			if (author == null)
				author = "";
			if (author == "") {
				Console.Write(file);
				char ch;
				do {
					Console.WriteLine();
					Console.Write("  Daniel/Matt/Other/None/Ignore (D/M/O/N/I): ");
				}
				while ((ch = char.ToUpper(Console.ReadKey().KeyChar)) != 'M'
				       && ch != 'N' && ch != 'I' && ch != 'O' && ch != 'D');
				Console.WriteLine();
				if (ch == 'M') {
					author = "Matthew Ward";
				} else if (ch == 'D') {
					author = "Daniel Grunwald";
				} else if (ch == 'O') {
					bool ok;
					do {
						Console.Write("Enter author name: ");
						author = Console.ReadLine();
						if (author == "David") author = "David Srbecky";
						if (author == "Markus") author = "Markus Palme";
						if (author == "Peter") author = "Peter Forstmeier";
						email = CheckAuthor(ref author);
						ok = author != null;
					} while (!ok);
				} else if (ch == 'I') {
					ignoreCount++;
					return;
				} else {
					author = "none";
				}
			}
			string oldAuthor = author;
			email = CheckAuthor(ref author);
			if (author == null) {
				Console.WriteLine("Unknown author: " + oldAuthor + " in " + file);
				Console.WriteLine("    File was ignored.");
				return;
			}
			StringBuilder builder = new StringBuilder();
			builder.AppendLine("// <file>");
			builder.AppendLine("//     <copyright see=\"prj:///doc/copyright.txt\"/>");
			builder.AppendLine("//     <license see=\"prj:///doc/license.txt\"/>");
			builder.Append("//     <owner name=\"");
			builder.Append(author);
			builder.Append("\" email=\"");
			builder.Append(email);
			builder.AppendLine("\"/>");
			
			// must be splitted because this file is under version control, too
			const string versionLine = "//     <version>$Revi" + "sion$</version>";
			builder.AppendLine(versionLine);
			builder.AppendLine("// </file>");
			builder.AppendLine();
			int offset = FindLineOffset(content, lastLine + 1);
			builder.Append(content.Substring(offset).Trim());
			builder.AppendLine();
			string newContent = builder.ToString();
			string resettedVersion = resetVersionRegex.Replace(content, versionLine);
			if (newContent != resettedVersion) {
				using (StreamWriter w = new StreamWriter(file, false, GetOptimalEncoding(newContent))) {
					changeCount++;
					w.Write(newContent);
				}
			}
		}
		
		string CheckAuthor(ref string author)
		{
			switch (author) {
				case "Mike Kr黦er":
					author = "Mike Kr黦er";
					return "mike@icsharpcode.net";
				case "Daniel Grunwald":
					return "daniel@danielgrunwald.de";
				case "David Srbeck

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?