Skip to content

Commit

Permalink
Merge pull request #5 from seesharper/feature-nuspec
Browse files Browse the repository at this point in the history
Feature nuspec
  • Loading branch information
seesharper committed Apr 4, 2020
2 parents e913722 + b3b7ef5 commit f122eeb
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ The following file types are supported by `dotnet-deps`

* C# script files (*.csx)

* NuGet metadata files (*.nuspec)



> `dotnet-deps` only looks for `<PackageReference>` nodes and WILL NOT try to resolve MSBuild variables.
Expand Down
2 changes: 1 addition & 1 deletion src/Dotnet.Deps.Core/Dotnet.Deps.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<RepositoryUrl>https://github.com/seesharper/dotnet-deps.git</RepositoryUrl>
<Authors>Bernhard Richter</Authors>
<Description>A simple library that can be used to analyze and update NuGet dependencies.</Description>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NuGet.Configuration" Version="5.4.0" />
Expand Down
5 changes: 0 additions & 5 deletions src/Dotnet.Deps.Core/ProjectSystem/MsBuildProjectLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ public IProjectFile<NuGetPackageReference> Load(string path)
continue;
}




//packageReferences.Add(new PackageReference(packageName, packageVersion));

if (FloatRange.TryParse(packageVersion, out var floatRange))
{
var nugetVersion = floatRange.MinVersion;
Expand Down
20 changes: 20 additions & 0 deletions src/Dotnet.Deps.Core/ProjectSystem/NuspecPackageReference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Xml.Linq;
using NuGet.Versioning;

namespace Dotnet.Deps.Core.ProjectSystem
{
public class NuspecPackageReference : NuGetPackageReference
{
private readonly XElement packageElement;

public NuspecPackageReference(string name, string versionString, FloatRange floatRange, XElement packageElement) : base(name, versionString, floatRange)
{
this.packageElement = packageElement;
}

public override void Update(string newVersion)
{
packageElement.Attribute("version").Value = newVersion;
}
}
}
29 changes: 29 additions & 0 deletions src/Dotnet.Deps.Core/ProjectSystem/NuspecProjectFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Xml.Linq;

namespace Dotnet.Deps.Core.ProjectSystem
{
/// <summary>
/// Represents a MsBuild SDK-style project file.
/// </summary>
public class NuspecProjectFile : IProjectFile<NuspecPackageReference>
{
private readonly XDocument msBuildProjectFile;


public NuspecProjectFile(XDocument msBuildProjectFile, string path)
{
this.msBuildProjectFile = msBuildProjectFile;
Path = path;
}

public NuspecPackageReference[] PackageReferences { get; set; }

public string Path { get; }

public void Save()
{
msBuildProjectFile.Save(Path);
}
}

}
58 changes: 58 additions & 0 deletions src/Dotnet.Deps.Core/ProjectSystem/NuspecProjectLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Collections.Generic;
using System.Xml.Linq;
using NuGet.Versioning;

namespace Dotnet.Deps.Core.ProjectSystem
{
public class NuspecProjectLoader : IProjectLoader
{
private readonly AppConsole console;

public NuspecProjectLoader(AppConsole console)
{
this.console = console;
}

public string FileExtensions { get => "nuspec"; }

public IProjectFile<NuGetPackageReference> Load(string path)
{
var projectFile = XDocument.Load(path);
var nameSpace = projectFile.Root.Name.Namespace;
var nugetSpecProjectFile = new NuspecProjectFile(projectFile, path);
var packageReferenceElements = projectFile.Descendants(nameSpace + "dependency");
var packageReferences = new List<NuspecPackageReference>();
foreach (var packageReferenceElement in packageReferenceElements)
{
var packageName = packageReferenceElement.Attribute("id")?.Value;
if (string.IsNullOrWhiteSpace(packageName))
{
continue;
}

var packageVersion = packageReferenceElement.Attribute("version")?.Value;
if (packageVersion == null)
{
continue;
}

if (FloatRange.TryParse(packageVersion, out var floatRange))
{
var nugetVersion = floatRange.MinVersion;
var nugetPackageReference = new NuspecPackageReference(packageName, packageVersion, floatRange, packageReferenceElement);
packageReferences.Add(nugetPackageReference);
}
else
{
console.WriteError($"Warning: The package '{packageName}' has an invalid version number '{packageVersion}'");
}
}

nugetSpecProjectFile.PackageReferences = packageReferences.ToArray();


return nugetSpecProjectFile;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ProjectCollectionLoader(AppConsole console, IProjectLoader[] projectLoade
this.projectLoaders = projectLoaders;
}

public ProjectCollectionLoader(AppConsole console) : this(console, new IProjectLoader[] { new MsBuildProjectLoader(console), new ScriptProjectLoader(console) })
public ProjectCollectionLoader(AppConsole console) : this(console, new IProjectLoader[] { new MsBuildProjectLoader(console), new ScriptProjectLoader(console), new NuspecProjectLoader(console) })
{
}

Expand Down
14 changes: 12 additions & 2 deletions src/Dotnet.Deps.Tests/DocumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ namespace Dotnet.Deps.Tests
{
public static class DocumentExtensions
{
public static void ShouldHavePackageReference(this XDocument document, string name, string version)
public static void ShouldHaveMsBuildPackageReference(this XDocument document, string name, string version)
{
document.Descendants("PackageReference").Where(e => e.Attribute("Include").Value == name && e.Attribute("Version").Value == version).Should().HaveCount(1);
}

public static void ShouldHavePackageReferenceWithLatestVersion(this XDocument document, string name, string version)
public static void ShouldHaveMsBuildPackageReferenceWithLatestVersion(this XDocument document, string name, string version)
{
document.Descendants("PackageReference").Where(e => e.Attribute("Include").Value == name && e.Attribute("Version").Value != version).Should().HaveCount(1);
}

public static void ShouldHaveNuspecPackageReference(this XDocument document, string name, string version)
{
document.Descendants("dependency").Where(e => e.Attribute("id").Value == name && e.Attribute("version").Value == version).Should().HaveCount(1);
}

public static void ShouldHaveNuspecPackageReferenceWithLatestVersion(this XDocument document, string name, string version)
{
document.Descendants("dependency").Where(e => e.Attribute("id").Value == name && e.Attribute("version").Value != version).Should().HaveCount(1);
}
}
}
4 changes: 2 additions & 2 deletions src/Dotnet.Deps.Tests/MsBuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void ShouldListOutdatedDependency()
.AddPackage("LightInject", "5.1.0")
.Execute();
result.StandardOut.Should().Contain("LightInject 5.1.0 =>");
result.ProjectFile.ShouldHavePackageReference("LightInject", "5.1.0");
result.ProjectFile.ShouldHaveMsBuildPackageReference("LightInject", "5.1.0");
result.ExitCode.Should().Be(0);
}

Expand All @@ -22,7 +22,7 @@ public void ShouldUpdateToLatestVersion()
var result = new MsBuildTestCase()
.AddPackage("LightInject", "5.1.0")
.Execute("--update");
result.ProjectFile.ShouldHavePackageReferenceWithLatestVersion("LightInject", "5.1.0");
result.ProjectFile.ShouldHaveMsBuildPackageReferenceWithLatestVersion("LightInject", "5.1.0");
}

[Fact]
Expand Down
75 changes: 75 additions & 0 deletions src/Dotnet.Deps.Tests/NuspecTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using FluentAssertions;
using Xunit;

namespace Dotnet.Deps.Tests
{
public partial class NuspecTests
{
[Fact]
public void ShouldListOutdatedDependency()
{
var result = new NuspecTestCase()
.AddPackage("LightInject", "5.1.0")
.Execute();
result.StandardOut.Should().Contain("LightInject 5.1.0 =>");
result.ProjectFile.ShouldHaveNuspecPackageReference("LightInject", "5.1.0");
result.ExitCode.Should().Be(0);
}

[Fact]
public void ShouldUpdateToLatestVersion()
{
var result = new NuspecTestCase()
.AddPackage("LightInject", "5.1.0")
.Execute("--update");
result.ProjectFile.ShouldHaveNuspecPackageReferenceWithLatestVersion("LightInject", "5.1.0");
}

[Fact]
public void ShouldListFloatingDependency()
{
var result = new NuspecTestCase()
.AddPackage("LightInject", "6.*")
.Execute();
}


[Fact]
public void ShouldHandleInvalidVersionNumber()
{
var result = new NuspecTestCase()
.AddPackage("LightInject", "Rubbish")
.Execute();
result.StandardOut.Should().Contain("Warning");
}

[Fact]
public void ShouldIgnorePackageWithMissingVersionNumber()
{
var result = new NuspecTestCase()
.AddPackage("LightInject")
.Execute();
result.StandardOut.Should().NotContain("LightInject 5.1.0 =>");
}

[Fact]
public void ShouldExcludeFilteredPackages()
{
var result = new NuspecTestCase()
.AddPackage("LightInject", "5.1.0")
.WithFilter("Microsoft")
.Execute();
result.StandardOut.Should().NotContain("LightInject 5.1.0 =>");
}

[Fact]
public void ShouldIncludeFilteredPackages()
{
var result = new NuspecTestCase()
.AddPackage("LightInject", "5.1.0")
.WithFilter("LightInject")
.Execute();
result.StandardOut.Should().Contain("LightInject 5.1.0 =>");
}
}
}
79 changes: 79 additions & 0 deletions src/Dotnet.Deps.Tests/TestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,85 @@ public MsBuildTestResult Execute(params string[] args)
}
}


