Skip to content

Commit

Permalink
fixing parsing (#269)
Browse files Browse the repository at this point in the history
Co-authored-by: blazej.kuhajda <blazej.kuhajda@mts.sk>
  • Loading branch information
PTKu and blazej.kuhajda authored Nov 28, 2023
1 parent d1f4013 commit a29123a
Showing 1 changed file with 71 additions and 10 deletions.
81 changes: 71 additions & 10 deletions src/AXSharp.compiler/src/AXSharp.Compiler/AxProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ private static IEnumerable<string> SearchForApaxFiles(string directory, int curr

static bool AreVersionsCompatible(string v1, string v2)
{
bool v1SemverCompliant = Version.TryParse(v1, out Version? versionA);
bool v2SemverCompliant = Version.TryParse(v2, out Version? versionB);

var versionA = ParseVersion(v1);
var versionB = ParseVersion(v2);

if(!v1SemverCompliant || !v2SemverCompliant)

if (versionA == null || versionB == null)
{
return v1?.Trim() == v2?.Trim();
}
Expand Down Expand Up @@ -197,16 +197,24 @@ static bool AreVersionsCompatible(string v1, string v2)
}
}

static Version ParseVersion(string versionString)
static SemVersion ParseVersion(string versionString)
{
// Check for caret or tilde and remove it
if (versionString.StartsWith("^") || versionString.StartsWith("~"))
try
{
versionString = versionString.Substring(1);
// Check for caret or tilde and remove it
if (versionString.StartsWith("^") || versionString.StartsWith("~"))
{
versionString = versionString.Substring(1);
}

// Parsing version string and creating a Version object
return SemVersion.Parse(versionString);
}
catch (Exception)
{

// Parsing version string and creating a Version object
return Version.Parse(versionString);
return null;
}
}


Expand Down Expand Up @@ -270,4 +278,57 @@ private IEnumerable<object> GetProjectDependencies()

return projectDependencies;
}


public class SemVersion
{
public int Major { get; private set; }
public int Minor { get; private set; }
public int Patch { get; private set; }
public string PreRelease { get; private set; }
public string BuildMetadata { get; private set; }

public SemVersion(int major, int minor, int patch, string preRelease = "", string buildMetadata = "")
{
Major = major;
Minor = minor;
Patch = patch;
PreRelease = preRelease;
BuildMetadata = buildMetadata;
}

public override string ToString()
{
string version = $"{Major}.{Minor}.{Patch}";
if (!string.IsNullOrEmpty(PreRelease))
{
version += $"-{PreRelease}";
}
if (!string.IsNullOrEmpty(BuildMetadata))
{
version += $"+{BuildMetadata}";
}
return version;
}

public static SemVersion Parse(string versionStr)
{
string[] mainParts = versionStr.Split(new char[] { '-', '+' }, 3);
string[] versionNumbers = mainParts[0].Split('.');
if (versionNumbers.Length != 3)
{
throw new FormatException("Invalid version format. Expected format is MAJOR.MINOR.PATCH[-PRERELEASE][+BUILDMETADATA]");
}

int major = int.Parse(versionNumbers[0]);
int minor = int.Parse(versionNumbers[1]);
int patch = int.Parse(versionNumbers[2]);

string preRelease = mainParts.Length > 1 ? mainParts[1] : "";
string buildMetadata = mainParts.Length > 2 ? mainParts[2] : "";

return new SemVersion(major, minor, patch, preRelease, buildMetadata);
}
}

}

0 comments on commit a29123a

Please sign in to comment.