Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Commit

Permalink
Add better errors, fix help output text, and add 'dotnet watch --list…
Browse files Browse the repository at this point in the history
…' to help us diagnose issues

Fixes #252 - help output shown twice in dotnet-watch
Fixes #250 - add dotnet-watch --list. Prints a list of all files discovered
Fixes #249 - better error message when GenerateWatchList fails
  • Loading branch information
Nate McMaster committed Jan 13, 2017
1 parent 7ee45af commit 03b2a3c
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 19 deletions.
11 changes: 9 additions & 2 deletions src/Microsoft.DotNet.Watcher.Tools/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal class CommandLineOptions
public bool IsQuiet { get; private set; }
public bool IsVerbose { get; private set; }
public IList<string> RemainingArguments { get; private set; }
public bool ListFiles { get; private set; }

public static CommandLineOptions Parse(string[] args, IConsole console)
{
Expand Down Expand Up @@ -63,6 +64,9 @@ public static CommandLineOptions Parse(string[] args, IConsole console)
CommandOptionType.NoValue);
var optVerbose = app.VerboseOption();

var optList = app.Option("--list", "Lists all files that would be watched without starting the watcher.",
CommandOptionType.NoValue);

app.VersionOptionFromAssemblyAttributes(typeof(Program).GetTypeInfo().Assembly);

if (app.Execute(args) != 0)
Expand All @@ -75,7 +79,9 @@ public static CommandLineOptions Parse(string[] args, IConsole console)
throw new CommandParsingException(app, Resources.Error_QuietAndVerboseSpecified);
}

