diff --git a/src/Cake.Npm/DistTag/NpmDistTagRunner.cs b/src/Cake.Npm/DistTag/NpmDistTagRunner.cs new file mode 100644 index 0000000..f40f81a --- /dev/null +++ b/src/Cake.Npm/DistTag/NpmDistTagRunner.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 NpmDistTagRunner : NpmTool + { + /// + /// Initializes a new instance of the class. + /// + /// The file system. + /// The environment. + /// The process runner. + /// The tool locator. + /// Cake log instance. + public NpmDistTagRunner( + 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 RunDistTags(NpmDistTagSettings settings) + { + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + RunCore(settings); + } + } +} diff --git a/src/Cake.Npm/DistTag/NpmDistTagSettings.cs b/src/Cake.Npm/DistTag/NpmDistTagSettings.cs new file mode 100644 index 0000000..9934dfe --- /dev/null +++ b/src/Cake.Npm/DistTag/NpmDistTagSettings.cs @@ -0,0 +1,88 @@ +using Cake.Core; +using Cake.Core.IO; +using System.Linq; + +namespace Cake.Npm.DistTag +{ + /// + /// Contains settings used by . + /// + public class NpmDistTagSettings : NpmSettings + { + /// + /// Initializes a new instance of the class. + /// + public NpmDistTagSettings() + : base("dist-tag") + { + + } + /// + /// Gets or sets the type of actions to do. + /// + public NpmDistTagCommand DistTagCommand { get; set; } + + /// + /// Gets or sets the package name on which the command will be executed. + /// + public string Package { get; set; } + + /// + /// Gets or sets the package version on which the tag will be applied. + /// This fields is only used to Add a dist-tag + /// + public string Version { get; set; } + + /// + /// Gets or sets the Tag to be added or removed. + /// This fields is useless if you want to list dist-tags + /// + 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); + + 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; + } + } + } + + /// + /// Type of the command to be executed + /// + public enum NpmDistTagCommand + { + /// + /// Add the dist-tag on package with a certain version + /// + Add, + /// + /// Remove the dist-tag on the package + /// + Remove, + /// + /// List the dist-tag of the package. + /// + List + } +} diff --git a/src/Cake.Npm/NpmDistTagsAliases.cs b/src/Cake.Npm/NpmDistTagsAliases.cs new file mode 100644 index 0000000..b5d9a85 --- /dev/null +++ b/src/Cake.Npm/NpmDistTagsAliases.cs @@ -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 +{ + /// + /// Npm dist-tag aliases + /// + [CakeAliasCategory("Npm")] + [CakeNamespaceImport("Cake.Npm")] + public static class NpmDistTagsAliases + { + /// + /// Run npm dist-tag commands with specific arguments + /// + /// The context. + /// /// The settings. + [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); + } + + /// + /// Run npm dist-tag commands with specific arguments paramtered with the configurator + /// + /// The context. + /// The package name + /// The package version + /// The package tag + /// The configurator + [CakeMethodAlias] + [CakeAliasCategory("DistTags")] + public static void NpmDistTagRun(this ICakeContext context, string package, string version, string tag, Action 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); + } + } +}