Skip to content

Commit

Permalink
fixes and added code cleanup for formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
dave-yotta committed Nov 5, 2020
1 parent a2b5130 commit 7eaf6fd
Show file tree
Hide file tree
Showing 10 changed files with 394 additions and 48 deletions.
12 changes: 12 additions & 0 deletions CodeMonitor/Models/CleanupCodeFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace CodeMonitor
{
public class CleanupCodeFile
{
public CleanupCodeFile(string path)
{
Path = path;
}

public string Path {get;}
}
}
187 changes: 187 additions & 0 deletions CodeMonitor/Models/CleanupCodeWatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.Text;
using System.Reactive.Subjects;
using System.Reactive.Linq;
using System.Reactive.Concurrency;
using DynamicData;

namespace CodeMonitor
{
public class CleanupCodeWatcher
{
public CleanupCodeWatcher(string slnPath)
{
Watch = Path.GetDirectoryName(slnPath);
Sln = Path.GetFileName(slnPath);
}

private readonly SourceList<CleanupCodeFile> results = new SourceList<CleanupCodeFile>();
public IObservableList<CleanupCodeFile> ToClean => results;

private readonly ReplaySubject<bool> active = new ReplaySubject<bool>(1);
public IObservable<bool> Active => active;

private readonly ReplaySubject<string> status = new ReplaySubject<string>();
public IObservable<string> Status => status;

private string Watch { get; }
private string Sln { get; }

private readonly object _mutex = new object();
private readonly HashSet<string> _changed = new HashSet<string>();

private void Handle(string s)
{
if (ShouldHandle(s))
{
lock (_mutex)
{
var path = Path.GetRelativePath(Watch, s).Replace('\\', '/');
if (_changed.Add(path))
{
results.Add(new CleanupCodeFile(path));
}
}
}
}

private bool ShouldHandle(string s)
{
if (s.StartsWith(Path.Combine(Watch, ".git")) ||
s.StartsWith(Path.Combine(Watch, ".vs")) ||
s.EndsWith("~") ||
s.EndsWith(".csproj") ||
s.EndsWith(".TMP")
) return false;

if (Directory.Exists(s))
{
return false;
}

var gitPath = new Uri(Watch).MakeRelativeUri(new Uri(s)).OriginalString;
var proc = Process.Start(new ProcessStartInfo
{
WorkingDirectory = Watch,
FileName = "git",
Arguments = "check-ignore " + gitPath,
RedirectStandardOutput = true,
CreateNoWindow = true
});
var result = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();

if (result != "") return false;

return true;
}

public void ResetChanged()
{
status.OnNext("Reseting cleanup tally against origin/dev");

var proc = Process.Start(new ProcessStartInfo
{
WorkingDirectory = Watch,
FileName = "git",
Arguments = "diff origin/dev --name-only",
RedirectStandardOutput = true,
CreateNoWindow = true
});
var result = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();

lock (_mutex)
{
results.Clear();
_changed.Clear();

var files = result.Split("\n", StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim('\r'));

_changed.UnionWith(files);
results.AddRange(files.Select(x => new CleanupCodeFile(x)));
}
}

public void CleanupFiles()
{
var psi = new ProcessStartInfo
{
FileName = @"c:\bin\cleanupcode.exe",
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
};

var config = $@"<?xml version=""1.0"" encoding=""utf-8""?>
<CleanupCodeOptions xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<Debug>false</Debug>
<Verbosity>INFO</Verbosity>
<SolutionFile>{Path.Combine(Watch, Sln)}</SolutionFile>
<CustomSettingFile>{Path.Combine(Watch, Sln + ".DotSettings")}</CustomSettingFile>
<DisabledSettingsLayers />
<SuppressBuildInSettings>false</SuppressBuildInSettings>
<Properties />
<TargetsForItems />
<Extensions />
<CleanupProfileName>Built-in: Reformat Code</CleanupProfileName>
<CleanupScopeInclude>{string.Join(";", _changed)}</CleanupScopeInclude>
</CleanupCodeOptions>";

File.WriteAllText("cleanupcode.config", config);

psi.ArgumentList.Add("--config=cleanupcode.config");

status.OnNext("Starting cleanup");
var proc = Process.Start(psi);
var opt = new StringBuilder();
proc.OutputDataReceived += (o, e) => { opt.AppendLine(e.Data); status.OnNext(e.Data); };
proc.ErrorDataReceived += (o, e) => { opt.AppendLine(e.Data); status.OnNext(e.Data); };
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
if (proc.ExitCode != 0)
{
throw new Exception("CleanupCode was sad");
}

lock(_mutex)
{
_changed.Clear();
results.Clear();
}
}

private FileSystemWatcher w;

internal void Stop()
{
w.Dispose();
}

public void Start()
{
// watch

w = new FileSystemWatcher(Watch)
{
IncludeSubdirectories = true,
EnableRaisingEvents = true,
NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size,
Filter = "*.*"
};

w.Changed += (o, e) => Handle(e.FullPath);
w.Created += (o, e) => Handle(e.FullPath);
w.Renamed += (o, e) => Handle(e.FullPath);

ResetChanged();
}

}
}
56 changes: 39 additions & 17 deletions CodeMonitor/Models/InspectCodeLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public InspectCodeLoop(string slnPath)
}