if (app.RemainingArguments.Count == 0)
if (app.RemainingArguments.Count == 0
&& !app.IsShowingInformation
&& !optList.HasValue())
{
app.ShowHelp();
}
Expand All @@ -86,7 +92,8 @@ public static CommandLineOptions Parse(string[] args, IConsole console)
IsQuiet = optQuiet.HasValue(),
IsVerbose = optVerbose.HasValue(),
RemainingArguments = app.RemainingArguments,
IsHelp = app.IsShowingInformation
IsHelp = app.IsShowingInformation,
ListFiles = optList.HasValue(),
};
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Microsoft.DotNet.Watcher.Tools/DotNetWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public DotNetWatcher(IReporter reporter)
while (true)
{
var fileSet = await fileSetFactory.CreateAsync(cancellationToken);

if (fileSet == null)
{
_reporter.Error("Failed to find a list of files to watch");
return;
}

if (cancellationToken.IsCancellationRequested)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ namespace Microsoft.DotNet.Watcher.Internal
public class MsBuildFileSetFactory : IFileSetFactory
{
private const string TargetName = "GenerateWatchList";
private const string ProjectExtensionFileExtension = ".dotnetwatch.targets";
private const string ProjectExtensionFileExtension = ".dotnetwatch.g.targets";
private const string WatchTargetsFileName = "DotNetWatchCommon.targets";
private readonly IReporter _reporter;
private readonly string _projectFile;
private readonly string _watchTargetsDir;
private readonly OutputSink _outputSink;
private readonly ProcessRunner _processRunner;
private readonly bool _waitOnError;

public MsBuildFileSetFactory(IReporter reporter, string projectFile)
public MsBuildFileSetFactory(IReporter reporter, string projectFile, bool waitOnError)
: this(reporter, projectFile, new OutputSink())
{
_waitOnError = waitOnError;
}

// output sink is for testing
Expand Down Expand Up @@ -86,7 +88,7 @@ public async Task<IFileSet> CreateAsync(CancellationToken cancellationToken)

var exitCode = await _processRunner.RunAsync(processSpec, cancellationToken);

if (exitCode == 0)
if (exitCode == 0 && File.Exists(watchList))
{
var fileset = new FileSet(
File.ReadAllLines(watchList)
Expand All @@ -109,28 +111,41 @@ public async Task<IFileSet> CreateAsync(CancellationToken cancellationToken)

_reporter.Error($"Error(s) finding watch items project file '{Path.GetFileName(_projectFile)}'");

_reporter.Output($"MSBuild output from target '{TargetName}'");
_reporter.Output($"MSBuild output from target '{TargetName}':");
_reporter.Output(string.Empty);

foreach (var line in capture.Lines)
{
_reporter.Output($" [MSBUILD] : {line}");
_reporter.Output($" {line}");
}

_reporter.Warn("Fix the error to continue or press Ctrl+C to exit.");
_reporter.Output(string.Empty);

var fileSet = new FileSet(new[] {_projectFile});

using (var watcher = new FileSetWatcher(fileSet))
if (!_waitOnError)
{
return null;
}
else
{
await watcher.GetChangedFileAsync(cancellationToken);
_reporter.Warn("Fix the error to continue or press Ctrl+C to exit.");

_reporter.Output($"File changed: {_projectFile}");
var fileSet = new FileSet(new[] { _projectFile });

using (var watcher = new FileSetWatcher(fileSet))
{
await watcher.GetChangedFileAsync(cancellationToken);

_reporter.Output($"File changed: {_projectFile}");
}
}
}
}
finally
{
File.Delete(watchList);
if (File.Exists(watchList))
{
File.Delete(watchList);
}
}
}

Expand Down
51 changes: 48 additions & 3 deletions src/Microsoft.DotNet.Watcher.Tools/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ public async Task<int> RunAsync(string[] args)

try
{
return await MainInternalAsync(reporter, options.Project, options.RemainingArguments, ctrlCTokenSource.Token);
if (options.ListFiles)
{
return await ListFilesAsync(reporter,
options.Project,
ctrlCTokenSource.Token);
}
else
{

return await MainInternalAsync(reporter,
options.Project,
options.RemainingArguments,
ctrlCTokenSource.Token);
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -115,8 +128,7 @@ public async Task<int> RunAsync(string[] args)
return 1;
}

var fileSetFactory = new MsBuildFileSetFactory(reporter, projectFile);

var fileSetFactory = new MsBuildFileSetFactory(reporter, projectFile, waitOnError: true);
var processInfo = new ProcessSpec
{
Executable = DotNetMuxer.MuxerPathOrDefault(),
Expand All @@ -130,6 +142,39 @@ public async Task<int> RunAsync(string[] args)
return 0;
}

private async Task<int> ListFilesAsync(
IReporter reporter,
string project,
CancellationToken cancellationToken)
{
// TODO multiple projects should be easy enough to add here
string projectFile;
try
{
projectFile = MsBuildProjectFinder.FindMsBuildProject(_workingDir, project);
}
catch (FileNotFoundException ex)
{
reporter.Error(ex.Message);
return 1;
}

var fileSetFactory = new MsBuildFileSetFactory(reporter, projectFile, waitOnError: false);
var files = await fileSetFactory.CreateAsync(cancellationToken);

if (files == null)
{
return 1;
}

foreach(var file in files)
{
_console.Out.WriteLine(file);
}

return 0;
}

private static IReporter CreateReporter(bool verbose, bool quiet, IConsole console)
{
const string prefix = "watch : ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ private void EnsureProjectExtensionTargetsExist(string projectFile)
using (var stream = new FileStream(projectExtensionsPath, FileMode.Create))
using (var writer = new StreamWriter(stream))
{
writer.WriteLine("<!-- Auto-generated by dotnet-user-secrets. This file can be deleted and should not be commited to source control. -->");
resource.CopyTo(stream);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,15 @@ public async Task ProjectReferences_Graph()
}

private Task<IFileSet> GetFileSet(TemporaryCSharpProject target)
=> GetFileSet(new MsBuildFileSetFactory(_reporter, target.Path));
=> GetFileSet(new MsBuildFileSetFactory(_reporter, target.Path, waitOnError: false));
private async Task<IFileSet> GetFileSet(MsBuildFileSetFactory filesetFactory)
{
_tempDir.Create();
var createTask = filesetFactory.CreateAsync(CancellationToken.None);
var finished = await Task.WhenAny(createTask, Task.Delay(TimeSpan.FromSeconds(10)));

Assert.Same(createTask, finished);
Assert.NotNull(createTask.Result);
return createTask.Result;
}

Expand Down

0 comments on commit 03b2a3c

Please sign in to comment.