From 2cf772b575d3da4a3dd4282dabf8bff48c3067d3 Mon Sep 17 00:00:00 2001 From: Victor Hurdugaci Date: Wed, 25 May 2016 15:15:28 -0700 Subject: [PATCH] - Bugfix: Trigger on new project.json file structure - Bugfix: Trigger on folder deletion - Add test for folder deletion --- .../IncludeContextExtensions.cs | 25 +++++++++++++ .../Internal/Implementation/Project.cs | 35 +++++++++++++++---- .../Internal/ProjectWatcher.cs | 4 +++ .../GlobbingAppTests.cs | 18 ++++++++++ test/TestApps/GlobbingApp/project.json | 18 +++++----- 5 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 src/Microsoft.DotNet.Watcher.Core/IncludeContextExtensions.cs diff --git a/src/Microsoft.DotNet.Watcher.Core/IncludeContextExtensions.cs b/src/Microsoft.DotNet.Watcher.Core/IncludeContextExtensions.cs new file mode 100644 index 0000000000..2f422c777d --- /dev/null +++ b/src/Microsoft.DotNet.Watcher.Core/IncludeContextExtensions.cs @@ -0,0 +1,25 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.DotNet.ProjectModel.Files; + +namespace Microsoft.DotNet.Watcher.Core +{ + internal static class IncludeContextExtensions + { + public static IEnumerable ResolveFiles(this IncludeContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + return IncludeFilesResolver + .GetIncludeFiles(context, "/", diagnostics: null) + .Select(f => f.SourcePath); + } + } +} diff --git a/src/Microsoft.DotNet.Watcher.Core/Internal/Implementation/Project.cs b/src/Microsoft.DotNet.Watcher.Core/Internal/Implementation/Project.cs index 4d5c2c31db..d8175329b3 100644 --- a/src/Microsoft.DotNet.Watcher.Core/Internal/Implementation/Project.cs +++ b/src/Microsoft.DotNet.Watcher.Core/Internal/Implementation/Project.cs @@ -1,9 +1,11 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using System.IO; using System.Linq; +using Microsoft.DotNet.ProjectModel.Files; using Microsoft.DotNet.ProjectModel.Graph; namespace Microsoft.DotNet.Watcher.Core.Internal @@ -15,15 +17,34 @@ namespace Microsoft.DotNet.Watcher.Core.Internal ProjectFile = runtimeProject.ProjectFilePath; ProjectDirectory = runtimeProject.ProjectDirectory; - Files = runtimeProject.Files.SourceFiles.Concat( - runtimeProject.Files.ResourceFiles.Values.Concat( - runtimeProject.Files.PreprocessSourceFiles.Concat( - runtimeProject.Files.SharedFiles))).Concat( - new string[] { runtimeProject.ProjectFilePath }) - .ToList(); + var compilerOptions = runtimeProject.GetCompilerOptions(targetFramework: null, configurationName: null); + + var filesToWatch = new List() { runtimeProject.ProjectFilePath }; + if (compilerOptions?.CompileInclude != null) + { + filesToWatch.AddRange(compilerOptions.CompileInclude.ResolveFiles()); + } + else + { + filesToWatch.AddRange(runtimeProject.Files.SourceFiles); + } + + if (compilerOptions?.EmbedInclude != null) + { + filesToWatch.AddRange(compilerOptions.EmbedInclude.ResolveFiles()); + } + else + { + filesToWatch.AddRange(runtimeProject.Files.ResourceFiles.Values); + } + + filesToWatch.AddRange(runtimeProject.Files.SharedFiles); + filesToWatch.AddRange(runtimeProject.Files.PreprocessSourceFiles); + + Files = filesToWatch; var projectLockJsonPath = Path.Combine(runtimeProject.ProjectDirectory, "project.lock.json"); - + if (File.Exists(projectLockJsonPath)) { var lockFile = LockFileReader.Read(projectLockJsonPath, designTime: false); diff --git a/src/Microsoft.DotNet.Watcher.Core/Internal/ProjectWatcher.cs b/src/Microsoft.DotNet.Watcher.Core/Internal/ProjectWatcher.cs index e94afb7dd4..b3438186d4 100644 --- a/src/Microsoft.DotNet.Watcher.Core/Internal/ProjectWatcher.cs +++ b/src/Microsoft.DotNet.Watcher.Core/Internal/ProjectWatcher.cs @@ -107,6 +107,10 @@ namespace Microsoft.DotNet.Watcher.Core.Internal foreach (var file in project.Files) { closure.Add(file); + + // We need to add the folder because folder deletions only trigger + // for the folder, not for the files inside it + closure.Add(Path.GetDirectoryName(file)); } foreach (var dependency in project.ProjectDependencies) diff --git a/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/GlobbingAppTests.cs b/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/GlobbingAppTests.cs index 0a8f48908d..aa730877de 100644 --- a/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/GlobbingAppTests.cs +++ b/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/GlobbingAppTests.cs @@ -85,6 +85,24 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests } } + // Delete an entire folder + [Fact] + public void DeleteSourceFolder() + { + using (var scenario = new GlobbingAppScenario()) + using (var wait = new WaitForFileToChange(scenario.StartedFile)) + { + scenario.Start(); + + var folderToDelete = Path.Combine(scenario.TestAppFolder, "include"); + Directory.Delete(folderToDelete, recursive: true); + + wait.Wait(_defaultTimeout, + expectedToChange: true, + errorMessage: $"Process did not restart because {scenario.StartedFile} was not changed"); + } + } + // Rename a file included in compilation [Fact] public void RenameCompiledFile() diff --git a/test/TestApps/GlobbingApp/project.json b/test/TestApps/GlobbingApp/project.json index fd675dbad6..68104394d3 100644 --- a/test/TestApps/GlobbingApp/project.json +++ b/test/TestApps/GlobbingApp/project.json @@ -1,15 +1,17 @@ { "version": "1.0.0-*", "buildOptions": { - "emitEntryPoint": true + "emitEntryPoint": true, + "compile": { + "include": [ + "Program.cs", + "include/*.cs" + ], + "exclude": [ + "exclude/*" + ] + } }, - "compile": [ - "Program.cs", - "include/*.cs" - ], - "exclude": [ - "exclude/*" - ], "frameworks": { "netcoreapp1.0": { "dependencies": {