From ddd0183687b7ba7cf9ebccfa42a84469caee23ce Mon Sep 17 00:00:00 2001 From: Kuinox Date: Mon, 29 Apr 2019 00:06:54 +0200 Subject: [PATCH] (GH-117) Add support for npm dist-tag --- .../DistTag/BaseNpmDistTagSettings.cs | 21 ++ src/Cake.Npm/DistTag/NpmDistTagAddSettings.cs | 61 ++++ .../DistTag/NpmDistTagListSettings.cs | 31 ++ .../DistTag/NpmDistTagRemoveSettings.cs | 52 +++ src/Cake.Npm/DistTag/NpmDistTagTool.cs | 46 +++ src/Cake.Npm/NpmDistTagsAliases.cs | 340 ++++++++++++++++++ 6 files changed, 551 insertions(+) create mode 100644 src/Cake.Npm/DistTag/BaseNpmDistTagSettings.cs create mode 100644 src/Cake.Npm/DistTag/NpmDistTagAddSettings.cs create mode 100644 src/Cake.Npm/DistTag/NpmDistTagListSettings.cs create mode 100644 src/Cake.Npm/DistTag/NpmDistTagRemoveSettings.cs create mode 100644 src/Cake.Npm/DistTag/NpmDistTagTool.cs create mode 100644 src/Cake.Npm/NpmDistTagsAliases.cs diff --git a/src/Cake.Npm/DistTag/BaseNpmDistTagSettings.cs b/src/Cake.Npm/DistTag/BaseNpmDistTagSettings.cs new file mode 100644 index 0000000..842793d --- /dev/null +++ b/src/Cake.Npm/DistTag/BaseNpmDistTagSettings.cs @@ -0,0 +1,21 @@ +namespace Cake.Npm.DistTag +{ + /// + /// Contains settings used by . + /// + public abstract class BaseNpmDistTagSettings : NpmSettings + { + /// + /// Initializes a new instance of the class. + /// + protected BaseNpmDistTagSettings() + : base("dist-tag") + { + } + + /// + /// Gets or sets the package name on which the command will be executed. + /// + public string Package { get; set; } + } +} diff --git a/src/Cake.Npm/DistTag/NpmDistTagAddSettings.cs b/src/Cake.Npm/DistTag/NpmDistTagAddSettings.cs new file mode 100644 index 0000000..e9473f7 --- /dev/null +++ b/src/Cake.Npm/DistTag/NpmDistTagAddSettings.cs @@ -0,0 +1,61 @@ +namespace Cake.Npm.DistTag +{ + using Cake.Core; + using Cake.Core.IO; + using System; + using System.Linq; + + /// + /// Contains settings used by to add distribution tags. + /// + public class NpmDistTagAddSettings : BaseNpmDistTagSettings + { + /// + /// Initializes a new instance of the class. + /// + /// Package to which the tag should be added. + /// The package version on which the tag will be applied + 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; + } + + /// + /// Gets the package version on which the tag will be applied. + /// + public string Version { get; } + + /// + /// Gets or sets the tag to be added. + /// + public string Tag { get; set; } + + /// + /// Evaluates the settings and writes them to . + /// + /// The argument builder into which the settings should be written. + protected override void EvaluateCore(ProcessArgumentBuilder args) + { + base.EvaluateCore(args); + + args.Append("add"); + args.Append($"{Package}@{Version}"); + + if (!string.IsNullOrWhiteSpace(Tag)) + { + args.Append(Tag); + } + } + } +} diff --git a/src/Cake.Npm/DistTag/NpmDistTagListSettings.cs b/src/Cake.Npm/DistTag/NpmDistTagListSettings.cs new file mode 100644 index 0000000..51e0af3 --- /dev/null +++ b/src/Cake.Npm/DistTag/NpmDistTagListSettings.cs @@ -0,0 +1,31 @@ +namespace Cake.Npm.DistTag +{ + using Cake.Core; + using Cake.Core.IO; + using System.Linq; + + /// + /// Contains settings used by to list distribution tags. + /// + public class NpmDistTagListSettings : BaseNpmDistTagSettings + { + /// + /// Initializes a new instance of the class. + /// + public NpmDistTagListSettings() + { + } + + /// + /// Evaluates the settings and writes them to . + /// + /// The argument builder into which the settings should be written. + protected override void EvaluateCore(ProcessArgumentBuilder args) + { + base.EvaluateCore(args); + + args.Append("ls"); + args.Append(Package); + } + } +} diff --git a/src/Cake.Npm/DistTag/NpmDistTagRemoveSettings.cs b/src/Cake.Npm/DistTag/NpmDistTagRemoveSettings.cs new file mode 100644 index 0000000..ad4295f --- /dev/null +++ b/src/Cake.Npm/DistTag/NpmDistTagRemoveSettings.cs @@ -0,0 +1,52 @@ +namespace Cake.Npm.DistTag +{ + using System; + using System.Linq; + using Cake.Core; + using Cake.Core.IO; + + /// + /// Contains settings used by to remove distribution tags. + /// + public class NpmDistTagRemoveSettings : BaseNpmDistTagSettings + { + /// + /// Initializes a new instance of the class. + /// + /// Package on which a tag should be removed. + /// Tag which should be removed. + 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; + } + + /// + /// Gets the tag to be removed. + /// + public string Tag { get; } + + /// + /// Evaluates the settings and writes them to . + /// + /// The argument builder into which the settings should be written. + protected override void EvaluateCore(ProcessArgumentBuilder args) + { + base.EvaluateCore(args); + + args.Append("rm"); + args.Append(Package); + args.Append(Tag); + } + } +} diff --git a/src/Cake.Npm/DistTag/NpmDistTagTool.cs b/src/Cake.Npm/DistTag/NpmDistTagTool.cs new file mode 100644 index 0000000..82cff8b --- /dev/null +++ b/src/Cake.Npm/DistTag/NpmDistTagTool.cs @@ -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; + + /// + /// Tool for running npm dist-tags. + /// + public class NpmDistTagTool : NpmTool + { + /// + /// Initializes a new instance of the class. + /// + /// The file system. + /// The environment. + /// The process runner. + /// The tool locator. + /// Cake log instance. + public NpmDistTagTool( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools, + ICakeLog log) + : base(fileSystem, environment, processRunner, tools, log) + { + } + + /// + /// Runs npm dist-tags with the specified settings. + /// + /// The settings. + public void RunDistTag(BaseNpmDistTagSettings settings) + { + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + RunCore(settings); + } + } +} diff --git a/src/Cake.Npm/NpmDistTagsAliases.cs b/src/Cake.Npm/NpmDistTagsAliases.cs new file mode 100644 index 0000000..99ba2f6 --- /dev/null +++ b/src/Cake.Npm/NpmDistTagsAliases.cs @@ -0,0 +1,340 @@ +namespace Cake.Npm +{ + using System; + using Cake.Core; + using Cake.Core.Annotations; + using Cake.Npm.DistTag; + + /// + /// Aliases for Npm distribution tags. + /// + [CakeAliasCategory("Npm")] + [CakeNamespaceImport("Cake.Npm.DistTag")] + public static class NpmDistTagsAliases + { + /// + /// Adds a distribution tag to a specific version of a package. + /// + /// The context. + /// Name of the package to which the tag should be applied. + /// Version of the package to which the tag should point. + /// Tag which should be applied. + [CakeMethodAlias] + [CakeAliasCategory("Distribution Tags")] + public static void NpmDistTagAdd( + this ICakeContext context, + string packageName, + string packageVersion, + string tag) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (string.IsNullOrWhiteSpace(packageName)) + { + throw new ArgumentNullException(nameof(packageName)); + } + + if (string.IsNullOrWhiteSpace(packageVersion)) + { + throw new ArgumentNullException(nameof(packageVersion)); + } + + if (string.IsNullOrWhiteSpace(tag)) + { + throw new ArgumentNullException(nameof(tag)); + } + + var settings = + new NpmDistTagAddSettings(packageName, packageVersion) + { + Tag = tag + }; + + context.NpmDistTagAdd(settings); + } + + /// + /// Adds a distribution tag to a specific version of a package using the settings returned by a configurator. + /// + /// The context. + /// Name of the package to which the tag should be applied. + /// Version of the package to which the tag should point. + /// The configurator. + [CakeMethodAlias] + [CakeAliasCategory("Distribution Tags")] + public static void NpmDistTagAdd( + this ICakeContext context, + string packageName, + string packageVersion, + Action configurator) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (string.IsNullOrWhiteSpace(packageName)) + { + throw new ArgumentNullException(nameof(packageName)); + } + + if (string.IsNullOrWhiteSpace(packageVersion)) + { + throw new ArgumentNullException(nameof(packageVersion)); + } + + if (configurator == null) + { + throw new ArgumentNullException(nameof(configurator)); + } + + + var settings = new NpmDistTagAddSettings(packageName, packageVersion); + configurator(settings); + context.NpmDistTagAdd(settings); + } + + /// + /// Adds a distribution tag to a specific version of a package using the specified settings. + /// + /// The context. + /// /// The settings. + [CakeMethodAlias] + [CakeAliasCategory("Distribution Tags")] + public static void NpmDistTagAdd( + this ICakeContext context, + NpmDistTagAddSettings settings) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + var tool = + new NpmDistTagTool( + context.FileSystem, + context.Environment, + context.ProcessRunner, + context.Tools, context.Log); + tool.RunDistTag(settings); + } + + /// + /// Removes a distribution tag from a package. + /// + /// The context. + /// Name of the package for which the tag should be removed. + /// Tag which should be removed. + [CakeMethodAlias] + [CakeAliasCategory("Distribution Tags")] + public static void NpmDistTagRemove( + this ICakeContext context, + string packageName, + string tag) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (string.IsNullOrWhiteSpace(packageName)) + { + throw new ArgumentNullException(nameof(packageName)); + } + + if (string.IsNullOrWhiteSpace(tag)) + { + throw new ArgumentNullException(nameof(tag)); + } + + var settings = new NpmDistTagRemoveSettings(packageName, tag); + + context.NpmDistTagRemove(settings); + } + + /// + /// Removes a distribution tag for a package using the settings returned by a configurator. + /// + /// The context. + /// Name of the package to which the tag should be applied. + /// Tag which should be removed. + /// The configurator. + [CakeMethodAlias] + [CakeAliasCategory("Distribution Tags")] + public static void NpmDistTagRemove( + this ICakeContext context, + string packageName, + string tag, + Action configurator) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (string.IsNullOrWhiteSpace(packageName)) + { + throw new ArgumentNullException(nameof(packageName)); + } + + if (string.IsNullOrWhiteSpace(tag)) + { + throw new ArgumentNullException(nameof(tag)); + } + + if (configurator == null) + { + throw new ArgumentNullException(nameof(configurator)); + } + + + var settings = new NpmDistTagRemoveSettings(packageName, tag); + configurator(settings); + context.NpmDistTagRemove(settings); + } + + /// + /// Removes a distribution tag from a package using the specified settings. + /// + /// The context. + /// /// The settings. + [CakeMethodAlias] + [CakeAliasCategory("Distribution Tags")] + public static void NpmDistTagRemove( + this ICakeContext context, + NpmDistTagRemoveSettings settings) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + var tool = + new NpmDistTagTool( + context.FileSystem, + context.Environment, + context.ProcessRunner, + context.Tools, context.Log); + tool.RunDistTag(settings); + } + + /// + /// Lists distribution tags for the package in the current prefix. + /// + /// The context. + [CakeMethodAlias] + [CakeAliasCategory("Distribution Tags")] + public static void NpmDistTagList( + this ICakeContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + var settings = new NpmDistTagListSettings(); + + context.NpmDistTagList(settings); + } + + /// + /// Lists distribution tags for a package. + /// + /// The context. + /// Name of the package whose distribution tags should be listed. + [CakeMethodAlias] + [CakeAliasCategory("Distribution Tags")] + public static void NpmDistTagList( + this ICakeContext context, + string packageName) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (string.IsNullOrWhiteSpace(packageName)) + { + throw new ArgumentNullException(nameof(packageName)); + } + + var settings = + new NpmDistTagListSettings + { + Package = packageName + }; + + context.NpmDistTagList(settings); + } + + /// + /// List distribution tags using the settings returned by a configurator. + /// + /// The context. + /// The configurator. + [CakeMethodAlias] + [CakeAliasCategory("Distribution Tags")] + public static void NpmDistTagList( + this ICakeContext context, + Action configurator) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (configurator == null) + { + throw new ArgumentNullException(nameof(configurator)); + } + + + var settings = new NpmDistTagListSettings(); + configurator(settings); + context.NpmDistTagList(settings); + } + + /// + /// Lists distribution tags using the specified settings. + /// + /// The context. + /// /// The settings. + [CakeMethodAlias] + [CakeAliasCategory("Distribution Tags")] + public static void NpmDistTagList( + this ICakeContext context, + NpmDistTagListSettings settings) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + var tool = + new NpmDistTagTool( + context.FileSystem, + context.Environment, + context.ProcessRunner, + context.Tools, context.Log); + tool.RunDistTag(settings); + } + } +}