From c97a047331db05b42f6572a97a11f27c82a735fc Mon Sep 17 00:00:00 2001 From: Kuinox Date: Mon, 29 Apr 2019 00:06:54 +0200 Subject: [PATCH] Add commands for npm dist-tag, view, get --- src/Cake.Npm/DistTag/NpmDistTagRunner.cs | 46 +++++++++++ src/Cake.Npm/DistTag/NpmDistTagSettings.cs | 88 ++++++++++++++++++++++ src/Cake.Npm/Get/NpmGetSettings.cs | 24 ++++++ src/Cake.Npm/Get/NpmGetTools.cs | 39 ++++++++++ src/Cake.Npm/NpmDistTagsAliases.cs | 60 +++++++++++++++ src/Cake.Npm/NpmGetAliases.cs | 40 ++++++++++ src/Cake.Npm/NpmViewAliases.cs | 36 +++++++++ src/Cake.Npm/View/NpmViewSettings.cs | 26 +++++++ src/Cake.Npm/View/NpmViewTools.cs | 43 +++++++++++ 9 files changed, 402 insertions(+) create mode 100644 src/Cake.Npm/DistTag/NpmDistTagRunner.cs create mode 100644 src/Cake.Npm/DistTag/NpmDistTagSettings.cs create mode 100644 src/Cake.Npm/Get/NpmGetSettings.cs create mode 100644 src/Cake.Npm/Get/NpmGetTools.cs create mode 100644 src/Cake.Npm/NpmDistTagsAliases.cs create mode 100644 src/Cake.Npm/NpmGetAliases.cs create mode 100644 src/Cake.Npm/NpmViewAliases.cs create mode 100644 src/Cake.Npm/View/NpmViewSettings.cs create mode 100644 src/Cake.Npm/View/NpmViewTools.cs 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/Get/NpmGetSettings.cs b/src/Cake.Npm/Get/NpmGetSettings.cs new file mode 100644 index 0000000..7936977 --- /dev/null +++ b/src/Cake.Npm/Get/NpmGetSettings.cs @@ -0,0 +1,24 @@ +using Cake.Core; +using Cake.Core.IO; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Cake.Npm.Get +{ + class NpmGetSettings : NpmSettings + { + public NpmGetSettings() : base("get") + { + RedirectStandardOutput = true; + } + public string Key { get; set; } + + protected override void EvaluateCore(ProcessArgumentBuilder args) + { + base.EvaluateCore(args); + args.Append(Key); + } + } +} diff --git a/src/Cake.Npm/Get/NpmGetTools.cs b/src/Cake.Npm/Get/NpmGetTools.cs new file mode 100644 index 0000000..e53a7b8 --- /dev/null +++ b/src/Cake.Npm/Get/NpmGetTools.cs @@ -0,0 +1,39 @@ +using Cake.Core; +using Cake.Core.Diagnostics; +using Cake.Core.IO; +using Cake.Core.Tooling; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Cake.Npm.Get +{ + class NpmGetTools : NpmTool + { + public NpmGetTools( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools, + ICakeLog log) + : base(fileSystem, environment, processRunner, tools, log) + { + } + + + public string Get(NpmGetSettings settings) + { + if (string.IsNullOrWhiteSpace(settings.Key)) + { + throw new ArgumentException(); + } + IEnumerable output = new List(); + RunCore(settings, new ProcessSettings(), process => + { + output = process.GetStandardOutput(); + }); + return output.SingleOrDefault(); + } + } +} 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); + } + } +} diff --git a/src/Cake.Npm/NpmGetAliases.cs b/src/Cake.Npm/NpmGetAliases.cs new file mode 100644 index 0000000..81bf9be --- /dev/null +++ b/src/Cake.Npm/NpmGetAliases.cs @@ -0,0 +1,40 @@ +using Cake.Core; +using Cake.Core.Annotations; +using Cake.Npm.Get; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Cake.Npm +{ + /// + /// Npm get aliases + /// + [CakeAliasCategory("Npm")] + [CakeNamespaceImport("Cake.Npm")] + public static class NpmGetAliases + { + /// + /// Gets the npm config on the given key + /// + /// The cake context. + /// The config key. + /// The working directory + /// The value on the given key. + [CakeMethodAlias] + [CakeAliasCategory("Get")] + public static string NpmGet(this ICakeContext context, string key, string workingDirectory = null) + { + if (key == null) + { + throw new ArgumentNullException("key can't be null"); + } + NpmGetSettings settings = new NpmGetSettings() + { + Key = key, + WorkingDirectory = workingDirectory + }; + return new NpmGetTools(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log).Get(settings); + } + } +} diff --git a/src/Cake.Npm/NpmViewAliases.cs b/src/Cake.Npm/NpmViewAliases.cs new file mode 100644 index 0000000..0541f5c --- /dev/null +++ b/src/Cake.Npm/NpmViewAliases.cs @@ -0,0 +1,36 @@ +using Cake.Core; +using Cake.Core.Annotations; +using Cake.Npm.View; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Cake.Npm +{ + /// + /// Npm view aliases + /// + [CakeAliasCategory("Npm")] + [CakeNamespaceImport("Cake.Npm")] + public static class NpmViewAliases + { + /// + /// Call npm view with --json attribute. + /// + /// + /// Name of the package + /// + /// An empty string if the package was not found on the repository + [CakeMethodAlias] + [CakeAliasCategory("View")] + public static string NpmView(this ICakeContext context, string packageName = null, string workingDirectory = null) + { + NpmViewSettings settings = new NpmViewSettings() + { + PackageName = packageName, + WorkingDirectory = workingDirectory + }; + return new NpmViewTools(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log).View(settings); + } + } +} diff --git a/src/Cake.Npm/View/NpmViewSettings.cs b/src/Cake.Npm/View/NpmViewSettings.cs new file mode 100644 index 0000000..19eca2f --- /dev/null +++ b/src/Cake.Npm/View/NpmViewSettings.cs @@ -0,0 +1,26 @@ +using Cake.Core; +using Cake.Core.IO; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Cake.Npm.View +{ + class NpmViewSettings : NpmSettings + { + public NpmViewSettings() : base("view") + { + RedirectStandardOutput = true; + } + + public string PackageName { get; set; } + + protected override void EvaluateCore(ProcessArgumentBuilder args) + { + base.EvaluateCore(args); + args.Append("--json"); + args.Append(PackageName); + } + } +} diff --git a/src/Cake.Npm/View/NpmViewTools.cs b/src/Cake.Npm/View/NpmViewTools.cs new file mode 100644 index 0000000..56c18ed --- /dev/null +++ b/src/Cake.Npm/View/NpmViewTools.cs @@ -0,0 +1,43 @@ +using Cake.Core; +using Cake.Core.Diagnostics; +using Cake.Core.IO; +using Cake.Core.Tooling; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Cake.Npm.View +{ + class NpmViewTools : NpmTool + { + public NpmViewTools( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools, + ICakeLog log) + : base(fileSystem, environment, processRunner, tools, log) + { + } + + public string View(NpmViewSettings settings) + { + IEnumerable output = new List(); + try + { + RunCore(settings, new ProcessSettings(), process => + { + output = process.GetStandardOutput(); + }); + return string.Join("\n", output); + + } + catch (CakeException) + { + CakeLog.Information("Error should be a 404 and i ignore it."); + return ""; + } + + } + } +}