forked from cake-contrib/Cake.Npm
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(cake-contribGH-20) Add support for npm publish
- Loading branch information
1 parent
e79f5fe
commit d16cc79
Showing
5 changed files
with
305 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,107 @@ | ||
using System; | ||
using Cake.Core; | ||
using Cake.Core.Annotations; | ||
using Cake.Npm.Publish; | ||
|
||
namespace Cake.Npm | ||
{ | ||
/// <summary> | ||
/// Npm publish aliases. | ||
/// </summary> | ||
[CakeAliasCategory("Npm")] | ||
[CakeNamespaceImport("Cake.Npm.Publish")] | ||
public static class NpmPublishAliases | ||
{ | ||
/// <summary> | ||
/// Publishes the npm package in the current working directory. | ||
/// </summary> | ||
/// <param name="context">The context.</param> | ||
/// <example> | ||
/// <code> | ||
/// <![CDATA[ | ||
/// NpmPublish(); | ||
/// ]]> | ||
/// </code> | ||
/// </example> | ||
[CakeMethodAlias] | ||
[CakeAliasCategory("Publish")] | ||
public static void NpmPublish(this ICakeContext context) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
context.NpmPublish(new NpmPublishSettings()); | ||
} | ||
|
||
/// <summary> | ||
/// Publishes the npm package created from a specific source. | ||
/// </summary> | ||
/// <param name="context">The context.</param> | ||
/// <param name="source">Source to publish. | ||
/// A folder containing a package.json file or an url or file path to a gzipped tar archive | ||
/// containing a single folder with a package.json file inside.</param> | ||
/// <example> | ||
/// <code> | ||
/// <![CDATA[ | ||
/// NpmPublish("c:\mypackagesource"); | ||
/// ]]> | ||
/// </code> | ||
/// </example> | ||
[CakeMethodAlias] | ||
[CakeAliasCategory("Publish")] | ||
public static void NpmPublish(this ICakeContext context, string source) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
if (String.IsNullOrWhiteSpace(source)) | ||
{ | ||
throw new ArgumentNullException(nameof(source)); | ||
} | ||
|
||
context.NpmPublish(new NpmPublishSettings { Source = source }); | ||
} | ||
|
||
/// <summary> | ||
/// Publishes a npm package based on the specified settings. | ||
/// </summary> | ||
/// <param name="context">The context.</param> | ||
/// <param name="settings">The settings.</param> | ||
/// <example> | ||
/// <code> | ||
/// <![CDATA[ | ||
/// var settings = | ||
/// new NpmPublishSettings | ||
/// { | ||
/// LogLevel = NpmLogLevel.Verbose, | ||
/// Source = "c:\mypackagesource" | ||
/// }; | ||
/// NpmPublish(settings); | ||
/// ]]> | ||
/// </code> | ||
/// </example> | ||
[CakeMethodAlias] | ||
[CakeAliasCategory("Publish")] | ||
public static void NpmPublish(this ICakeContext context, NpmPublishSettings settings) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
if (settings == null) | ||
{ | ||
throw new ArgumentNullException(nameof(settings)); | ||
} | ||
|
||
AddinInformation.LogVersionInformation(context.Log); | ||
|
||
var publisher = new NpmPublisher(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log); | ||
publisher.Publish(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,40 @@ | ||
namespace Cake.Npm.Publish | ||
{ | ||
using Core; | ||
using Core.IO; | ||
|
||
/// <summary> | ||
/// Contains settings used by <see cref="NpmPublisher"/>. | ||
/// </summary> | ||
public class NpmPublishSettings : NpmSettings | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NpmPublishSettings"/> class. | ||
/// </summary> | ||
public NpmPublishSettings() | ||
: base("publish") | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Source to publish. | ||
/// A folder containing a package.json file or an url or file path to a gzipped tar archive | ||
/// containing a single folder with a package.json file inside. | ||
/// </summary> | ||
public string Source { 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); | ||
|
||
if (!string.IsNullOrWhiteSpace(Source)) | ||
{ | ||
args.AppendQuoted(Source); | ||
} | ||
} | ||
} | ||
} |
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.Publish | ||
{ | ||
using System; | ||
using Core; | ||
using Core.Diagnostics; | ||
using Core.IO; | ||
using Core.Tooling; | ||
|
||
/// <summary> | ||
/// Tool for publishing npm modules. | ||
/// </summary> | ||
public class NpmPublisher : NpmTool<NpmPublishSettings> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NpmPublisher"/> 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 NpmPublisher( | ||
IFileSystem fileSystem, | ||
ICakeEnvironment environment, | ||
IProcessRunner processRunner, | ||
IToolLocator tools, | ||
ICakeLog log) | ||
: base(fileSystem, environment, processRunner, tools, log) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Publishes a npm package based on the specified settings. | ||
/// </summary> | ||
/// <param name="settings">The settings.</param> | ||
public void Publish(NpmPublishSettings 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,17 @@ | ||
namespace Cake.Npm.Tests.Publish | ||
{ | ||
using Npm.Publish; | ||
|
||
internal sealed class NpmPublisherFixture : NpmFixture<NpmPublishSettings> | ||
{ | ||
public NpmPublisherFixture() | ||
{ | ||
} | ||
|
||
protected override void RunTool() | ||
{ | ||
var tool = new NpmPublisher(FileSystem, Environment, ProcessRunner, Tools, Log); | ||
tool.Publish(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,95 @@ | ||
namespace Cake.Npm.Tests.Publish | ||
{ | ||
using Core.Diagnostics; | ||
using Xunit; | ||
|
||
public class NpmPublisherTests | ||
{ | ||
public sealed class ThePublishMethod | ||
{ | ||
[Fact] | ||
public void Should_Throw_If_Settings_Are_Null() | ||
{ | ||
// Given | ||
var fixture = new NpmPublisherFixture(); | ||
fixture.Settings = null; | ||
|
||
// When | ||
var result = Record.Exception(() => fixture.Run()); | ||
|
||
// Then | ||
result.IsArgumentNullException("settings"); | ||
} | ||
|
||
[Fact] | ||
public void Should_Add_Mandatory_Arguments() | ||
{ | ||
// Given | ||
var fixture = new NpmPublisherFixture(); | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.Equal("publish", result.Args); | ||
} | ||
|
||
[Fact] | ||
public void Should_Add_Source_To_Arguments_If_Not_Null() | ||
{ | ||
// Given | ||
var fixture = new NpmPublisherFixture(); | ||
fixture.Settings.Source = "c:\\mypackage"; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.Equal("publish \"c:\\mypackage\"", result.Args); | ||
} | ||
|
||
[Theory] | ||
[InlineData(NpmLogLevel.Default, "publish")] | ||
[InlineData(NpmLogLevel.Info, "publish --loglevel info")] | ||
[InlineData(NpmLogLevel.Silent, "publish --silent")] | ||
[InlineData(NpmLogLevel.Silly, "publish --loglevel silly")] | ||
[InlineData(NpmLogLevel.Verbose, "publish --loglevel verbose")] | ||
[InlineData(NpmLogLevel.Warn, "publish --warn")] | ||
public void Should_Add_LogLevel_To_Arguments_If_Not_Null( | ||
NpmLogLevel logLevel, | ||
string expected) | ||
{ | ||
// Given | ||
var fixture = new NpmPublisherFixture(); | ||
fixture.Settings.LogLevel = logLevel; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.Equal(expected, result.Args); | ||
} | ||
|
||
[Theory] | ||
[InlineData(Verbosity.Diagnostic, "publish --loglevel verbose")] | ||
[InlineData(Verbosity.Minimal, "publish --warn")] | ||
[InlineData(Verbosity.Normal, "publish")] | ||
[InlineData(Verbosity.Quiet, "publish --silent")] | ||
[InlineData(Verbosity.Verbose, "publish --loglevel info")] | ||
public void Should_Use_Cake_LogLevel_If_LogLevel_Is_Set_To_Default( | ||
Verbosity verbosity, | ||
string expected) | ||
{ | ||
// Given | ||
var fixture = new NpmPublisherFixture(); | ||
fixture.Settings.CakeVerbosityLevel = verbosity; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.Equal(expected, result.Args); | ||
} | ||
} | ||
} | ||
} |