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
  • Loading branch information
peters committed Jun 12, 2020
1 parent 964dee2 commit cd37290
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 1 deletion.
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>
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 string RewriteNupkg(string nupkgAbsolutePath, string repositoryUrl, NuGetVersion nuGetVersion = null)
{
if (nupkgAbsolutePath == null) throw new ArgumentNullException(nameof(nupkgAbsolutePath));
if (repositoryUrl == null) throw new ArgumentNullException(nameof(repositoryUrl));

if (repositoryUrl.EndsWith(".git", StringComparison.OrdinalIgnoreCase))
{
repositoryUrl = repositoryUrl.Substring(0, repositoryUrl.Length - 4);
}

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

using var tmpDirectory = new DisposableDirectory(workingDirectory);
using var packageArchiveReader = new PackageArchiveReader(File.OpenRead(nupkgAbsolutePath));
using var nuspecMemoryStream = new MemoryStream();

var nuspecXDocument = packageArchiveReader.NuspecReader.Xml;
if (nuspecXDocument == null)
{
throw new Exception("Failed to parse nuspec");
}

var packageXElement = nuspecXDocument.SingleOrDefault(XName.Get("package"));
if (packageXElement == null)
{
throw new Exception("The required element 'package' is missing from the nuspec");
}

var metadataXElement = packageXElement.SingleOrDefault(XName.Get("metadata"));
if (metadataXElement == null)
{
throw new Exception("The required element 'metadata' is missing from the nuspec");
}

var packageId = packageXElement.SingleOrDefault(XName.Get("id"))?.Value;
if (packageId == null)
{
throw new Exception("The required element 'id' is missing from the nuspec");
}

var versionXElement = metadataXElement.SingleOrDefault(XName.Get("version"));
if (versionXElement == null)
{
throw new Exception("The required element 'version' is missing from the nuspec");
}

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

var repositoryXElement = metadataXElement.SingleOrDefault(XName.Get("repository"));
if (repositoryXElement == null)
{
repositoryXElement = new XElement(XName.Get("repository"));
repositoryXElement.SetAttributeValue(XName.Get("url"), repositoryUrl);
repositoryXElement.SetAttributeValue(XName.Get("type"), "git");
repositoryXElement.Attributes().Where( e => e.IsNamespaceDeclaration).Remove();

metadataXElement.Add(repositoryXElement);
}
else
{
repositoryXElement.SetAttributeValue(XName.Get("url"), repositoryUrl);
repositoryXElement.SetAttributeValue(XName.Get("type"), "git");
}

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

ZipFile.ExtractToDirectory(nupkgAbsolutePath, tmpDirectory.WorkingDirectory);

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);
}
}
}
46 changes: 46 additions & 0 deletions src/GprTool/NugetExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Xml.Linq;

namespace GprTool
{
public static class NugetExtensions
{
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)
{
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;
}
return null;
}
}
}
50 changes: 49 additions & 1 deletion 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,6 +322,46 @@ public class PushCommand : GprCommandBase
{
protected override Task OnExecute(CommandLineApplication app)
{
using var rewrittenNupkgStream = new MemoryStream();

if (RepositoryUrl != null)
{
var repositoryUrl = RepositoryUrl;
if (Uri.TryCreate(repositoryUrl, UriKind.Absolute, out var repositoryUri))
{
repositoryUrl = repositoryUri.PathAndQuery.Substring(1);
if (repositoryUrl.EndsWith(".git", StringComparison.OrdinalIgnoreCase))
{
repositoryUrl = repositoryUrl.Substring(0, repositoryUrl.Length - 4);
}
}

var ownerAndRepositoryName = repositoryUrl.Split("/", StringSplitOptions.RemoveEmptyEntries).ToList();
if (!ownerAndRepositoryName.Any())
{
ownerAndRepositoryName.AddRange(repositoryUrl.Split("\\", StringSplitOptions.RemoveEmptyEntries).ToList());
}

if (ownerAndRepositoryName.Count != 2)
{
Console.WriteLine("Owner must use the following format: owner/name. Example: jcansdale/gpr");
return Task.CompletedTask;
}

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

Owner = ownerAndRepositoryName[0];
var repositoryName = ownerAndRepositoryName[1];

repositoryUrl = $"https://github.com/{Owner}/{repositoryName}";
PackageFile = NuGetUtilities.RewriteNupkg(PackageFile, repositoryUrl, nuGetVersion);
}

var user = "GprTool";
var token = GetAccessToken();
var client = new RestClient($"https://nuget.pkg.github.com/{Owner}/");
Expand Down Expand Up @@ -354,7 +396,13 @@ protected override Task OnExecute(CommandLineApplication app)
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";
public string Owner { get; private set; } = "GPR-TOOL-DEFAULT-OWNER";

[Option("--repository-url", Description = "Override repository URL specified in nupkg/nuspec")]
public string RepositoryUrl { get; set; }

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

[Command(Description = "View package details")]
Expand Down
91 changes: 91 additions & 0 deletions test/GprTool.Tests/NuGetUtilitiesTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using System;
using System.IO;
using System.Text;
using System.Xml;
using NUnit.Framework;
using GprTool;
using NuGet.Packaging;
using NuGet.Versioning;

public class NuGetUtilitiesTests
{
Expand Down Expand Up @@ -111,5 +116,91 @@ public void UpdatePackageSourceCredentials()
var packageSourceCredentialsElements = xmlDoc.SelectNodes($"/configuration/packageSourceCredentials/*");
Assert.That(packageSourceCredentialsElements.Count, Is.EqualTo(1));
}

[TestCase("https://github.com/organization/repository.git", "https://github.com/organization/repository")]
[TestCase("https://github.com/organization/repository", "https://github.com/organization/repository")]
public void RewriteNuspec(string repositoryUrl, string expectedRepositoryUrl)
{
var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<package xmlns=""http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"">
<metadata>
<id>test</id>
<version>1.0.0</version>
<authors>abc123</authors>
<description>abc123</description>
<dependencies>
<group targetFramework="".NETStandard2.0"">
<dependency id=""test"" version=""0.0.0"" />
</group>
</dependencies>
</metadata>
</package>";

using var tmpDirectory = new DisposableDirectory(Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid().ToString("N")));
using var nuspecMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml));

