Skip to content

Instantly share code, notes, and snippets.

@cwigley
Created August 17, 2017 13:58
Show Gist options
  • Save cwigley/52135671eed828ee90b08a3020a4367a to your computer and use it in GitHub Desktop.
Save cwigley/52135671eed828ee90b08a3020a4367a to your computer and use it in GitHub Desktop.
Can copy visual studio warnings to a file and generate the xml for whitelisting them in the config.nsdepcop. Much better ways to do this, just a quick and dirty solution when setting up some really large projects.
void Main()
{
WarningParser parser = new WarningParser();
var lines = parser.GetRows(@"C:\Users\cwigley\Downloads\warnings.txt");
var allowed = parser.GetAllowed(lines).OrderBy(p => p.From).ThenBy(p => p.To).ThenBy(p => p.VisibleMembers.Types[0].Name).ToList();
var reconciled = parser.Reconcile(allowed);
AllowedSerializer serializer = new AllowedSerializer();
var result = serializer.GetEntries(reconciled);
result.Dump();
File.WriteAllText(@"c:\users\cwigley\downloads\nsconfig.txt",result);
}
public class AllowedSerializer
{
public string GetEntries(List<Allowed> reconciled)
{
StringBuilder builder = new StringBuilder();
foreach (var item in reconciled)
{
builder.AppendLine($"<Allowed From=\"{item.From}\" To=\"{item.To}\" >");
builder.AppendLine("\t<VisibleMembers>");
foreach (var type in item.VisibleMembers.Types)
{
builder.AppendLine($"\t\t<Type Name=\"{type.Name}\" />");
}
builder.AppendLine("\t</VisibleMembers>");
builder.AppendLine("</Allowed>");
}
return builder.ToString();
}
}
public class WarningParser
{
public List<string> GetRows(string raw)
{
return File.ReadAllLines(raw).ToList();
}
public List<Allowed> GetAllowed(List<string> rows)
{
List<Allowed> result = new List<UserQuery.Allowed>();
foreach (var row in rows)
{
result.Add(GetAllowed(row));
}
return result;
}
public Allowed GetAllowed(string row)
{
//Warning Illegal namespace reference: From.Namespace->To.Namespace (Type: FromType->ToType)
Regex regex = new Regex(@"^[A-Za-z\s]*: (\b[A-Za-z0-9\.]*\b)->(\b[A-Za-z0-9\.]*\b).*->([A-Za-z0-9`]*)");
Match match = regex.Match(row);
var result = new Allowed { From = match.Groups[1].Value, To = match.Groups[2].Value };
var member = new VisibleMember();
member.Types.Add(new VisibleType { Name=match.Groups[3].Value});
result.VisibleMembers = member;
if (!result.IsValid())
{
throw new InvalidOperationException(row);
}
return result;
}
public List<Allowed> Reconcile(List<Allowed> raw)
{
List<Allowed> result = new List<Allowed> ();
Dictionary<string,Allowed> allowedSections = new Dictionary<string, Allowed>();
foreach (var item in raw)
{
string key = $"{item.From}-{item.To}";
if (!allowedSections.ContainsKey(key))
{
allowedSections.Add(key,item);
}
}
foreach (var item in raw)
{
string key = $"{item.From}-{item.To}";
if (allowedSections[key].VisibleMembers.Types.Any(t => t.Name.Equals(item.VisibleMembers.Types[0].Name)))
{
continue;
}
allowedSections[key].VisibleMembers.Types.Add(item.VisibleMembers.Types[0]);
}
return allowedSections.Select(s => s.Value).ToList();
}
}
public class Allowed
{
public string From { get; set; }
public string To { get; set; }
public VisibleMember VisibleMembers { get; set;}
public bool IsValid()
{
return !string.IsNullOrWhiteSpace(From) && !string.IsNullOrWhiteSpace(To);
}
}
public class VisibleMember
{
public List<VisibleType> Types { get; set; }
public VisibleMember()
{
Types=new List<VisibleType>();
}
}
public class VisibleType
{
public string Name { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment