Skip to content

Commit

Permalink
perf: unnecessary exceptions when checking if a project is versionable (
Browse files Browse the repository at this point in the history
#106)

In the case of checking whether a project can be versioned,
the invalid operation exception is swallowed because it is
not really invalid for that use case.  This update only
builds the exception for the Create case and so skips the
cost of the exception in the more common case
  • Loading branch information
smitchamwork committed Oct 21, 2023
1 parent b1cfb7c commit 4714d2f
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions Versionize/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ private Project(string projectFile, SemanticVersion version)

public static Project Create(string projectFile)
{
var version = ReadVersion(projectFile);
var (success,version, error) = ReadVersion(projectFile);

if (!success)
{
throw new InvalidOperationException(error);
}

return new Project(projectFile, version);
}
Expand All @@ -25,33 +30,37 @@ public static bool IsVersionable(string projectFile)
{
try
{
ReadVersion(projectFile);
return true;
var (success, _, _) = ReadVersion(projectFile);
return success;
}
catch (Exception)
{
return false;
}
}

private static SemanticVersion ReadVersion(string projectFile)
private static (bool, SemanticVersion, string) ReadVersion(string projectFile)
{
var doc = ReadProject(projectFile);

var versionString = SelectVersionNode(doc)?.InnerText;

if (string.IsNullOrWhiteSpace(versionString))
{
throw new InvalidOperationException($"Project {projectFile} contains no or an empty <Version> XML Element. Please add one if you want to version this project - for example use <Version>1.0.0</Version>");
return (false,
null,
$"Project {projectFile} contains no or an empty <Version> XML Element. Please add one if you want to version this project - for example use <Version>1.0.0</Version>"
);
}

try
{
return SemanticVersion.Parse(versionString);
return (true, SemanticVersion.Parse(versionString),null);
}
catch (Exception)
{
throw new InvalidOperationException($"Project {projectFile} contains an invalid version {versionString}. Please fix the currently contained version - for example use <Version>1.0.0</Version>");
return (false, null,
$"Project {projectFile} contains an invalid version {versionString}. Please fix the currently contained version - for example use <Version>1.0.0</Version>");
}
}

Expand Down

0 comments on commit 4714d2f

Please sign in to comment.