From 5a0d9378227bdf8949ecd81c9309d4d9f696981b Mon Sep 17 00:00:00 2001 From: Alistair Chapman Date: Sun, 11 Sep 2016 18:56:02 +1000 Subject: [PATCH] Add initial support for npm pack command --- src/Cake.Npm.Tests/Cake.Npm.Tests.csproj | 1 + src/Cake.Npm.Tests/NpmPackTests.cs | 29 ++++++++++++++++ src/Cake.Npm.Tests/NpmRunnerFixture.cs | 14 ++++++++ src/Cake.Npm/Cake.Npm.csproj | 1 + src/Cake.Npm/NpmPackSettings.cs | 44 ++++++++++++++++++++++++ src/Cake.Npm/NpmRunner.cs | 19 ++++++++++ src/Cake.Npm/NpmRunnerAliases.cs | 11 ++++++ usage.cake | 5 ++- 8 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 src/Cake.Npm.Tests/NpmPackTests.cs create mode 100644 src/Cake.Npm/NpmPackSettings.cs diff --git a/src/Cake.Npm.Tests/Cake.Npm.Tests.csproj b/src/Cake.Npm.Tests/Cake.Npm.Tests.csproj index 480cf61..b3e22cc 100644 --- a/src/Cake.Npm.Tests/Cake.Npm.Tests.csproj +++ b/src/Cake.Npm.Tests/Cake.Npm.Tests.csproj @@ -69,6 +69,7 @@ + diff --git a/src/Cake.Npm.Tests/NpmPackTests.cs b/src/Cake.Npm.Tests/NpmPackTests.cs new file mode 100644 index 0000000..94b187d --- /dev/null +++ b/src/Cake.Npm.Tests/NpmPackTests.cs @@ -0,0 +1,29 @@ +using Shouldly; +using Xunit; + +namespace Cake.Npm.Tests +{ + public class NpmPackTests + { + private readonly NpmPackFixture _fixture; + public NpmPackTests() + { + _fixture = new NpmPackFixture(); + } + + [Fact] + public void Not_Setting_Target_Should_Use_Empty() + { + var result = _fixture.Run(); + result.Args.ShouldBe("pack"); + } + + [Fact] + public void Including_Target_Should_Set_Correct_Argument() + { + _fixture.Target = "package.tgz"; + var result = _fixture.Run(); + result.Args.ShouldBe("pack package.tgz"); + } + } +} diff --git a/src/Cake.Npm.Tests/NpmRunnerFixture.cs b/src/Cake.Npm.Tests/NpmRunnerFixture.cs index bf230fe..0a8785a 100644 --- a/src/Cake.Npm.Tests/NpmRunnerFixture.cs +++ b/src/Cake.Npm.Tests/NpmRunnerFixture.cs @@ -47,4 +47,18 @@ protected override void RunTool() tool.RunScript(ScriptName, RunScriptSettings); } } + + public class NpmPackFixture : ToolFixture + { + internal string Target { get; set; } + public NpmPackFixture() : base("npm") + { + } + + protected override void RunTool() + { + var tool = new NpmRunner(FileSystem, Environment, ProcessRunner, Tools); + tool.Pack(Target); + } + } } diff --git a/src/Cake.Npm/Cake.Npm.csproj b/src/Cake.Npm/Cake.Npm.csproj index 908e512..f918f73 100644 --- a/src/Cake.Npm/Cake.Npm.csproj +++ b/src/Cake.Npm/Cake.Npm.csproj @@ -49,6 +49,7 @@ + Properties\VersionAssemblyInfo.cs diff --git a/src/Cake.Npm/NpmPackSettings.cs b/src/Cake.Npm/NpmPackSettings.cs new file mode 100644 index 0000000..a946524 --- /dev/null +++ b/src/Cake.Npm/NpmPackSettings.cs @@ -0,0 +1,44 @@ +using Cake.Core; +using Cake.Core.IO; + +namespace Cake.Npm +{ + /// + /// npm pack options + /// + public class NpmPackSettings : NpmRunnerSettings + { + /// + /// 'npm pack' settings with target + /// + public NpmPackSettings(string target) : base("pack") + { + Target = target; + } + + /// + /// 'npm pack' settings + /// + public NpmPackSettings() : this(null) + { + } + + /// + /// Installation target + /// + public string Target { get; set; } + + /// + /// evaluate options + /// + /// + protected override void EvaluateCore(ProcessArgumentBuilder args) + { + base.EvaluateCore(args); + if (!string.IsNullOrWhiteSpace(Target)) + { + args.Append(Target); + } + } + } +} diff --git a/src/Cake.Npm/NpmRunner.cs b/src/Cake.Npm/NpmRunner.cs index 936b236..09a6f3d 100644 --- a/src/Cake.Npm/NpmRunner.cs +++ b/src/Cake.Npm/NpmRunner.cs @@ -167,6 +167,25 @@ private static ProcessArgumentBuilder GetNpmRunArguments(NpmRunScriptSettings se return args; } + /// + /// execute 'npm pack' with an optional installable target + /// + /// package folder, tarball, or name. Defaults to the current directory + public INpmRunnerCommands Pack(string target = null) + { + var settings = new NpmPackSettings(target); + var args = GetNpmPackArguments(settings); + Run(settings, args); + return this; + } + + private ProcessArgumentBuilder GetNpmPackArguments(NpmPackSettings settings) + { + var args = new ProcessArgumentBuilder(); + settings?.Evaluate(args); + return args; + } + /// /// Gets the name of the tool /// diff --git a/src/Cake.Npm/NpmRunnerAliases.cs b/src/Cake.Npm/NpmRunnerAliases.cs index 9d0b3dd..8017a8b 100644 --- a/src/Cake.Npm/NpmRunnerAliases.cs +++ b/src/Cake.Npm/NpmRunnerAliases.cs @@ -111,6 +111,17 @@ public static class NpmRunnerAliases /// }); /// ]]> /// + /// Run 'npm pack' + /// Cake task: + /// + /// + /// { + /// Npm.Pack(); + /// }); + /// ]]> + /// /// [CakePropertyAlias] public static NpmRunner Npm(this ICakeContext context) diff --git a/usage.cake b/usage.cake index 009fc6b..a4517ad 100644 --- a/usage.cake +++ b/usage.cake @@ -26,9 +26,12 @@ Task("Default") // npm run hello Npm.RunScript("hello"); - //npm run arguments -- -debug "arg-value.file" + // npm run arguments -- -debug "arg-value.file" Npm.RunScript("arguments", settings => settings.WithArgument("-debug").WithArgument("arg-value.file")); + // npm pack + Npm.Pack(); + //npm install gulp. Executedwith ./usage set as the working directory Npm .WithLogLevel(NpmLogLevel.Silent)