From 66ebbf00d5e98430a993c36aaaf73d6974b1fc49 Mon Sep 17 00:00:00 2001 From: Kuinox Date: Mon, 29 Apr 2019 00:06:54 +0200 Subject: [PATCH] (GH-121) Add support for reading npm configuration --- src/Cake.Npm/Get/NpmGetSettings.cs | 24 ++++++++++++++++++ src/Cake.Npm/Get/NpmGetTools.cs | 39 +++++++++++++++++++++++++++++ src/Cake.Npm/NpmGetAliases.cs | 40 ++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 src/Cake.Npm/Get/NpmGetSettings.cs create mode 100644 src/Cake.Npm/Get/NpmGetTools.cs create mode 100644 src/Cake.Npm/NpmGetAliases.cs 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/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); + } + } +}