Skip to content

Commit

Permalink
add AddRawContents methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gdivis committed May 25, 2018
1 parent 085d0c6 commit 7a6d503
Showing 1 changed file with 53 additions and 21 deletions.
74 changes: 53 additions & 21 deletions src/Inedo.UPack/Packaging/UniversalPackageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Inedo.UPack.Packaging
/// </summary>
public sealed class UniversalPackageBuilder : IDisposable
{
private ZipArchive zip;
private readonly ZipArchive zip;

/// <summary>
/// Initializes a new instance of the <see cref="UniversalPackageBuilder"/> class.
Expand Down Expand Up @@ -163,7 +163,57 @@ public void AddEmptyDirectory(string path)
/// <param name="cancellationToken">Cancellation token for asynchronous operations.</param>
/// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is null or empty.</exception>
/// <exception cref="ArgumentException"><paramref name="sourcePath"/> is not an absolute path.</exception>
public async Task AddContentsAsync(string sourcePath, string targetPath, bool recursive, Predicate<string> shouldInclude, CancellationToken cancellationToken)
public Task AddContentsAsync(string sourcePath, string targetPath, bool recursive, Predicate<string> shouldInclude, CancellationToken cancellationToken) => this.AddContentsInternalAsync(sourcePath, targetPath, recursive, shouldInclude, this.AddFileAsync, cancellationToken);

/// <summary>
/// Adds the files and directories from the specified source path to the specified raw target path in the package.
/// </summary>
/// <param name="sourcePath">Full source path of files and directories to include. This must be an absolute path.</param>
/// <param name="targetPath">Target prefix path inside the package.</param>
/// <param name="recursive">When true, subdirectories will be recursively added to the package.</param>
/// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is null or empty.</exception>
/// <exception cref="ArgumentException"><paramref name="sourcePath"/> is not an absolute path.</exception>
public Task AddRawContentsAsync(string sourcePath, string targetPath, bool recursive) => this.AddRawContentsAsync(sourcePath, targetPath, recursive, null);
/// <summary>
/// Adds the files and directories from the specified source path to the specified raw target path in the package.
/// </summary>
/// <param name="sourcePath">Full source path of files and directories to include. This must be an absolute path.</param>
/// <param name="targetPath">Target prefix path inside the package.</param>
/// <param name="recursive">When true, subdirectories will be recursively added to the package.</param>
/// <param name="shouldInclude">Method invoked for each file to determine if it should be added to the package. The full source path is the argument supplied to the method.</param>
/// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is null or empty.</exception>
/// <exception cref="ArgumentException"><paramref name="sourcePath"/> is not an absolute path.</exception>
public Task AddRawContentsAsync(string sourcePath, string targetPath, bool recursive, Predicate<string> shouldInclude) => this.AddRawContentsAsync(sourcePath, targetPath, recursive, shouldInclude, new CancellationToken());
/// <summary>
/// Adds the files and directories from the specified source path to the specified raw target path in the package.
/// </summary>
/// <param name="sourcePath">Full source path of files and directories to include. This must be an absolute path.</param>
/// <param name="targetPath">Target prefix path inside the package.</param>
/// <param name="recursive">When true, subdirectories will be recursively added to the package.</param>
/// <param name="shouldInclude">Method invoked for each file to determine if it should be added to the package. The full source path is the argument supplied to the method.</param>
/// <param name="cancellationToken">Cancellation token for asynchronous operations.</param>
/// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is null or empty.</exception>
/// <exception cref="ArgumentException"><paramref name="sourcePath"/> is not an absolute path.</exception>
public Task AddRawContentsAsync(string sourcePath, string targetPath, bool recursive, Predicate<string> shouldInclude, CancellationToken cancellationToken) => this.AddContentsInternalAsync(sourcePath, targetPath, recursive, shouldInclude, this.AddFileRawAsync, cancellationToken);

private static Stream CreateFile(string fileName)
{
if (string.IsNullOrEmpty(fileName))
throw new ArgumentNullException(nameof(fileName));

return new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
}
private void WriteMetadata(UniversalPackageMetadata metadata)
{
var entry = this.zip.CreateEntry("upack.json");
using (var entryStream = entry.Open())
using (var writer = new StreamWriter(entryStream, new UTF8Encoding(false)))
using (var jsonWriter = new JsonTextWriter(writer) { Formatting = Formatting.Indented })
{
metadata.WriteJson(jsonWriter);
}
}
private async Task AddContentsInternalAsync(string sourcePath, string targetPath, bool recursive, Predicate<string> shouldInclude, Func<Stream, string, DateTimeOffset, CancellationToken, Task> add, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(sourcePath))
throw new ArgumentNullException(nameof(sourcePath));
Expand All @@ -190,7 +240,7 @@ public async Task AddContentsAsync(string sourcePath, string targetPath, bool re
for (int i = 1; i < pathParts.Length - 1; i++)
addedDirs.Add(string.Join("/", pathParts.Take(i)));

await this.AddFileAsync(sourceStream, itemPath, File.GetLastWriteTimeUtc(sourceFileName), cancellationToken).ConfigureAwait(false);
await add(sourceStream, itemPath, File.GetLastWriteTimeUtc(sourceFileName), cancellationToken).ConfigureAwait(false);
}
}

Expand All @@ -212,23 +262,5 @@ string getFullTargetPath(string fullSourcePath)
return string.IsNullOrEmpty(targetPath) ? path : (root + "/" + path);
}
}

private static Stream CreateFile(string fileName)
{
if (string.IsNullOrEmpty(fileName))
throw new ArgumentNullException(nameof(fileName));

return new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
}
private void WriteMetadata(UniversalPackageMetadata metadata)
{
var entry = this.zip.CreateEntry("upack.json");
using (var entryStream = entry.Open())
using (var writer = new StreamWriter(entryStream, new UTF8Encoding(false)))
using (var jsonWriter = new JsonTextWriter(writer) { Formatting = Formatting.Indented })
{
metadata.WriteJson(jsonWriter);
}
}
}
}

0 comments on commit 7a6d503

Please sign in to comment.