diff --git a/src/GitVersion.Core/Core/Abstractions/IFileSystem.cs b/src/GitVersion.Core/Core/Abstractions/IFileSystem.cs index eadb410d9a..7678d8c373 100644 --- a/src/GitVersion.Core/Core/Abstractions/IFileSystem.cs +++ b/src/GitVersion.Core/Core/Abstractions/IFileSystem.cs @@ -17,4 +17,5 @@ public interface IFileSystem string[] GetDirectories(string path); IEnumerable DirectoryEnumerateFiles(string? directory, string searchPattern, SearchOption searchOption); long GetLastDirectoryWrite(string path); + long GetLastWriteTime(string path); } diff --git a/src/GitVersion.Core/Core/FileSystem.cs b/src/GitVersion.Core/Core/FileSystem.cs index 7da239b288..1187b06528 100644 --- a/src/GitVersion.Core/Core/FileSystem.cs +++ b/src/GitVersion.Core/Core/FileSystem.cs @@ -48,6 +48,8 @@ public IEnumerable DirectoryEnumerateFiles(string? directory, string sea return Directory.EnumerateFiles(directory, searchPattern, searchOption); } + public long GetLastWriteTime(string path) => File.GetLastWriteTime(path).Ticks; + public long GetLastDirectoryWrite(string path) => new DirectoryInfo(path) .GetDirectories("*.*", SearchOption.AllDirectories) .Select(d => d.LastWriteTimeUtc) diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index d86fbfefd4..b5c8a9949f 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -1,3 +1,4 @@ #nullable enable GitVersion.IFileSystem.GetDirectories(string! path) -> string![]! GitVersion.IFileSystem.GetFiles(string! path) -> string![]! +GitVersion.IFileSystem.GetLastWriteTime(string! path) -> long diff --git a/src/GitVersion.Core/VersionCalculation/Caching/GitVersionCacheKeyFactory.cs b/src/GitVersion.Core/VersionCalculation/Caching/GitVersionCacheKeyFactory.cs index 70609d9250..c91350e733 100644 --- a/src/GitVersion.Core/VersionCalculation/Caching/GitVersionCacheKeyFactory.cs +++ b/src/GitVersion.Core/VersionCalculation/Caching/GitVersionCacheKeyFactory.cs @@ -116,8 +116,8 @@ private List CalculateDirectoryContents(string root) { try { - var fi = new FileInfo(file); - result.Add(fi.Name); + if (!this.fileSystem.Exists(file)) continue; + result.Add(Path.GetFileName(file)); result.Add(this.fileSystem.ReadAllText(file)); } catch (IOException e) diff --git a/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs b/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs index ccff9b9549..d51d364d95 100644 --- a/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs +++ b/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs @@ -46,6 +46,10 @@ public void UpdateAssemblyInfo(UpdateAssemblyInfo task) var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "AssemblyInfo"); task.AssemblyInfoTempFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName); + if (!this.fileSystem.DirectoryExists(fileWriteInfo.WorkingDirectory)) + { + this.fileSystem.CreateDirectory(fileWriteInfo.WorkingDirectory); + } var gitVersionOptions = this.options.Value; gitVersionOptions.WorkingDirectory = fileWriteInfo.WorkingDirectory; gitVersionOptions.AssemblySettingsInfo.UpdateAssemblyInfo = true; @@ -68,6 +72,10 @@ public void GenerateGitVersionInformation(GenerateGitVersionInformation task) var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "GitVersionInformation"); task.GitVersionInformationFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName); + if (!this.fileSystem.DirectoryExists(fileWriteInfo.WorkingDirectory)) + { + this.fileSystem.CreateDirectory(fileWriteInfo.WorkingDirectory); + } var gitVersionOptions = this.options.Value; gitVersionOptions.WorkingDirectory = fileWriteInfo.WorkingDirectory; var targetNamespace = GetTargetNamespace(task); @@ -102,18 +110,19 @@ public void WriteVersionInfoToBuildLog(WriteVersionInfoToBuildLog task) private void DeleteTempFiles() { - if (!this.fileSystem.DirectoryExists(FileHelper.TempPath)) + var tempPath = FileHelper.TempPath; + if (!this.fileSystem.DirectoryExists(tempPath)) { return; } - foreach (var file in this.fileSystem.GetFiles(FileHelper.TempPath)) + foreach (var file in this.fileSystem.GetFiles(tempPath)) { - if (File.GetLastWriteTime(file) >= DateTime.Now.AddDays(-1)) + if (this.fileSystem.GetLastWriteTime(file) >= DateTime.Now.AddDays(-1).Ticks) continue; try { - File.Delete(file); + this.fileSystem.Delete(file); } catch (UnauthorizedAccessException) { diff --git a/src/GitVersion.MsBuild/Helpers/FileHelper.cs b/src/GitVersion.MsBuild/Helpers/FileHelper.cs index e8a4479af6..ce3d1e9da7 100644 --- a/src/GitVersion.MsBuild/Helpers/FileHelper.cs +++ b/src/GitVersion.MsBuild/Helpers/FileHelper.cs @@ -10,12 +10,7 @@ internal static class FileHelper { public static readonly string TempPath = MakeAndGetTempPath(); - private static string MakeAndGetTempPath() - { - var tempPath = PathHelper.Combine(Path.GetTempPath(), "GitVersionTask"); - Directory.CreateDirectory(tempPath); - return tempPath; - } + private static string MakeAndGetTempPath() => PathHelper.Combine(Path.GetTempPath(), "GitVersionTask"); public static string GetFileExtension(string language) => language switch {