Fixed some small usage issues

- Simplify waiting for changes
- Fixed project dependencies
This commit is contained in:
David Fowler 2015-09-27 22:31:20 -07:00
parent 3f40980d02
commit 22d13c4b34
3 changed files with 34 additions and 45 deletions

View File

@ -182,41 +182,36 @@ namespace Microsoft.Dnx.Watcher.Core
foreach (var projFile in project.ProjectDependencies)
{
//var fullProjectFilePath = Path.Combine(Path.GetDirectoryName(project.ProjectFile), projFile);
AddProjectAndDependeciesToWatcher(projFile, fileWatcher);
}
}
private Task<string> WatchForFileChangeAsync(IFileWatcher fileWatcher, CancellationToken cancellationToken)
private async Task<string> WatchForFileChangeAsync(IFileWatcher fileWatcher, CancellationToken cancellationToken)
{
return Task.Run(() =>
var tcs = new TaskCompletionSource<string>();
cancellationToken.Register(() => tcs.TrySetResult(null));
Action<string> callback = path =>
{
using (var fileChangeEvent = new ManualResetEvent(false))
{
string changedFile = null;
tcs.TrySetResult(path);
};
fileWatcher.OnChanged += path =>
{
changedFile = path;
fileChangeEvent.Set();
};
fileWatcher.OnChanged += callback;
while (!cancellationToken.IsCancellationRequested &&
!fileChangeEvent.WaitOne(500))
{
}
var changedPath = await tcs.Task;
return changedFile;
}
});
// Don't need to listen anymore
fileWatcher.OnChanged -= callback;
return changedPath;
}
public static DnxWatcher CreateDefault(ILoggerFactory loggerFactory)
{
return new DnxWatcher(
fileWatcherFactory: root => { return new FileWatcher(root); },
processWatcherFactory: () => { return new ProcessWatcher(); },
fileWatcherFactory: root => new FileWatcher(root),
processWatcherFactory: () => new ProcessWatcher(),
projectProvider: new ProjectProvider(),
loggerFactory: loggerFactory);
}

View File

@ -37,33 +37,19 @@ namespace Microsoft.Dnx.Watcher.Core
return _runningProcess.Id;
}
public async Task<int> WaitForExitAsync(CancellationToken cancellationToken)
public Task<int> WaitForExitAsync(CancellationToken cancellationToken)
{
try
cancellationToken.Register(() => _runningProcess?.Kill());
return Task.Run(() =>
{
await Task.Run(() =>
{
while (!cancellationToken.IsCancellationRequested)
{
if (_runningProcess.WaitForExit(500))
{
break;
}
}
_runningProcess.WaitForExit();
if (!_runningProcess.HasExited)
{
_runningProcess.Kill();
}
});
return _runningProcess.ExitCode;
}
finally
{
var exitCode = _runningProcess.ExitCode;
_runningProcess = null;
}
return exitCode;
});
}
private static void RemoveCompilationPortEnvironmentVariable(ProcessStartInfo procStartInfo)

View File

@ -14,6 +14,7 @@ namespace Microsoft.Dnx.Watcher.Core
public Project(Runtime.Project runtimeProject)
{
ProjectFile = runtimeProject.ProjectFilePath;
ProjectDirectory = runtimeProject.ProjectDirectory;
Files = runtimeProject.Files.SourceFiles.Concat(
runtimeProject.Files.ResourceFiles.Values.Concat(
@ -28,7 +29,7 @@ namespace Microsoft.Dnx.Watcher.Core
if (File.Exists(projectLockJsonPath))
{
var lockFile = lockFileReader.Read(projectLockJsonPath);
ProjectDependencies = lockFile.ProjectLibraries.Select(dep => dep.Path).ToList();
ProjectDependencies = lockFile.ProjectLibraries.Select(dep => GetProjectRelativeFullPath(dep.Path)).ToList();
}
else
{
@ -41,5 +42,12 @@ namespace Microsoft.Dnx.Watcher.Core
public IEnumerable<string> Files { get; private set; }
public string ProjectFile { get; private set; }
public string ProjectDirectory { get; private set; }
private string GetProjectRelativeFullPath(string path)
{
return Path.GetFullPath(Path.Combine(ProjectDirectory, path));
}
}
}