Skip to content

Commit c7f5ec2

Browse files
Kuinoxpascalberger
authored andcommitted
(GH-117) Add support for npm dist-tag
1 parent af4da1b commit c7f5ec2

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace Cake.Npm.DistTag
2+
{
3+
using System;
4+
using Cake.Core;
5+
using Cake.Core.Diagnostics;
6+
using Cake.Core.IO;
7+
using Cake.Core.Tooling;
8+
9+
/// <summary>
10+
/// Tool for running npm dist-tags.
11+
/// </summary>
12+
public class NpmDistTagRunner : NpmTool<NpmDistTagSettings>
13+
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="NpmDistTagRunner"/> class.
16+
/// </summary>
17+
/// <param name="fileSystem">The file system.</param>
18+
/// <param name="environment">The environment.</param>
19+
/// <param name="processRunner">The process runner.</param>
20+
/// <param name="tools">The tool locator.</param>
21+
/// <param name="log">Cake log instance.</param>
22+
public NpmDistTagRunner(
23+
IFileSystem fileSystem,
24+
ICakeEnvironment environment,
25+
IProcessRunner processRunner,
26+
IToolLocator tools,
27+
ICakeLog log)
28+
: base(fileSystem, environment, processRunner, tools, log)
29+
{
30+
}
31+
32+
/// <summary>
33+
/// Runs <c>npm dist-tags</c> with the specified settings.
34+
/// </summary>
35+
/// <param name="settings">The settings.</param>
36+
public void RunDistTags(NpmDistTagSettings settings)
37+
{
38+
if (settings == null)
39+
{
40+
throw new ArgumentNullException(nameof(settings));
41+
}
42+
43+
RunCore(settings);
44+
}
45+
}
46+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using Cake.Core;
2+
using Cake.Core.IO;
3+
using System.Linq;
4+
5+
namespace Cake.Npm.DistTag
6+
{
7+
/// <summary>
8+
/// Contains settings used by <see cref="NpmDistTagRunner"/>.
9+
/// </summary>
10+
public class NpmDistTagSettings : NpmSettings
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="NpmDistTagSettings"/> class.
14+
/// </summary>
15+
public NpmDistTagSettings()
16+
: base("dist-tag")
17+
{
18+
19+
}
20+
/// <summary>
21+
/// Gets or sets the type of actions to do.
22+
/// </summary>
23+
public NpmDistTagCommand DistTagCommand { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets the package name on which the command will be executed.
27+
/// </summary>
28+
public string Package { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets the package version on which the tag will be applied.
32+
/// This fields is only used to Add a dist-tag
33+
/// </summary>
34+
public string Version { get; set; }
35+
36+
/// <summary>
37+
/// Gets or sets the Tag to be added or removed.
38+
/// This fields is useless if you want to list dist-tags
39+
/// </summary>
40+
public string Tag { get; set; }
41+
42+
/// <summary>
43+
/// Evaluates the settings and writes them to <paramref name="args" />.
44+
/// </summary>
45+
/// <param name="args">The argument builder into which the settings should be written.</param>
46+
protected override void EvaluateCore(ProcessArgumentBuilder args)
47+
{
48+
base.EvaluateCore(args);
49+
50+
switch (DistTagCommand)
51+
{
52+
case NpmDistTagCommand.Add:
53+
args.Append("add");
54+
args.Append($"{Package}@{Version}");
55+
args.Append(Tag);
56+
break;
57+
case NpmDistTagCommand.Remove:
58+
args.Append("rm");
59+
args.Append(Package);
60+
args.Append(Tag);
61+
break;
62+
case NpmDistTagCommand.List:
63+
args.Append("ls");
64+
args.Append(Package);
65+
break;
66+
}
67+
}
68+
}
69+
70+
/// <summary>
71+
/// Type of the command to be executed
72+
/// </summary>
73+
public enum NpmDistTagCommand
74+
{
75+
/// <summary>
76+
/// Add the dist-tag on package with a certain version
77+
/// </summary>
78+
Add,
79+
/// <summary>
80+
/// Remove the dist-tag on the package
81+
/// </summary>
82+
Remove,
83+
/// <summary>
84+
/// List the dist-tag of the package.
85+
/// </summary>
86+
List
87+
}
88+
}

src/Cake.Npm/NpmDistTagsAliases.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Cake.Core;
2+
using Cake.Core.Annotations;
3+
using Cake.Npm.DistTag;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
8+
namespace Cake.Npm
9+
{
10+
/// <summary>
11+
/// Npm dist-tag aliases
12+
/// </summary>
13+
[CakeAliasCategory("Npm")]
14+
[CakeNamespaceImport("Cake.Npm")]
15+
public static class NpmDistTagsAliases
16+
{
17+
/// <summary>
18+
/// Run npm dist-tag commands with specific arguments
19+
/// </summary>
20+
/// <param name="context">The context.</param>
21+
/// /// <param name="settings">The settings.</param>
22+
[CakeMethodAlias]
23+
[CakeAliasCategory("DistTags")]
24+
public static void NpmDistTag(this ICakeContext context, NpmDistTagSettings settings)
25+
{
26+
if (context == null) throw new ArgumentNullException(nameof(context));
27+
if (settings == null) throw new ArgumentNullException(nameof(settings));
28+
29+
new NpmDistTagRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log).RunDistTags(settings);
30+
}
31+
32+
/// <summary>
33+
/// Run npm dist-tag commands with specific arguments paramtered with the configurator
34+
/// </summary>
35+
/// <param name="context">The context.</param>
36+
/// <param name="package">The package name</param>
37+
/// <param name="version">The package version</param>
38+
/// <param name="tag">The package tag</param>
39+
/// <param name="configurator">The configurator</param>
40+
[CakeMethodAlias]
41+
[CakeAliasCategory("DistTags")]
42+
public static void NpmDistTagRun(this ICakeContext context, string package, string version, string tag, Action<NpmDistTagSettings> configurator = null)
43+
{
44+
if (context == null) throw new ArgumentNullException(nameof(context));
45+
if (string.IsNullOrEmpty(package)) throw new ArgumentNullException(nameof(package));
46+
if (string.IsNullOrEmpty(version)) throw new ArgumentNullException(nameof(version));
47+
if (string.IsNullOrEmpty(tag)) throw new ArgumentNullException(nameof(tag));
48+
49+
NpmDistTagSettings s = new NpmDistTagSettings()
50+
{
51+
Package = package,
52+
Version = version,
53+
Tag = tag,
54+
};
55+
configurator?.Invoke(s);
56+
57+
new NpmDistTagRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log).RunDistTags(s);
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)