diff --git a/registry.json b/registry.json index d01e459..9dfd3fc 100644 --- a/registry.json +++ b/registry.json @@ -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" diff --git a/src/UnityNuGet.Tests/PlatformDefinitionTests.cs b/src/UnityNuGet.Tests/PlatformDefinitionTests.cs index 34723c0..c703486 100644 --- a/src/UnityNuGet.Tests/PlatformDefinitionTests.cs +++ b/src/UnityNuGet.Tests/PlatformDefinitionTests.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; using System.Linq; using NUnit.Framework; diff --git a/src/UnityNuGet.Tests/RegistryCacheTests.cs b/src/UnityNuGet.Tests/RegistryCacheTests.cs index c9337a3..be84552 100644 --- a/src/UnityNuGet.Tests/RegistryCacheTests.cs +++ b/src/UnityNuGet.Tests/RegistryCacheTests.cs @@ -3,10 +3,11 @@ 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; @@ -14,6 +15,17 @@ 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() { diff --git a/src/UnityNuGet.Tests/VersionRangeConverterTests.cs b/src/UnityNuGet.Tests/VersionRangeConverterTests.cs index 9ced619..4da862f 100644 --- a/src/UnityNuGet.Tests/VersionRangeConverterTests.cs +++ b/src/UnityNuGet.Tests/VersionRangeConverterTests.cs @@ -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)!; diff --git a/src/UnityNuGet/RegistryCache.cs b/src/UnityNuGet/RegistryCache.cs index 01bcdc1..d4af623 100644 --- a/src/UnityNuGet/RegistryCache.cs +++ b/src/UnityNuGet/RegistryCache.cs @@ -168,13 +168,13 @@ public async Task Build() } } - private async Task?> GetMetadataFromSources(string packageName) + private async Task?> GetMetadataFromSources(string packageName, bool includePrerelease) { foreach (SourceRepository source in _sourceRepositories) { PackageMetadataResource packageMetadataResource = source.GetResource(); - IEnumerable result = await packageMetadataResource.GetMetadataAsync(packageName, includePrerelease: false, includeUnlisted: false, _sourceCacheContext, _logger, CancellationToken.None); + IEnumerable result = await packageMetadataResource.GetMetadataAsync(packageName, includePrerelease, includeUnlisted: false, _sourceCacheContext, _logger, CancellationToken.None); if (result.Any()) { @@ -251,7 +251,7 @@ private async Task BuildInternal() } } - IEnumerable? packageMetaIt = await GetMetadataFromSources(packageName); + IEnumerable? packageMetaIt = await GetMetadataFromSources(packageName, packageEntry.IncludePrerelease); IPackageSearchMetadata[] packageMetas = packageMetaIt != null ? packageMetaIt.ToArray() : []; foreach (IPackageSearchMetadata? packageMeta in packageMetas) { @@ -375,7 +375,7 @@ private async Task BuildInternal() } else if (!deps.VersionRange.IsSubSetOrEqualTo(packageEntryDep.Version)) { - IEnumerable? dependencyPackageMetaIt = await GetMetadataFromSources(deps.Id); + IEnumerable? dependencyPackageMetaIt = await GetMetadataFromSources(deps.Id, packageEntryDep.IncludePrerelease); IPackageSearchMetadata[] dependencyPackageMetas = dependencyPackageMetaIt != null ? dependencyPackageMetaIt.ToArray() : []; PackageDependency? packageDependency = null; @@ -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; } diff --git a/src/UnityNuGet/RegistryEntry.cs b/src/UnityNuGet/RegistryEntry.cs index 94ffdcf..428d558 100644 --- a/src/UnityNuGet/RegistryEntry.cs +++ b/src/UnityNuGet/RegistryEntry.cs @@ -23,5 +23,8 @@ public class RegistryEntry [JsonPropertyName("analyzer")] public bool Analyzer { get; set; } + + [JsonPropertyName("includePrerelease")] + public bool IncludePrerelease { get; set; } } }