Skip to content

Commit

Permalink
Fix file loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabii committed Oct 18, 2023
1 parent 7ae782c commit 93b6949
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
21 changes: 16 additions & 5 deletions src/IKVM.MSBuild.Tasks/IkvmAssemblyInfoUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,29 @@ public IkvmAssemblyInfoUtil()
/// <returns></returns>
public void LoadStateXml(XElement root)
{
if (root == null)
if (root is null)
throw new ArgumentNullException(nameof(root));

foreach (var element in root.Elements(XML_ASSEMBLY_ELEMENT_NAME))
{
var path = (string)element.Attribute(XML_PATH_ATTRIBUTE_NAME);
if (path is null)
continue;

var lastWriteTimeUtc = (DateTime?)element.Attribute(XML_LAST_WRITE_TIME_UTC_ATTRIBUTE_NAME);
if (lastWriteTimeUtc is null)
continue;

var name = (string)element.Attribute(XML_NAME_ATTRIBUTE_NAME);
if (name is null)
continue;

var mvid = (Guid?)element.Attribute(XML_MVID_ATTRIBUTE_NAME);
var references = element.Elements(XML_REFERENCE_ELEMENT_NAME).Cast<string>().ToArray();
if (mvid is null)
continue;

if (path == null || lastWriteTimeUtc == null || name == null || mvid == null)
var references = element.Elements(XML_REFERENCE_ELEMENT_NAME).Select(i => i.Value).ToArray();
if (references is null)
continue;

state[path] = new AssemblyInfo(path, lastWriteTimeUtc.Value, name, mvid.Value, references);
Expand All @@ -120,7 +131,7 @@ public async System.Threading.Tasks.Task SaveStateXmlAsync(XElement root)
foreach (var i in cache)
{
var info = await i.Value;
if (info == null)
if (info is null)
continue;

root.Add(
Expand Down Expand Up @@ -173,7 +184,7 @@ public async System.Threading.Tasks.Task SaveStateXmlAsync(XElement root)
using var fsstm = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
using var perdr = new PEReader(fsstm);
var mrdr = perdr.GetMetadataReader();
return new AssemblyInfo(path, lastWriteTimeUtc, mrdr.GetString(mrdr.GetAssemblyDefinition().Name), mrdr.GetGuid(mrdr.GetModuleDefinition().Mvid), mrdr.AssemblyReferences.Select(i => mrdr.GetString(mrdr.GetAssemblyReference(i).Name)).ToArray());
return new AssemblyInfo(path, lastWriteTimeUtc, mrdr.GetString(mrdr.GetAssemblyDefinition().Name), mrdr.GetGuid(mrdr.GetModuleDefinition().Mvid), mrdr.AssemblyReferences.Select(i => mrdr.GetString(mrdr.GetAssemblyReference(i).Name)).OrderBy(i => i).ToArray());
}
catch (Exception e)
{
Expand Down
8 changes: 6 additions & 2 deletions src/IKVM.MSBuild.Tasks/IkvmFileIdentityUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -76,7 +75,12 @@ public async System.Threading.Tasks.Task SaveStateXmlAsync(XElement root)
foreach (var i in cache)
{
var (lastWriteTimeUtc, identity) = await i.Value;
root.Add(new XElement(XML_FILE_ELEMENT_NAME, new XAttribute(XML_PATH_ATTRIBUTE_NAME, i.Key), new XAttribute(XML_LAST_WRITE_TIME_UTC_ATTRIBUTE_NAME, lastWriteTimeUtc), new XAttribute(XML_IDENTITY_ATTRIBUTE_NAME, identity)));

root.Add(
new XElement(XML_FILE_ELEMENT_NAME,
new XAttribute(XML_PATH_ATTRIBUTE_NAME, i.Key),
new XAttribute(XML_LAST_WRITE_TIME_UTC_ATTRIBUTE_NAME, lastWriteTimeUtc),
new XAttribute(XML_IDENTITY_ATTRIBUTE_NAME, identity)));
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/IKVM.MSBuild.Tasks/IkvmReferenceExportItemPrepare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
Expand Down Expand Up @@ -136,6 +136,9 @@ public void Cancel()
/// <returns></returns>
async Task<bool> ExecuteAsync(CancellationToken cancellationToken)
{
var sw = new Stopwatch();
sw.Start();

LoadState();

// execute task and return newly sorted items
Expand All @@ -144,6 +147,10 @@ async Task<bool> ExecuteAsync(CancellationToken cancellationToken)
Items = items.OrderBy(i => i.RandomIndex).Select(i => i.Item).ToArray(); // randomize order to allow multiple processes to interleave

await SaveStateAsync();

sw.Stop();
Log.LogMessage("Total time spent in IkvmReferenceExportItemPrepare: {0}", sw.Elapsed);

return true;
}

Expand Down Expand Up @@ -176,9 +183,9 @@ internal void LoadState()
}
}
}
catch
catch (Exception e)
{
Log.LogWarning("Could not load IkvmReferenceExportItemPrepare state file. File is potentially corrupt.");
Log.LogWarning("Could not load IkvmReferenceExportItemPrepare state file. File is potentially corrupt. {0}", e.Message);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/IKVM.MSBuild.Tasks/IkvmReferenceItemPrepare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -209,6 +210,9 @@ public void Cancel()
/// <returns></returns>
async Task<bool> ExecuteAsync(CancellationToken cancellationToken)
{
var sw = new Stopwatch();
sw.Start();

LoadState();

var items = IkvmReferenceItem.Import(Items);
Expand All @@ -228,6 +232,10 @@ async Task<bool> ExecuteAsync(CancellationToken cancellationToken)
Items = Sort(items).Select(i => i.Item).ToArray();

await SaveStateAsync();

sw.Stop();
Log.LogMessage("Total time spent in IkvmReferenceItemPrepare: {0}", sw.Elapsed);

return true;
}

Expand Down

0 comments on commit 93b6949

Please sign in to comment.