Skip to content

Commit c97a047

Browse files
Kuinoxpascalberger
authored andcommitted
Add commands for npm dist-tag, view, get
1 parent af4da1b commit c97a047

9 files changed

Lines changed: 402 additions & 0 deletions

File tree

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/Get/NpmGetSettings.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Cake.Core;
2+
using Cake.Core.IO;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
8+
namespace Cake.Npm.Get
9+
{
10+
class NpmGetSettings : NpmSettings
11+
{
12+
public NpmGetSettings() : base("get")
13+
{
14+
RedirectStandardOutput = true;
15+
}
16+
public string Key { get; set; }
17+
18+
protected override void EvaluateCore(ProcessArgumentBuilder args)
19+
{
20+
base.EvaluateCore(args);
21+
args.Append(Key);
22+
}
23+
}
24+
}

src/Cake.Npm/Get/NpmGetTools.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Cake.Core;
2+
using Cake.Core.Diagnostics;
3+
using Cake.Core.IO;
4+
using Cake.Core.Tooling;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
10+
namespace Cake.Npm.Get
11+
{
12+
class NpmGetTools : NpmTool<NpmSettings>
13+
{
14+
public NpmGetTools(
15+
IFileSystem fileSystem,
16+
ICakeEnvironment environment,
17+
IProcessRunner processRunner,
18+
IToolLocator tools,
19+
ICakeLog log)
20+
: base(fileSystem, environment, processRunner, tools, log)
21+
{
22+
}
23+
24+
25+
public string Get(NpmGetSettings settings)
26+
{
27+
if (string.IsNullOrWhiteSpace(settings.Key))
28+
{
29+
throw new ArgumentException();
30+
}
31+
IEnumerable<string> output = new List<string>();
32+
RunCore(settings, new ProcessSettings(), process =>
33+
{
34+
output = process.GetStandardOutput();
35+
});
36+
return output.SingleOrDefault();
37+
}
38+
}
39+
}

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+
}

src/Cake.Npm/NpmGetAliases.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Cake.Core;
2+
using Cake.Core.Annotations;
3+
using Cake.Npm.Get;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
8+
namespace Cake.Npm
9+
{
10+
/// <summary>
11+
/// Npm get aliases
12+
/// </summary>
13+
[CakeAliasCategory("Npm")]
14+
[CakeNamespaceImport("Cake.Npm")]
15+
public static class NpmGetAliases
16+
{
17+
/// <summary>
18+
/// Gets the npm config on the given key
19+
/// </summary>
20+
/// <param name="context">The cake context.</param>
21+
/// <param name="key">The config key.</param>
22+
/// <param name="workingDirectory">The working directory</param>
23+
/// <returns>The value on the given key.</returns>
24+
[CakeMethodAlias]
25+
[CakeAliasCategory("Get")]
26+
public static string NpmGet(this ICakeContext context, string key, string workingDirectory = null)
27+
{
28+
if (key == null)
29+
{
30+
throw new ArgumentNullException("key can't be null");
31+
}
32+
NpmGetSettings settings = new NpmGetSettings()
33+
{
34+
Key = key,
35+
WorkingDirectory = workingDirectory
36+
};
37+
return new NpmGetTools(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log).Get(settings);
38+
}
39+
}
40+
}

src/Cake.Npm/NpmViewAliases.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Cake.Core;
2+
using Cake.Core.Annotations;
3+
using Cake.Npm.View;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
8+
namespace Cake.Npm
9+
{
10+
/// <summary>
11+
/// Npm view aliases
12+
/// </summary>
13+
[CakeAliasCategory("Npm")]
14+
[CakeNamespaceImport("Cake.Npm")]
15+
public static class NpmViewAliases
16+
{
17+
/// <summary>
18+
/// Call npm view with --json attribute.
19+
/// </summary>
20+
/// <param name="context"></param>
21+
/// <param name="packageName">Name of the package</param>
22+
/// <param name="workingDirectory"></param>
23+
/// <returns>An empty string if the package was not found on the repository</returns>
24+
[CakeMethodAlias]
25+
[CakeAliasCategory("View")]
26+
public static string NpmView(this ICakeContext context, string packageName = null, string workingDirectory = null)
27+
{
28+
NpmViewSettings settings = new NpmViewSettings()
29+
{
30+
PackageName = packageName,
31+
WorkingDirectory = workingDirectory
32+
};
33+
return new NpmViewTools(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log).View(settings);
34+
}
35+
}
36+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Cake.Core;
2+
using Cake.Core.IO;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
8+
namespace Cake.Npm.View
9+
{
10+
class NpmViewSettings : NpmSettings
11+
{
12+
public NpmViewSettings() : base("view")
13+
{
14+
RedirectStandardOutput = true;
15+
}
16+
17+
public string PackageName { get; set; }
18+
19+
protected override void EvaluateCore(ProcessArgumentBuilder args)
20+
{
21+
base.EvaluateCore(args);
22+
args.Append("--json");
23+
args.Append(PackageName);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)