Skip to content

Commit

Permalink
Add support for overriding nuspec repository url and version before p…
Browse files Browse the repository at this point in the history
…ushing nupkg to GPR. Fixes #48

Update error message.

Remove dead code because there are no "xmlns" attributes to remove when instantiating a new XElement.

Rename file.

Refactor: Owner property is no longer required. Address feedback regarding --repository option that should be in the following format: owner/repository. This makes it trivial to change repository url in a github actions scenario: gpr push your.nupkg --repository ${{ github.repository }} -k ${{ secrets.GITHUB_TOKEN }}. I have added additional test cases that verifies that we only rewrite nuspec if repository url, repository type or version has been changed. /cc @jcansdale

Bugfix: Dispose nuspec context property.

Only attempt to parse version if repository option has been set.

Bugfix: Remove "_gpr.nupkg" prefix if nupkg is rewritten before uploading nupkg to GPR.
  • Loading branch information
peters committed Jun 13, 2020
1 parent 964dee2 commit 84e711d
Show file tree
Hide file tree
Showing 6 changed files with 458 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/GprTool/GprTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.4.1" />
<PackageReference Include="Octokit.GraphQL" Version="0.1.4-packages-preview2" />
<PackageReference Include="RestSharp" Version="106.10.1" />
<PackageReference Include="NuGet.Packaging" Version="5.6.0"/>
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions src/GprTool/IoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.IO;

namespace GprTool
{
public static class IoExtensions
{
public static FileStream OpenReadShared(this string filename)
{
return File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
}

public static MemoryStream ReadSharedToStream(this string filename)
{
using var fileStream = filename.OpenReadShared();
var outputStream = new MemoryStream();
fileStream.CopyTo(outputStream);
outputStream.Seek(0, SeekOrigin.Begin);
return outputStream;
}
}
}
117 changes: 117 additions & 0 deletions src/GprTool/NuGetUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,111 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using NuGet.Packaging;
using NuGet.Versioning;

