-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Metadata tracking * Verify metadata in tool * fix * more
- Loading branch information
Showing
6 changed files
with
166 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Basic.CompilerLog.Util; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Basic.CompilerLog.UnitTests; | ||
|
||
public sealed class MetadataTests | ||
{ | ||
private static Metadata Parse(string content) | ||
{ | ||
using var reader = new StringReader(content); | ||
return Metadata.Read(reader); | ||
} | ||
|
||
[Fact] | ||
public void ParseVersion0() | ||
{ | ||
var content = """ | ||
count:50 | ||
"""; | ||
var metadata = Parse(content); | ||
Assert.Equal(0, metadata.MetadataVersion); | ||
Assert.Equal(50, metadata.Count); | ||
Assert.Null(metadata.IsWindows); | ||
} | ||
|
||
[Fact] | ||
public void ParseVersion1() | ||
{ | ||
var content = """ | ||
version:1 | ||
count:50 | ||
windows:true | ||
"""; | ||
var metadata = Parse(content); | ||
Assert.Equal(1, metadata.MetadataVersion); | ||
Assert.Equal(50, metadata.Count); | ||
Assert.True(metadata.IsWindows); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Basic.CompilerLog.Util; | ||
|
||
internal sealed class Metadata | ||
{ | ||
internal static readonly int LatestMetadataVersion = 1; | ||
|
||
internal int MetadataVersion { get; } | ||
internal int Count { get; } | ||
internal bool? IsWindows { get; } | ||
|
||
private Metadata( | ||
int metadataVersion, | ||
int count, | ||
bool? isWindows) | ||
{ | ||
MetadataVersion = metadataVersion; | ||
Count = count; | ||
IsWindows = isWindows; | ||
} | ||
|
||
internal static Metadata Create(int count) => | ||
new Metadata( | ||
LatestMetadataVersion, | ||
count, | ||
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); | ||
|
||
internal static Metadata Read(TextReader reader) | ||
{ | ||
try | ||
{ | ||
var line = reader.ReadLineOrThrow(); | ||
if (line.StartsWith("count", StringComparison.Ordinal)) | ||
{ | ||
// This is a version 0, there is just a count method | ||
var count = ParseLine(line, "count", int.Parse); | ||
return new Metadata(metadataVersion: 0, count, isWindows: null); | ||
} | ||
else | ||
{ | ||
var metadataVersion = ParseLine(line, "version", int.Parse); | ||
var count = ParseLine(reader.ReadLineOrThrow(), "count", int.Parse); | ||
var isWindows = ParseLine(reader.ReadLineOrThrow(), "windows", bool.Parse); | ||
return new Metadata( | ||
metadataVersion, | ||
count, | ||
isWindows); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new InvalidOperationException("Cannot parse metadata", ex); | ||
} | ||
|
||
T ParseLine<T>(string line, string label, Func<string, T> func) | ||
{ | ||
var items = line.Split(':', StringSplitOptions.RemoveEmptyEntries); | ||
if (items.Length != 2 || items[0] != label) | ||
throw new InvalidOperationException("Line has wrong format"); | ||
|
||
return func(items[1]); | ||
} | ||
} | ||
|
||
internal void Write(StreamWriter writer) | ||
{ | ||
writer.WriteLine($"version:{MetadataVersion}"); | ||
writer.WriteLine($"count:{Count}"); | ||
writer.WriteLine($"windows:{RuntimeInformation.IsOSPlatform(OSPlatform.Windows)}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters