Skip to content

Commit

Permalink
(cake-contribGH-117) Add support for npm dist-tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuinox authored and pascalberger committed Aug 29, 2020
1 parent af4da1b commit ddd0183
Show file tree
Hide file tree
Showing 6 changed files with 551 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Cake.Npm/DistTag/BaseNpmDistTagSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Cake.Npm.DistTag
{
/// <summary>
/// Contains settings used by <see cref="NpmDistTagTool"/>.
/// </summary>
public abstract class BaseNpmDistTagSettings : NpmSettings
{
/// <summary>
/// Initializes a new instance of the <see cref="BaseNpmDistTagSettings"/> class.
/// </summary>
protected BaseNpmDistTagSettings()
: base("dist-tag")
{
}

/// <summary>
/// Gets or sets the package name on which the command will be executed.
/// </summary>
public string Package { get; set; }
}
}
61 changes: 61 additions & 0 deletions src/Cake.Npm/DistTag/NpmDistTagAddSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace Cake.Npm.DistTag
{
using Cake.Core;
using Cake.Core.IO;
using System;
using System.Linq;

/// <summary>
/// Contains settings used by <see cref="NpmDistTagTool"/> to add distribution tags.
/// </summary>
public class NpmDistTagAddSettings : BaseNpmDistTagSettings
{
/// <summary>
/// Initializes a new instance of the <see cref="NpmDistTagAddSettings"/> class.
/// </summary>
/// <param name="packageName">Package to which the tag should be added.</param>
/// <param name="packageVersion">The package version on which the tag will be applied</param>
public NpmDistTagAddSettings(string packageName, string packageVersion)
{
if (string.IsNullOrWhiteSpace(packageName))
{
throw new ArgumentNullException(nameof(packageName));
}

if (string.IsNullOrWhiteSpace(packageVersion))
{
throw new ArgumentNullException(nameof(packageVersion));
}

Package = packageName;
Version = packageVersion;
}

/// <summary>
/// Gets the package version on which the tag will be applied.
/// </summary>
public string Version { get; }

/// <summary>
/// Gets or sets the tag to be added.
/// </summary>
public string Tag { get; set; }

/// <summary>
/// Evaluates the settings and writes them to <paramref name="args" />.
/// </summary>
/// <param name="args">The argument builder into which the settings should be written.</param>
protected override void EvaluateCore(ProcessArgumentBuilder args)
{
base.EvaluateCore(args);

args.Append("add");
args.Append($"{Package}@{Version}");

if (!string.IsNullOrWhiteSpace(Tag))
{
args.Append(Tag);
}
}
}
}
31 changes: 31 additions & 0 deletions src/Cake.Npm/DistTag/NpmDistTagListSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Cake.Npm.DistTag
{
using Cake.Core;
using Cake.Core.IO;
using System.Linq;

/// <summary>
/// Contains settings used by <see cref="NpmDistTagTool"/> to list distribution tags.
/// </summary>
public class NpmDistTagListSettings : BaseNpmDistTagSettings
{
/// <summary>
/// Initializes a new instance of the <see cref="NpmDistTagListSettings"/> class.
/// </summary>
public NpmDistTagListSettings()
{
}

/// <summary>
/// Evaluates the settings and writes them to <paramref name="args" />.
/// </summary>
/// <param name="args">The argument builder into which the settings should be written.</param>
protected override void EvaluateCore(ProcessArgumentBuilder args)
{
base.EvaluateCore(args);

args.Append("ls");
args.Append(Package);
}
}
}
52 changes: 52 additions & 0 deletions src/Cake.Npm/DistTag/NpmDistTagRemoveSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace Cake.Npm.DistTag
{
using System;
using System.Linq;
using Cake.Core;
using Cake.Core.IO;

/// <summary>
/// Contains settings used by <see cref="NpmDistTagTool"/> to remove distribution tags.
/// </summary>
public class NpmDistTagRemoveSettings : BaseNpmDistTagSettings
{
/// <summary>
/// Initializes a new instance of the <see cref="NpmDistTagRemoveSettings"/> class.
/// </summary>
/// <param name="package">Package on which a tag should be removed.</param>
/// <param name="tag">Tag which should be removed.</param>
public NpmDistTagRemoveSettings(string package, string tag)
{
if (string.IsNullOrWhiteSpace(package))
{
throw new ArgumentNullException(nameof(package));
}

if (string.IsNullOrWhiteSpace(tag))
{
throw new ArgumentNullException(nameof(tag));
}

this.Package = package;
this.Tag = tag;
}

/// <summary>
/// Gets the tag to be removed.
/// </summary>
public string Tag { get; }

/// <summary>
/// Evaluates the settings and writes them to <paramref name="args" />.
/// </summary>
/// <param name="args">The argument builder into which the settings should be written.</param>
protected override void EvaluateCore(ProcessArgumentBuilder args)
{
base.EvaluateCore(args);

args.Append("rm");
args.Append(Package);
args.Append(Tag);
}
}
}
46 changes: 46 additions & 0 deletions src/Cake.Npm/DistTag/NpmDistTagTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace Cake.Npm.DistTag
{
using System;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Core.Tooling;

/// <summary>
/// Tool for running npm dist-tags.
/// </summary>
public class NpmDistTagTool : NpmTool<BaseNpmDistTagSettings>
{
/// <summary>
/// Initializes a new instance of the <see cref="NpmDistTagTool"/> class.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="environment">The environment.</param>
/// <param name="processRunner">The process runner.</param>
/// <param name="tools">The tool locator.</param>
/// <param name="log">Cake log instance.</param>
public NpmDistTagTool(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools,
ICakeLog log)
: base(fileSystem, environment, processRunner, tools, log)
{
}

/// <summary>
/// Runs <c>npm dist-tags</c> with the specified settings.
/// </summary>
/// <param name="settings">The settings.</param>
public void RunDistTag(BaseNpmDistTagSettings settings)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

RunCore(settings);
}
}
}
Loading

0 comments on commit ddd0183

Please sign in to comment.