Skip to content
Open
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
23 changes: 15 additions & 8 deletions cmf-cli/Builders/ProcessCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
using System;
using System.IO.Abstractions;
using System.Linq;
using System.Media;
using System.Threading.Tasks;

namespace Cmf.CLI.Builders
{
/// <summary>
///
/// </summary>
public abstract class ProcessCommand : IProcessCommand
{
/// <summary>
/// the underlying file system
/// the underlying file system
/// </summary>
protected IFileSystem fileSystem = new FileSystem();

/// <summary>
/// Gets or sets the working directory.
/// Gets or sets the working directory.
/// </summary>
/// <value>
/// The working directory.
/// The working directory.
/// </value>
public IDirectoryInfo WorkingDirectory { get; set; }

Expand All @@ -33,9 +33,10 @@ public virtual bool Condition()
}

/// <summary>
/// Executes this instance.
/// Executes this instance.
/// </summary>
/// <returns></returns>
/// <returns>
/// </returns>
public Task Exec()
{
foreach (var step in this.GetSteps())
Expand Down Expand Up @@ -88,6 +89,11 @@ public Task Exec()
ps.WaitForExit();
if (ps.ExitCode != 0)
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
// SoundPlayer is only supported on windows
new SoundPlayer(@"C:\Windows\Media\chord.wav").PlaySync();
}
throw new CliException($"Command '{command} {String.Join(' ', step.Args)}' did not finish successfully: Exit code {ps.ExitCode}. Please check the log for more details");
}
ps.Dispose();
Expand All @@ -102,9 +108,10 @@ public Task Exec()
}

