Skip to content

Commit 332429b

Browse files
Kuinoxpascalberger
authored andcommitted
(GH-117) Add support for npm dist-tag
1 parent 0955917 commit 332429b

8 files changed

+706
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Cake.Npm.DistTag
2+
{
3+
/// <summary>
4+
/// Contains settings used by <see cref="NpmDistTagTool"/>.
5+
/// </summary>
6+
public abstract class BaseNpmDistTagSettings : NpmSettings
7+
{
8+
/// <summary>
9+
/// Initializes a new instance of the <see cref="BaseNpmDistTagSettings"/> class.
10+
/// </summary>
11+
protected BaseNpmDistTagSettings()
12+
: base("dist-tag")
13+
{
14+
}
15+
}
16+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
namespace Cake.Npm.DistTag
2+
{
3+
using Cake.Core;
4+
using Cake.Core.IO;
5+
using System;
6+
using System.Linq;
7+
8+
/// <summary>
9+
/// Contains settings used by <see cref="NpmDistTagTool"/> to add distribution tags.
10+
/// </summary>
11+
public class NpmDistTagAddSettings : BaseNpmDistTagSettings
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="NpmDistTagAddSettings"/> class.
15+
/// </summary>
16+
/// <param name="packageName">Package to which the tag should be added.</param>
17+
/// <param name="packageVersion">The package version on which the tag will be applied</param>
18+
public NpmDistTagAddSettings(string packageName, string packageVersion)
19+
{
20+
if (string.IsNullOrWhiteSpace(packageName))
21+
{
22+
throw new ArgumentNullException(nameof(packageName));
23+
}
24+
25+
if (string.IsNullOrWhiteSpace(packageVersion))
26+
{
27+
throw new ArgumentNullException(nameof(packageVersion));
28+
}
29+
30+
PackageName = packageName;
31+
PacakgeVersion = packageVersion;
32+
}
33+
34+
/// <summary>
35+
/// Gets the bane of the package on which the tag should be applied.
36+
/// </summary>
37+
public string PackageName { get; }
38+
39+
/// <summary>
40+
/// Gets the vrsion of the package on which the tag will be applied.
41+
/// </summary>
42+
public string PacakgeVersion { get; }
43+
44+
/// <summary>
45+
/// Gets or sets the tag to be added.
46+
/// </summary>
47+
public string Tag { get; set; }
48+
49+
/// <summary>
50+
/// Evaluates the settings and writes them to <paramref name="args" />.
51+
/// </summary>
52+
/// <param name="args">The argument builder into which the settings should be written.</param>
53+
protected override void EvaluateCore(ProcessArgumentBuilder args)
54+
{
55+
base.EvaluateCore(args);
56+
57+
args.Append("add");
58+
args.Append($"{PackageName}@{PacakgeVersion}");
59+
60+
if (!string.IsNullOrWhiteSpace(Tag))
61+
{
62+
args.Append(Tag);
63+
}
64+
}
65+
}
66+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace Cake.Npm.DistTag
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// Extensions for <see cref="NpmDistTagAddSettings"/>.
7+
/// </summary>
8+
public static class NpmDistTagAddSettingsExtensions
9+
{
10+
/// <summary>
11+
/// Sets the tag which should be set on the package.
12+
/// </summary>
13+
/// <param name="settings">The settings.</param>
14+
/// <param name="tag">Tag with which should be set on the package.</param>
15+
/// <returns>The <paramref name="settings"/> instance with <see cref="NpmDistTagAddSettings.Tag"/> set to <paramref name="tag"/>.</returns>
16+
public static NpmDistTagAddSettings WithTag(this NpmDistTagAddSettings settings, string tag)
17+
{
18+
if (settings == null)
19+
{
20+
throw new ArgumentNullException(nameof(settings));
21+
}
22+
23+
if (string.IsNullOrWhiteSpace(tag))
24+
{
25+
throw new ArgumentNullException(nameof(tag));
26+
}
27+
28+
settings.Tag = tag;
29+
30+
return settings;
31+
}
32+
}
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace Cake.Npm.DistTag
2+
{
3+
using Cake.Core;
4+
using Cake.Core.IO;
5+
using System.Linq;
6+
7+
/// <summary>
8+
/// Contains settings used by <see cref="NpmDistTagTool"/> to list distribution tags.
9+
/// </summary>
10+
public class NpmDistTagListSettings : BaseNpmDistTagSettings
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="NpmDistTagListSettings"/> class.
14+
/// </summary>
15+
public NpmDistTagListSettings()
16+
{
17+
}
18+
19+
/// <summary>
20+
/// Gets or sets the name of the package for which tags should be returned.
21+
/// </summary>
22+
public string PackageName { get; set; }
23+
24+
/// <summary>
25+
/// Evaluates the settings and writes them to <paramref name="args" />.
26+
/// </summary>
27+
/// <param name="args">The argument builder into which the settings should be written.</param>
28+
protected override void EvaluateCore(ProcessArgumentBuilder args)
29+
{
30+
base.EvaluateCore(args);
31+
32+
args.Append("ls");
33+
args.Append(PackageName);
34+
}
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace Cake.Npm.DistTag
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// Extensions for <see cref="NpmDistTagListSettings"/>.
7+
/// </summary>
8+
public static class NpmDistTagListSettingsExtensions
9+
{
10+
/// <summary>
11+
/// Sets the name of the package for which tags should be listed.
12+
/// </summary>
13+
/// <param name="settings">The settings.</param>
14+
/// <param name="packageName">Tag with which should be set on the package.</param>
15+
/// <returns>The <paramref name="settings"/> instance with <see cref="NpmDistTagAddSettings.PackageName"/> set to <paramref name="packageName"/>.</returns>
16+
public static NpmDistTagListSettings ForPackage(this NpmDistTagListSettings settings, string packageName)
17+
{
18+
if (settings == null)
19+
{
20+
throw new ArgumentNullException(nameof(settings));
21+
}
22+
23+
if (string.IsNullOrWhiteSpace(packageName))
24+
{
25+
throw new ArgumentNullException(nameof(packageName));
26+
}
27+
28+
settings.PackageName = packageName;
29+
30+
return settings;
31+
}
32+
}
33+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace Cake.Npm.DistTag
2+
{
3+
using System;
4+
using System.Linq;
5+
using Cake.Core;
6+
using Cake.Core.IO;
7+
8+
/// <summary>
9+
/// Contains settings used by <see cref="NpmDistTagTool"/> to remove distribution tags.
10+
/// </summary>
11+
public class NpmDistTagRemoveSettings : BaseNpmDistTagSettings
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="NpmDistTagRemoveSettings"/> class.
15+
/// </summary>
16+
/// <param name="packageName">Package on which a tag should be removed.</param>
17+
/// <param name="tag">Tag which should be removed.</param>
18+
public NpmDistTagRemoveSettings(string packageName, string tag)
19+
{
20+
if (string.IsNullOrWhiteSpace(packageName))
21+
{
22+
throw new ArgumentNullException(nameof(packageName));
23+
}
24+
25+
if (string.IsNullOrWhiteSpace(tag))
26+
{
27+
throw new ArgumentNullException(nameof(tag));
28+
}
29+
30+
this.PackageName = packageName;
31+
this.Tag = tag;
32+
}
33+
34+
/// <summary>
35+
/// Gets the name of the package on which the tag should be removed.
36+
/// </summary>
37+
public string PackageName { get; }
38+
39+
/// <summary>
40+
/// Gets the tag to be removed.
41+
/// </summary>
42+
public string Tag { get; }
43+
44+
/// <summary>
45+
/// Evaluates the settings and writes them to <paramref name="args" />.
46+
/// </summary>
47+
/// <param name="args">The argument builder into which the settings should be written.</param>
48+
protected override void EvaluateCore(ProcessArgumentBuilder args)
49+
{
50+
base.EvaluateCore(args);
51+
52+
args.Append("rm");
53+
args.Append(PackageName);
54+
args.Append(Tag);
55+
}
56+
}
57+
}
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 NpmDistTagTool : NpmTool<BaseNpmDistTagSettings>
13+
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="NpmDistTagTool"/> 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 NpmDistTagTool(
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 RunDistTag(BaseNpmDistTagSettings settings)
37+
{
38+
if (settings == null)
39+
{
40+
throw new ArgumentNullException(nameof(settings));
41+
}
42+
43+
RunCore(settings);
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)