From d16cc7931bc4473e12f456d120140a5b51ea4624 Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Wed, 3 May 2017 15:55:35 +0200 Subject: [PATCH] (GH-20) Add support for npm publish --- src/Cake.Npm/NpmPublishAliases.cs | 107 ++++++++++++++++++ src/Cake.Npm/Publish/NpmPublishSettings.cs | 40 +++++++ src/Cake.Npm/Publish/NpmPublisher.cs | 46 ++++++++ .../Publish/NpmPublisherFixture.cs | 17 +++ .../Publish/NpmPublisherTests.cs | 95 ++++++++++++++++ 5 files changed, 305 insertions(+) create mode 100644 src/Cake.Npm/NpmPublishAliases.cs create mode 100644 src/Cake.Npm/Publish/NpmPublishSettings.cs create mode 100644 src/Cake.Npm/Publish/NpmPublisher.cs create mode 100644 tests/Cake.Npm.Tests/Publish/NpmPublisherFixture.cs create mode 100644 tests/Cake.Npm.Tests/Publish/NpmPublisherTests.cs diff --git a/src/Cake.Npm/NpmPublishAliases.cs b/src/Cake.Npm/NpmPublishAliases.cs new file mode 100644 index 0000000..d54f092 --- /dev/null +++ b/src/Cake.Npm/NpmPublishAliases.cs @@ -0,0 +1,107 @@ +using System; +using Cake.Core; +using Cake.Core.Annotations; +using Cake.Npm.Publish; + +namespace Cake.Npm +{ + /// + /// Npm publish aliases. + /// + [CakeAliasCategory("Npm")] + [CakeNamespaceImport("Cake.Npm.Publish")] + public static class NpmPublishAliases + { + /// + /// Publishes the npm package in the current working directory. + /// + /// The context. + /// + /// + /// + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Publish")] + public static void NpmPublish(this ICakeContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + context.NpmPublish(new NpmPublishSettings()); + } + + /// + /// Publishes the npm package created from a specific source. + /// + /// The context. + /// 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. + /// + /// + /// + /// + /// + [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 }); + } + + /// + /// Publishes a npm package based on the specified settings. + /// + /// The context. + /// The settings. + /// + /// + /// + /// + /// + [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); + } + } +} \ No newline at end of file diff --git a/src/Cake.Npm/Publish/NpmPublishSettings.cs b/src/Cake.Npm/Publish/NpmPublishSettings.cs new file mode 100644 index 0000000..50a560c --- /dev/null +++ b/src/Cake.Npm/Publish/NpmPublishSettings.cs @@ -0,0 +1,40 @@ +namespace Cake.Npm.Publish +{ + using Core; + using Core.IO; + + /// + /// Contains settings used by . + /// + public class NpmPublishSettings : NpmSettings + { + /// + /// Initializes a new instance of the class. + /// + public NpmPublishSettings() + : base("publish") + { + } + + /// + /// 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. + /// + public string Source { get; set; } + + /// + /// Evaluates the settings and writes them to . + /// + /// The argument builder into which the settings should be written. + protected override void EvaluateCore(ProcessArgumentBuilder args) + { + base.EvaluateCore(args); + + if (!string.IsNullOrWhiteSpace(Source)) + { + args.AppendQuoted(Source); + } + } + } +} diff --git a/src/Cake.Npm/Publish/NpmPublisher.cs b/src/Cake.Npm/Publish/NpmPublisher.cs new file mode 100644 index 0000000..e213d75 --- /dev/null +++ b/src/Cake.Npm/Publish/NpmPublisher.cs @@ -0,0 +1,46 @@ +namespace Cake.Npm.Publish +{ + using System; + using Core; + using Core.Diagnostics; + using Core.IO; + using Core.Tooling; + + /// + /// Tool for publishing npm modules. + /// + public class NpmPublisher : NpmTool + { + /// + /// Initializes a new instance of the class. + /// + /// The file system. + /// The environment. + /// The process runner. + /// The tool locator. + /// Cake log instance. + public NpmPublisher( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools, + ICakeLog log) + : base(fileSystem, environment, processRunner, tools, log) + { + } + + /// + /// Publishes a npm package based on the specified settings. + /// + /// The settings. + public void Publish(NpmPublishSettings settings) + { + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + RunCore(settings); + } + } +} diff --git a/tests/Cake.Npm.Tests/Publish/NpmPublisherFixture.cs b/tests/Cake.Npm.Tests/Publish/NpmPublisherFixture.cs new file mode 100644 index 0000000..8fd29cf --- /dev/null +++ b/tests/Cake.Npm.Tests/Publish/NpmPublisherFixture.cs @@ -0,0 +1,17 @@ +namespace Cake.Npm.Tests.Publish +{ + using Npm.Publish; + + internal sealed class NpmPublisherFixture : NpmFixture + { + public NpmPublisherFixture() + { + } + + protected override void RunTool() + { + var tool = new NpmPublisher(FileSystem, Environment, ProcessRunner, Tools, Log); + tool.Publish(Settings); + } + } +} diff --git a/tests/Cake.Npm.Tests/Publish/NpmPublisherTests.cs b/tests/Cake.Npm.Tests/Publish/NpmPublisherTests.cs new file mode 100644 index 0000000..b200cc0 --- /dev/null +++ b/tests/Cake.Npm.Tests/Publish/NpmPublisherTests.cs @@ -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); + } + } + } +}