Skip to content

Commit

Permalink
Merge pull request #37 from pascalberger/publish
Browse files Browse the repository at this point in the history
(GH-20) Add support for npm publish
  • Loading branch information
pascalberger authored May 19, 2017
2 parents 8d2f042 + fb71288 commit 253b976
Show file tree
Hide file tree
Showing 9 changed files with 719 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Cake.Npm/Namespaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ internal class NamespaceDoc
}
}

// ReSharper disable once CheckNamespace
namespace Cake.Npm.Publish
{
/// <summary>
/// This namespace contain types used for publishing npm packages.
/// </summary>
[CompilerGenerated]
internal class NamespaceDoc
{
}
}

// ReSharper disable once CheckNamespace
namespace Cake.Npm.RunScript
{
Expand Down
138 changes: 138 additions & 0 deletions src/Cake.Npm/NpmPublishAliases.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
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 using the settings returned by a configurator.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="configurator">The settings configurator.</param>
/// <example>
/// <code>
/// <![CDATA[
/// NpmPack(settings => settings.WithLogLevel(NpmLogLevel.Verbose));
/// ]]>
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Publish")]
public static void NpmPublish(this ICakeContext context, Action<NpmPublishSettings> configurator)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (configurator == null)
{
throw new ArgumentNullException(nameof(configurator));
}

var settings = new NpmPublishSettings();
configurator(settings);
context.NpmPublish(settings);
}

/// <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);
}
}
}
23 changes: 23 additions & 0 deletions src/Cake.Npm/Publish/NpmPublishAccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Cake.Npm.Publish
{
/// <summary>
/// Possible values for <see cref="NpmPublishSettings.Access"/>.
/// </summary>
public enum NpmPublishAccess
{
/// <summary>
/// Default value for <c>npm publish</c>.
/// </summary>
Default,

/// <summary>
/// Public access.
/// </summary>
Public,

/// <summary>
/// Restricted access.
/// </summary>
Restricted
}
}
72 changes: 72 additions & 0 deletions src/Cake.Npm/Publish/NpmPublishSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace Cake.Npm.Publish
{
using System;
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>
/// Gets or sets the 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>
/// Gets or sets the tag with which the package will be published.
/// By default the <c>latest</c> tag is used.
/// </summary>
public string Tag { get; set; }

/// <summary>
/// Gets or sets whether the package should be published as public or restricted.
/// </summary>
public NpmPublishAccess Access { get; set; }

/// <summary>
/// Gets or sets the registry where the package should be published to.
/// </summary>
public Uri Registry { 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);
}

if (!string.IsNullOrWhiteSpace(Tag))
{
args.AppendSwitch("--tag", Tag);
}

if (Access != NpmPublishAccess.Default)
{
args.AppendSwitch("--access", Access.ToString().ToLowerInvariant());
}

if (Registry != null)
{
args.AppendSwitch("--registry", Registry.ToString());
}
}
}
}
99 changes: 99 additions & 0 deletions src/Cake.Npm/Publish/NpmPublishSettingsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
namespace Cake.Npm.Publish
{
using System;

/// <summary>
/// Extensions for <see cref="NpmPublishSettings"/>.
/// </summary>
public static class NpmPublishSettingsExtensions
{
/// <summary>
/// Sets the source to publish.
/// </summary>
/// <param name="settings">The settings.</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>
/// <returns>The <paramref name="settings"/> instance with <see cref="NpmPublishSettings.Source"/> set to <paramref name="source"/>.</returns>
public static NpmPublishSettings FromSource(this NpmPublishSettings settings, string source)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

if (string.IsNullOrWhiteSpace(source))
{
throw new ArgumentNullException(nameof(source));
}

settings.Source = source;

return settings;
}

/// <summary>
/// Sets the tag of the package to publish.
/// </summary>
/// <param name="settings">The settings.</param>
/// <param name="tag">Tag with which the package will be published.</param>
/// <returns>The <paramref name="settings"/> instance with <see cref="NpmPublishSettings.Tag"/> set to <paramref name="tag"/>.</returns>
public static NpmPublishSettings WithTag(this NpmPublishSettings settings, string tag)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

if (string.IsNullOrWhiteSpace(tag))
{
throw new ArgumentNullException(nameof(tag));
}

settings.Tag = tag;

return settings;
}

/// <summary>
/// Sets the access of the published package.
/// </summary>
/// <param name="settings">The settings.</param>
/// <param name="access">Access of the published package.</param>
/// <returns>The <paramref name="settings"/> instance with <see cref="NpmPublishSettings.Access"/> set to <paramref name="access"/>.</returns>
public static NpmPublishSettings WithAccess(this NpmPublishSettings settings, NpmPublishAccess access)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

settings.Access = access;

return settings;
}

/// <summary>
/// Sets the registry where the package will be published to.
/// </summary>
/// <param name="settings">The settings.</param>
/// <param name="registry">Registry where the package will be published to.</param>
/// <returns>The <paramref name="settings"/> instance with <see cref="NpmPublishSettings.Registry"/> set to <paramref name="registry"/>.</returns>
public static NpmPublishSettings ToRegistry(this NpmPublishSettings settings, Uri registry)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

if (registry == null)
{
throw new ArgumentNullException(nameof(registry));
}

settings.Registry = registry;

return settings;
}
}
}
Loading

0 comments on commit 253b976

Please sign in to comment.