var originalNupkgAbsolutePath = Path.Combine(tmpDirectory.WorkingDirectory, "test.nupkg");
using var originalPackageBuilderOutputStream = new MemoryStream();
var originalPackageBuilder = new PackageBuilder(nuspecMemoryStream, tmpDirectory.WorkingDirectory, s => throw new NotImplementedException());
originalPackageBuilder.Save(originalPackageBuilderOutputStream);
File.WriteAllBytes(originalNupkgAbsolutePath, originalPackageBuilderOutputStream.ToArray());

var rewrittenNupkgAbsolutePath = NuGetUtilities.RewriteNupkg(originalNupkgAbsolutePath,
repositoryUrl, NuGetVersion.Parse("2.0.0"));
using var rewrittenNupkgPackageReader = new PackageArchiveReader(File.OpenRead(rewrittenNupkgAbsolutePath));

var rewrittenNupkgPackageIdentity = rewrittenNupkgPackageReader.GetIdentity();
var rewrittenNupkgRepositoryMetadata = rewrittenNupkgPackageReader.NuspecReader.GetRepositoryMetadata();

Assert.That(rewrittenNupkgPackageIdentity, Is.Not.Null);
Assert.That(rewrittenNupkgPackageIdentity.Id, Is.EqualTo("test"));
Assert.That(rewrittenNupkgPackageIdentity.Version, Is.EqualTo(NuGetVersion.Parse("2.0.0")));
Assert.That(rewrittenNupkgRepositoryMetadata, Is.Not.Null);
Assert.That(rewrittenNupkgRepositoryMetadata.Url, Is.EqualTo(expectedRepositoryUrl));
Assert.That(rewrittenNupkgRepositoryMetadata.Type, Is.EqualTo("git"));
}

[Test]
public void RewriteNuspec_Overwrites_Existing_Repository_Url()
{
var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<package xmlns=""http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"">
<metadata>
<id>test</id>
<version>1.0.0</version>
<authors>abc123</authors>
<description>abc123</description>
<repository url=""https://google.com"" type=""google"" />
<dependencies>
<group targetFramework="".NETStandard2.0"">
<dependency id=""test"" version=""0.0.0"" />
</group>
</dependencies>
</metadata>
</package>";

using var tmpDirectory = new DisposableDirectory(Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid().ToString("N")));
using var nuspecMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml));

var originalNupkgAbsolutePath = Path.Combine(tmpDirectory.WorkingDirectory, "test.nupkg");
using var originalPackageBuilderOutputStream = new MemoryStream();
var originalPackageBuilder = new PackageBuilder(nuspecMemoryStream, tmpDirectory.WorkingDirectory, s => throw new NotImplementedException());
originalPackageBuilder.Save(originalPackageBuilderOutputStream);
File.WriteAllBytes(originalNupkgAbsolutePath, originalPackageBuilderOutputStream.ToArray());

var rewrittenNupkgAbsolutePath = NuGetUtilities.RewriteNupkg(originalNupkgAbsolutePath,
"https://github.com/organization/repository", NuGetVersion.Parse("2.0.0"));
using var rewrittenNupkgPackageReader = new PackageArchiveReader(File.OpenRead(rewrittenNupkgAbsolutePath));

var rewrittenNupkgPackageIdentity = rewrittenNupkgPackageReader.GetIdentity();
var rewrittenNupkgRepositoryMetadata = rewrittenNupkgPackageReader.NuspecReader.GetRepositoryMetadata();

Assert.That(rewrittenNupkgPackageIdentity, Is.Not.Null);
Assert.That(rewrittenNupkgPackageIdentity.Id, Is.EqualTo("test"));
Assert.That(rewrittenNupkgPackageIdentity.Version, Is.EqualTo(NuGetVersion.Parse("2.0.0")));
Assert.That(rewrittenNupkgRepositoryMetadata, Is.Not.Null);
Assert.That(rewrittenNupkgRepositoryMetadata.Url, Is.EqualTo("https://github.com/organization/repository"));
Assert.That(rewrittenNupkgRepositoryMetadata.Type, Is.EqualTo("git"));
}
}
}

0 comments on commit cd37290

Please sign in to comment.