/// <summary>
/// Gets the steps.
/// Gets the steps.
/// </summary>
/// <returns></returns>
/// <returns>
/// </returns>
public abstract ProcessBuildStep[] GetSteps();
}
}
106 changes: 84 additions & 22 deletions cmf-cli/Commands/bump/BumpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
namespace Cmf.CLI.Commands
{
/// <summary>
///
/// </summary>
/// <seealso cref="BaseCommand" />
/// <seealso cref="BaseCommand"/>
[CmfCommand("bump", Id = "bump")]
public class BumpCommand : BaseCommand
{
/// <summary>
/// Configure command
/// Configure command
/// </summary>
/// <param name="cmd"></param>
/// <param name="cmd">
/// </param>
public override void Configure(Command cmd)
{
cmd.AddArgument(new Argument<DirectoryInfo>(
Expand All @@ -43,20 +43,44 @@ public override void Configure(Command cmd)
aliases: new string[] { "-r", "--root" },
description: "Will bump only versions under a specific root folder (i.e. 1.0.0)"));

cmd.AddOption(new Option<bool>(
aliases: new string[] { "-p", "--parentDep" },
getDefaultValue: () => false,
description: "Will bump the dependency version on parent packages"));

cmd.AddOption(new Option<bool>(
aliases: new string[] { "-f", "--renameVersionFolders" },
getDefaultValue: () => false,
description: "Will rename Version folders from old to new version"));

// Add the handler
cmd.Handler = CommandHandler.Create<DirectoryInfo, string, string, string>(Execute);
cmd.Handler = CommandHandler.Create<DirectoryInfo, string, string, string, bool, bool>(Execute);
}

/// <summary>
/// Executes the specified package path.
/// Executes the specified package path.
/// </summary>
/// <param name="packagePath">The package path.</param>
/// <param name="version">The version.</param>
/// <param name="buildNr">The version for build Nr.</param>
/// <param name="root">The root.</param>
/// <exception cref="CliException"></exception>
/// <exception cref="CliException"></exception>
public void Execute(DirectoryInfo packagePath, string version, string buildNr, string root)
/// <param name="packagePath">
/// The package path.
/// </param>
/// <param name="version">
/// The version.
/// </param>
/// <param name="buildNr">
/// The version for build Nr.
/// </param>
/// <param name="root">
/// The root.
/// </param>
/// <param name="parentDep">
/// Flag indicating if the dependency version in all parent packages will be updated.
/// </param>
/// <param name="renameVersionFolders">
/// Flag indicating if version folders will be renamed.
/// </param>
/// <exception cref="CliException">
/// </exception>
public void Execute(DirectoryInfo packagePath, string version, string buildNr, string root, bool parentDep, bool renameVersionFolders)
{
using var activity = ExecutionContext.ServiceProvider?.GetService<ITelemetryService>()?.StartExtendedActivity(this.GetType().Name);
IFileInfo cmfpackageFile = this.fileSystem.FileInfo.New($"{packagePath}/{CliConstants.CmfPackageFileName}");
Expand All @@ -69,20 +93,44 @@ public void Execute(DirectoryInfo packagePath, string version, string buildNr, s
// Reading cmfPackage
CmfPackage cmfPackage = CmfPackage.Load(cmfpackageFile);

Execute(cmfPackage, version, buildNr, root);
Execute(
cmfPackage,
version,
buildNr,
root,
packagesToUpdateDep: parentDep ? FileSystemUtilities.GetAllPackages(fileSystem) : null,
renameVersionFolders: renameVersionFolders
);
}

/// <summary>
/// Executes the specified CMF package.
/// Executes the specified CMF package.
/// </summary>
/// <param name="cmfPackage">The CMF package.</param>
/// <param name="version">The version.</param>
/// <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 root)
/// <param name="cmfPackage">
/// The CMF package.
/// </param>
/// <param name="version">
/// The version.
/// </param>
/// <param name="buildNr">
/// The version for build Nr.
/// </param>
/// <param name="root">
/// The root.
/// </param>
/// <param name="packagesToUpdateDep">
/// <para>CmfPackages to check and update dependency version.</para>
/// <para>Default = null (means that dependency version wont be updated in any package)</para>
/// </param>
/// <param name="renameVersionFolders">
/// Flag indicating if version folders will be renamed.
/// </param>
/// <exception cref="CliException">
/// </exception>
public void Execute(CmfPackage cmfPackage, string version, string buildNr, string root, CmfPackageCollection packagesToUpdateDep = null, bool renameVersionFolders = false)
{
IDirectoryInfo packageDirectory = cmfPackage.GetFileInfo().Directory;
string oldVersion = cmfPackage.Version;

IPackageTypeHandler packageTypeHandler = PackageTypeFactory.GetPackageTypeHandler(cmfPackage);

// Will execute specific bump code per Package Type
Expand All @@ -95,6 +143,20 @@ public void Execute(CmfPackage cmfPackage, string version, string buildNr, strin

// will save with new version
cmfPackage.SaveCmfPackage();

// Update Version in package dependencies
if (packagesToUpdateDep.HasAny())
{
foreach (CmfPackage packageToUpdate in packagesToUpdateDep)
{
packageToUpdate.UpdateDependency(cmfPackage);
}
}

if (renameVersionFolders)
{
cmfPackage.RenameVersionFolders(oldVersion);
}
}
}
}
84 changes: 84 additions & 0 deletions cmf-cli/Commands/bump/BumpInteractiveCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Cmf.CLI.Core;
using Cmf.CLI.Core.Attributes;
using Cmf.CLI.Core.Objects;
using Cmf.CLI.Utilities;
using System;
using System.CommandLine;
using System.CommandLine.NamingConventionBinder;

namespace Cmf.CLI.Commands.Bump
{
/// <summary>
/// BumpInteractiveCommand
/// </summary>
/// <seealso cref="BaseCommand"/>
[CmfCommand("interactive", Id = "bump_interactive", ParentId = "bump")]
public class BumpInteractiveCommand : BaseCommand
{
/// <summary>
/// Configure command
/// </summary>
/// <param name="cmd">
/// Command
/// </param>
public override void Configure(Command cmd)
{
cmd.Handler = CommandHandler.Create(Execute);
}

/// <summary>
/// Executes the command
/// </summary>
public void Execute()
{
CmfPackageCollection allPackages = FileSystemUtilities.GetAllPackages(fileSystem);

foreach (CmfPackage package in allPackages)
{
Log.Verbose("");
Log.Verbose($"{package.PackageId}");
Log.Verbose($"Current version: {package.Version}");
string option = GenericUtilities.ReadStringValueFromConsole(prompt: "Bump? (Y/n):", allowEmptyValueInput: true, allowedValues: new string[] { "y", "n" });

if (string.IsNullOrEmpty(option) || option.IgnoreCaseEquals("y"))
{
Version version = new Version(package.Version);
string newVersion = string.Empty;
switch (GenericUtilities.ReadStringValueFromConsole(prompt: "Version to bump (a - Major) (i - Minor) (p - Patch):", allowedValues: new string[] { "a", "i", "p" }))
{
case "a":
{
newVersion = $"{version.Major + 1}.0.0";
break;
}
case "i":
{
newVersion = $"{version.Major}.{version.Minor + 1}.0";
break;
}
case "p":
{
newVersion = $"{version.Major}.{version.Minor}.{version.Build + 1}";
break;
}
}

string updateDepInParOption = GenericUtilities.ReadStringValueFromConsole(prompt: "Update in Parent Packages? (Y/n):", allowEmptyValueInput: true, allowedValues: new string[] { "y", "n" });
string renameVersionFoldersOption = GenericUtilities.ReadStringValueFromConsole(prompt: "Rename Version folders? (Y/n):", allowEmptyValueInput: true, allowedValues: new string[] { "y", "n" });

// Change Current Directory because BumpCommand uses it
Environment.CurrentDirectory = package.GetFileInfo().Directory.FullName;

new BumpCommand().Execute(
cmfPackage: package,
version: newVersion,
buildNr: null,
root: null,
packagesToUpdateDep: (string.IsNullOrEmpty(updateDepInParOption) || updateDepInParOption.IgnoreCaseEquals("y")) ? allPackages : null,
renameVersionFolders: string.IsNullOrEmpty(renameVersionFoldersOption) || renameVersionFoldersOption.IgnoreCaseEquals("y")
);
}
}
}
}
}
66 changes: 66 additions & 0 deletions cmf-cli/Commands/dbManager/BackupDatabaseCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Cmf.CLI.Core;
using Cmf.CLI.Core.Attributes;
using Cmf.CLI.Core.Constants;
using Cmf.CLI.Core.Objects;
using Cmf.CLI.Utilities;
using System.CommandLine;
using System.CommandLine.NamingConventionBinder;

namespace Cmf.CLI.Commands.DbManager
{
/// <summary>
/// BackupDatabaseCommand
/// </summary>
[CmfCommand("backup", Id = "dbmanager_backup", ParentId = "dbmanager")]
public class BackupDatabaseCommand : BaseCommand
{
/// <summary>
/// Configure command
/// </summary>
/// <param name="cmd">
/// Command
/// </param>
public override void Configure(Command cmd)
{
cmd.AddOption(
new Option<string>(
aliases: new[] { "--backupName" },
getDefaultValue: () => null,
description: "Backup Name."
)
);

cmd.Handler = CommandHandler.Create(Execute);
}

/// <summary>
/// Executes the command
/// </summary>
public void Execute(string backupName = null)
{
string defaultDbName = ExecutionContext.Instance.ProjectConfig.EnvironmentName;
string currentDb = GenericUtilities.GetCurrentDb(fileSystem);

if (backupName.IsNullOrEmpty())
{
backupName = GenericUtilities.ReadStringValueFromConsole(prompt: $"Backup name (Press ENTER to use the current '{currentDb}'):", allowEmptyValueInput: true);
if (backupName.IsNullOrEmpty())
{
backupName = currentDb;
}
}

string backupFileName = $"Custom-{backupName}.bak";

Log.Status("Backing up database", (_) =>
{
GenericUtilities.ExecuteSqlCommand(
connectionString: $"Data Source=127.0.0.1,1433;Initial Catalog=master;User ID={CoreConstants.LocalDbUser};Password={CoreConstants.LocalDbPassword}",
sqlCommand: $"USE[{defaultDbName}]; BACKUP DATABASE [{defaultDbName}] TO DISK = N'/DBDumps/{backupFileName}' WITH NOFORMAT, INIT, NAME = N'{defaultDbName}-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10"
);

Log.Information($"DB backup '{backupFileName}' created.");
});
}
}
}
Loading