CCNet.Build.CheckProject.AllFilesShouldUseUtf8.Check C# (CSharp) Method

Check() public method

public Check ( CheckContext context ) : void
context CheckContext
return void
		public void Check(CheckContext context)
		{
			var encodings = GetTfsEncodings(context);
			var signatures = GetLocalUtf8BomSignatures(encodings.Keys);

			// skip files which are marked as UTF-8 in TFS and using BOM
			var exclude = encodings
				.Where(i => i.Value == 65001)
				.Select(i => i.Key)
				.Where(signatures.ContainsKey)
				.Where(i => signatures[i])
				.ToList();

			foreach (var file in exclude)
			{
				encodings.Remove(file);
				signatures.Remove(file);
			}

			var required = new List<string>();
			var mismatched = new List<string>();
			var exceptional = new List<string>();

			var ansi = GetLocalOnlyAnsiCharacters(encodings);
			foreach (var file in encodings.Keys)
			{
				if (ShouldAlwaysUseUtf8WithBom(file))
				{
					// some files (like *.cs or *.config) should always be saved with BOM and marked as UTF-8 in TFS
					required.Add(file);
				}
				else if (signatures[file])
				{
					// file saved with BOM but not marked as UTF-8 in TFS
					mismatched.Add(file);
				}
				else if (!ansi[file])
				{
					// all other files are forced to be UTF-8 + BOM only if they are using non-ANSI characters
					exceptional.Add(file);
				}
			}

			if (required.Count == 0 && exceptional.Count == 0)
				return;

			var sb = new StringBuilder();

			Func<string, string> format = file =>
			{
				return String.Format(
					"- {0} ({1} BOM, marked as {2})",
					file,
					signatures[file] ? "uses" : "no",
					encodings[file]);
			};

			if (required.Count > 0)
			{
				sb.AppendLine("The following files should always be saved with BOM and marked as UTF-8 (65001) in TFS:");
				foreach (var file in required)
				{
					sb.AppendLine(format(file));
				}
			}

			if (mismatched.Count > 0)
			{
				if (sb.Length > 0)
					sb.AppendLine("                               ");

				sb.AppendLine("The following files are saved with BOM, so they should rather be marked as UTF-8 (65001) in TFS:");
				foreach (var file in mismatched)
				{
					sb.AppendLine(format(file));
				}
			}

			if (exceptional.Count > 0)
			{
				if (sb.Length > 0)
					sb.AppendLine("                               ");

				sb.AppendLine("The following files use non-ANSI characters, so they should rather be saved with BOM and marked as UTF-8 (65001) in TFS:");
				foreach (var file in exceptional)
				{
					sb.AppendLine(format(file));
				}
			}

			throw new FailedCheckException(
				@"{0}
                               
Please update TFS encodings using 'Advanced/Properties...' dialog, and/or save required files using 'Advanced Save Options...' command in Visual Studio.
This should help other team memebers to avoid possible conflicts while working with such files in Visual Studio and other tools.",
				sb.ToString());
		}