-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add commands for npm dist-tag, view, get
- Loading branch information
1 parent
af4da1b
commit c97a047
Showing
9 changed files
with
402 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.