Skip to content

Commit

Permalink
Remove checks for File.Exists. We can allow this to fail and just cat…
Browse files Browse the repository at this point in the history
…ch. File.Exist was top usage.
  • Loading branch information
wasabii committed Oct 16, 2023
1 parent bd14101 commit a35e2e8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ public class IkvmReferenceExportItemPrepareTests

[DataTestMethod]
[DataRow("net472", ".NETFramework", "4.7.2")]
[DataRow("net48", ".NETFramework", "4.8")]
[DataRow("net6.0", ".NETCore", "6.0")]
[DataRow("net7.0", ".NETCore", "7.0")]
public void CanPrepare(string tfm, string targetFrameworkIdentifier, string targetFrameworkVersion)
{
// no reference assemblies for NetFX on Unix
if (tfm == "net472" && RuntimeInformation.IsOSPlatform(OSPlatform.Windows) == false)
return;
if (tfm == "net472" || tfm == "net48")
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) == false)
return;

var engine = new Mock<IBuildEngine7>();
var errors = new List<BuildErrorEventArgs>();
Expand Down
33 changes: 19 additions & 14 deletions src/IKVM.MSBuild.Tasks/IkvmAssemblyInfoUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ public async Task SaveStateXmlAsync(XElement root)
{
if (string.IsNullOrWhiteSpace(path))
throw new ArgumentException($"'{nameof(path)}' cannot be null or whitespace.", nameof(path));
if (File.Exists(path) == false)
throw new FileNotFoundException($"Could not find file '{path}'.");

return (await cache.GetOrAdd(path, CreateAssemblyInfoAsync)).Info;
}
Expand All @@ -149,23 +147,30 @@ public async Task SaveStateXmlAsync(XElement root)
{
return Task.Run(() =>
{
var lastWriteTimeUtc = File.GetLastWriteTimeUtc(path);

// check if loaded state contains up to date information
if (state.TryGetValue(path, out var entry))
if (entry.LastWriteTimeUtc == lastWriteTimeUtc)
return (lastWriteTimeUtc, entry.Info);

try
{
using var fsstm = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
using var perdr = new PEReader(fsstm);
var mrdr = perdr.GetMetadataReader();
return (lastWriteTimeUtc, new AssemblyInfo(mrdr.GetString(mrdr.GetAssemblyDefinition().Name), mrdr.GetGuid(mrdr.GetModuleDefinition().Mvid), mrdr.AssemblyReferences.Select(i => mrdr.GetString(mrdr.GetAssemblyReference(i).Name)).ToList()));
var lastWriteTimeUtc = File.GetLastWriteTimeUtc(path);

// check if loaded state contains up to date information
if (state.TryGetValue(path, out var entry))
if (entry.LastWriteTimeUtc == lastWriteTimeUtc)
return (lastWriteTimeUtc, entry.Info);

try
{
using var fsstm = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
using var perdr = new PEReader(fsstm);
var mrdr = perdr.GetMetadataReader();
return (lastWriteTimeUtc, new AssemblyInfo(mrdr.GetString(mrdr.GetAssemblyDefinition().Name), mrdr.GetGuid(mrdr.GetModuleDefinition().Mvid), mrdr.AssemblyReferences.Select(i => mrdr.GetString(mrdr.GetAssemblyReference(i).Name)).ToList()));
}
catch
{
return (lastWriteTimeUtc, null);
}
}
catch
{
return (lastWriteTimeUtc, null);
return (default, null);
}
});
}
Expand Down
2 changes: 0 additions & 2 deletions src/IKVM.MSBuild.Tasks/IkvmFileIdentityUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ public async Task<string> GetIdentityForFileAsync(string path, CancellationToken
{
if (string.IsNullOrWhiteSpace(path))
throw new ArgumentException($"'{nameof(path)}' cannot be null or whitespace.", nameof(path));
if (File.Exists(path) == false)
throw new FileNotFoundException($"Could not find file '{path}'.");

return (await cache.GetOrAdd(path, CreateIdentityForFileAsync)).Identity;
}
Expand Down
6 changes: 5 additions & 1 deletion src/IKVM.MSBuild.Tasks/IkvmReferenceExportItemPrepare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,11 @@ async Task<string> GetIdentityAsync(string value, CancellationToken cancellation

// others should exist
if (File.Exists(value))
return await fileIdentityUtil.GetIdentityForFileAsync(value, cancellationToken);
{
var identity = await fileIdentityUtil.GetIdentityForFileAsync(value, cancellationToken);
if (identity != null)
return identity;
}

throw new Exception($"Could not resolve identity for '{value}'.");
}
Expand Down

0 comments on commit a35e2e8

Please sign in to comment.