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
1 change: 1 addition & 0 deletions cmf-cli/Commands/BaseCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using Cmf.CLI.Utilities;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
Expand Down
108 changes: 78 additions & 30 deletions cmf-cli/Commands/bump/BumpCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using NuGet.Versioning;
using Cmf.CLI.Constants;
using Cmf.CLI.Core.Attributes;
using Cmf.CLI.Core;
using Cmf.CLI.Core.Interfaces;
using Cmf.CLI.Core.Objects;
using Cmf.CLI.Factories;
Expand All @@ -9,6 +12,7 @@
using System.CommandLine;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Threading.Tasks;

namespace Cmf.CLI.Commands
Expand All @@ -20,6 +24,60 @@ namespace Cmf.CLI.Commands
[CmfCommand("bump", Id = "bump")]
public class BumpCommand : BaseCommand
{
internal static void ValidateVersionOptions<T>(Command command, Option<T> version,
Option<string> build, Option<string> prerelease, bool iot = false, bool requireVersion = true, bool npm = false)
where T : SemanticVersion
{
command.Validators.Add(result =>
{
if (result.GetResult(version)?.Errors.Any() == true ||
result.GetResult(build)?.Errors.Any() == true ||
result.GetResult(prerelease)?.Errors.Any() == true) return;
try
{
ValidateBumpVersion(VersionUtilities.GetOptionalVersion(result, version), result.GetValue(build),
result.GetValue(prerelease), iot, requireVersion, npm);
}
catch (CliException exception)
{
result.AddError(exception.Message);
}
catch (InvalidOperationException)
{
// Failed option parsers already supply their own diagnostics.
}
});
}

private static void ValidateBumpVersion(SemanticVersion version, string build, string prerelease,
bool iot = false, bool requireVersion = true, bool npm = false)
{
if (requireVersion && version == null)
throw new CliException(string.Format(CliMessages.MissingMandatoryProperty, "version"));
var buildField = iot ? "--buildNrVersion" : "--buildNr";
var prereleaseField = iot ? "--preReleaseVersion" : "--preRelease";
if (!string.IsNullOrEmpty(build) && !string.IsNullOrEmpty(prerelease))
throw new CliException(string.Format(CliMessages.MutuallyExclusiveProperties,
$"{buildField.TrimStart('-')}, {prereleaseField.TrimStart('-')}"));
var suffix = !string.IsNullOrEmpty(build) ? build : prerelease;
if (version != null)
{
if (!string.IsNullOrEmpty(suffix))
{
var finalVersion = GenericUtilities.RetrieveNewVersion(null, VersionUtilities.GetVersionText(version), suffix);
if (npm) VersionUtilities.ParseNpmVersion(finalVersion, "--version");
else VersionUtilities.ParseNuGetVersion(finalVersion, "--version");
}
}
}

/// <summary>Creates the command.</summary>
public BumpCommand() { }

/// <summary>Creates the command with a filesystem.</summary>
/// <param name="fileSystem">The project filesystem.</param>
public BumpCommand(IFileSystem fileSystem) : base(fileSystem) { }

/// <summary>
/// Configure command
/// </summary>
Expand All @@ -33,23 +91,26 @@ public override void Configure(Command cmd)
};
cmd.Add(packagePathArgument);

var versionOption = new Option<string>("--version", "-v")
var versionOption = new Option<NuGetVersion>("--version", "-v")
{
Description = "Will bump all versions to the version specified"
Description = "Will bump all versions to the version specified",
CustomParser = result => VersionUtilities.ParseVersion(result, text => VersionUtilities.ParseNuGetVersion(text, "--version"))
};
cmd.Add(versionOption);

var buildNrOption = new Option<string>("--buildNr", "-b")
var buildOption = new Option<string>("--buildNr", "-b")
{
Description = "Will add this version next to the version (v-b)"
Description = "Will add this version next to the version (v-b)",
CustomParser = result => VersionUtilities.ParseSuffix(result, "--buildNr")
};
cmd.Add(buildNrOption);

var preReleaseOption = new Option<string>("--preRelease", "-p")
var prereleaseOption = new Option<string>("--preRelease", "-p")
{
Description = "Will add this version as pre-release version (v-p)"
Description = "Will add this version as pre-release version (v-p)",
CustomParser = result => VersionUtilities.ParseSuffix(result, "--preRelease")
};
cmd.Add(preReleaseOption);
cmd.Add(versionOption);
cmd.Add(buildOption);
cmd.Add(prereleaseOption);
BumpCommand.ValidateVersionOptions(cmd, versionOption, buildOption, prereleaseOption,
iot: false, requireVersion: true, npm: false);