private readonly SourceCache<InspectCodeFileProblems, string> results = new SourceCache<InspectCodeFileProblems, string>(x => x.File);
public IObservable<IChangeSet<InspectCodeFileProblems,string>> Problems => results.Connect();
public IObservableCache<InspectCodeFileProblems, string> Problems => results;

private readonly ReplaySubject<bool> active = new ReplaySubject<bool>(1);
public IObservable<bool> Active => active;

private readonly ReplaySubject<string> status = new ReplaySubject<string>();
public IObservable<string> Status => status;
Expand Down Expand Up @@ -65,7 +68,7 @@ private bool ShouldHandle(string s)
FileName = "git",
Arguments = "check-ignore " + gitPath,
RedirectStandardOutput = true,
CreateNoWindow=true
CreateNoWindow = true
});
var result = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
Expand All @@ -82,7 +85,8 @@ private void ExamineFiles(ICollection<string> filePaths)
var psi = new ProcessStartInfo
{
FileName = @"c:\bin\inspectcode.exe",
CreateNoWindow = true
CreateNoWindow = true,
RedirectStandardOutput = true
};

var args = new List<string>
Expand All @@ -99,12 +103,19 @@ private void ExamineFiles(ICollection<string> filePaths)
args.Add($"--profile={tryProfile}");
}

args.AddRange(filePaths.Select(x => $"--input={x}"));
//TODO: Use a config file
if(filePaths.Count < 100)
{
args.AddRange(filePaths.Select(x => $"--input={x}"));
}

args.Add($"{Path.Combine(Watch, Sln)}");

args.ForEach(psi.ArgumentList.Add);

var proc = Process.Start(psi);
proc.OutputDataReceived += (o,e) => status.OnNext(e.Data);
proc.BeginOutputReadLine();
proc.WaitForExit();
var xml = File.ReadAllText(outFile);

Expand All @@ -127,7 +138,7 @@ private void ExamineFiles(ICollection<string> filePaths)

results.RemoveKeys(kees);

foreach(var x in problems)
foreach (var x in problems)
{
_examined.Add(x.Key);
results.AddOrUpdate(new InspectCodeFileProblems(x.Key, x.Value.ToList()));
Expand All @@ -139,13 +150,14 @@ private void ExamineFiles(ICollection<string> filePaths)
internal void Stop()
{
subscription.Dispose();
w.Dispose();
}

public void Start()
{
// watch

var w = new FileSystemWatcher(Watch)
w = new FileSystemWatcher(Watch)
{
IncludeSubdirectories = true,
EnableRaisingEvents = true,
Expand All @@ -163,18 +175,21 @@ public void Start()
.Subscribe(Loop);
}

private bool initial = true;

private bool initial = true;
private FileSystemWatcher w;

private void Loop(long _)
{
// initial
if (initial)
{
active.OnNext(true);
ExamineFiles(new List<string>());
initial = false;
active.OnNext(false);
return;
}
}

lock (_mutex)
{
var gone = _examined.Select(key => (key, full: Path.Combine(Watch, key)))
Expand All @@ -200,18 +215,25 @@ private void Loop(long _)
}
changeNote.AppendLine("Analyzing...");

status.OnNext(changeNote.ToString());

foreach(var g in gone)
foreach (var g in gone)
{
results.Remove(g);
}

ExamineFiles(_changed);

status.OnNext("Idle");

_changed.Clear();
active.OnNext(true);
status.OnNext(changeNote.ToString());
try
{
ExamineFiles(_changed);
_changed.Clear();
}
finally
{

status.OnNext("Idle");
active.OnNext(false);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion CodeMonitor/Models/InspectCodeProblem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public InspectCodeProblem(string message, int line, string type)
{
Message = message;
Line = line;
Type = type;
Type = type;
}

public string Message { get; }
Expand Down
24 changes: 24 additions & 0 deletions CodeMonitor/ViewModels/FileToCleanViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using DynamicData;
using ReactiveUI;
using SharpDX.Direct2D1;

#warning subscriptions need disposing
namespace CodeMonitor.ViewModels
{
public class FileToCleanViewModel
{
public FileToCleanViewModel(string path)
{
Path = path;
}

public string Path {get;}
}
}
Loading

0 comments on commit 7eaf6fd

Please sign in to comment.