Skip to content

Commit

Permalink
Refactor MakeAndGetTempPath to use expression-bodied member
Browse files Browse the repository at this point in the history
Simplified the MakeAndGetTempPath method by converting it to an expression-bodied member.
  • Loading branch information
arturcic committed Dec 9, 2024
1 parent 5147e95 commit 012c377
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/GitVersion.Core/Core/Abstractions/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public interface IFileSystem
string[] GetDirectories(string path);
IEnumerable<string> DirectoryEnumerateFiles(string? directory, string searchPattern, SearchOption searchOption);
long GetLastDirectoryWrite(string path);
long GetLastWriteTime(string path);
}
2 changes: 2 additions & 0 deletions src/GitVersion.Core/Core/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public IEnumerable<string> 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)
Expand Down
1 change: 1 addition & 0 deletions src/GitVersion.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
GitVersion.IFileSystem.GetDirectories(string! path) -> string![]!
GitVersion.IFileSystem.GetFiles(string! path) -> string![]!
GitVersion.IFileSystem.GetLastWriteTime(string! path) -> long
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ private List<string> 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)
Expand Down
17 changes: 13 additions & 4 deletions src/GitVersion.MsBuild/GitVersionTaskExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down
7 changes: 1 addition & 6 deletions src/GitVersion.MsBuild/Helpers/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 012c377

Please sign in to comment.