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 aa9ecb0
Show file tree
Hide file tree
Showing 7 changed files with 627 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Cake.Npm/DistTag/BaseNpmDistTagSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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")
{
}
}
}
66 changes: 66 additions & 0 deletions src/Cake.Npm/DistTag/NpmDistTagAddSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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 name on which the tag should be applied.
/// </summary>
public string Package { get; }

/// <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);
}
}
}
}
33 changes: 33 additions & 0 deletions src/Cake.Npm/DistTag/NpmDistTagAddSettingsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Cake.Npm.DistTag
{
using System;

/// <summary>
/// Extensions for <see cref="NpmDistTagAddSettings"/>.
/// </summary>
public static class NpmDistTagAddSettingsExtensions
{
/// <summary>
/// Sets the tag which should be set on the package.
/// </summary>
/// <param name="settings">The settings.</param>
/// <param name="tag">Tag with which should be set on the package.</param>
/// <returns>The <paramref name="settings"/> instance with <see cref="NpmDistTagAddSettings.Tag"/> set to <paramref name="tag"/>.</returns>
public static NpmDistTagAddSettings WithTag(this NpmDistTagAddSettings settings, string tag)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

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

settings.Tag = tag;

return settings;
}
}
}
36 changes: 36 additions & 0 deletions src/Cake.Npm/DistTag/NpmDistTagListSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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>
/// Gets or sets the package name for which tags should be returned.
/// </summary>
public string Package { 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("ls");
args.Append(Package);
}
}
}
57 changes: 57 additions & 0 deletions src/Cake.Npm/DistTag/NpmDistTagRemoveSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 package name on which the tag should be removed.
/// </summary>
public string Package { get; }

/// <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 aa9ecb0

Please sign in to comment.