From 26d399109287b8deceb9c82e6a8cd15507a67544 Mon Sep 17 00:00:00 2001 From: Martin Scholz Date: Tue, 2 Aug 2016 17:34:56 +0200 Subject: [PATCH 01/11] keep folder structure for pattern overload --- src/Cake.Common/IO/FileAliases.cs | 20 ++++++ src/Cake.Common/IO/FileCopier.cs | 104 +++++++++++++++++++++++++++++- 2 files changed, 123 insertions(+), 1 deletion(-) diff --git a/src/Cake.Common/IO/FileAliases.cs b/src/Cake.Common/IO/FileAliases.cs index 72cf58163d..29248acbb1 100644 --- a/src/Cake.Common/IO/FileAliases.cs +++ b/src/Cake.Common/IO/FileAliases.cs @@ -104,6 +104,26 @@ public static void CopyFiles(this ICakeContext context, string pattern, Director FileCopier.CopyFiles(context, pattern, targetDirectoryPath); } + /// + /// Copies existing files to a new location. + /// + /// The context. + /// The pattern. + /// The target directory path. + /// Keep the folder structure. + /// + /// + /// var files = GetFiles("./**/Cake.*"); + /// CopyFiles(files, "destination"); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Copy")] + public static void CopyFiles(this ICakeContext context, string pattern, DirectoryPath targetDirectoryPath, bool preserveFolderStructure) + { + FileCopier.CopyFiles(context, pattern, targetDirectoryPath, preserveFolderStructure); + } + /// /// Copies existing files to a new location. /// diff --git a/src/Cake.Common/IO/FileCopier.cs b/src/Cake.Common/IO/FileCopier.cs index 648340a008..ebb8d9c2ce 100644 --- a/src/Cake.Common/IO/FileCopier.cs +++ b/src/Cake.Common/IO/FileCopier.cs @@ -9,6 +9,7 @@ using Cake.Core; using Cake.Core.Diagnostics; using Cake.Core.IO; +using System.Linq; namespace Cake.Common.IO { @@ -78,6 +79,26 @@ public static void CopyFiles(ICakeContext context, string pattern, DirectoryPath CopyFiles(context, files, targetDirectoryPath); } + public static void CopyFiles(ICakeContext context, string pattern, DirectoryPath targetDirectoryPath, bool preserverFolderStructure) + { + if (context == null) + { + throw new ArgumentNullException("context"); + } + if (pattern == null) + { + throw new ArgumentNullException("pattern"); + } + var files = context.GetFiles(pattern); + + if (files.Count == 0) + { + context.Log.Verbose("The provided pattern did not match any files."); + return; + } + CopyFiles(context, files, targetDirectoryPath, preserverFolderStructure); + } + public static void CopyFiles(ICakeContext context, IEnumerable filePaths, DirectoryPath targetDirectoryPath) { if (context == null) @@ -110,6 +131,57 @@ public static void CopyFiles(ICakeContext context, IEnumerable filePat } } + public static void CopyFiles(ICakeContext context, IEnumerable filePaths, DirectoryPath targetDirectoryPath, bool preserverFolderStructure) + { + if (context == null) + { + throw new ArgumentNullException("context"); + } + if (filePaths == null) + { + throw new ArgumentNullException("filePaths"); + } + if (targetDirectoryPath == null) + { + throw new ArgumentNullException("targetDirectoryPath"); + } + + var absoluteTargetDirectoryPath = targetDirectoryPath.MakeAbsolute(context.Environment); + + // Make sure the target directory exist. + if (!context.FileSystem.Exist(absoluteTargetDirectoryPath)) + { + const string format = "The directory '{0}' do not exist."; + var message = string.Format(CultureInfo.InvariantCulture, format, absoluteTargetDirectoryPath.FullPath); + throw new DirectoryNotFoundException(message); + } + + if (preserverFolderStructure) + { + var matches = from len in Enumerable.Range(0, filePaths.Min(s => s.ToString().Length)).Reverse() //http://stackoverflow.com/a/8578268/2845744 + let possibleMatch = filePaths.First().ToString().Substring(0, len) + where filePaths.All(f => f.ToString().StartsWith(possibleMatch)) + select possibleMatch; + + var commonPath = System.IO.Path.GetDirectoryName(matches.First()); + + // Iterate all files and copy them. + foreach (var filePath in filePaths) + { + CopyFileCore(context, filePath, absoluteTargetDirectoryPath.GetFilePath(filePath), commonPath, targetDirectoryPath.ToString()); + } + } + else + { + // Iterate all files and copy them. + foreach (var filePath in filePaths) + { + CopyFileCore(context, filePath, absoluteTargetDirectoryPath.GetFilePath(filePath)); + } + } + + } + private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePath targetFilePath) { var absoluteFilePath = filePath.MakeAbsolute(context.Environment); @@ -128,5 +200,35 @@ private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePa var file = context.FileSystem.GetFile(absoluteFilePath); file.Copy(absoluteTargetPath, true); } + + private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePath targetFilePath, string commonPath, string targetDirectoryPath) + { + var absoluteFilePath = filePath.MakeAbsolute(context.Environment); + + // Get the file. + if (!context.FileSystem.Exist(absoluteFilePath)) + { + const string format = "The file '{0}' do not exist."; + var message = string.Format(CultureInfo.InvariantCulture, format, absoluteFilePath.FullPath); + throw new FileNotFoundException(message, absoluteFilePath.FullPath); + } + + // Copy the file. + var absoluteTargetPath = targetFilePath.MakeAbsolute(context.Environment); + var file = context.FileSystem.GetFile(absoluteFilePath); + + //Get the parent folder structure and create it. + var newRelativeFolderPath = filePath.GetDirectory().ToString().Replace(commonPath.Replace("\\", "/"), ""); + var newTargetPath = absoluteTargetPath.GetDirectory() + newRelativeFolderPath; + var newAbsoluteTargetPath = newTargetPath + @"/" + filePath.GetFilename(); + context.Log.Verbose("Copying file {0} to {1}", absoluteFilePath.GetFilename(), newAbsoluteTargetPath); + + if (!Directory.Exists(newTargetPath)) + { + Directory.CreateDirectory(newTargetPath); + } + + file.Copy(newAbsoluteTargetPath, true); + } } -} \ No newline at end of file +} From cc6bea457eeaacbb3ed9840c0699191389ccf018 Mon Sep 17 00:00:00 2001 From: Martin Scholz Date: Wed, 10 Aug 2016 13:34:42 +0200 Subject: [PATCH 02/11] add unit test yet it is not possible to test behavior without to change fixture --- src/Cake.Common.Tests/Unit/IO/FileAliasesTests.cs | 14 ++++++++++++++ src/Cake.Common/IO/FileCopier.cs | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Cake.Common.Tests/Unit/IO/FileAliasesTests.cs b/src/Cake.Common.Tests/Unit/IO/FileAliasesTests.cs index c4400ae7e2..b5e1f53a08 100644 --- a/src/Cake.Common.Tests/Unit/IO/FileAliasesTests.cs +++ b/src/Cake.Common.Tests/Unit/IO/FileAliasesTests.cs @@ -492,6 +492,20 @@ public void Should_Throw_If_Any_File_Do_Not_Exist() Assert.Equal("The file '/Working/file2.txt' do not exist.", result.Message); } + [Fact] + public void Should_Keep_Folder_Structure() + { + // Given + var fixture = new FileCopyFixture(); + + // When + FileAliases.CopyFiles(fixture.Context, "*", "./target", true); + + // Then + fixture.TargetFiles[0].Received(1).Copy(Arg.Any(), true); + fixture.TargetFiles[1].Received(1).Copy(Arg.Any(), true); + } + [Fact] public void Should_Copy_Files() { diff --git a/src/Cake.Common/IO/FileCopier.cs b/src/Cake.Common/IO/FileCopier.cs index ebb8d9c2ce..4355d5154e 100644 --- a/src/Cake.Common/IO/FileCopier.cs +++ b/src/Cake.Common/IO/FileCopier.cs @@ -214,7 +214,7 @@ private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePa } // Copy the file. - var absoluteTargetPath = targetFilePath.MakeAbsolute(context.Environment); + var absoluteTargetPath = targetFilePath.MakeAbsolute(context.Environment); var file = context.FileSystem.GetFile(absoluteFilePath); //Get the parent folder structure and create it. From 68ee6933737102fcbfa2126a834ea849222f1611 Mon Sep 17 00:00:00 2001 From: Martin Scholz Date: Wed, 10 Aug 2016 14:06:17 +0200 Subject: [PATCH 03/11] change overloads for default value --- src/Cake.Common/IO/FileAliases.cs | 33 +++++--------------- src/Cake.Common/IO/FileCopier.cs | 51 ------------------------------- 2 files changed, 8 insertions(+), 76 deletions(-) diff --git a/src/Cake.Common/IO/FileAliases.cs b/src/Cake.Common/IO/FileAliases.cs index 29248acbb1..5fc423fbc1 100644 --- a/src/Cake.Common/IO/FileAliases.cs +++ b/src/Cake.Common/IO/FileAliases.cs @@ -92,34 +92,15 @@ public static void CopyFile(this ICakeContext context, FilePath filePath, FilePa /// The context. /// The pattern. /// The target directory path. - /// - /// - /// CopyFiles("Cake.*", "./publish"); - /// - /// - [CakeMethodAlias] - [CakeAliasCategory("Copy")] - public static void CopyFiles(this ICakeContext context, string pattern, DirectoryPath targetDirectoryPath) - { - FileCopier.CopyFiles(context, pattern, targetDirectoryPath); - } - - /// - /// Copies existing files to a new location. - /// - /// The context. - /// The pattern. - /// The target directory path. /// Keep the folder structure. /// /// - /// var files = GetFiles("./**/Cake.*"); - /// CopyFiles(files, "destination"); + /// CopyFiles("Cake.*", "./publish"); /// /// [CakeMethodAlias] [CakeAliasCategory("Copy")] - public static void CopyFiles(this ICakeContext context, string pattern, DirectoryPath targetDirectoryPath, bool preserveFolderStructure) + public static void CopyFiles(this ICakeContext context, string pattern, DirectoryPath targetDirectoryPath, bool preserveFolderStructure = false) { FileCopier.CopyFiles(context, pattern, targetDirectoryPath, preserveFolderStructure); } @@ -130,6 +111,7 @@ public static void CopyFiles(this ICakeContext context, string pattern, Director /// The context. /// The file paths. /// The target directory path. + /// Keep the folder structure. /// /// /// var files = GetFiles("./**/Cake.*"); @@ -138,9 +120,9 @@ public static void CopyFiles(this ICakeContext context, string pattern, Director /// [CakeMethodAlias] [CakeAliasCategory("Copy")] - public static void CopyFiles(this ICakeContext context, IEnumerable filePaths, DirectoryPath targetDirectoryPath) + public static void CopyFiles(this ICakeContext context, IEnumerable filePaths, DirectoryPath targetDirectoryPath, bool preserveFolderStructure = false) { - FileCopier.CopyFiles(context, filePaths, targetDirectoryPath); + FileCopier.CopyFiles(context, filePaths, targetDirectoryPath, preserveFolderStructure); } /// @@ -149,6 +131,7 @@ public static void CopyFiles(this ICakeContext context, IEnumerable fi /// The context. /// The file paths. /// The target directory path. + /// Keep the folder structure. /// /// /// CreateDirectory("destination"); @@ -161,14 +144,14 @@ public static void CopyFiles(this ICakeContext context, IEnumerable fi /// [CakeMethodAlias] [CakeAliasCategory("Copy")] - public static void CopyFiles(this ICakeContext context, IEnumerable filePaths, DirectoryPath targetDirectoryPath) + public static void CopyFiles(this ICakeContext context, IEnumerable filePaths, DirectoryPath targetDirectoryPath, bool preserveFolderStructure = false) { if (filePaths == null) { throw new ArgumentNullException("filePaths"); } var paths = filePaths.Select(p => new FilePath(p)); - FileCopier.CopyFiles(context, paths, targetDirectoryPath); + FileCopier.CopyFiles(context, paths, targetDirectoryPath, preserveFolderStructure); } /// diff --git a/src/Cake.Common/IO/FileCopier.cs b/src/Cake.Common/IO/FileCopier.cs index 4355d5154e..f1c8e12f46 100644 --- a/src/Cake.Common/IO/FileCopier.cs +++ b/src/Cake.Common/IO/FileCopier.cs @@ -60,25 +60,6 @@ public static void CopyFile(ICakeContext context, FilePath filePath, FilePath ta CopyFileCore(context, filePath, targetFilePath); } - public static void CopyFiles(ICakeContext context, string pattern, DirectoryPath targetDirectoryPath) - { - if (context == null) - { - throw new ArgumentNullException("context"); - } - if (pattern == null) - { - throw new ArgumentNullException("pattern"); - } - var files = context.GetFiles(pattern); - if (files.Count == 0) - { - context.Log.Verbose("The provided pattern did not match any files."); - return; - } - CopyFiles(context, files, targetDirectoryPath); - } - public static void CopyFiles(ICakeContext context, string pattern, DirectoryPath targetDirectoryPath, bool preserverFolderStructure) { if (context == null) @@ -99,38 +80,6 @@ public static void CopyFiles(ICakeContext context, string pattern, DirectoryPath CopyFiles(context, files, targetDirectoryPath, preserverFolderStructure); } - public static void CopyFiles(ICakeContext context, IEnumerable filePaths, DirectoryPath targetDirectoryPath) - { - if (context == null) - { - throw new ArgumentNullException("context"); - } - if (filePaths == null) - { - throw new ArgumentNullException("filePaths"); - } - if (targetDirectoryPath == null) - { - throw new ArgumentNullException("targetDirectoryPath"); - } - - var absoluteTargetDirectoryPath = targetDirectoryPath.MakeAbsolute(context.Environment); - - // Make sure the target directory exist. - if (!context.FileSystem.Exist(absoluteTargetDirectoryPath)) - { - const string format = "The directory '{0}' do not exist."; - var message = string.Format(CultureInfo.InvariantCulture, format, absoluteTargetDirectoryPath.FullPath); - throw new DirectoryNotFoundException(message); - } - - // Iterate all files and copy them. - foreach (var filePath in filePaths) - { - CopyFileCore(context, filePath, absoluteTargetDirectoryPath.GetFilePath(filePath)); - } - } - public static void CopyFiles(ICakeContext context, IEnumerable filePaths, DirectoryPath targetDirectoryPath, bool preserverFolderStructure) { if (context == null) From 3e1b825fae99a1659bea75e02fb3ca2a2b6fccb0 Mon Sep 17 00:00:00 2001 From: Martin Scholz Date: Mon, 22 Aug 2016 10:48:36 +0200 Subject: [PATCH 04/11] fix comments --- src/Cake.Common/IO/FileCopier.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Cake.Common/IO/FileCopier.cs b/src/Cake.Common/IO/FileCopier.cs index f1c8e12f46..3f2f8a587b 100644 --- a/src/Cake.Common/IO/FileCopier.cs +++ b/src/Cake.Common/IO/FileCopier.cs @@ -6,10 +6,10 @@ using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Linq; using Cake.Core; using Cake.Core.Diagnostics; using Cake.Core.IO; -using System.Linq; namespace Cake.Common.IO { @@ -107,7 +107,7 @@ public static void CopyFiles(ICakeContext context, IEnumerable filePat if (preserverFolderStructure) { - var matches = from len in Enumerable.Range(0, filePaths.Min(s => s.ToString().Length)).Reverse() //http://stackoverflow.com/a/8578268/2845744 + var matches = from len in Enumerable.Range(0, filePaths.Min(s => s.ToString().Length)).Reverse() ////http://stackoverflow.com/a/8578268/2845744 let possibleMatch = filePaths.First().ToString().Substring(0, len) where filePaths.All(f => f.ToString().StartsWith(possibleMatch)) select possibleMatch; @@ -128,7 +128,6 @@ where filePaths.All(f => f.ToString().StartsWith(possibleMatch)) CopyFileCore(context, filePath, absoluteTargetDirectoryPath.GetFilePath(filePath)); } } - } private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePath targetFilePath) @@ -166,8 +165,8 @@ private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePa var absoluteTargetPath = targetFilePath.MakeAbsolute(context.Environment); var file = context.FileSystem.GetFile(absoluteFilePath); - //Get the parent folder structure and create it. - var newRelativeFolderPath = filePath.GetDirectory().ToString().Replace(commonPath.Replace("\\", "/"), ""); + // Get the parent folder structure and create it. + var newRelativeFolderPath = filePath.GetDirectory().ToString().Replace(commonPath.Replace("\\", "/"), string.Empty); var newTargetPath = absoluteTargetPath.GetDirectory() + newRelativeFolderPath; var newAbsoluteTargetPath = newTargetPath + @"/" + filePath.GetFilename(); context.Log.Verbose("Copying file {0} to {1}", absoluteFilePath.GetFilename(), newAbsoluteTargetPath); From b947c493d5e0483f9316d9fe05a1ba6defbe5052 Mon Sep 17 00:00:00 2001 From: Martin Scholz Date: Mon, 22 Aug 2016 11:00:35 +0200 Subject: [PATCH 05/11] add tests for WithFilePaths and WithStrings --- .../Unit/IO/FileAliasesTests.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Cake.Common.Tests/Unit/IO/FileAliasesTests.cs b/src/Cake.Common.Tests/Unit/IO/FileAliasesTests.cs index b5e1f53a08..3a6cf52468 100644 --- a/src/Cake.Common.Tests/Unit/IO/FileAliasesTests.cs +++ b/src/Cake.Common.Tests/Unit/IO/FileAliasesTests.cs @@ -304,6 +304,20 @@ public void Should_Throw_If_Any_File_Do_Not_Exist() Assert.Equal("The file '/Working/file2.txt' do not exist.", result.Message); } + [Fact] + public void Should_Keep_Folder_Structure() + { + // Given + var fixture = new FileCopyFixture(); + + // When + FileAliases.CopyFiles(fixture.Context, fixture.SourceFilePaths, "./target"); + + // Then + fixture.TargetFiles[0].Received(1).Copy(Arg.Any(), true); + fixture.TargetFiles[1].Received(1).Copy(Arg.Any(), true); + } + [Fact] public void Should_Copy_Files() { @@ -401,6 +415,21 @@ public void Should_Throw_If_Any_File_Do_Not_Exist() Assert.Equal("The file '/Working/file2.txt' do not exist.", result.Message); } + [Fact] + public void Should_Keep_Folder_Structure() + { + // Given + var fixture = new FileCopyFixture(); + var filePaths = fixture.SourceFilePaths.Select(x => x.FullPath); + + // When + FileAliases.CopyFiles(fixture.Context, filePaths, "./target"); + + // Then + fixture.TargetFiles[0].Received(1).Copy(Arg.Any(), true); + fixture.TargetFiles[1].Received(1).Copy(Arg.Any(), true); + } + [Fact] public void Should_Copy_Files() { From da15709e62e289cfa1e8dd67b5a50679984a9c71 Mon Sep 17 00:00:00 2001 From: Martin Scholz Date: Mon, 22 Aug 2016 11:14:46 +0200 Subject: [PATCH 06/11] fix whitespace --- src/Cake.Common.Tests/project.json | 2 +- src/Cake.Common/IO/FileCopier.cs | 2 +- src/Cake.Common/project.json | 2 +- src/Cake.Core.Tests/project.json | 2 +- src/Cake.Core/project.json | 2 +- src/Cake.NuGet.Tests/project.json | 2 +- src/Cake.NuGet/project.json | 2 +- src/Cake.Testing.Xunit/project.json | 2 +- src/Cake.Testing/project.json | 2 +- src/Cake.Tests/project.json | 2 +- src/Cake/project.json | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Cake.Common.Tests/project.json b/src/Cake.Common.Tests/project.json index 97aed8cd19..8e44eb7ead 100644 --- a/src/Cake.Common.Tests/project.json +++ b/src/Cake.Common.Tests/project.json @@ -1,5 +1,5 @@ { - "version": "0.16.0-*", + "version": "0.15.3-*", "buildOptions": { "additionalArguments": [ "/ruleset:../Test.ruleset", diff --git a/src/Cake.Common/IO/FileCopier.cs b/src/Cake.Common/IO/FileCopier.cs index 4b7fcb57b3..2703a089b3 100644 --- a/src/Cake.Common/IO/FileCopier.cs +++ b/src/Cake.Common/IO/FileCopier.cs @@ -165,7 +165,7 @@ private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePa var absoluteTargetPath = targetFilePath.MakeAbsolute(context.Environment); var file = context.FileSystem.GetFile(absoluteFilePath); - // Get the parent folder structure and create it. + // Get the parent folder structure and create it. var newRelativeFolderPath = filePath.GetDirectory().ToString().Replace(commonPath.Replace("\\", "/"), string.Empty); var newTargetPath = absoluteTargetPath.GetDirectory() + newRelativeFolderPath; var newAbsoluteTargetPath = newTargetPath + @"/" + filePath.GetFilename(); diff --git a/src/Cake.Common/project.json b/src/Cake.Common/project.json index 6a33c2612a..e35557ef89 100644 --- a/src/Cake.Common/project.json +++ b/src/Cake.Common/project.json @@ -1,5 +1,5 @@ { - "version": "0.16.0-*", + "version": "0.15.3-*", "description": "Provides aliases (extension methods on Cake context) that support CI, build, unit tests, zip, signing, etc. for Cake.", "copyright": "Copyright (c) .NET Foundation and contributors", "authors": [ diff --git a/src/Cake.Core.Tests/project.json b/src/Cake.Core.Tests/project.json index 86a9ea8ff8..bd37c2321d 100644 --- a/src/Cake.Core.Tests/project.json +++ b/src/Cake.Core.Tests/project.json @@ -1,5 +1,5 @@ { - "version": "0.16.0-*", + "version": "0.15.3-*", "dependencies": { "dotnet-test-xunit": "2.2.0-preview2-build1029", "Cake.Core": { diff --git a/src/Cake.Core/project.json b/src/Cake.Core/project.json index e68d071d3b..efa03b18ec 100644 --- a/src/Cake.Core/project.json +++ b/src/Cake.Core/project.json @@ -1,5 +1,5 @@ { - "version": "0.16.0-*", + "version": "0.15.3-*", "description": "The Cake core library.", "copyright": "Copyright (c) .NET Foundation and contributors", "authors": [ diff --git a/src/Cake.NuGet.Tests/project.json b/src/Cake.NuGet.Tests/project.json index e1c547d7b6..5ae1697aa3 100644 --- a/src/Cake.NuGet.Tests/project.json +++ b/src/Cake.NuGet.Tests/project.json @@ -1,5 +1,5 @@ { - "version": "0.16.0-*", + "version": "0.15.3-*", "buildOptions": { "additionalArguments": [ "/ruleset:../Test.ruleset", diff --git a/src/Cake.NuGet/project.json b/src/Cake.NuGet/project.json index 40d9aff1e7..61ec871b69 100644 --- a/src/Cake.NuGet/project.json +++ b/src/Cake.NuGet/project.json @@ -1,5 +1,5 @@ { - "version": "0.16.0-*", + "version": "0.15.3-*", "dependencies": { "Cake.Core": { "target": "project" diff --git a/src/Cake.Testing.Xunit/project.json b/src/Cake.Testing.Xunit/project.json index e69c624cbb..3bbf2b234d 100644 --- a/src/Cake.Testing.Xunit/project.json +++ b/src/Cake.Testing.Xunit/project.json @@ -1,5 +1,5 @@ { - "version": "0.16.0-*", + "version": "0.15.3-*", "configurations": { "Release": { "buildOptions": { diff --git a/src/Cake.Testing/project.json b/src/Cake.Testing/project.json index da0e2c6d20..bec33b8446 100644 --- a/src/Cake.Testing/project.json +++ b/src/Cake.Testing/project.json @@ -1,5 +1,5 @@ { - "version": "0.16.0-*", + "version": "0.15.3-*", "description": "Contains testing utilities for Cake.", "copyright": "Copyright (c) .NET Foundation and contributors", "authors": [ diff --git a/src/Cake.Tests/project.json b/src/Cake.Tests/project.json index 6548104f67..51256c787d 100644 --- a/src/Cake.Tests/project.json +++ b/src/Cake.Tests/project.json @@ -1,5 +1,5 @@ { - "version": "0.16.0-*", + "version": "0.15.3-*", "dependencies": { "dotnet-test-xunit": "2.2.0-preview2-build1029", "Cake": { diff --git a/src/Cake/project.json b/src/Cake/project.json index 2ac80e80ef..32754d67c9 100644 --- a/src/Cake/project.json +++ b/src/Cake/project.json @@ -1,5 +1,5 @@ { - "version": "0.16.0-*", + "version": "0.15.3-*", "buildOptions": { "emitEntryPoint": true, "xmlDoc": true, From 59750394ad3c5f969d6ecadef8836f06563e9dc0 Mon Sep 17 00:00:00 2001 From: Martin Scholz Date: Mon, 22 Aug 2016 16:25:34 +0200 Subject: [PATCH 07/11] use IFileSystem instead of System.File.IO --- src/Cake.Common/IO/FileCopier.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Cake.Common/IO/FileCopier.cs b/src/Cake.Common/IO/FileCopier.cs index 2703a089b3..3bff853759 100644 --- a/src/Cake.Common/IO/FileCopier.cs +++ b/src/Cake.Common/IO/FileCopier.cs @@ -166,14 +166,14 @@ private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePa var file = context.FileSystem.GetFile(absoluteFilePath); // Get the parent folder structure and create it. - var newRelativeFolderPath = filePath.GetDirectory().ToString().Replace(commonPath.Replace("\\", "/"), string.Empty); - var newTargetPath = absoluteTargetPath.GetDirectory() + newRelativeFolderPath; - var newAbsoluteTargetPath = newTargetPath + @"/" + filePath.GetFilename(); + var newRelativeFolderPath = context.Directory(commonPath).Path.GetRelativePath(filePath.GetDirectory()); + var newTargetPath = targetFilePath.GetDirectory().Combine(newRelativeFolderPath); + var newAbsoluteTargetPath = newTargetPath.CombineWithFilePath(filePath.GetFilename()); context.Log.Verbose("Copying file {0} to {1}", absoluteFilePath.GetFilename(), newAbsoluteTargetPath); - if (!Directory.Exists(newTargetPath)) + if (!context.DirectoryExists(newTargetPath)) { - Directory.CreateDirectory(newTargetPath); + context.CreateDirectory(newTargetPath); } file.Copy(newAbsoluteTargetPath, true); From 0de966e9bd953b63741e2a6e623f07e271b75a11 Mon Sep 17 00:00:00 2001 From: Martin Scholz Date: Mon, 22 Aug 2016 16:34:36 +0200 Subject: [PATCH 08/11] reomve unused param --- src/Cake.Common/IO/FileCopier.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cake.Common/IO/FileCopier.cs b/src/Cake.Common/IO/FileCopier.cs index 3bff853759..0f25e8acd8 100644 --- a/src/Cake.Common/IO/FileCopier.cs +++ b/src/Cake.Common/IO/FileCopier.cs @@ -117,7 +117,7 @@ where filePaths.All(f => f.ToString().StartsWith(possibleMatch)) // Iterate all files and copy them. foreach (var filePath in filePaths) { - CopyFileCore(context, filePath, absoluteTargetDirectoryPath.GetFilePath(filePath), commonPath, targetDirectoryPath.ToString()); + CopyFileCore(context, filePath, absoluteTargetDirectoryPath.GetFilePath(filePath), commonPath); } } else @@ -149,7 +149,7 @@ private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePa file.Copy(absoluteTargetPath, true); } - private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePath targetFilePath, string commonPath, string targetDirectoryPath) + private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePath targetFilePath, string commonPath) { var absoluteFilePath = filePath.MakeAbsolute(context.Environment); From 122c1ee55f1c97e1d8713743d44509b42df17afe Mon Sep 17 00:00:00 2001 From: Martin Scholz Date: Tue, 23 Aug 2016 12:39:29 +0200 Subject: [PATCH 09/11] change logic for getting common path --- src/Cake.Common/IO/FileCopier.cs | 73 +++++++++++++++++--------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/src/Cake.Common/IO/FileCopier.cs b/src/Cake.Common/IO/FileCopier.cs index 0f25e8acd8..ab03258521 100644 --- a/src/Cake.Common/IO/FileCopier.cs +++ b/src/Cake.Common/IO/FileCopier.cs @@ -57,7 +57,7 @@ public static void CopyFile(ICakeContext context, FilePath filePath, FilePath ta throw new DirectoryNotFoundException(message); } - CopyFileCore(context, filePath, targetFilePath); + CopyFileCore(context, filePath, targetFilePath, null); } public static void CopyFiles(ICakeContext context, string pattern, DirectoryPath targetDirectoryPath, bool preserverFolderStructure) @@ -107,17 +107,32 @@ public static void CopyFiles(ICakeContext context, IEnumerable filePat if (preserverFolderStructure) { - var matches = from len in Enumerable.Range(0, filePaths.Min(s => s.ToString().Length)).Reverse() ////http://stackoverflow.com/a/8578268/2845744 - let possibleMatch = filePaths.First().ToString().Substring(0, len) - where filePaths.All(f => f.ToString().StartsWith(possibleMatch)) - select possibleMatch; + var commonPath = string.Empty; + List separatedPath = filePaths + .First(str => str.ToString().Length == filePaths.Max(st2 => st2.ToString().Length)).ToString() + .Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries) + .ToList(); - var commonPath = System.IO.Path.GetDirectoryName(matches.First()); + foreach (string pathSegment in separatedPath.AsEnumerable()) + { + if (commonPath.Length == 0 && filePaths.All(str => str.ToString().StartsWith(pathSegment))) + { + commonPath = pathSegment; + } + else if (filePaths.All(str => str.ToString().StartsWith(commonPath + "/" + pathSegment))) + { + commonPath += "/" + pathSegment; + } + else + { + break; + } + } // Iterate all files and copy them. foreach (var filePath in filePaths) { - CopyFileCore(context, filePath, absoluteTargetDirectoryPath.GetFilePath(filePath), commonPath); + CopyFileCore(context, filePath, absoluteTargetDirectoryPath.GetFilePath(filePath), context.DirectoryExists(commonPath) ? commonPath : null); } } else @@ -125,12 +140,12 @@ where filePaths.All(f => f.ToString().StartsWith(possibleMatch)) // Iterate all files and copy them. foreach (var filePath in filePaths) { - CopyFileCore(context, filePath, absoluteTargetDirectoryPath.GetFilePath(filePath)); + CopyFileCore(context, filePath, absoluteTargetDirectoryPath.GetFilePath(filePath), null); } } } - private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePath targetFilePath) + private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePath targetFilePath, string commonPath) { var absoluteFilePath = filePath.MakeAbsolute(context.Environment); @@ -144,39 +159,27 @@ private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePa // Copy the file. var absoluteTargetPath = targetFilePath.MakeAbsolute(context.Environment); - context.Log.Verbose("Copying file {0} to {1}", absoluteFilePath.GetFilename(), absoluteTargetPath); var file = context.FileSystem.GetFile(absoluteFilePath); - file.Copy(absoluteTargetPath, true); - } - - private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePath targetFilePath, string commonPath) - { - var absoluteFilePath = filePath.MakeAbsolute(context.Environment); - // Get the file. - if (!context.FileSystem.Exist(absoluteFilePath)) + if (!string.IsNullOrEmpty(commonPath)) { - const string format = "The file '{0}' do not exist."; - var message = string.Format(CultureInfo.InvariantCulture, format, absoluteFilePath.FullPath); - throw new FileNotFoundException(message, absoluteFilePath.FullPath); - } - - // Copy the file. - var absoluteTargetPath = targetFilePath.MakeAbsolute(context.Environment); - var file = context.FileSystem.GetFile(absoluteFilePath); + // Get the parent folder structure and create it. + var newRelativeFolderPath = context.Directory(commonPath).Path.GetRelativePath(filePath.GetDirectory()); + var newTargetPath = targetFilePath.GetDirectory().Combine(newRelativeFolderPath); + var newAbsoluteTargetPath = newTargetPath.CombineWithFilePath(filePath.GetFilename()); + context.Log.Verbose("Copying file {0} to {1}", absoluteFilePath.GetFilename(), newAbsoluteTargetPath); - // Get the parent folder structure and create it. - var newRelativeFolderPath = context.Directory(commonPath).Path.GetRelativePath(filePath.GetDirectory()); - var newTargetPath = targetFilePath.GetDirectory().Combine(newRelativeFolderPath); - var newAbsoluteTargetPath = newTargetPath.CombineWithFilePath(filePath.GetFilename()); - context.Log.Verbose("Copying file {0} to {1}", absoluteFilePath.GetFilename(), newAbsoluteTargetPath); + if (!context.DirectoryExists(newTargetPath)) + { + context.CreateDirectory(newTargetPath); + } - if (!context.DirectoryExists(newTargetPath)) + file.Copy(newAbsoluteTargetPath, true); + } + else { - context.CreateDirectory(newTargetPath); + file.Copy(absoluteTargetPath, true); } - - file.Copy(newAbsoluteTargetPath, true); } } } From 57834c1f6448838fab91378f53418d21d216ce8d Mon Sep 17 00:00:00 2001 From: Martin Scholz Date: Tue, 23 Aug 2016 12:52:56 +0200 Subject: [PATCH 10/11] fix copy log message --- src/Cake.Common/IO/FileCopier.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Cake.Common/IO/FileCopier.cs b/src/Cake.Common/IO/FileCopier.cs index ab03258521..207f33ceb1 100644 --- a/src/Cake.Common/IO/FileCopier.cs +++ b/src/Cake.Common/IO/FileCopier.cs @@ -178,6 +178,7 @@ private static void CopyFileCore(ICakeContext context, FilePath filePath, FilePa } else { + context.Log.Verbose("Copying file {0} to {1}", absoluteFilePath.GetFilename(), absoluteTargetPath); file.Copy(absoluteTargetPath, true); } } From 04950ed876912372179ff2ac3b24cfb8a0eb0b64 Mon Sep 17 00:00:00 2001 From: maddin2016 Date: Thu, 25 Aug 2016 16:10:56 +0200 Subject: [PATCH 11/11] remove unnecessary casting --- src/Cake.Common/IO/FileCopier.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cake.Common/IO/FileCopier.cs b/src/Cake.Common/IO/FileCopier.cs index 207f33ceb1..eb8bc694ef 100644 --- a/src/Cake.Common/IO/FileCopier.cs +++ b/src/Cake.Common/IO/FileCopier.cs @@ -113,7 +113,7 @@ public static void CopyFiles(ICakeContext context, IEnumerable filePat .Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries) .ToList(); - foreach (string pathSegment in separatedPath.AsEnumerable()) + foreach (string pathSegment in separatedPath) { if (commonPath.Length == 0 && filePaths.All(str => str.ToString().StartsWith(pathSegment))) {