Skip to content

Commit

Permalink
Allow prerelease versions (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdovaz authored Dec 22, 2024
1 parent 1f4a1fb commit b93e8b5
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 11 deletions.
11 changes: 11 additions & 0 deletions registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,17 @@
"listed": true,
"version": "1.4.1"
},
"StyleCop.Analyzers": {
"listed": true,
"version": "1.0.0",
"analyzer": true,
"includePrerelease": true
},
"StyleCop.Analyzers.Unstable": {
"listed": true,
"version": "1.1.0.47",
"analyzer": true
},
"SunCalcNet": {
"listed": true,
"version": "1.1.0"
Expand Down
3 changes: 1 addition & 2 deletions src/UnityNuGet.Tests/PlatformDefinitionTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
Expand Down
14 changes: 13 additions & 1 deletion src/UnityNuGet.Tests/RegistryCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Options;
using Moq;
using NuGet.Versioning;
using NUnit.Framework;
using UnityNuGet.Npm;

namespace UnityNuGet.Tests
{
public class RegistryCacheTests
{
[Test]
[TestCase("1.0.0", "1.0.0")]
[TestCase("1.0.0.0", "1.0.0")]
[TestCase("1.0.0.1", "1.0.0-1")]
[TestCase("1.0.0-preview.1.24080.9", "1.0.0-preview.1.24080.9")]
[TestCase("1.0.0.1-preview.1.24080.9", "1.0.0-1.preview.1.24080.9")]
public void GetNpmVersion(string version, string expected)
{
Assert.That(RegistryCache.GetNpmVersion(NuGetVersion.Parse(version)), Is.EqualTo(expected));
}

[Test]
public async Task TestBuild()
{
Expand Down
2 changes: 1 addition & 1 deletion src/UnityNuGet.Tests/VersionRangeConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class VersionRangeConverterTests
[Test]
public void Read_Write_Success()
{
string json = @"{""ignore"":false,""listed"":false,""version"":""[1.2.3, )"",""defineConstraints"":[],""analyzer"":false}";
string json = @"{""ignore"":false,""listed"":false,""version"":""[1.2.3, )"",""defineConstraints"":[],""analyzer"":false,""includePrerelease"":false}";

RegistryEntry registryEntry = JsonSerializer.Deserialize(json, UnityNugetJsonSerializerContext.Default.RegistryEntry)!;

Expand Down
38 changes: 31 additions & 7 deletions src/UnityNuGet/RegistryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ public async Task Build()
}
}

private async Task<IEnumerable<IPackageSearchMetadata>?> GetMetadataFromSources(string packageName)
private async Task<IEnumerable<IPackageSearchMetadata>?> GetMetadataFromSources(string packageName, bool includePrerelease)
{
foreach (SourceRepository source in _sourceRepositories)
{
PackageMetadataResource packageMetadataResource = source.GetResource<PackageMetadataResource>();

IEnumerable<IPackageSearchMetadata> result = await packageMetadataResource.GetMetadataAsync(packageName, includePrerelease: false, includeUnlisted: false, _sourceCacheContext, _logger, CancellationToken.None);
IEnumerable<IPackageSearchMetadata> result = await packageMetadataResource.GetMetadataAsync(packageName, includePrerelease, includeUnlisted: false, _sourceCacheContext, _logger, CancellationToken.None);

if (result.Any())
{
Expand Down Expand Up @@ -251,7 +251,7 @@ private async Task BuildInternal()
}
}

IEnumerable<IPackageSearchMetadata>? packageMetaIt = await GetMetadataFromSources(packageName);
IEnumerable<IPackageSearchMetadata>? packageMetaIt = await GetMetadataFromSources(packageName, packageEntry.IncludePrerelease);
IPackageSearchMetadata[] packageMetas = packageMetaIt != null ? packageMetaIt.ToArray() : [];
foreach (IPackageSearchMetadata? packageMeta in packageMetas)
{
Expand Down Expand Up @@ -375,7 +375,7 @@ private async Task BuildInternal()
}
else if (!deps.VersionRange.IsSubSetOrEqualTo(packageEntryDep.Version))
{
IEnumerable<IPackageSearchMetadata>? dependencyPackageMetaIt = await GetMetadataFromSources(deps.Id);
IEnumerable<IPackageSearchMetadata>? dependencyPackageMetaIt = await GetMetadataFromSources(deps.Id, packageEntryDep.IncludePrerelease);
IPackageSearchMetadata[] dependencyPackageMetas = dependencyPackageMetaIt != null ? dependencyPackageMetaIt.ToArray() : [];

PackageDependency? packageDependency = null;
Expand Down Expand Up @@ -468,14 +468,38 @@ private async Task BuildInternal()
}
}

private static string GetNpmVersion(NuGetVersion currentVersion)
// Unity only supports the SemVer format: https://docs.unity3d.com/6000.1/Documentation/Manual/upm-lifecycle.html
internal static string GetNpmVersion(NuGetVersion currentVersion)
{
string npmCurrentVersion = $"{currentVersion.Major}.{currentVersion.Minor}.{currentVersion.Patch}";

if (currentVersion.Revision != 0)
if (currentVersion.IsPrerelease || currentVersion.Revision != 0)
{
npmCurrentVersion += $"-{currentVersion.Revision}";
StringBuilder stringBuilder = new();

if (currentVersion.Revision != 0)
{
stringBuilder.Append(currentVersion.Revision);
}

if (currentVersion.IsPrerelease)
{
if (stringBuilder.Length > 0)
{
stringBuilder.Append('.');
}

stringBuilder.Append(currentVersion.Release);
}

if (stringBuilder.Length > 0)
{
stringBuilder.Insert(0, '-');
}

npmCurrentVersion += stringBuilder.ToString();
}

return npmCurrentVersion;
}

Expand Down
3 changes: 3 additions & 0 deletions src/UnityNuGet/RegistryEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ public class RegistryEntry

[JsonPropertyName("analyzer")]
public bool Analyzer { get; set; }

[JsonPropertyName("includePrerelease")]
public bool IncludePrerelease { get; set; }
}
}

0 comments on commit b93e8b5

Please sign in to comment.