Skip to content

Commit

Permalink
Add commands for npm dist-tag, view, get
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuinox authored and pascalberger committed Aug 29, 2020
1 parent af4da1b commit c97a047
Show file tree
Hide file tree
Showing 9 changed files with 402 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Cake.Npm/DistTag/NpmDistTagRunner.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Tool for running npm dist-tags.
/// </summary>
public class NpmDistTagRunner : NpmTool<NpmDistTagSettings>
{
/// <summary>
/// Initializes a new instance of the <see cref="NpmDistTagRunner"/> class.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="environment">The environment.</param>
/// <param name="processRunner">The process runner.</param>
/// <param name="tools">The tool locator.</param>
/// <param name="log">Cake log instance.</param>
public NpmDistTagRunner(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools,
ICakeLog log)
: base(fileSystem, environment, processRunner, tools, log)
{
}

/// <summary>
/// Runs <c>npm dist-tags</c> with the specified settings.
/// </summary>
/// <param name="settings">The settings.</param>
public void RunDistTags(NpmDistTagSettings settings)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

RunCore(settings);
}
}
}
88 changes: 88 additions & 0 deletions src/Cake.Npm/DistTag/NpmDistTagSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Cake.Core;
using Cake.Core.IO;
using System.Linq;

namespace Cake.Npm.DistTag
{
/// <summary>
/// Contains settings used by <see cref="NpmDistTagRunner"/>.
/// </summary>
public class NpmDistTagSettings : NpmSettings
{
/// <summary>
/// Initializes a new instance of the <see cref="NpmDistTagSettings"/> class.
/// </summary>
public NpmDistTagSettings()
: base("dist-tag")
{

}
/// <summary>
/// Gets or sets the type of actions to do.
/// </summary>
public NpmDistTagCommand DistTagCommand { get; set; }

/// <summary>
/// Gets or sets the package name on which the command will be executed.
/// </summary>
public string Package { get; set; }

/// <summary>
/// Gets or sets the package version on which the tag will be applied.
/// This fields is only used to Add a dist-tag
/// </summary>
public string Version { get; set; }

/// <summary>
/// Gets or sets the Tag to be added or removed.
/// This fields is useless if you want to list dist-tags
/// </summary>
public string Tag { get; set; }

/// <summary>
/// Evaluates the settings and writes them to <paramref name="args" />.
/// </summary>
/// <param name="args">The argument builder into which the settings should be written.</param>
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;
}
}
}

/// <summary>
/// Type of the command to be executed
/// </summary>
public enum NpmDistTagCommand
{
/// <summary>
/// Add the dist-tag on package with a certain version
/// </summary>
Add,
/// <summary>
/// Remove the dist-tag on the package
/// </summary>
Remove,
/// <summary>
/// List the dist-tag of the package.
/// </summary>
List
}
}
24 changes: 24 additions & 0 deletions src/Cake.Npm/Get/NpmGetSettings.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
39 changes: 39 additions & 0 deletions src/Cake.Npm/Get/NpmGetTools.cs
Original file line number Diff line number Diff line change
@@ -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<NpmSettings>
{
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<string> output = new List<string>();
RunCore(settings, new ProcessSettings(), process =>
{
output = process.GetStandardOutput();
});
return output.SingleOrDefault();
}
}
}
60 changes: 60 additions & 0 deletions src/Cake.Npm/NpmDistTagsAliases.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Npm dist-tag aliases
/// </summary>
[CakeAliasCategory("Npm")]
[CakeNamespaceImport("Cake.Npm")]
public static class NpmDistTagsAliases
{
/// <summary>
/// Run npm dist-tag commands with specific arguments
/// </summary>
/// <param name="context">The context.</param>
/// /// <param name="settings">The settings.</param>
[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);
}

/// <summary>
/// Run npm dist-tag commands with specific arguments paramtered with the configurator
/// </summary>
/// <param name="context">The context.</param>
/// <param name="package">The package name</param>
/// <param name="version">The package version</param>
/// <param name="tag">The package tag</param>
/// <param name="configurator">The configurator</param>
[CakeMethodAlias]
[CakeAliasCategory("DistTags")]
public static void NpmDistTagRun(this ICakeContext context, string package, string version, string tag, Action<NpmDistTagSettings> 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);
}
}
}
40 changes: 40 additions & 0 deletions src/Cake.Npm/NpmGetAliases.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Npm get aliases
/// </summary>
[CakeAliasCategory("Npm")]
[CakeNamespaceImport("Cake.Npm")]
public static class NpmGetAliases
{
/// <summary>
/// Gets the npm config on the given key
/// </summary>
/// <param name="context">The cake context.</param>
/// <param name="key">The config key.</param>
/// <param name="workingDirectory">The working directory</param>
/// <returns>The value on the given key.</returns>
[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);
}
}
}
36 changes: 36 additions & 0 deletions src/Cake.Npm/NpmViewAliases.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Npm view aliases
/// </summary>
[CakeAliasCategory("Npm")]
[CakeNamespaceImport("Cake.Npm")]
public static class NpmViewAliases
{
/// <summary>
/// Call npm view with --json attribute.
/// </summary>
/// <param name="context"></param>
/// <param name="packageName">Name of the package</param>
/// <param name="workingDirectory"></param>
/// <returns>An empty string if the package was not found on the repository</returns>
[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);
}
}
}
26 changes: 26 additions & 0 deletions src/Cake.Npm/View/NpmViewSettings.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Loading

0 comments on commit c97a047

Please sign in to comment.