Skip to content

Commit

Permalink
Fix plugin package install, get credentials in multifeed environment,…
Browse files Browse the repository at this point in the history
… and add get service index request/response.
  • Loading branch information
dtivel committed May 24, 2017
1 parent 4727daa commit a767c82
Show file tree
Hide file tree
Showing 37 changed files with 2,701 additions and 450 deletions.
61 changes: 56 additions & 5 deletions src/NuGet.Core/NuGet.Common/Preprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace NuGet.Common
{
Expand All @@ -12,27 +14,76 @@ namespace NuGet.Common
/// </summary>
public class Preprocessor
{
public static string Process(Func<Stream> fileStreamFactory, Func<string, string> tokenReplacement)
/// <summary>
/// Asynchronously performs token replacement on a file stream.
/// </summary>
/// <param name="streamTaskFactory">A stream task factory.</param>
/// <param name="tokenReplacement">A token replacement function.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task that represents the asynchronous operation.
/// The task result (<see cref="Task{TResult}.Result" />) returns a <see cref="string" />.</returns>
/// <exception cref="ArgumentException">Thrown if <paramref name="streamTaskFactory" />
/// is either <c>null</c> or empty.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="tokenReplacement" />
/// is <c>null</c>.</exception>
/// <exception cref="OperationCanceledException">Thrown if <paramref name="cancellationToken" />
/// is cancelled.</exception>
public static async Task<string> ProcessAsync(
Func<Task<Stream>> streamTaskFactory,
Func<string, string> tokenReplacement,
CancellationToken cancellationToken)
{
using (var stream = fileStreamFactory())
if (streamTaskFactory == null)
{
throw new ArgumentNullException(nameof(streamTaskFactory));
}

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

cancellationToken.ThrowIfCancellationRequested();

using (var stream = await streamTaskFactory())
{
return Process(stream, tokenReplacement);
}
}

/// <summary>
/// Performs token replacement on a stream and returns the result.
/// </summary>
/// <param name="stream">A stream.</param>
/// <param name="tokenReplacement">A token replacement funciton.</param>
/// <returns>The token-replaced stream content.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="stream" />
/// is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="tokenReplacement" />
/// is <c>null</c>.</exception>
public static string Process(Stream stream, Func<string, string> tokenReplacement)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}

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

string text;
using (var streamReader = new StreamReader(stream))
{
text = streamReader.ReadToEnd();
}

var tokenizer = new Tokenizer(text);
StringBuilder result = new StringBuilder();
var result = new StringBuilder();
for (; ; )
{
Token token = tokenizer.Read();
var token = tokenizer.Read();
if (token == null)
{
break;
Expand All @@ -52,4 +103,4 @@ public static string Process(Stream stream, Func<string, string> tokenReplacemen
return result.ToString();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,61 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace NuGet.ProjectManagement
{
/// <summary>
/// Represents a package file transformer.
/// </summary>
public interface IPackageFileTransformer
{
/// <summary>
/// Transforms the file
/// Asynchronously transforms a file.
/// </summary>
void TransformFile(Func<Stream> fileStreamFactory, string targetPath, IMSBuildNuGetProjectSystem projectSystem);
/// <param name="streamTaskFactory">A stream task factory.</param>
/// <param name="targetPath">A path to the file to be transformed.</param>
/// <param name="projectSystem">The project where this change is taking place.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="streamTaskFactory" />
/// is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="projectSystem" />
/// is <c>null</c>.</exception>
/// <exception cref="OperationCanceledException">Thrown if <paramref name="cancellationToken" />
/// is cancelled.</exception>
Task TransformFileAsync(
Func<Task<Stream>> streamTaskFactory,
string targetPath,
IMSBuildNuGetProjectSystem projectSystem,
CancellationToken cancellationToken);

/// <summary>
/// Reverses the transform on the targetPath, using all the potential source of change
/// Asynchronously reverses the transform on the targetPath, using all the potential source of change.
/// </summary>
/// <param name="fileStreamFactory">A factory for accessing the file to be reverted from the nupkg being uninstalled.</param>
/// <param name="streamTaskFactory">A factory for accessing the file to be reverted from the nupkg being uninstalled.</param>
/// <param name="targetPath">A path to the file to be reverted.</param>
/// <param name="matchingFiles">Other files in other packages that may have changed the <paramref name="targetPath"/>.</param>
/// <param name="matchingFiles">Other files in other packages that may have changed the <paramref name="targetPath" />.</param>
/// <param name="projectSystem">The project where this change is taking place.</param>
void RevertFile(Func<Stream> fileStreamFactory,
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="streamTaskFactory" />
/// is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="matchingFiles" />
/// is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="projectSystem" />
/// is <c>null</c>.</exception>
/// <exception cref="OperationCanceledException">Thrown if <paramref name="cancellationToken" />
/// is cancelled.</exception>
Task RevertFileAsync(
Func<Task<Stream>> streamTaskFactory,
string targetPath,
IEnumerable<InternalZipFileInfo> matchingFiles,
IMSBuildNuGetProjectSystem projectSystem);
IMSBuildNuGetProjectSystem projectSystem,
CancellationToken cancellationToken);
}
}
}

This file was deleted.

108 changes: 108 additions & 0 deletions src/NuGet.Core/NuGet.PackageManagement/FileModifiers/Preprocessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace NuGet.ProjectManagement
{
/// <summary>
/// Simple token replacement system for content files.
/// </summary>
public class Preprocessor : IPackageFileTransformer
{
/// <summary>
/// Asynchronously transforms a file.
/// </summary>
/// <param name="streamTaskFactory">A stream task factory.</param>
/// <param name="targetPath">A path to the file to be transformed.</param>
/// <param name="projectSystem">The project where this change is taking place.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="streamTaskFactory" />
/// is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="projectSystem" />
/// is <c>null</c>.</exception>
/// <exception cref="OperationCanceledException">Thrown if <paramref name="cancellationToken" />
/// is cancelled.</exception>
public async Task TransformFileAsync(
Func<Task<Stream>> streamTaskFactory,
string targetPath,
IMSBuildNuGetProjectSystem projectSystem,
CancellationToken cancellationToken)
{
if (streamTaskFactory == null)
{
throw new ArgumentNullException(nameof(streamTaskFactory));
}

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

cancellationToken.ThrowIfCancellationRequested();

await MSBuildNuGetProjectSystemUtility.TryAddFileAsync(
projectSystem,
targetPath,
async () => StreamUtility.StreamFromString(await ProcessAsync(streamTaskFactory, projectSystem, cancellationToken)),
cancellationToken);
}

/// <summary>
/// Asynchronously reverses the transform on the targetPath, using all the potential source of change.
/// </summary>
/// <param name="streamTaskFactory">A factory for accessing the file to be reverted from the nupkg being uninstalled.</param>
/// <param name="targetPath">A path to the file to be reverted.</param>
/// <param name="matchingFiles">Other files in other packages that may have changed the <paramref name="targetPath" />.</param>
/// <param name="projectSystem">The project where this change is taking place.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="streamTaskFactory" />
/// is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="projectSystem" />
/// is <c>null</c>.</exception>
/// <exception cref="OperationCanceledException">Thrown if <paramref name="cancellationToken" />
/// is cancelled.</exception>
public async Task RevertFileAsync(
Func<Task<Stream>> streamTaskFactory,
string targetPath,
IEnumerable<InternalZipFileInfo> matchingFiles,
IMSBuildNuGetProjectSystem projectSystem,
CancellationToken cancellationToken)
{
if (streamTaskFactory == null)
{
throw new ArgumentNullException(nameof(streamTaskFactory));
}

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

cancellationToken.ThrowIfCancellationRequested();

await MSBuildNuGetProjectSystemUtility.DeleteFileSafeAsync(
targetPath,
async () => StreamUtility.StreamFromString(await ProcessAsync(streamTaskFactory, projectSystem, cancellationToken)),
projectSystem,
cancellationToken);
}

internal static Task<string> ProcessAsync(
Func<Task<Stream>> streamTaskFactory,
IMSBuildNuGetProjectSystem projectSystem,
CancellationToken cancellationToken)
{
return Common.Preprocessor.ProcessAsync(
streamTaskFactory,
propName => projectSystem.GetPropertyValue(propName),
cancellationToken);
}
}
}
Loading

0 comments on commit a767c82

Please sign in to comment.