From 45f072ef98f5a6aa0f8c7d1ee99cc64ba6825a3c 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 | 16 + src/Cake.Npm/DistTag/NpmDistTagAddSettings.cs | 66 +++ .../NpmDistTagAddSettingsExtensions.cs | 33 ++ .../DistTag/NpmDistTagListSettings.cs | 36 ++ .../NpmDistTagListSettingsExtensions.cs | 33 ++ .../DistTag/NpmDistTagRemoveSettings.cs | 57 +++ src/Cake.Npm/DistTag/NpmDistTagTool.cs | 46 ++ src/Cake.Npm/NpmDistTagsAliases.cs | 419 ++++++++++++++++++ 8 files changed, 706 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/NpmDistTagAddSettingsExtensions.cs create mode 100644 src/Cake.Npm/DistTag/NpmDistTagListSettings.cs create mode 100644 src/Cake.Npm/DistTag/NpmDistTagListSettingsExtensions.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..89f20d6 --- /dev/null +++ b/src/Cake.Npm/DistTag/BaseNpmDistTagSettings.cs @@ -0,0 +1,16 @@ +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") + { + } + } +} diff --git a/src/Cake.Npm/DistTag/NpmDistTagAddSettings.cs b/src/Cake.Npm/DistTag/NpmDistTagAddSettings.cs new file mode 100644 index 0000000..a78a05c --- /dev/null +++ b/src/Cake.Npm/DistTag/NpmDistTagAddSettings.cs @@ -0,0 +1,66 @@ +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)); + } + + PackageName = packageName; + PacakgeVersion = packageVersion; + } + + /// + /// Gets the bane of the package on which the tag should be applied. + /// + public string PackageName { get; } + + /// + /// Gets the vrsion of the package on which the tag will be applied. + /// + public string PacakgeVersion { 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($"{PackageName}@{PacakgeVersion}"); + + if (!string.IsNullOrWhiteSpace(Tag)) + { + args.Append(Tag); + } + } + } +} diff --git a/src/Cake.Npm/DistTag/NpmDistTagAddSettingsExtensions.cs b/src/Cake.Npm/DistTag/NpmDistTagAddSettingsExtensions.cs new file mode 100644 index 0000000..e8710b6 --- /dev/null +++ b/src/Cake.Npm/DistTag/NpmDistTagAddSettingsExtensions.cs @@ -0,0 +1,33 @@ +namespace Cake.Npm.DistTag +{ + using System; + + /// + /// Extensions for . + /// + public static class NpmDistTagAddSettingsExtensions + { + /// + /// Sets the tag which should be set on the package. + /// + /// The settings. + /// Tag with which should be set on the package. + /// The instance with set to . + 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; + } + } +} diff --git a/src/Cake.Npm/DistTag/NpmDistTagListSettings.cs b/src/Cake.Npm/DistTag/NpmDistTagListSettings.cs new file mode 100644 index 0000000..82805b3 --- /dev/null +++ b/src/Cake.Npm/DistTag/NpmDistTagListSettings.cs @@ -0,0 +1,36 @@ +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() + { + } + + /// + /// Gets or sets the name of the package for which tags should be returned. + /// + public string PackageName { 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("ls"); + args.Append(PackageName); + } + } +} diff --git a/src/Cake.Npm/DistTag/NpmDistTagListSettingsExtensions.cs b/src/Cake.Npm/DistTag/NpmDistTagListSettingsExtensions.cs new file mode 100644 index 0000000..b890ece --- /dev/null +++ b/src/Cake.Npm/DistTag/NpmDistTagListSettingsExtensions.cs @@ -0,0 +1,33 @@ +namespace Cake.Npm.DistTag +{ + using System; + + /// + /// Extensions for . + /// + public static class NpmDistTagListSettingsExtensions + { + /// + /// Sets the name of the package for which tags should be listed. + /// + /// The settings. + /// Tag with which should be set on the package. + /// The instance with set to . + public static NpmDistTagListSettings ForPackage(this NpmDistTagListSettings settings, string packageName) + { + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + if (string.IsNullOrWhiteSpace(packageName)) + { + throw new ArgumentNullException(nameof(packageName)); + } + + settings.PackageName = packageName; + + return settings; + } + } +} diff --git a/src/Cake.Npm/DistTag/NpmDistTagRemoveSettings.cs b/src/Cake.Npm/DistTag/NpmDistTagRemoveSettings.cs new file mode 100644 index 0000000..59880b9 --- /dev/null +++ b/src/Cake.Npm/DistTag/NpmDistTagRemoveSettings.cs @@ -0,0 +1,57 @@ +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 packageName, string tag) + { + if (string.IsNullOrWhiteSpace(packageName)) + { + throw new ArgumentNullException(nameof(packageName)); + } + + if (string.IsNullOrWhiteSpace(tag)) + { + throw new ArgumentNullException(nameof(tag)); + } + + this.PackageName = packageName; + this.Tag = tag; + } + + /// + /// Gets the name of the package on which the tag should be removed. + /// + public string PackageName { get; } + + /// + /// 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(PackageName); + 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..b944632 --- /dev/null +++ b/src/Cake.Npm/NpmDistTagsAliases.cs @@ -0,0 +1,419 @@ +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. + /// + /// Tags version 1.2.3 of the package mypackage with the tag stable. + /// + /// + /// + /// + [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. + /// + /// Tags version 1.2.3 of the package mypackage with the tag stable and uses verbose logging. + /// + /// settings.WithTag("stable").WithLogLevel(NpmLogLevel.Verbose)); + /// ]]> + /// + /// + [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. + /// + /// Tags version 1.2.3 of the package mypackage with the tag stable and uses verbose logging. + /// + /// + /// + /// + [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. + /// + /// Removes tag stable from the package mypackage. + /// + /// + /// + /// + [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. + /// + /// Removes tag stable from the package mypackage using verbose logging. + /// + /// settings.WithLogLevel(NpmLogLevel.Verbose)); + /// ]]> + /// + /// + [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. + /// + /// Removes tag stable from the package mypackage using verbose logging. + /// + /// + /// + /// + [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 a package. + /// + /// The context. + /// Name of the package whose distribution tags should be listed. + /// + /// Lists distribution tags for the package mypackage. + /// + /// + /// + /// + [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 + { + PackageName = packageName + }; + + context.NpmDistTagList(settings); + } + + /// + /// List distribution tags using the settings returned by a configurator. + /// + /// The context. + /// The configurator. + /// + /// Lists distribution tags for the package mypackage using verbose logging. + /// + /// settings.ForPackage("mypackage").WithLogLevel(NpmLogLevel.Verbose)); + /// ]]> + /// + /// + [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. + /// + /// Lists distribution tags for the package mypackage using verbose logging. + /// + /// + /// + /// + [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); + } + } +}