Skip to content

Commit 9b28b24

Browse files
authored
Fixes dotnet#4505 Remove reliance on getting product version for model.zip/version.txt from FileVersionInfo and replace with using assembly custom attributes (dotnet#4512)
Co-authored-by: Ross Eccleston <[email protected]>
1 parent 15eeef7 commit 9b28b24

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/Microsoft.ML.Core/Data/Repository.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Diagnostics;
99
using System.IO;
1010
using System.IO.Compression;
11+
using System.Linq;
12+
using System.Reflection;
1113
using Microsoft.ML.Internal.Utilities;
1214
using Microsoft.ML.Runtime;
1315

@@ -285,10 +287,10 @@ public static RepositoryWriter CreateNew(Stream stream, IExceptionContext ectx =
285287
Contracts.CheckValueOrNull(ectx);
286288
ectx.CheckValue(stream, nameof(stream));
287289
var rep = new RepositoryWriter(stream, ectx, useFileSystem);
288-
var versionInfo = FileVersionInfo.GetVersionInfo(typeof(RepositoryWriter).Assembly.Location);
290+
289291
using (var ent = rep.CreateEntry(DirTrainingInfo, "Version.txt"))
290292
using (var writer = Utils.OpenWriter(ent.Stream))
291-
writer.WriteLine(versionInfo.ProductVersion);
293+
writer.WriteLine(GetProductVersion());
292294
return rep;
293295
}
294296

@@ -410,6 +412,24 @@ public void Commit()
410412
Flush();
411413
Dispose(true);
412414
}
415+
416+
private static string GetProductVersion()
417+
{
418+
var assembly = typeof(RepositoryWriter).Assembly;
419+
420+
var assemblyInternationalVersionAttribute = assembly.CustomAttributes.FirstOrDefault(a =>
421+
a.AttributeType == typeof(AssemblyInformationalVersionAttribute));
422+
423+
if (assemblyInternationalVersionAttribute == null)
424+
{
425+
throw new ApplicationException($"Cannot determine product version from assembly {assembly.FullName}.");
426+
}
427+
428+
return assemblyInternationalVersionAttribute.ConstructorArguments
429+
.First()
430+
.Value
431+
.ToString();
432+
}
413433
}
414434

415435
[BestFriend]

0 commit comments

Comments
 (0)