Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions dotnet-version-cli.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.4
# Visual Studio Version 17
VisualStudioVersion = 17.6.33723.286
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-version", "src\dotnet-version.csproj", "{F030BEE0-CA2F-43E2-A099-0EA07FD45393}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-version", "src\dotnet-version.csproj", "{1AE7AFF7-E333-4205-AA1B-B8A8A79B4A87}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-version-test", "test\dotnet-version-test.csproj", "{93AA179C-46C2-402A-BA58-7168F7F81FE2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-version-test", "test\dotnet-version-test.csproj", "{FB420ACF-9E12-42B6-B724-1EEE9CBF251E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -16,30 +16,30 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F030BEE0-CA2F-43E2-A099-0EA07FD45393}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F030BEE0-CA2F-43E2-A099-0EA07FD45393}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F030BEE0-CA2F-43E2-A099-0EA07FD45393}.Debug|x64.ActiveCfg = Debug|Any CPU
{F030BEE0-CA2F-43E2-A099-0EA07FD45393}.Debug|x64.Build.0 = Debug|Any CPU
{F030BEE0-CA2F-43E2-A099-0EA07FD45393}.Debug|x86.ActiveCfg = Debug|Any CPU
{F030BEE0-CA2F-43E2-A099-0EA07FD45393}.Debug|x86.Build.0 = Debug|Any CPU
{F030BEE0-CA2F-43E2-A099-0EA07FD45393}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F030BEE0-CA2F-43E2-A099-0EA07FD45393}.Release|Any CPU.Build.0 = Release|Any CPU
{F030BEE0-CA2F-43E2-A099-0EA07FD45393}.Release|x64.ActiveCfg = Release|Any CPU
{F030BEE0-CA2F-43E2-A099-0EA07FD45393}.Release|x64.Build.0 = Release|Any CPU
{F030BEE0-CA2F-43E2-A099-0EA07FD45393}.Release|x86.ActiveCfg = Release|Any CPU
{F030BEE0-CA2F-43E2-A099-0EA07FD45393}.Release|x86.Build.0 = Release|Any CPU
{93AA179C-46C2-402A-BA58-7168F7F81FE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{93AA179C-46C2-402A-BA58-7168F7F81FE2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{93AA179C-46C2-402A-BA58-7168F7F81FE2}.Debug|x64.ActiveCfg = Debug|Any CPU
{93AA179C-46C2-402A-BA58-7168F7F81FE2}.Debug|x64.Build.0 = Debug|Any CPU
{93AA179C-46C2-402A-BA58-7168F7F81FE2}.Debug|x86.ActiveCfg = Debug|Any CPU
{93AA179C-46C2-402A-BA58-7168F7F81FE2}.Debug|x86.Build.0 = Debug|Any CPU
{93AA179C-46C2-402A-BA58-7168F7F81FE2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{93AA179C-46C2-402A-BA58-7168F7F81FE2}.Release|Any CPU.Build.0 = Release|Any CPU
{93AA179C-46C2-402A-BA58-7168F7F81FE2}.Release|x64.ActiveCfg = Release|Any CPU
{93AA179C-46C2-402A-BA58-7168F7F81FE2}.Release|x64.Build.0 = Release|Any CPU
{93AA179C-46C2-402A-BA58-7168F7F81FE2}.Release|x86.ActiveCfg = Release|Any CPU
{93AA179C-46C2-402A-BA58-7168F7F81FE2}.Release|x86.Build.0 = Release|Any CPU
{1AE7AFF7-E333-4205-AA1B-B8A8A79B4A87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1AE7AFF7-E333-4205-AA1B-B8A8A79B4A87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1AE7AFF7-E333-4205-AA1B-B8A8A79B4A87}.Debug|x64.ActiveCfg = Debug|Any CPU
{1AE7AFF7-E333-4205-AA1B-B8A8A79B4A87}.Debug|x64.Build.0 = Debug|Any CPU
{1AE7AFF7-E333-4205-AA1B-B8A8A79B4A87}.Debug|x86.ActiveCfg = Debug|Any CPU
{1AE7AFF7-E333-4205-AA1B-B8A8A79B4A87}.Debug|x86.Build.0 = Debug|Any CPU
{1AE7AFF7-E333-4205-AA1B-B8A8A79B4A87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1AE7AFF7-E333-4205-AA1B-B8A8A79B4A87}.Release|Any CPU.Build.0 = Release|Any CPU
{1AE7AFF7-E333-4205-AA1B-B8A8A79B4A87}.Release|x64.ActiveCfg = Release|Any CPU
{1AE7AFF7-E333-4205-AA1B-B8A8A79B4A87}.Release|x64.Build.0 = Release|Any CPU
{1AE7AFF7-E333-4205-AA1B-B8A8A79B4A87}.Release|x86.ActiveCfg = Release|Any CPU
{1AE7AFF7-E333-4205-AA1B-B8A8A79B4A87}.Release|x86.Build.0 = Release|Any CPU
{FB420ACF-9E12-42B6-B724-1EEE9CBF251E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB420ACF-9E12-42B6-B724-1EEE9CBF251E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB420ACF-9E12-42B6-B724-1EEE9CBF251E}.Debug|x64.ActiveCfg = Debug|Any CPU
{FB420ACF-9E12-42B6-B724-1EEE9CBF251E}.Debug|x64.Build.0 = Debug|Any CPU
{FB420ACF-9E12-42B6-B724-1EEE9CBF251E}.Debug|x86.ActiveCfg = Debug|Any CPU
{FB420ACF-9E12-42B6-B724-1EEE9CBF251E}.Debug|x86.Build.0 = Debug|Any CPU
{FB420ACF-9E12-42B6-B724-1EEE9CBF251E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB420ACF-9E12-42B6-B724-1EEE9CBF251E}.Release|Any CPU.Build.0 = Release|Any CPU
{FB420ACF-9E12-42B6-B724-1EEE9CBF251E}.Release|x64.ActiveCfg = Release|Any CPU
{FB420ACF-9E12-42B6-B724-1EEE9CBF251E}.Release|x64.Build.0 = Release|Any CPU
{FB420ACF-9E12-42B6-B724-1EEE9CBF251E}.Release|x86.ActiveCfg = Release|Any CPU
{FB420ACF-9E12-42B6-B724-1EEE9CBF251E}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
39 changes: 28 additions & 11 deletions src/CsProj/ProjectFileParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,46 @@ namespace Skarp.Version.Cli.CsProj
public class ProjectFileParser
{
public virtual string PackageName { get; private set; }

public virtual string PackageVersion { get; private set; }

public virtual string Version { get; private set; }


public virtual string VersionPrefix { get; private set; }

public virtual string VersionSuffix { get; private set; }

public ProjectFileProperty VersionSource { get
{
return !string.IsNullOrEmpty(Version) ? ProjectFileProperty.Version : ProjectFileProperty.VersionPrefix;
}
}

private IEnumerable<XElement> _propertyGroup { get; set; }

public virtual void Load(string xmlDocument, ProjectFileProperty property)
{
LoadPropertyGroup(xmlDocument);

var propertyElement = LoadProperty(property);
switch(property)
XElement propertyElement = LoadProperty(property);

switch (property)
{
case ProjectFileProperty.Version:
Version = propertyElement?.Value ?? "0.0.0";
Version = propertyElement?.Value ?? string.Empty;
break;
case ProjectFileProperty.PackageVersion:
PackageVersion = propertyElement?.Value ?? Version;
PackageVersion = propertyElement?.Value ?? string.Empty;
break;
case ProjectFileProperty.Title:
var defaultPropertyElement = LoadProperty(ProjectFileProperty.PackageId);
PackageName = propertyElement?.Value ?? defaultPropertyElement?.Value ?? string.Empty;

break;
case ProjectFileProperty.VersionPrefix:
VersionPrefix = propertyElement?.Value ?? string.Empty;
break;
case ProjectFileProperty.VersionSuffix:
VersionSuffix = propertyElement?.Value ?? string.Empty;
break;
}
}
Expand All @@ -47,8 +62,10 @@ public virtual void Load(string xmlDocument, params ProjectFileProperty[] proper
ProjectFileProperty.Version,
ProjectFileProperty.PackageId,
ProjectFileProperty.PackageVersion,
ProjectFileProperty.VersionPrefix,
ProjectFileProperty.VersionSuffix
};
}
}
// Try to load xmlDocument even if there is no properties to be loaded
// in order to verify if project file is well formed
LoadPropertyGroup(xmlDocument);
Expand All @@ -61,7 +78,7 @@ public virtual void Load(string xmlDocument, params ProjectFileProperty[] proper

private XElement LoadProperty(ProjectFileProperty property)
{
var propertyElement = (
XElement propertyElement = (
from prop in _propertyGroup.Elements()
where prop.Name == property.ToString("g")
select prop
Expand Down
2 changes: 2 additions & 0 deletions src/CsProj/ProjectFileProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ public enum ProjectFileProperty
PackageVersion,
PackageId,
Title,
VersionPrefix,
VersionSuffix
}
}
17 changes: 7 additions & 10 deletions src/CsProj/ProjectFileVersionPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public ProjectFileVersionPatcher(IFileSystemProvider fileSystem)
{
_fileSystem = fileSystem;
}

public virtual void Load(string xmlDocument)
{
_doc = XDocument.Parse(xmlDocument, LoadOptions.PreserveWhitespace);
Expand All @@ -23,23 +23,20 @@ public virtual void Load(string xmlDocument)
/// <summary>
/// Replace the existing version number in the csproj xml with the new version
/// </summary>
/// <param name="oldVersion">The old version number present in the xml</param>
/// <param name="newVersion">The new version number to persist in the csproj file</param>
/// <param name="newValue">The new version number to persist in the csproj file</param>
/// <returns></returns>
public virtual void PatchVersionField(string oldVersion, string newVersion)
public virtual void PatchField(string newValue, ProjectFileProperty versionField)
{
var elementName = "Version";
PatchGenericField(elementName, oldVersion, newVersion);
PatchGenericField(versionField.ToString(), newValue);
}

/// <summary>
/// Helper method for patching up a generic XML field in the loaded XML
/// </summary>
/// <param name="elementName">The name to find and update or add it to the tree</param>
/// <param name="oldVal">Old value</param>
/// <param name="newVal">New value</param>
/// <exception cref="InvalidOperationException"></exception>
private void PatchGenericField(string elementName, string oldVal, string newVal)
private void PatchGenericField(string elementName, string newVal)
{
if (_doc == null)
{
Expand All @@ -49,7 +46,7 @@ private void PatchGenericField(string elementName, string oldVal, string newVal)
// If the element is not present, add it to the XML document (csproj file
if (!ContainsElement(elementName))
{
AddMissingElementToCsProj(elementName, oldVal);
AddMissingElementToCsProj(elementName, newVal);
}

var elm = _doc.Descendants(elementName).First();
Expand All @@ -61,7 +58,7 @@ private bool ContainsElement(string elementName)
var nodes = _doc.Descendants(elementName);
return nodes.Any();
}

private void AddMissingElementToCsProj(string elementName, string value)
{
// try to locate the PropertyGroup where the element belongs
Expand Down
2 changes: 1 addition & 1 deletion src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ string vcsTag
if (Enum.TryParse(arg, true, out bump)) break;

var ver = SemVer.FromString(arg);
args.SpecificVersionToApply = ver.ToSemVerVersionString();
args.SpecificVersionToApply = ver.ToSemVerVersionString(null);
bump = VersionBump.Specific;
}

Expand Down
7 changes: 6 additions & 1 deletion src/VersionBump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public enum VersionBump
/// <summary>
/// Apply a specific, given, version to the project file
/// </summary>
Specific
Specific,

/// <summary>
/// Do not apply any changes.
/// </summary>
None
}
}
30 changes: 19 additions & 11 deletions src/VersionCli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,24 @@ public VersionInfo Execute(VersionCliArgs args)
}

var csProjXml = _fileDetector.FindAndLoadCsProj(args.CsProjFilePath);
_fileParser.Load(csProjXml, ProjectFileProperty.Version, ProjectFileProperty.PackageVersion);
_fileParser.Load(
csProjXml,
args.ProjectFilePropertyName);



var currentSemVer = SemVer.FromString(
_fileParser.VersionSource == ProjectFileProperty.Version ?
_fileParser.Version :
_fileParser.VersionPrefix);

var semVer = _bumper.Bump(
SemVer.FromString(_fileParser.PackageVersion),
var bumpedSemVer = _bumper.Bump(
currentSemVer,
args.VersionBump,
args.SpecificVersionToApply,
args.BuildMeta,
args.PreReleasePrefix
);
var versionString = semVer.ToSemVerVersionString();

var theOutput = new VersionInfo
{
Expand All @@ -67,8 +75,8 @@ public VersionInfo Execute(VersionCliArgs args)
Name = ProductInfo.Name,
Version = ProductInfo.Version
},
OldVersion = _fileParser.PackageVersion,
NewVersion = versionString,
OldVersion = currentSemVer.ToSemVerVersionString(_fileParser),
NewVersion = bumpedSemVer.ToSemVerVersionString(_fileParser),
ProjectFile = _fileDetector.ResolvedCsProjFile,
VersionStrategy = args.VersionBump.ToString().ToLowerInvariant()
};
Expand All @@ -77,9 +85,9 @@ public VersionInfo Execute(VersionCliArgs args)
{
_fileVersionPatcher.Load(csProjXml);

_fileVersionPatcher.PatchVersionField(
_fileParser.Version,
versionString
_fileVersionPatcher.PatchField(
bumpedSemVer.ToSemVerVersionString(_fileParser),
_fileParser.VersionSource
);

_fileVersionPatcher.Flush(
Expand All @@ -101,11 +109,11 @@ public VersionInfo Execute(VersionCliArgs args)
}
else if (args.OutputFormat == OutputFormat.Bare)
{
Console.WriteLine(versionString);
Console.WriteLine(bumpedSemVer.ToSemVerVersionString(_fileParser));
}
else
{
Console.WriteLine($"Bumped {_fileDetector.ResolvedCsProjFile} to version {versionString}");
Console.WriteLine($"Bumped {_fileDetector.ResolvedCsProjFile} to version {bumpedSemVer.ToSemVerVersionString(_fileParser)}");
}

return theOutput;
Expand Down
9 changes: 8 additions & 1 deletion src/VersionCliArgs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Skarp.Version.Cli
using Skarp.Version.Cli.CsProj;

namespace Skarp.Version.Cli
{
public class VersionCliArgs
{
Expand Down Expand Up @@ -40,5 +42,10 @@ public class VersionCliArgs
/// Override for the default `v<version>` vcs tag
/// </summary>
public string VersionControlTag { get; set; }

/// <summary>
/// Specify the Version-Tag that should be targeted. Default is PackageVersion
/// </summary>
public ProjectFileProperty ProjectFilePropertyName { get; set; } = ProjectFileProperty.PackageVersion;
}
}
20 changes: 17 additions & 3 deletions src/Versioning/SemVer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Skarp.Version.Cli.CsProj;
using System;
using System.Text;
using System.Text.RegularExpressions;
Expand All @@ -20,7 +21,7 @@ public class SemVer
/// Serialize the parsed version information into a SemVer version string including pre and build meta
/// </summary>
/// <returns></returns>
public string ToSemVerVersionString()
public string ToSemVerVersionString(ProjectFileParser projectFileParser)
{
var sb = new StringBuilder();
sb.Append($"{Major}.{Minor}.{Patch}");
Expand All @@ -34,9 +35,16 @@ public string ToSemVerVersionString()
}
}

if (projectFileParser != null
&& projectFileParser.VersionSource == ProjectFileProperty.VersionPrefix
&& !string.IsNullOrWhiteSpace(projectFileParser.VersionSuffix))
{
sb.AppendFormat("-{0}", projectFileParser.VersionSuffix);
}

return sb.ToString();
}

/// <summary>
/// Create a new instance of a SemVer based off the version string
/// </summary>
Expand All @@ -57,10 +65,15 @@ public static SemVer FromString(string versionString)
Minor = Convert.ToInt32(matches.Groups[2].Value),
Patch = Convert.ToInt32(matches.Groups[3].Value),
PreRelease = matches.Groups[4].Value,
BuildMeta = matches.Groups[5].Value,
BuildMeta = matches.Groups[5].Value
};
}

public object Clone()
{
return this.MemberwiseClone();
}

/// <summary>
/// The parsed major version
/// </summary>
Expand Down Expand Up @@ -88,5 +101,6 @@ public static SemVer FromString(string versionString)
/// Build mtadata semver 2 information (the stuff added with a + sign after PreRelease info)
/// </summary>
public string BuildMeta { get; set; }

}
}
5 changes: 4 additions & 1 deletion src/Versioning/SemVerBumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public SemVer Bump(
string preReleasePrefix = ""
)
{
var newVersion = SemVer.FromString(currentVersion.ToSemVerVersionString());
var newVersion = (SemVer)currentVersion.Clone();
newVersion.BuildMeta = buildMeta;

switch (bump)
Expand Down Expand Up @@ -65,6 +65,9 @@ public SemVer Bump(
HandleSpecificVersion(specificVersionToApply, newVersion);
break;
}
case VersionBump.None:
//Do nothing;
break;
default:
{
throw new ArgumentOutOfRangeException(nameof(bump), $"VersionBump : {bump} not supported");
Expand Down
Loading