var rootOption = new Option<string>("--root", "-r")
{
Expand All @@ -61,12 +122,10 @@ public override void Configure(Command cmd)
cmd.SetAction((parseResult, cancellationToken) =>
{
var packagePath = parseResult.GetValue(packagePathArgument);
var version = parseResult.GetValue(versionOption);
var buildNr = parseResult.GetValue(buildNrOption);
var preRelease = parseResult.GetValue(preReleaseOption);
var root = parseResult.GetValue(rootOption);

Execute(packagePath, version, buildNr, preRelease, root);
Execute(packagePath, parseResult.GetValue(versionOption),
parseResult.GetValue(buildOption), parseResult.GetValue(prereleaseOption), root);
return Task.FromResult(0);
});
}
Expand All @@ -81,21 +140,11 @@ public override void Configure(Command cmd)
/// <param name="root">The root.</param>
/// <exception cref="CliException"></exception>
/// <exception cref="CliException"></exception>
public void Execute(DirectoryInfo packagePath, string version, string buildNr, string preRelease, string root)
public void Execute(DirectoryInfo packagePath, NuGetVersion version, string buildNr, string preRelease, string root)
{
using var activity = ExecutionContext.ServiceProvider?.GetService<ITelemetryService>()?.StartExtendedActivity(this.GetType().Name);
IFileInfo cmfpackageFile = this.fileSystem.FileInfo.New($"{packagePath}/{CliConstants.CmfPackageFileName}");

if (string.IsNullOrEmpty(version) && (string.IsNullOrEmpty(buildNr) || string.IsNullOrEmpty(preRelease)))
{
throw new CliException(string.Format(CliMessages.MissingMandatoryProperties, "version, buildNr, preRelease"));
}

if (!string.IsNullOrEmpty(buildNr) && !string.IsNullOrEmpty(preRelease))
{
throw new CliException(string.Format(CliMessages.MutuallyExclusiveProperties, "buildNr, preRelease"));
}

// Reading cmfPackage
CmfPackage cmfPackage = CmfPackage.Load(cmfpackageFile);

Expand All @@ -110,8 +159,9 @@ public void Execute(DirectoryInfo packagePath, string version, string buildNr, s
/// <param name="buildNr">The version for build Nr.</param>
/// <param name="root">The root.</param>
/// <exception cref="CliException"></exception>
public void Execute(CmfPackage cmfPackage, string version, string buildNr, string preRelease, string root)
public void Execute(CmfPackage cmfPackage, NuGetVersion version, string buildNr, string preRelease, string root)
{
var suffix = !string.IsNullOrEmpty(buildNr) ? buildNr : preRelease;
IDirectoryInfo packageDirectory = cmfPackage.GetFileInfo().Directory;
IPackageTypeHandler packageTypeHandler = PackageTypeFactory.GetPackageTypeHandler(cmfPackage);

Expand All @@ -121,9 +171,7 @@ public void Execute(CmfPackage cmfPackage, string version, string buildNr, strin
{ "root", root }
};

string versionSuffix = !string.IsNullOrEmpty(buildNr) ? buildNr : preRelease;

packageTypeHandler.Bump(version, versionSuffix, bumpInformation);
packageTypeHandler.Bump(VersionUtilities.GetVersionText(version), suffix, bumpInformation);

// will save with new version
cmfPackage.SaveCmfPackage();
Expand Down
65 changes: 34 additions & 31 deletions cmf-cli/Commands/bump/BumpIoTConfigurationCommand.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using NuGet.Versioning;
using System;
using System.Collections.Generic;
using System.CommandLine;
Expand All @@ -20,6 +21,13 @@ namespace Cmf.CLI.Commands
[CmfCommand(name: "configuration", ParentId = "bump_iot")]
public class BumpIoTConfigurationCommand : BumpCommand
{
/// <summary>Creates the command.</summary>
public BumpIoTConfigurationCommand() { }

/// <summary>Creates the command with a filesystem.</summary>
/// <param name="fileSystem">The project filesystem.</param>
public BumpIoTConfigurationCommand(IFileSystem fileSystem) : base(fileSystem) { }

/// <summary>
/// Configure command
/// </summary>
Expand All @@ -34,23 +42,26 @@ public override void Configure(Command cmd)
};
cmd.Add(pathArgument);

var versionOption = new Option<string>("--version", "-v")
{
Description = "Will bump all versions to the version specified"
};
cmd.Add(versionOption);

var buildNrVersionOption = new Option<string>("--buildNrVersion", "-b")
{
Description = "Will add this version next to the version (v-b)"
};
cmd.Add(buildNrVersionOption);

var preReleaseVersionOption = new Option<string>("--preReleaseVersion", "-p")
{
Description = "Will add this version as pre-release version (v-p)"
};
cmd.Add(preReleaseVersionOption);
var versionOption = new Option<NuGetVersion>("--version", "-v")
{
Description = "Will bump all versions to the version specified",
CustomParser = result => VersionUtilities.ParseVersion(result, text => string.IsNullOrEmpty(text) ? null : VersionUtilities.ParseNuGetVersion(text, "--version"))
};
var buildOption = new Option<string>("--buildNrVersion", "-b")
{
Description = "Will add this version next to the version (v-b)",
CustomParser = result => VersionUtilities.ParseSuffix(result, "--buildNrVersion")
};
var prereleaseOption = new Option<string>("--preReleaseVersion", "-p")
{
Description = "Will add this version as pre-release version (v-p)",
CustomParser = result => VersionUtilities.ParseSuffix(result, "--preReleaseVersion")
};
cmd.Add(versionOption);
cmd.Add(buildOption);
cmd.Add(prereleaseOption);
BumpCommand.ValidateVersionOptions(cmd, versionOption, buildOption, prereleaseOption,
iot: true, requireVersion: false, npm: false);

var masterDataOption = new Option<bool>("--masterData", "-md")
{
Expand Down Expand Up @@ -108,9 +119,6 @@ public override void Configure(Command cmd)
cmd.SetAction((parseResult, cancellationToken) =>
{
var path = parseResult.GetValue(pathArgument);
var version = parseResult.GetValue(versionOption);
var buildNr = parseResult.GetValue(buildNrVersionOption);
var preReleaseVersion = parseResult.GetValue(preReleaseVersionOption);
var isToBumpMasterdata = parseResult.GetValue(masterDataOption);
var isToBumpIoT = parseResult.GetValue(iotOption);
var packageNames = parseResult.GetValue(packageNamesOption);
Expand All @@ -120,7 +128,8 @@ public override void Configure(Command cmd)
var isToTag = parseResult.GetValue(isToTagOption);
var onlyMdCustomization = parseResult.GetValue(mdCustomizationOption);

Execute(path, version, buildNr, preReleaseVersion, isToBumpMasterdata, isToBumpIoT, packageNames, root, group, workflowName, isToTag, onlyMdCustomization);
Execute(path, VersionUtilities.GetOptionalVersion(parseResult.CommandResult, versionOption),
parseResult.GetValue(buildOption), parseResult.GetValue(prereleaseOption), isToBumpMasterdata, isToBumpIoT, packageNames, root, group, workflowName, isToTag, onlyMdCustomization);
return Task.FromResult(0);
});
}
Expand All @@ -142,8 +151,9 @@ public override void Configure(Command cmd)
/// <param name="onlyMdCustomization">if set to <c>true</c> [only md customization].</param>
/// <exception cref="CliException"></exception>
/// <returns></returns>
public void Execute(IDirectoryInfo path, string version, string buildNrVersion, string preReleaseVersion, bool isToBumpMasterdata, bool isToBumpIoT, string packageNames, string root, string group, string workflowName, bool isToTag, bool onlyMdCustomization)
public void Execute(IDirectoryInfo path, NuGetVersion version, string buildNrVersion, string preReleaseVersion, bool isToBumpMasterdata, bool isToBumpIoT, string packageNames, string root, string group, string workflowName, bool isToTag, bool onlyMdCustomization)
{
var suffix = !string.IsNullOrEmpty(buildNrVersion) ? buildNrVersion : preReleaseVersion;
using var activity = ExecutionContext.ServiceProvider?.GetService<ITelemetryService>()?.StartExtendedActivity(this.GetType().Name);

// Get All AutomationWorkflowFiles Folders
Expand All @@ -159,13 +169,6 @@ public void Execute(IDirectoryInfo path, string version, string buildNrVersion,
automationWorkflowDirectories = automationWorkflowDirectories.Where(awf => awf.Contains(root))?.ToList();
}

if (!string.IsNullOrEmpty(buildNrVersion) && !string.IsNullOrEmpty(preReleaseVersion))
{
throw new CliException(string.Format(CliMessages.MutuallyExclusiveProperties, "buildNrVersion, preReleaseVersion"));
}

string versionSuffix = !string.IsNullOrEmpty(buildNrVersion) ? buildNrVersion : preReleaseVersion;

foreach (string automationWorkflowDirectory in automationWorkflowDirectories)
{
#region Bump AutomationWorkflow
Expand All @@ -180,7 +183,7 @@ public void Execute(IDirectoryInfo path, string version, string buildNrVersion,
groups = groups.Where(gr => gr.Contains(group)).ToList();
}

groups.ForEach(group => IoTUtilities.BumpWorkflowFiles(group, version, versionSuffix, workflowName, packageNames, this.fileSystem));
groups.ForEach(group => IoTUtilities.BumpWorkflowFiles(group, VersionUtilities.GetVersionText(version), suffix, workflowName, packageNames, this.fileSystem));
}

#endregion Bump AutomationWorkflow
Expand All @@ -189,7 +192,7 @@ public void Execute(IDirectoryInfo path, string version, string buildNrVersion,

if (isToBumpMasterdata)
{
IoTUtilities.BumpIoTMasterData(automationWorkflowDirectory, version, versionSuffix, this.fileSystem, onlyCustomization: onlyMdCustomization);
IoTUtilities.BumpIoTMasterData(automationWorkflowDirectory, VersionUtilities.GetVersionText(version), suffix, this.fileSystem, onlyCustomization: onlyMdCustomization);
}

#endregion Bump IoT Masterdata
Expand Down
Loading
Loading