Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add commands to validate qlpack.yml and YAML MaD definitions #8

Closed
wants to merge 8 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@
<Folder Include="Templates\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Semver" Version="2.3.0" />
<PackageReference Include="YamlDotNet" Version="13.4.0" />
</ItemGroup>

</Project>
63 changes: 0 additions & 63 deletions src/CodeQLToolkit.Features.Pack/Commands/PackCommandFeature.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace CodeQLToolkit.Features.Pack.Commands.Validate.Schemas
{
public class ExtQlpackYmlFileSchema : IYmlSchema
{
public bool Library { get; set; }
public string Name { get; set; }
public string Version { get; set; }
public Dictionary<string, string> ExtensionTargets { get; set; }
public List<string> DataExtensions { get; set; }

override public string ToString()
{
string ExtensionTargetsString = "\n";
if (ExtensionTargets is not null)
foreach (KeyValuePair<string, string> pair in ExtensionTargets)
ExtensionTargetsString += $"{pair.Key}: {pair.Value}\n";

string DataExtensionString = "\n";
if (DataExtensions is not null)
foreach (string DataExtension in DataExtensions)
DataExtensionString += $"{DataExtension}, ";

return $@"Ext qlpack.yml file:
Library: {Library},
Name: {Name},
Version: {Version},
ExtensionTargets: {ExtensionTargetsString}
DataExtensions: {DataExtensionString}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace CodeQLToolkit.Features.Pack.Commands.Validate.Schemas {
public interface IYmlSchema {
public string Name { get; set; }
public string Version { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace CodeQLToolkit.Features.Pack.Commands.Validate.Schemas
{
public class LibQlpackYmlFileSchema : IYmlSchema
{
public bool Library { get; set; }
public string Name { get; set; }
public string Version { get; set; }
public string Suites { get; set; }
public string Extractor { get; set; }
public Dictionary<string, string> Dependencies { get; set; }

override public string ToString()
{
string DependenciesString = "\n";
if (Dependencies is not null)
foreach (KeyValuePair<string, string> pair in Dependencies)
DependenciesString += $"{pair.Key}: {pair.Value}\n";

return $@"Lib qlpack.yml file:
Library: {Library},
Name: {Name},
Version: {Version},
Suites: {Suites},
Extractor: {Extractor},
Dependencies: {DependenciesString}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace CodeQLToolkit.Features.Pack.Commands.Validate.Schemas
{
public class SrcQlpackYmlSchema : IYmlSchema
{
public bool Library { get; set; }
public string Name { get; set; }
public string Version { get; set; }
public string Suites { get; set; }
public string Extractor { get; set; }
public Dictionary<string, string> Dependencies { get; set; }

[YamlMember(Alias = "default-suite-file", ApplyNamingConventions = false)]
public string DefaultSuiteFile { get; set; }

public override string ToString()
{
string DependenciesString = "\n";
if (Dependencies is not null)
foreach (KeyValuePair<string, string> pair in Dependencies)
DependenciesString += $"{pair.Key}: {pair.Value}\n";

return $@"Src qlpack.yml file:
Library: {Library},
Name: {Name},
Version: {Version},
Suites: {Suites},
Extractor: {Extractor},
Dependencies: {DependenciesString},
DefaultSuiteFile: {DefaultSuiteFile}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace CodeQLToolkit.Features.Pack.Commands.Validate.Schemas
{
public class TestQlpackYmlFileSchema : IYmlSchema
{
public string Name { get; set; }
public string Version { get; set; }
public string Extractor { get; set; }
public Dictionary<string, string> Dependencies { get; set; }

override public string ToString()
{
string DependenciesString = "\n";
if (Dependencies is not null)
foreach (KeyValuePair<string, string> pair in Dependencies)
DependenciesString += $"{pair.Key}: {pair.Value}\n";

return $@"Test qlpack.yml file:
Name: {Name},
Version: {Version},
Extractor: {Extractor},
Dependencies: {DependenciesString}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using CodeQLToolkit.Shared.Logging;
using CodeQLToolkit.Shared.Utils;
using YamlDotNet.Serialization;

namespace CodeQLToolkit.Features.Pack.Commands.Validate.Targets
{
public class ValidateSyntaxTarget : CommandTarget
{
public string ExtFilePath { get; set; }

public override void Run()
{
Console.WriteLine("Hello world!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using CodeQLToolkit.Shared.Logging;
using CodeQLToolkit.Shared.Utils;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using Semver;
using CodeQLToolkit.Features.Pack.Commands.Validate.Schemas;
using System.Diagnostics;

namespace CodeQLToolkit.Features.Pack.Commands.Validate.Targets
{
public class YamlParseException : Exception
{
public string message { get; set; }
public YamlParseException(string failMessage)
{
message = failMessage;
}
}
public class ValidateVersionTarget : CommandTarget
{
public string[] QlpackYmlFiles { get; set; }
private static Deserializer YamlDeserializer = (Deserializer)new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build();

public override void Run()
{
Console.WriteLine("Hello world! I got: ");
foreach (string qlpackYmlFile in QlpackYmlFiles)
private static UnbrokenSemVersionRange FindUnbrokenSemverRangeOverlap(
UnbrokenSemVersionRange range1,
UnbrokenSemVersionRange range2
)
{
if (range1.End.ComparePrecedenceTo(range2.Start) == -1 || range2.End.ComparePrecedenceTo(range1.Start) == -1) {
return UnbrokenSemVersionRange.Empty;
} else {
var start = range1.Start.ComparePrecedenceTo(range2.Start) == -1 ? range2.Start : range1.Start;
var end = range1.End.ComparePrecedenceTo(range2.End) == -1 ? range1.End : range2.End;
return UnbrokenSemVersionRange.Inclusive(start, end);
}
}

private static SemVersionRange FindSemverRangeOverlap(
SemVersionRange range1,
SemVersionRange range2
)
{
var acc = new List<UnbrokenSemVersionRange>();
foreach (UnbrokenSemVersionRange unbrokenRange1 in range1)
foreach (UnbrokenSemVersionRange unbrokenRange2 in range2)
acc.Add(FindUnbrokenSemverRangeOverlap(unbrokenRange1, unbrokenRange2));

return SemVersionRange.Create(acc);
}

private static bool thereIsSemVersionRangeOverlap(
SemVersionRange range1,
SemVersionRange range2
)
{
var intersection = FindSemverRangeOverlap(range1, range2);
bool foundNonemptyUnbrokenSemVersionRange = false;

foreach (UnbrokenSemVersionRange unbrokenRange in intersection)
{
Console.WriteLine($"Inspecting [{unbrokenRange.Start}, {unbrokenRange.End})...");
if (!unbrokenRange.Equals(UnbrokenSemVersionRange.Empty))
foundNonemptyUnbrokenSemVersionRange = true;
break;
}

return foundNonemptyUnbrokenSemVersionRange;
}

private static void testRangeOverlap()
{
var semver21 = SemVersionRange.Parse("1.2.1");
var semver22 = SemVersionRange.Parse("^1.2.0");

Debug.Assert(thereIsSemVersionRangeOverlap(semver21, semver22));

var semver31 = SemVersionRange.Parse("^1.2.0");
var semver32 = SemVersionRange.Parse("1.2.1");

Debug.Assert(thereIsSemVersionRangeOverlap(semver31, semver32));

var semver41 = UnbrokenSemVersionRange.AtLeast(SemVersion.Parse("1.2.0"));
var semver42 = UnbrokenSemVersionRange.Equals(SemVersion.Parse("1.2.1"));

Console.WriteLine($"semver41: {semver41.Start}, {semver41.End}");
Console.WriteLine($"semver42: {semver42.Start}, {semver42.End}");

var intersection = FindUnbrokenSemverRangeOverlap(semver41, semver42);
Console.WriteLine($"intersection: {intersection.Start}, {intersection.End}");
}
}
}
Loading
Loading