namespace GprTool
{
public class NuGetUtilities
{
public static bool ShouldRewriteNupkg(string nupkgPath, string repositoryUrl, NuGetVersion nuGetVersion = null)
{
if (nupkgPath == null) throw new ArgumentNullException(nameof(nupkgPath));
if (repositoryUrl == null) throw new ArgumentNullException(nameof(repositoryUrl));

using var packageArchiveReader = new PackageArchiveReader(nupkgPath.OpenReadShared(), false);

var nuspecXDocument = packageArchiveReader.NuspecReader.Xml;
var packageXElement = nuspecXDocument.Single("package");
var metadataXElement = packageXElement.Single("metadata");
var versionXElement = metadataXElement.Single("version");

if (!NuGetVersion.TryParse(versionXElement.Value, out var nuspecVersion)
|| nuGetVersion != null && !nuGetVersion.Equals(nuspecVersion))
{
return true;
}

var repositoryXElement = metadataXElement.SingleOrDefault("repository");
if (repositoryXElement == null)
{
return true;
}

var nuspecRepositoryUrl = repositoryXElement.Attribute("url")?.Value;
var nuspecRepositoryType = repositoryXElement.Attribute("type")?.Value;

return !string.Equals(repositoryUrl, nuspecRepositoryUrl, StringComparison.Ordinal)
|| !string.Equals("git", nuspecRepositoryType, StringComparison.Ordinal);
}

public static string RewriteNupkg(string nupkgPath, string repositoryUrl, NuGetVersion nuGetVersion = null)
{
if (nupkgPath == null) throw new ArgumentNullException(nameof(nupkgPath));
if (repositoryUrl == null) throw new ArgumentNullException(nameof(repositoryUrl));

var randomDirectoryId = Guid.NewGuid().ToString("N");
var nupkgFilename = Path.GetFileName(nupkgPath);
var nupkgFilenameWithoutExt = Path.GetFileNameWithoutExtension(nupkgFilename);
var nupkgWorkingDirectoryAbsolutePath = Path.GetDirectoryName(nupkgPath);
var workingDirectory = Path.Combine(nupkgWorkingDirectoryAbsolutePath, $"{nupkgFilenameWithoutExt}_{randomDirectoryId}");

using var tmpDirectory = new DisposableDirectory(workingDirectory);
using var packageArchiveReader = new PackageArchiveReader(nupkgPath.ReadSharedToStream(), false);
using var nuspecMemoryStream = new MemoryStream();

var nuspecXDocument = packageArchiveReader.NuspecReader.Xml;
var packageXElement = nuspecXDocument.Single("package");
var metadataXElement = packageXElement.Single("metadata");
var packageId = packageXElement.Single("id").Value;
var versionXElement = metadataXElement.Single("version");

if (nuGetVersion != null)
{
versionXElement.SetValue(nuGetVersion);
}
else
{
nuGetVersion = NuGetVersion.Parse(versionXElement.Value);
}

var repositoryXElement = metadataXElement.SingleOrDefault("repository");
if (repositoryXElement == null)
{
repositoryXElement = new XElement("repository");
repositoryXElement.SetAttributeValue("url", repositoryUrl);
repositoryXElement.SetAttributeValue("type", "git");
metadataXElement.Add(repositoryXElement);
}
else
{
repositoryXElement.SetAttributeValue("url", repositoryUrl);
repositoryXElement.SetAttributeValue("type", "git");
}

nuspecXDocument.Save(nuspecMemoryStream);
nuspecMemoryStream.Seek(0, SeekOrigin.Begin);

ZipFile.ExtractToDirectory(nupkgPath, tmpDirectory.WorkingDirectory, true);

var nuspecDstFilename = Path.Combine(tmpDirectory.WorkingDirectory, $"{packageId}.nuspec");
File.WriteAllBytes(nuspecDstFilename, nuspecMemoryStream.ToArray());

using var outputStream = new MemoryStream();

var packageBuilder = new PackageBuilder(nuspecMemoryStream, tmpDirectory.WorkingDirectory, propertyProvider => throw new NotImplementedException());
packageBuilder.Save(outputStream);

var nupkgDstFilenameAbsolutePath = Path.Combine(nupkgWorkingDirectoryAbsolutePath, $"{packageId}.{nuGetVersion}_gpr.nupkg");

File.WriteAllBytes(nupkgDstFilenameAbsolutePath, outputStream.ToArray());

return nupkgDstFilenameAbsolutePath;
}

public static string FindTokenInNuGetConfig(Action<string> warning = null)
{
var configFile = GetDefaultConfigFile(warning);
Expand Down Expand Up @@ -97,5 +197,22 @@ public static string GetDefaultConfigFile(Action<string> warning = null)

return Path.Combine(baseDir, "NuGet", "NuGet.Config");
}

}

public class DisposableDirectory : IDisposable
{
public string WorkingDirectory { get; }

public DisposableDirectory(string workingDirectory)
{
WorkingDirectory = workingDirectory;
Directory.CreateDirectory(workingDirectory);
}

public void Dispose()
{
Directory.Delete(WorkingDirectory, true);
}
}
}
58 changes: 54 additions & 4 deletions src/GprTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using McMaster.Extensions.CommandLineUtils;
using NuGet.Packaging;
using NuGet.Versioning;
using RestSharp;
using RestSharp.Authenticators;
using Octokit.GraphQL;
Expand Down Expand Up @@ -320,12 +322,57 @@ public class PushCommand : GprCommandBase
{
protected override Task OnExecute(CommandLineApplication app)
{
var owner = "GPR-TOOL-DEFAULT-OWNER";
string rewrittenPackageFile = null;

if (Repository != null)
{
NuGetVersion nuGetVersion = null;
if (Version != null && !NuGetVersion.TryParse(Version, out nuGetVersion))
{
Console.WriteLine("Unable to parse version");
return Task.CompletedTask;
}

var ownerAndRepositoryName = Repository
.Replace("\\", "/")
.Split("/", StringSplitOptions.RemoveEmptyEntries)
.Take(2)
.ToList();

if (ownerAndRepositoryName.Count != 2)
{
Console.WriteLine(
"Invalid repository value. Please use the following format: owner/repository. E.g: jcansdale/gpr");
return Task.CompletedTask;
}

owner = ownerAndRepositoryName[0];
var repositoryName = ownerAndRepositoryName[1];
var repositoryUrl = $"https://github.com/{owner}/{repositoryName}";

if (NuGetUtilities.ShouldRewriteNupkg(PackageFile, repositoryUrl, nuGetVersion))
{
rewrittenPackageFile = NuGetUtilities.RewriteNupkg(PackageFile, repositoryUrl, nuGetVersion);
}
}

var user = "GprTool";
var token = GetAccessToken();
var client = new RestClient($"https://nuget.pkg.github.com/{Owner}/");
var client = new RestClient($"https://nuget.pkg.github.com/{owner}/");
client.Authenticator = new HttpBasicAuthenticator(user, token);
var request = new RestRequest(Method.PUT);
request.AddFile("package", PackageFile);
if (rewrittenPackageFile != null)
{
using var packageStream = rewrittenPackageFile.ReadSharedToStream();

rewrittenPackageFile = rewrittenPackageFile.Replace("_gpr.nupkg", ".nupkg", StringComparison.OrdinalIgnoreCase);
request.AddFile("package", packageStream.ToArray(), Path.GetFileName(rewrittenPackageFile));
}
else
{
request.AddFile("package", PackageFile);
}
var response = client.Execute(request);

if (response.StatusCode == HttpStatusCode.OK)
Expand Down Expand Up @@ -353,8 +400,11 @@ protected override Task OnExecute(CommandLineApplication app)
[Argument(0, Description = "Path to the package file")]
public string PackageFile { get; set; }

[Option("--owner", Description = "The owner if repository URL wasn't specified in nupkg/nuspec")]
public string Owner { get; } = "GPR-TOOL-DEFAULT-OWNER";
[Option("--repository", Description = "Override current nupkg repository url. Format: owner/repository. E.g: jcansdale/gpr")]
public string Repository { get; set; }

[Option("--version", Description = "Override current nupkg version")]
public string Version { get; set; }
}

[Command(Description = "View package details")]
Expand Down
67 changes: 67 additions & 0 deletions src/GprTool/XElementExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Xml.Linq;

namespace GprTool
{
public static class NugetExtensions
{
public static XElement Single([NotNull] this XDocument xDocument, [NotNull] XName name, bool ignoreCase = true, bool ignoreNamespace = true)
{
if (xDocument == null) throw new ArgumentNullException(nameof(xDocument));
if (name == null) throw new ArgumentNullException(nameof(name));
return xDocument.Descendants().SingleOrDefault(name, ignoreCase, ignoreNamespace, true);
}

public static XElement Single([NotNull] this XElement xElement, [NotNull] XName name, bool ignoreCase = true, bool ignoreNamespace = true)
{
if (xElement == null) throw new ArgumentNullException(nameof(xElement));
if (name == null) throw new ArgumentNullException(nameof(name));
return xElement.Descendants().SingleOrDefault(name, ignoreCase, ignoreNamespace, true);
}

[SuppressMessage("ReSharper", "UnusedMember.Global")]
public static XElement SingleOrDefault([NotNull] this XDocument xDocument, [NotNull] XName name, bool ignoreCase = true, bool ignoreNamespace = true)
{
if (xDocument == null) throw new ArgumentNullException(nameof(xDocument));
if (name == null) throw new ArgumentNullException(nameof(name));
return xDocument.Descendants().SingleOrDefault(name, ignoreCase, ignoreNamespace);
}

public static XElement SingleOrDefault([NotNull] this XElement xElement, [NotNull] XName name, bool ignoreCase = true, bool ignoreNamespace = true)
{
if (xElement == null) throw new ArgumentNullException(nameof(xElement));
if (name == null) throw new ArgumentNullException(nameof(name));
return xElement.Descendants().SingleOrDefault(name, ignoreCase, ignoreNamespace);
}

public static XElement SingleOrDefault([NotNull] this IEnumerable<XElement> xElements, [NotNull] XName name, bool ignoreCase = true, bool ignoreNamespace = true, bool throwifNotFound = false)
{
if (xElements == null) throw new ArgumentNullException(nameof(xElements));
if (name == null) throw new ArgumentNullException(nameof(name));
foreach (var node in xElements)
{
var comperator = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
if (!string.Equals(node.Name.LocalName, name.LocalName, comperator))
{
continue;
}

if (!ignoreNamespace && !string.Equals(node.Name.NamespaceName, name.NamespaceName, comperator))
{
continue;
}

return node;
}

if (throwifNotFound)
{
throw new Exception($"The required element '{name}' is missing");
}

return null;
}
}
}
Loading

0 comments on commit 84e711d

Please sign in to comment.