From 94d7dc7e61993996d99a680487c13ea3ceb525e9 Mon Sep 17 00:00:00 2001 From: Ben Taylor Date: Fri, 15 Jan 2016 21:21:45 +0000 Subject: [PATCH] WIP Function to remove Compile elems missing files As well as keeping project files in sync, I also need to be able to remove compile nodes from a project, where the Include attribute points to a file that is missing. This is useful when I have two projects including the same files, and I delete a bunch of them from the 'master' project I happen to be working on. This PR adds two functions. RemoveCompileNodesWithMissingFiles is intended for build script use and removeCompileNodesWithMissingFiles exists for testing purposes, because unlike the former, it does not write to the file system. I noticed this convention elsewhere in FAKE, but I'm not sure if it is considered idiomatic. Let me know. --- src/app/FakeLib/MSBuild/ProjectSystem.fs | 20 +++++- src/test/Test.FAKECore/ProjectSystemSpecs.cs | 28 ++++++++- .../ProjectTestFiles/CSharpApp.csproj | 62 +++++++++++++++++++ src/test/Test.FAKECore/Test.FAKECore.csproj | 3 + 4 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/test/Test.FAKECore/ProjectTestFiles/CSharpApp.csproj diff --git a/src/app/FakeLib/MSBuild/ProjectSystem.fs b/src/app/FakeLib/MSBuild/ProjectSystem.fs index 9cc2e312f78..1646e2f9191 100644 --- a/src/app/FakeLib/MSBuild/ProjectSystem.fs +++ b/src/app/FakeLib/MSBuild/ProjectSystem.fs @@ -159,4 +159,22 @@ let CompareProjectsTo templateProject projects = |> toLines if isNotNullOrEmpty errors then - failwith errors \ No newline at end of file + failwith errors + +let removeCompileNodesWithMissingFiles includeExistsF (project:ProjectFile) = + let projectDir = System.IO.Path.GetDirectoryName(project.ProjectFileName) + let missingFiles = + seq { for filePath in project.Files do + let includePath = System.IO.Path.Combine([|projectDir; filePath|]) + if not (includeExistsF(includePath)) then yield filePath } + missingFiles + |> Seq.fold (fun (project:ProjectFile) file -> project.RemoveFile(file)) project + +/// Removes projects Compile nodes that have Include attributes pointing to files missing from the file system. Saves updated projects. +let RemoveCompileNodesWithMissingFiles project = + let newProject = removeCompileNodesWithMissingFiles System.IO.File.Exists (ProjectFile.FromFile project) + newProject.Save() + + + + diff --git a/src/test/Test.FAKECore/ProjectSystemSpecs.cs b/src/test/Test.FAKECore/ProjectSystemSpecs.cs index b078bd7d463..9e22be6d485 100644 --- a/src/test/Test.FAKECore/ProjectSystemSpecs.cs +++ b/src/test/Test.FAKECore/ProjectSystemSpecs.cs @@ -1,4 +1,7 @@ -using Fake.MSBuild; +using System; +using System.Collections.Generic; +using System.IO; +using Fake.MSBuild; using Machine.Specifications; namespace Test.FAKECore @@ -76,4 +79,27 @@ public class when_removing_duplicate_files _project.FindDuplicateFiles().ShouldContain("Git\\CommitMessage.fs"); } + + public class when_removing_compile_nodes_with_missing_files + { + const string ProjectFilePath = @"ProjectTestFiles/CSharpApp.csproj"; + private static ProjectSystem.ProjectFile _project; + + private Because of = () => + { + Func fileExists = s => + { + var pathsToRemove = new List() {Path.Combine("ProjectTestFiles", "Class1.cs"), Path.Combine("ProjectTestFiles", "Folder", "FolderFile2.cs")}; + return !pathsToRemove.Exists(pathToRemove => s.Equals(pathToRemove, StringComparison.InvariantCulture)); + }; + var projectFile = ProjectSystem.ProjectFile.FromFile(ProjectFilePath); + _project = ProjectSystem.removeCompileNodesWithMissingFiles(fileExists.Convert(), projectFile); + }; + + It should_delete_missing_files_in_csharpapp = () => + { + _project.Files.ShouldNotContain(new []{"Class1.cs", @"Folder\FolderFile2.cs"}); + _project.Files.ShouldContain(new [] {@"Folder\FolderFile1.cs", @"Program.cs", @"Properties\AssemblyInfo.cs"}); + }; + } } \ No newline at end of file diff --git a/src/test/Test.FAKECore/ProjectTestFiles/CSharpApp.csproj b/src/test/Test.FAKECore/ProjectTestFiles/CSharpApp.csproj new file mode 100644 index 00000000000..3a00b8119a3 --- /dev/null +++ b/src/test/Test.FAKECore/ProjectTestFiles/CSharpApp.csproj @@ -0,0 +1,62 @@ + + + + + Debug + AnyCPU + {5C1A3730-057D-4C82-AF8B-32135611CB4B} + Exe + Properties + ConsoleApplication2 + ConsoleApplication2 + v4.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/Test.FAKECore/Test.FAKECore.csproj b/src/test/Test.FAKECore/Test.FAKECore.csproj index 335ed661cd9..c6ba205bb58 100644 --- a/src/test/Test.FAKECore/Test.FAKECore.csproj +++ b/src/test/Test.FAKECore/Test.FAKECore.csproj @@ -203,6 +203,9 @@ Always + + Always + Always