public class NuspecTestCase
{
private const string msBuildProjectFile = @"<?xml version=""1.0""?>
<package >
<metadata>
<dependencies>
</dependencies>
</metadata>
</package>";

protected List<(string name, string version)> packageReferences = new List<(string name, string version)>();

protected string filter;

public NuspecTestCase AddPackage(string name, string version = "")
{
packageReferences.Add((name, version));
return this;
}

public NuspecTestCase WithFilter(string filter)
{
this.filter = filter;
return this;
}

protected string CreateProjectFile()
{
XDocument projectFile = XDocument.Parse(msBuildProjectFile);
var itemGroupElement = projectFile.Descendants("dependencies").Single();
foreach (var packageReference in packageReferences)
{
if (string.IsNullOrWhiteSpace(packageReference.version))
{
var packageElement = new XElement("dependency", new XAttribute("id", packageReference.name));
itemGroupElement.Add(packageElement);
}
else
{
var packageElement = new XElement("dependency", new XAttribute("id", packageReference.name), new XAttribute("version", packageReference.version));
itemGroupElement.Add(packageElement);
}
}

return projectFile.ToString();
}

public MsBuildTestResult Execute(params string[] args)
{
var stdOut = new StringBuilder();
var stdErr = new StringBuilder();

var app = new App(new AppConsole(new StringWriter(stdOut), new StringWriter(stdErr)));

using (var projectFolder = new DisposableFolder())
{
List<string> allArgs = new List<string>();
allArgs.Add("-cwd");
allArgs.Add(projectFolder.Path);
allArgs.AddRange(args);

if (!string.IsNullOrEmpty(filter))
{
allArgs.Add("--filter");
allArgs.Add(filter);
}

var projectFileContent = CreateProjectFile();
var pathToProjectFile = Path.Combine(projectFolder.Path, "project.nuspec");
File.WriteAllText(pathToProjectFile, projectFileContent);
int exitCode = app.Execute(allArgs.ToArray());
return new MsBuildTestResult(XDocument.Load(pathToProjectFile), stdOut.ToString(), stdErr.ToString(), exitCode);
}
}
}



public class ScriptTestCase
{
protected List<(string name, string version)> packageReferences = new List<(string name, string version)>();
Expand Down
2 changes: 1 addition & 1 deletion src/Dotnet.Deps/Dotnet.Deps.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/seesharper/dotnet-deps.git</RepositoryUrl>
<Version>2.0.2</Version>
<Version>2.1.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit f122eeb

Please sign in to comment.