Skip to content

Commit

Permalink
(GH-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 c7f5ec2
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Cake.Npm/DistTag/NpmDistTagRunner.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 NpmDistTagRunner : NpmTool<NpmDistTagSettings>
{
/// <summary>
/// Initializes a new instance of the <see cref="NpmDistTagRunner"/> 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 NpmDistTagRunner(
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 RunDistTags(NpmDistTagSettings settings)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

RunCore(settings);
}
}
}
88 changes: 88 additions & 0 deletions src/Cake.Npm/DistTag/NpmDistTagSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Cake.Core;
using Cake.Core.IO;
using System.Linq;

namespace Cake.Npm.DistTag
{
/// <summary>
/// Contains settings used by <see cref="NpmDistTagRunner"/>.
/// </summary>
public class NpmDistTagSettings : NpmSettings
{
/// <summary>
/// Initializes a new instance of the <see cref="NpmDistTagSettings"/> class.
/// </summary>
public NpmDistTagSettings()
: base("dist-tag")
{

}
/// <summary>
/// Gets or sets the type of actions to do.
/// </summary>
public NpmDistTagCommand DistTagCommand { get; set; }

/// <summary>
/// Gets or sets the package name on which the command will be executed.
/// </summary>
public string Package { get; set; }

/// <summary>
/// Gets or sets the package version on which the tag will be applied.
/// This fields is only used to Add a dist-tag
/// </summary>
public string Version { get; set; }

/// <summary>
/// Gets or sets the Tag to be added or removed.
/// This fields is useless if you want to list dist-tags
/// </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);

switch (DistTagCommand)
{
case NpmDistTagCommand.Add:
args.Append("add");
args.Append($"{Package}@{Version}");
args.Append(Tag);
break;
case NpmDistTagCommand.Remove:
args.Append("rm");
args.Append(Package);
args.Append(Tag);
break;
case NpmDistTagCommand.List:
args.Append("ls");
args.Append(Package);
break;
}
}
}

/// <summary>
/// Type of the command to be executed
/// </summary>
public enum NpmDistTagCommand
{
/// <summary>
/// Add the dist-tag on package with a certain version
/// </summary>
Add,
/// <summary>
/// Remove the dist-tag on the package
/// </summary>
Remove,
/// <summary>
/// List the dist-tag of the package.
/// </summary>
List
}
}
60 changes: 60 additions & 0 deletions src/Cake.Npm/NpmDistTagsAliases.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Cake.Core;
using Cake.Core.Annotations;
using Cake.Npm.DistTag;
using System;
using System.Collections.Generic;
using System.Text;

namespace Cake.Npm
{
/// <summary>
/// Npm dist-tag aliases
/// </summary>
[CakeAliasCategory("Npm")]
[CakeNamespaceImport("Cake.Npm")]
public static class NpmDistTagsAliases
{
/// <summary>
/// Run npm dist-tag commands with specific arguments
/// </summary>
/// <param name="context">The context.</param>
/// /// <param name="settings">The settings.</param>
[CakeMethodAlias]
[CakeAliasCategory("DistTags")]
public static void NpmDistTag(this ICakeContext context, NpmDistTagSettings settings)
{
if (context == null) throw new ArgumentNullException(nameof(context));
if (settings == null) throw new ArgumentNullException(nameof(settings));

new NpmDistTagRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log).RunDistTags(settings);
}

/// <summary>
/// Run npm dist-tag commands with specific arguments paramtered with the configurator
/// </summary>
/// <param name="context">The context.</param>
/// <param name="package">The package name</param>
/// <param name="version">The package version</param>
/// <param name="tag">The package tag</param>
/// <param name="configurator">The configurator</param>
[CakeMethodAlias]
[CakeAliasCategory("DistTags")]
public static void NpmDistTagRun(this ICakeContext context, string package, string version, string tag, Action<NpmDistTagSettings> configurator = null)
{
if (context == null) throw new ArgumentNullException(nameof(context));
if (string.IsNullOrEmpty(package)) throw new ArgumentNullException(nameof(package));
if (string.IsNullOrEmpty(version)) throw new ArgumentNullException(nameof(version));
if (string.IsNullOrEmpty(tag)) throw new ArgumentNullException(nameof(tag));

NpmDistTagSettings s = new NpmDistTagSettings()
{
Package = package,
Version = version,
Tag = tag,
};
configurator?.Invoke(s);

new NpmDistTagRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log).RunDistTags(s);
}
}
}

0 comments on commit c7f5ec2

Please sign in to comment.