From 7891f45f600aa73db5c77e0df799aad5e84ff3af Mon Sep 17 00:00:00 2001 From: bdovaz Date: Tue, 10 Dec 2024 20:57:59 +0100 Subject: [PATCH] Apply dotnet/runtime repository .editorconfig --- src/.editorconfig | 313 +++++++----- .../ApplicationBuilderExtension.cs | 8 +- .../Controllers/ApiController.cs | 18 +- .../EndpointRouteBuilderExtensions.cs | 4 +- src/UnityNuGet.Server/Program.cs | 4 +- .../RegistryCacheInitializer.cs | 34 +- src/UnityNuGet.Server/RegistryCacheReport.cs | 6 +- src/UnityNuGet.Server/RegistryCacheUpdater.cs | 4 +- src/UnityNuGet.Tests/NativeTests.cs | 13 +- .../PlatformDefinitionTests.cs | 34 +- src/UnityNuGet.Tests/RegistryCacheTests.cs | 13 +- src/UnityNuGet.Tests/RegistryTests.cs | 86 ++-- src/UnityNuGet.Tests/UnityMetaTests.cs | 42 +- src/UnityNuGet/DotNetHelper.cs | 8 +- src/UnityNuGet/NativeLibraries.cs | 144 +++--- .../Npm/NpmPackageListAllResponse.cs | 2 +- src/UnityNuGet/Npm/NpmPackageRegistry.cs | 4 +- src/UnityNuGet/NuGetHelper.cs | 16 +- src/UnityNuGet/PlatformDefinition.cs | 53 +- src/UnityNuGet/Registry.cs | 18 +- src/UnityNuGet/RegistryCache.cs | 204 ++++---- src/UnityNuGet/RuntimeLibraries.cs | 198 +++---- src/UnityNuGet/UnityMeta.cs | 482 +++++++++--------- 23 files changed, 880 insertions(+), 828 deletions(-) diff --git a/src/.editorconfig b/src/.editorconfig index 49de0d37..e006dbb4 100644 --- a/src/.editorconfig +++ b/src/.editorconfig @@ -1,123 +1,190 @@ -# To learn more about .editorconfig see https://aka.ms/editorconfigdocs -############################### -# Core EditorConfig Options # -############################### -# All files -[*] -indent_style = space -# Code files -[*.{cs,csx,vb,vbx}] -indent_size = 4 -insert_final_newline = true -charset = utf-8-bom -############################### -# .NET Coding Conventions # -############################### -[*.{cs,vb}] -# Organize usings -dotnet_sort_system_directives_first = true -# this. preferences -dotnet_style_qualification_for_field = false:silent -dotnet_style_qualification_for_property = false:silent -dotnet_style_qualification_for_method = false:silent -dotnet_style_qualification_for_event = false:silent -# Language keywords vs BCL types preferences -dotnet_style_predefined_type_for_locals_parameters_members = true:silent -dotnet_style_predefined_type_for_member_access = true:silent -# Parentheses preferences -dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent -dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent -dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent -dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent -# Modifier preferences -dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent -dotnet_style_readonly_field = true:suggestion -# Expression-level preferences -dotnet_style_object_initializer = true:suggestion -dotnet_style_collection_initializer = true:suggestion -dotnet_style_explicit_tuple_names = true:suggestion -dotnet_style_null_propagation = true:suggestion -dotnet_style_coalesce_expression = true:suggestion -dotnet_style_prefer_is_null_check_over_reference_equality_method = true:silent -dotnet_style_prefer_inferred_tuple_names = true:suggestion -dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion -dotnet_style_prefer_auto_properties = true:silent -dotnet_style_prefer_conditional_expression_over_assignment = true:silent -dotnet_style_prefer_conditional_expression_over_return = true:silent -############################### -# Naming Conventions # -############################### -# Style Definitions -dotnet_naming_style.pascal_case_style.capitalization = pascal_case -# Use PascalCase for constant fields -dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion -dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields -dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style -dotnet_naming_symbols.constant_fields.applicable_kinds = field -dotnet_naming_symbols.constant_fields.applicable_accessibilities = * -dotnet_naming_symbols.constant_fields.required_modifiers = const -############################### -# C# Coding Conventions # -############################### -[*.cs] -# var preferences -csharp_style_var_for_built_in_types = true:silent -csharp_style_var_when_type_is_apparent = true:silent -csharp_style_var_elsewhere = true:silent -# Expression-bodied members -csharp_style_expression_bodied_methods = false:silent -csharp_style_expression_bodied_constructors = false:silent -csharp_style_expression_bodied_operators = false:silent -csharp_style_expression_bodied_properties = true:silent -csharp_style_expression_bodied_indexers = true:silent -csharp_style_expression_bodied_accessors = true:silent -# Pattern matching preferences -csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion -csharp_style_pattern_matching_over_as_with_null_check = true:suggestion -# Null-checking preferences -csharp_style_throw_expression = true:suggestion -csharp_style_conditional_delegate_call = true:suggestion -# Modifier preferences -csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion -# Expression-level preferences -csharp_prefer_braces = true:silent -csharp_style_deconstructed_variable_declaration = true:suggestion -csharp_prefer_simple_default_expression = true:suggestion -csharp_style_pattern_local_over_anonymous_function = true:suggestion -csharp_style_inlined_variable_declaration = true:suggestion -############################### -# C# Formatting Rules # -############################### -# New line preferences -csharp_new_line_before_open_brace = all -csharp_new_line_before_else = true -csharp_new_line_before_catch = true -csharp_new_line_before_finally = true -csharp_new_line_before_members_in_object_initializers = true -csharp_new_line_before_members_in_anonymous_types = true -csharp_new_line_between_query_expression_clauses = true -# Indentation preferences -csharp_indent_case_contents = true -csharp_indent_switch_labels = true -csharp_indent_labels = flush_left -# Space preferences -csharp_space_after_cast = false -csharp_space_after_keywords_in_control_flow_statements = true -csharp_space_between_method_call_parameter_list_parentheses = false -csharp_space_between_method_declaration_parameter_list_parentheses = false -csharp_space_between_parentheses = false -csharp_space_before_colon_in_inheritance_clause = true -csharp_space_after_colon_in_inheritance_clause = true -csharp_space_around_binary_operators = before_and_after -csharp_space_between_method_declaration_empty_parameter_list_parentheses = false -csharp_space_between_method_call_name_and_opening_parenthesis = false -csharp_space_between_method_call_empty_parameter_list_parentheses = false -# Wrapping preferences -csharp_preserve_single_line_statements = true -csharp_preserve_single_line_blocks = true -############################### -# VB Coding Conventions # -############################### -[*.vb] -# Modifier preferences -visual_basic_preferred_modifier_order = Partial,Default,Private,Protected,Public,Friend,NotOverridable,Overridable,MustOverride,Overloads,Overrides,MustInherit,NotInheritable,Static,Shared,Shadows,ReadOnly,WriteOnly,Dim,Const,WithEvents,Widening,Narrowing,Custom,Async:suggestion +# editorconfig.org + +# top-most EditorConfig file +root = true + +# Default settings: +# A newline ending every file +# Use 4 spaces as indentation +[*] +insert_final_newline = true +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true + +# Generated code +[*{_AssemblyInfo.cs,.notsupported.cs,AsmOffsets.cs}] +generated_code = true + +# C# files +[*.cs] +# New line preferences +csharp_new_line_before_open_brace = all +csharp_new_line_before_else = true +csharp_new_line_before_catch = true +csharp_new_line_before_finally = true +csharp_new_line_before_members_in_object_initializers = true +csharp_new_line_before_members_in_anonymous_types = true +csharp_new_line_between_query_expression_clauses = true + +# Indentation preferences +csharp_indent_block_contents = true +csharp_indent_braces = false +csharp_indent_case_contents = true +csharp_indent_case_contents_when_block = false +csharp_indent_switch_labels = true +csharp_indent_labels = one_less_than_current + +# Modifier preferences +csharp_preferred_modifier_order = public,private,protected,internal,file,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,async:suggestion + +# avoid this. unless absolutely necessary +dotnet_style_qualification_for_field = false:suggestion +dotnet_style_qualification_for_property = false:suggestion +dotnet_style_qualification_for_method = false:suggestion +dotnet_style_qualification_for_event = false:suggestion + +# Types: use keywords instead of BCL types, and permit var only when the type is clear +csharp_style_var_for_built_in_types = false:suggestion +csharp_style_var_when_type_is_apparent = false:none +csharp_style_var_elsewhere = false:suggestion +dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion +dotnet_style_predefined_type_for_member_access = true:suggestion + +# name all constant fields using PascalCase +dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields +dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style +dotnet_naming_symbols.constant_fields.applicable_kinds = field +dotnet_naming_symbols.constant_fields.required_modifiers = const +dotnet_naming_style.pascal_case_style.capitalization = pascal_case + +# static fields should have s_ prefix +dotnet_naming_rule.static_fields_should_have_prefix.severity = suggestion +dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields +dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style +dotnet_naming_symbols.static_fields.applicable_kinds = field +dotnet_naming_symbols.static_fields.required_modifiers = static +dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected +dotnet_naming_style.static_prefix_style.required_prefix = s_ +dotnet_naming_style.static_prefix_style.capitalization = camel_case + +# internal and private fields should be _camelCase +dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion +dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields +dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style +dotnet_naming_symbols.private_internal_fields.applicable_kinds = field +dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal +dotnet_naming_style.camel_case_underscore_style.required_prefix = _ +dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case + +# Code style defaults +csharp_using_directive_placement = outside_namespace:suggestion +dotnet_sort_system_directives_first = true +csharp_prefer_braces = true:silent +csharp_preserve_single_line_blocks = true:none +csharp_preserve_single_line_statements = false:none +csharp_prefer_static_local_function = true:suggestion +csharp_prefer_simple_using_statement = false:none +csharp_style_prefer_switch_expression = true:suggestion +dotnet_style_readonly_field = true:suggestion + +# Expression-level preferences +dotnet_style_object_initializer = true:suggestion +dotnet_style_collection_initializer = true:suggestion +dotnet_style_prefer_collection_expression = when_types_exactly_match +dotnet_style_explicit_tuple_names = true:suggestion +dotnet_style_coalesce_expression = true:suggestion +dotnet_style_null_propagation = true:suggestion +dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion +dotnet_style_prefer_inferred_tuple_names = true:suggestion +dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion +dotnet_style_prefer_auto_properties = true:suggestion +dotnet_style_prefer_conditional_expression_over_assignment = true:silent +dotnet_style_prefer_conditional_expression_over_return = true:silent +csharp_prefer_simple_default_expression = true:suggestion + +# Expression-bodied members +csharp_style_expression_bodied_methods = true:silent +csharp_style_expression_bodied_constructors = true:silent +csharp_style_expression_bodied_operators = true:silent +csharp_style_expression_bodied_properties = true:silent +csharp_style_expression_bodied_indexers = true:silent +csharp_style_expression_bodied_accessors = true:silent +csharp_style_expression_bodied_lambdas = true:silent +csharp_style_expression_bodied_local_functions = true:silent + +# Pattern matching +csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion +csharp_style_pattern_matching_over_as_with_null_check = true:suggestion +csharp_style_inlined_variable_declaration = true:suggestion + +# Null checking preferences +csharp_style_throw_expression = true:suggestion +csharp_style_conditional_delegate_call = true:suggestion + +# Other features +csharp_style_prefer_index_operator = false:none +csharp_style_prefer_range_operator = false:none +csharp_style_pattern_local_over_anonymous_function = false:none + +# Space preferences +csharp_space_after_cast = false +csharp_space_after_colon_in_inheritance_clause = true +csharp_space_after_comma = true +csharp_space_after_dot = false +csharp_space_after_keywords_in_control_flow_statements = true +csharp_space_after_semicolon_in_for_statement = true +csharp_space_around_binary_operators = before_and_after +csharp_space_around_declaration_statements = do_not_ignore +csharp_space_before_colon_in_inheritance_clause = true +csharp_space_before_comma = false +csharp_space_before_dot = false +csharp_space_before_open_square_brackets = false +csharp_space_before_semicolon_in_for_statement = false +csharp_space_between_empty_square_brackets = false +csharp_space_between_method_call_empty_parameter_list_parentheses = false +csharp_space_between_method_call_name_and_opening_parenthesis = false +csharp_space_between_method_call_parameter_list_parentheses = false +csharp_space_between_method_declaration_empty_parameter_list_parentheses = false +csharp_space_between_method_declaration_name_and_open_parenthesis = false +csharp_space_between_method_declaration_parameter_list_parentheses = false +csharp_space_between_parentheses = false +csharp_space_between_square_brackets = false + +# License header +file_header_template = Licensed to the .NET Foundation under one or more agreements.\nThe .NET Foundation licenses this file to you under the MIT license. + +# C++ Files +[*.{cpp,h,in}] +curly_bracket_next_line = true +indent_brace_style = Allman + +# Xml project files +[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}] +indent_size = 2 + +[*.{csproj,vbproj,proj,nativeproj,locproj}] +charset = utf-8 + +# Xml build files +[*.builds] +indent_size = 2 + +# Xml files +[*.{xml,stylecop,resx,ruleset}] +indent_size = 2 + +# Xml config files +[*.{props,targets,config,nuspec}] +indent_size = 2 + +# YAML config files +[*.{yml,yaml}] +indent_size = 2 + +# Shell scripts +[*.sh] +end_of_line = lf +[*.{cmd,bat}] +end_of_line = crlf \ No newline at end of file diff --git a/src/UnityNuGet.Server/ApplicationBuilderExtension.cs b/src/UnityNuGet.Server/ApplicationBuilderExtension.cs index 7463c8fd..7c753eaa 100644 --- a/src/UnityNuGet.Server/ApplicationBuilderExtension.cs +++ b/src/UnityNuGet.Server/ApplicationBuilderExtension.cs @@ -1,7 +1,9 @@ using System; +using System.Collections.Generic; using System.Text; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Primitives; namespace UnityNuGet.Server { @@ -12,11 +14,11 @@ public static class ApplicationBuilderExtensions /// public static void LogRequestHeaders(this IApplicationBuilder app, ILoggerFactory loggerFactory) { - var logger = loggerFactory.CreateLogger("Request Headers"); + ILogger logger = loggerFactory.CreateLogger("Request Headers"); app.Use(async (context, next) => { var builder = new StringBuilder(Environment.NewLine); - foreach (var header in context.Request.Headers) + foreach (KeyValuePair header in context.Request.Headers) { builder.AppendLine($"{header.Key}:{header.Value}"); } @@ -25,7 +27,7 @@ public static void LogRequestHeaders(this IApplicationBuilder app, ILoggerFactor builder.Length = 0; builder.AppendLine(); - foreach (var header in context.Response.Headers) + foreach (KeyValuePair header in context.Response.Headers) { builder.AppendLine($"{header.Key}:{header.Value}"); } diff --git a/src/UnityNuGet.Server/Controllers/ApiController.cs b/src/UnityNuGet.Server/Controllers/ApiController.cs index 43d3cc15..fd81cacb 100644 --- a/src/UnityNuGet.Server/Controllers/ApiController.cs +++ b/src/UnityNuGet.Server/Controllers/ApiController.cs @@ -31,9 +31,9 @@ public IActionResult Home() [HttpGet("-/all")] public JsonResult GetAll() { - if (!TryGetInstance(out var instance, out var error)) return new JsonResult(error); + if (!TryGetInstance(out RegistryCache? instance, out NpmError? error)) return new JsonResult(error); - var result = instance?.All(); + NpmPackageListAllResponse? result = instance?.All(); return new JsonResult(result); } @@ -41,9 +41,9 @@ public JsonResult GetAll() [HttpGet("{id}")] public JsonResult GetPackage(string id) { - if (!TryGetInstance(out var instance, out var error)) return new JsonResult(error); + if (!TryGetInstance(out RegistryCache? instance, out NpmError? error)) return new JsonResult(error); - var package = instance?.GetPackage(id); + NpmPackage? package = instance?.GetPackage(id); if (package == null) { return new JsonResult(NpmError.NotFound); @@ -57,9 +57,9 @@ public JsonResult GetPackage(string id) [HttpHead("{id}/-/{file}")] public IActionResult DownloadPackage(string id, string file) { - if (!TryGetInstance(out var instance, out var error)) return new JsonResult(error); + if (!TryGetInstance(out RegistryCache? instance, out NpmError? error)) return new JsonResult(error); - var package = instance?.GetPackage(id); + NpmPackage? package = instance?.GetPackage(id); if (package == null) { return new JsonResult(NpmError.NotFound); @@ -70,7 +70,7 @@ public IActionResult DownloadPackage(string id, string file) return new JsonResult(NpmError.NotFound); } - var filePath = instance?.GetPackageFilePath(file); + string? filePath = instance?.GetPackageFilePath(file); if (string.IsNullOrEmpty(filePath) || !System.IO.File.Exists(filePath)) { return new JsonResult(NpmError.NotFound); @@ -91,7 +91,7 @@ public IActionResult DownloadPackage(string id, string file) private bool TryGetInstance(out RegistryCache? cacheInstance, out NpmError? npmError) { - var instance = _cacheSingleton.Instance; + RegistryCache? instance = _cacheSingleton.Instance; cacheInstance = instance; if (instance == null) @@ -101,7 +101,7 @@ private bool TryGetInstance(out RegistryCache? cacheInstance, out NpmError? npmE var stringBuilder = new StringBuilder(); stringBuilder.AppendLine("Error initializing the server:"); - foreach (var error in _registryCacheReport.ErrorMessages) + foreach (string error in _registryCacheReport.ErrorMessages) { stringBuilder.AppendLine(error); } diff --git a/src/UnityNuGet.Server/EndpointRouteBuilderExtensions.cs b/src/UnityNuGet.Server/EndpointRouteBuilderExtensions.cs index ee5986ec..ee7d3660 100644 --- a/src/UnityNuGet.Server/EndpointRouteBuilderExtensions.cs +++ b/src/UnityNuGet.Server/EndpointRouteBuilderExtensions.cs @@ -47,7 +47,7 @@ public static void MapStatus(this IEndpointRouteBuilder builder) "; builder.MapGet("/status", async context => { - var registryCacheReport = context.RequestServices.GetRequiredService(); + RegistryCacheReport registryCacheReport = context.RequestServices.GetRequiredService(); string message; @@ -74,7 +74,7 @@ public static void MapStatus(this IEndpointRouteBuilder builder) var scriptObject = new ScriptObject(); scriptObject.Import(model); - var templateContext = template.LexerOptions.Lang == ScriptLang.Liquid ? new LiquidTemplateContext() : new TemplateContext(); + TemplateContext templateContext = template.LexerOptions.Lang == ScriptLang.Liquid ? new LiquidTemplateContext() : new TemplateContext(); templateContext.LoopLimit = 0; templateContext.PushGlobal(scriptObject); diff --git a/src/UnityNuGet.Server/Program.cs b/src/UnityNuGet.Server/Program.cs index f4df42a4..50583e69 100644 --- a/src/UnityNuGet.Server/Program.cs +++ b/src/UnityNuGet.Server/Program.cs @@ -6,7 +6,7 @@ using UnityNuGet; using UnityNuGet.Server; -var builder = WebApplication.CreateBuilder(args); +WebApplicationBuilder builder = WebApplication.CreateBuilder(args); // Add the registry builder.Services.AddSingleton(); @@ -28,7 +28,7 @@ // Also enable NewtonsoftJson serialization builder.Services.AddControllers().AddNewtonsoftJson(); -var app = builder.Build(); +WebApplication app = builder.Build(); if (app.Environment.IsDevelopment()) { diff --git a/src/UnityNuGet.Server/RegistryCacheInitializer.cs b/src/UnityNuGet.Server/RegistryCacheInitializer.cs index 1b461a3c..4e3e3153 100644 --- a/src/UnityNuGet.Server/RegistryCacheInitializer.cs +++ b/src/UnityNuGet.Server/RegistryCacheInitializer.cs @@ -13,30 +13,30 @@ namespace UnityNuGet.Server { public class RegistryCacheInitializer(IConfiguration configuration, IHostEnvironment hostEnvironment, ILoggerFactory loggerFactory, IOptions registryOptionsAccessor, RegistryCacheSingleton registryCacheSingleton) : IHostedService { - private readonly IConfiguration configuration = configuration; - private readonly IHostEnvironment hostEnvironment = hostEnvironment; - private readonly ILoggerFactory loggerFactory = loggerFactory; - private readonly RegistryOptions registryOptions = registryOptionsAccessor.Value; - private readonly RegistryCacheSingleton registryCacheSingleton = registryCacheSingleton; + private readonly IConfiguration _configuration = configuration; + private readonly IHostEnvironment _hostEnvironment = hostEnvironment; + private readonly ILoggerFactory _loggerFactory = loggerFactory; + private readonly RegistryOptions _registryOptions = registryOptionsAccessor.Value; + private readonly RegistryCacheSingleton _registryCacheSingleton = registryCacheSingleton; public Task StartAsync(CancellationToken cancellationToken) { - var logger = loggerFactory.CreateLogger("NuGet"); + ILogger logger = _loggerFactory.CreateLogger("NuGet"); var loggerRedirect = new NuGetRedirectLogger(logger); - Uri uri = registryOptions.RootHttpUrl!; + Uri uri = _registryOptions.RootHttpUrl!; - bool isDevelopment = hostEnvironment.IsDevelopment(); + bool isDevelopment = _hostEnvironment.IsDevelopment(); if (isDevelopment) { - var urls = configuration[WebHostDefaults.ServerUrlsKey]; + string? urls = _configuration[WebHostDefaults.ServerUrlsKey]; // Select HTTPS in production, HTTP in development - var url = (urls?.Split(';').FirstOrDefault(x => !x.StartsWith("https"))) ?? throw new InvalidOperationException($"Unable to find a proper server URL from `{urls}`. Expecting a `http://...` URL in development"); + string url = (urls?.Split(';').FirstOrDefault(x => !x.StartsWith("https"))) ?? throw new InvalidOperationException($"Unable to find a proper server URL from `{urls}`. Expecting a `http://...` URL in development"); // https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables#dotnet_running_in_container-and-dotnet_running_in_containers - bool runningInContainer = configuration.GetValue("DOTNET_RUNNING_IN_CONTAINER"); + bool runningInContainer = _configuration.GetValue("DOTNET_RUNNING_IN_CONTAINER"); uri = new Uri(runningInContainer ? url.Replace("+", "localhost") : url); } @@ -44,9 +44,9 @@ public Task StartAsync(CancellationToken cancellationToken) // Get the current directory from registry options (prepend binary folder in dev) string unityPackageFolder; - if (Path.IsPathRooted(registryOptions.RegistryFilePath)) + if (Path.IsPathRooted(_registryOptions.RegistryFilePath)) { - unityPackageFolder = registryOptions.RootPersistentFolder!; + unityPackageFolder = _registryOptions.RootPersistentFolder!; } else { @@ -61,15 +61,15 @@ public Task StartAsync(CancellationToken cancellationToken) currentDirectory = Directory.GetCurrentDirectory(); } - unityPackageFolder = Path.Combine(currentDirectory, registryOptions.RootPersistentFolder!); + unityPackageFolder = Path.Combine(currentDirectory, _registryOptions.RootPersistentFolder!); } logger.LogInformation("Using Unity Package folder `{UnityPackageFolder}`", unityPackageFolder); // Add the cache accessible from the services - registryCacheSingleton.UnityPackageFolder = unityPackageFolder; - registryCacheSingleton.ServerUri = uri; - registryCacheSingleton.NuGetRedirectLogger = loggerRedirect; + _registryCacheSingleton.UnityPackageFolder = unityPackageFolder; + _registryCacheSingleton.ServerUri = uri; + _registryCacheSingleton.NuGetRedirectLogger = loggerRedirect; return Task.CompletedTask; } diff --git a/src/UnityNuGet.Server/RegistryCacheReport.cs b/src/UnityNuGet.Server/RegistryCacheReport.cs index f50b4ad3..ab9bf204 100644 --- a/src/UnityNuGet.Server/RegistryCacheReport.cs +++ b/src/UnityNuGet.Server/RegistryCacheReport.cs @@ -30,9 +30,9 @@ public double Progress { get { - var currentIndex = _registryCacheSingleton.ProgressPackageIndex; - var totalCount = _registryCacheSingleton.ProgressTotalPackageCount; - var percent = totalCount != 0 ? (double)currentIndex * 100 / totalCount : 0; + int currentIndex = _registryCacheSingleton.ProgressPackageIndex; + int totalCount = _registryCacheSingleton.ProgressTotalPackageCount; + double percent = totalCount != 0 ? (double)currentIndex * 100 / totalCount : 0; return percent; } diff --git a/src/UnityNuGet.Server/RegistryCacheUpdater.cs b/src/UnityNuGet.Server/RegistryCacheUpdater.cs index b85260cb..948fafb6 100644 --- a/src/UnityNuGet.Server/RegistryCacheUpdater.cs +++ b/src/UnityNuGet.Server/RegistryCacheUpdater.cs @@ -13,7 +13,7 @@ namespace UnityNuGet.Server /// internal sealed class RegistryCacheUpdater(Registry registry, RegistryCacheReport registryCacheReport, RegistryCacheSingleton currentRegistryCache, ILogger logger, IOptions registryOptionsAccessor) : BackgroundService { - private readonly Registry registry = registry; + private readonly Registry _registry = registry; private readonly RegistryCacheReport _registryCacheReport = registryCacheReport; private readonly RegistryCacheSingleton _currentRegistryCache = currentRegistryCache; private readonly ILogger _logger = logger; @@ -29,7 +29,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) _registryCacheReport.Start(); - var newRegistryCache = new RegistryCache(registry, _currentRegistryCache.UnityPackageFolder!, _currentRegistryCache.ServerUri!, _registryOptions.UnityScope!, _registryOptions.MinimumUnityVersion!, _registryOptions.PackageNameNuGetPostFix!, _registryOptions.TargetFrameworks!, _currentRegistryCache.NuGetRedirectLogger!) + var newRegistryCache = new RegistryCache(_registry, _currentRegistryCache.UnityPackageFolder!, _currentRegistryCache.ServerUri!, _registryOptions.UnityScope!, _registryOptions.MinimumUnityVersion!, _registryOptions.PackageNameNuGetPostFix!, _registryOptions.TargetFrameworks!, _currentRegistryCache.NuGetRedirectLogger!) { // Update progress OnProgress = (current, total) => diff --git a/src/UnityNuGet.Tests/NativeTests.cs b/src/UnityNuGet.Tests/NativeTests.cs index 7bed975c..ca9748e2 100644 --- a/src/UnityNuGet.Tests/NativeTests.cs +++ b/src/UnityNuGet.Tests/NativeTests.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Options; using Moq; using NUnit.Framework; +using UnityNuGet.Npm; namespace UnityNuGet.Tests { @@ -23,10 +24,10 @@ public async Task TestBuild() var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new FakeLoggerProvider()); - var unityPackages = Path.Combine(Path.GetDirectoryName(typeof(RegistryCacheTests).Assembly.Location)!, "unity_packages"); + string unityPackages = Path.Combine(Path.GetDirectoryName(typeof(RegistryCacheTests).Assembly.Location)!, "unity_packages"); Directory.Delete(unityPackages, true); - var errorsTriggered = false; + bool errorsTriggered = false; var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(new RegistryOptions { RegistryFilePath = "registry.json" })); @@ -55,14 +56,14 @@ public async Task TestBuild() await registryCache.Build(); Assert.That(errorsTriggered, Is.False, "The registry failed to build, check the logs"); - var allResult = registryCache.All(); - var allResultJson = allResult.ToJson(); + NpmPackageListAllResponse allResult = registryCache.All(); + string allResultJson = allResult.ToJson(); Assert.That(allResultJson, Does.Contain("org.nuget.rhino3dm")); - var rhinoPackage = registryCache.GetPackage("org.nuget.rhino3dm"); + NpmPackage? rhinoPackage = registryCache.GetPackage("org.nuget.rhino3dm"); Assert.That(rhinoPackage, Is.Not.Null); - var rhinopackageJson = rhinoPackage!.ToJson(); + string rhinopackageJson = rhinoPackage!.ToJson(); Assert.That(rhinopackageJson, Does.Contain("org.nuget.rhino3dm")); Assert.That(rhinopackageJson, Does.Contain("7.11.0")); } diff --git a/src/UnityNuGet.Tests/PlatformDefinitionTests.cs b/src/UnityNuGet.Tests/PlatformDefinitionTests.cs index 1c6516c5..001c5ca0 100644 --- a/src/UnityNuGet.Tests/PlatformDefinitionTests.cs +++ b/src/UnityNuGet.Tests/PlatformDefinitionTests.cs @@ -14,12 +14,12 @@ public void CanFindDefinitions() var platformDefs = PlatformDefinition.CreateAllPlatforms(); // Look-up by OS should return the most general configuration - var win = platformDefs.Find(UnityOs.Windows); + PlatformDefinition? win = platformDefs.Find(UnityOs.Windows); Assert.That(win, Is.Not.Null); Assert.That(win!.Cpu, Is.EqualTo(UnityCpu.AnyCpu)); // Look-up explicit configuration - var win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); + PlatformDefinition? win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); Assert.Multiple(() => { Assert.That(win64, Is.Not.Null); @@ -32,7 +32,7 @@ public void CanFindDefinitions() }); // Look-up invalid configuration - var and = platformDefs.Find(UnityOs.Android, UnityCpu.None); + PlatformDefinition? and = platformDefs.Find(UnityOs.Android, UnityCpu.None); Assert.That(and, Is.Null); } @@ -43,7 +43,7 @@ public void RemainingPlatforms_NoneVisited() var visited = new HashSet(); // If no platform was visited, the remaining platforms should be the (AnyOS, AnyCPU) config. - var remaining = platformDefs.GetRemainingPlatforms(visited); + HashSet remaining = platformDefs.GetRemainingPlatforms(visited); Assert.That(remaining, Is.Not.Null); Assert.Multiple(() => { @@ -57,14 +57,14 @@ public void RemainingPlatforms_OneVisited() { var platformDefs = PlatformDefinition.CreateAllPlatforms(); - foreach (var child in platformDefs.Children) + foreach (PlatformDefinition child in platformDefs.Children) { var visited = new HashSet() { child }; - var remaining = platformDefs.GetRemainingPlatforms(visited); + HashSet remaining = platformDefs.GetRemainingPlatforms(visited); // We should get all other children, except the one already visited Assert.That(remaining.Count + 1, Is.EqualTo(platformDefs.Children.Count)); - foreach (var r in remaining) + foreach (PlatformDefinition r in remaining) { Assert.Multiple(() => { @@ -79,7 +79,7 @@ public void RemainingPlatforms_OneVisited() public void RemainingPlatforms_LeafVisited() { var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); + PlatformDefinition? win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); var visited = new HashSet() { win64! }; // The remaining platforms should be all non-windows, as well as all !x64 windows @@ -89,7 +89,7 @@ public void RemainingPlatforms_LeafVisited() win64.Parent!.Children .Except([win64])) .ToHashSet(); - var actual = platformDefs.GetRemainingPlatforms(visited); + HashSet actual = platformDefs.GetRemainingPlatforms(visited); Assert.That(expected.SetEquals(actual), Is.True); } @@ -101,8 +101,8 @@ public void TestConfigPath_Root(string basePath) var file = new PlatformFile("a/b/c.dll", platformDefs); // We don't use extra paths for the (AnyOS, AnyCPU) configuration - var actual = file.GetDestinationPath(basePath); - var expected = Path.Combine( + string actual = file.GetDestinationPath(basePath); + string expected = Path.Combine( basePath, Path.GetFileName(file.SourcePath)); Assert.That(expected, Is.EqualTo(actual)); @@ -113,11 +113,11 @@ public void TestConfigPath_Root(string basePath) public void TestConfigPath_OsOnly(string basePath) { var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var win = platformDefs.Find(UnityOs.Windows); + PlatformDefinition? win = platformDefs.Find(UnityOs.Windows); var file = new PlatformFile("a/b/c.dll", win!); - var actual = file.GetDestinationPath(basePath); - var expected = Path.Combine( + string actual = file.GetDestinationPath(basePath); + string expected = Path.Combine( basePath, "Windows", Path.GetFileName(file.SourcePath)); @@ -129,11 +129,11 @@ public void TestConfigPath_OsOnly(string basePath) public void TestConfigPath_Full(string basePath) { var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); + PlatformDefinition? win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); var file = new PlatformFile("a/b/c.dll", win64!); - var actual = file.GetDestinationPath(basePath); - var expected = Path.Combine( + string actual = file.GetDestinationPath(basePath); + string expected = Path.Combine( basePath, "Windows", "x86_64", diff --git a/src/UnityNuGet.Tests/RegistryCacheTests.cs b/src/UnityNuGet.Tests/RegistryCacheTests.cs index 2a7a3ce2..0dc174e0 100644 --- a/src/UnityNuGet.Tests/RegistryCacheTests.cs +++ b/src/UnityNuGet.Tests/RegistryCacheTests.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Options; using Moq; using NUnit.Framework; +using UnityNuGet.Npm; namespace UnityNuGet.Tests { @@ -16,7 +17,7 @@ public class RegistryCacheTests [Test] public async Task TestBuild() { - var errorsTriggered = false; + bool errorsTriggered = false; var hostEnvironmentMock = new Mock(); hostEnvironmentMock.Setup(h => h.EnvironmentName).Returns(Environments.Development); @@ -24,7 +25,7 @@ public async Task TestBuild() var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new FakeLoggerProvider()); - var unityPackages = Path.Combine(Path.GetDirectoryName(typeof(RegistryCacheTests).Assembly.Location)!, "unity_packages"); + string unityPackages = Path.Combine(Path.GetDirectoryName(typeof(RegistryCacheTests).Assembly.Location)!, "unity_packages"); var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(new RegistryOptions { RegistryFilePath = "registry.json" })); await registry.StartAsync(CancellationToken.None); @@ -55,16 +56,16 @@ public async Task TestBuild() Assert.That(errorsTriggered, Is.False, "The registry failed to build, check the logs"); - var allResult = registryCache.All(); + NpmPackageListAllResponse allResult = registryCache.All(); Assert.That(allResult.Packages, Has.Count.GreaterThanOrEqualTo(3)); - var allResultJson = allResult.ToJson(); + string allResultJson = allResult.ToJson(); Assert.That(allResultJson, Does.Contain("org.nuget.scriban")); Assert.That(allResultJson, Does.Contain("org.nuget.system.runtime.compilerservices.unsafe")); - var scribanPackage = registryCache.GetPackage("org.nuget.scriban"); + NpmPackage? scribanPackage = registryCache.GetPackage("org.nuget.scriban"); Assert.That(scribanPackage, Is.Not.Null); - var scribanPackageJson = scribanPackage!.ToJson(); + string scribanPackageJson = scribanPackage!.ToJson(); Assert.That(scribanPackageJson, Does.Contain("org.nuget.scriban")); Assert.That(scribanPackageJson, Does.Contain("2.1.0")); } diff --git a/src/UnityNuGet.Tests/RegistryTests.cs b/src/UnityNuGet.Tests/RegistryTests.cs index 8e7b6ee7..e80b8ed8 100644 --- a/src/UnityNuGet.Tests/RegistryTests.cs +++ b/src/UnityNuGet.Tests/RegistryTests.cs @@ -12,9 +12,11 @@ using Moq; using NuGet.Configuration; using NuGet.PackageManagement; +using NuGet.Packaging; using NuGet.Packaging.Core; using NuGet.Protocol; using NuGet.Protocol.Core.Types; +using NuGet.Versioning; using NUnit.Framework; using static NuGet.Frameworks.FrameworkConstants; @@ -22,7 +24,7 @@ namespace UnityNuGet.Tests { public class RegistryTests { - private readonly RegistryOptions registryOptions = new() { RegistryFilePath = "registry.json" }; + private readonly RegistryOptions _registryOptions = new() { RegistryFilePath = "registry.json" }; [Test] [TestCase("scriban")] @@ -35,13 +37,13 @@ public async Task Make_Sure_That_The_Registry_Is_Case_Insensitive(string package var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new FakeLoggerProvider()); - var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(registryOptions)); + var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(_registryOptions)); await registry.StartAsync(CancellationToken.None); Assert.Multiple(() => { - Assert.That(registry.TryGetValue(packageName, out var result), Is.True); + Assert.That(registry.TryGetValue(packageName, out RegistryEntry? result), Is.True); Assert.That(result, Is.Not.Null); }); } @@ -55,12 +57,12 @@ public async Task Make_Sure_That_The_Order_In_The_Registry_Is_Respected() var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new FakeLoggerProvider()); - var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(registryOptions)); + var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(_registryOptions)); await registry.StartAsync(CancellationToken.None); - var originalPackageNames = registry.Select(r => r.Key).ToArray(); - var sortedPackageNames = originalPackageNames.OrderBy(p => p).ToArray(); + string[] originalPackageNames = registry.Select(r => r.Key).ToArray(); + string[] sortedPackageNames = [.. originalPackageNames.OrderBy(p => p)]; Assert.That(originalPackageNames, Is.EqualTo(sortedPackageNames)); } @@ -74,11 +76,11 @@ public async Task Ensure_That_Packages_Already_Included_In_Net_Standard_Are_not_ var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new FakeLoggerProvider()); - var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(registryOptions)); + var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(_registryOptions)); await registry.StartAsync(CancellationToken.None); - var packageNames = registry.Select(r => r.Key).Where(DotNetHelper.IsNetStandard20Assembly).ToArray(); + string[] packageNames = registry.Select(r => r.Key).Where(DotNetHelper.IsNetStandard20Assembly).ToArray(); Assert.That(packageNames, Is.Empty); } @@ -87,37 +89,37 @@ public async Task Ensure_That_Packages_Already_Included_In_Net_Standard_Are_not_ public async Task CanParse_PackageWithRuntimes() { var logger = new NuGetConsoleTestLogger(); - var cancellationToken = CancellationToken.None; + CancellationToken cancellationToken = CancellationToken.None; var cache = new SourceCacheContext(); - var settings = Settings.LoadDefaultSettings(root: null); - var repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json"); + ISettings settings = Settings.LoadDefaultSettings(root: null); + SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json"); // Fetch a package that has runtime overrides as described here: https://learn.microsoft.com/en-us/nuget/create-packages/supporting-multiple-target-frameworks - var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync( + DownloadResourceResult downloadResult = await PackageDownloader.GetDownloadResourceResultAsync( [repository], - new PackageIdentity("System.Security.Cryptography.ProtectedData", new NuGet.Versioning.NuGetVersion(6, 0, 0)), + new PackageIdentity("System.Security.Cryptography.ProtectedData", new NuGetVersion(6, 0, 0)), new PackageDownloadContext(cache), SettingsUtility.GetGlobalPackagesFolder(settings), logger, cancellationToken); // Make sure we have runtime libraries - var runtimeLibs = await RuntimeLibraries + List<(string file, UnityOs, UnityCpu?)> runtimeLibs = await RuntimeLibraries .GetSupportedRuntimeLibsAsync(downloadResult.PackageReader, CommonFrameworks.NetStandard20, logger) .ToListAsync(); Assert.That(runtimeLibs, Is.Not.Empty); // Make sure these runtime libraries are only for Windows var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var win = platformDefs.Find(UnityOs.Windows); - foreach (var (file, os, cpu) in runtimeLibs) + PlatformDefinition? win = platformDefs.Find(UnityOs.Windows); + foreach ((string file, UnityOs os, UnityCpu? cpu) in runtimeLibs) { Assert.That(platformDefs.Find(os, cpu), Is.EqualTo(win)); } // Get the lib files - var versions = await downloadResult.PackageReader.GetLibItemsAsync(cancellationToken); - var closestVersions = NuGetHelper.GetClosestFrameworkSpecificGroups( + IEnumerable versions = await downloadResult.PackageReader.GetLibItemsAsync(cancellationToken); + IEnumerable<(FrameworkSpecificGroup, RegistryTargetFramework)> closestVersions = NuGetHelper.GetClosestFrameworkSpecificGroups( versions, [ new() @@ -147,20 +149,20 @@ public async Task Ensure_Min_Version_Is_Correct_Ignoring_Analyzers_And_Native_Li var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new FakeLoggerProvider()); - var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(registryOptions)); + var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(_registryOptions)); var logger = new NuGetConsoleTestLogger(); - var cancellationToken = CancellationToken.None; + CancellationToken cancellationToken = CancellationToken.None; await registry.StartAsync(cancellationToken); var cache = new SourceCacheContext(); - var repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json"); - var resource = await repository.GetResourceAsync(); + SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json"); + PackageMetadataResource resource = await repository.GetResourceAsync(); var nuGetFrameworks = new RegistryTargetFramework[] { new() { Framework = CommonFrameworks.NetStandard20 } }; - var excludedPackages = new string[] { + string[] excludedPackages = [ // All versions target "Any" and not .netstandard2.0 / 2.1 // It has too many versions, the minimum version is lifted so as not to process so many versions @"AWSSDK.*", @@ -205,22 +207,22 @@ public async Task Ensure_Min_Version_Is_Correct_Ignoring_Analyzers_And_Native_Li @"GraphQL.Client.Serializer.Newtonsoft", // Version 3.1.8 has dependency on `Panic.StringUtils` which doesn't support .netstandard2.0 or 2.1. Rest of versions are fine. @"GraphQL.Client.Serializer.SystemTextJson" - }; + ]; var excludedPackagesRegex = new Regex(@$"^{string.Join('|', excludedPackages)}$"); - foreach (var registryKvp in registry.Where(r => !r.Value.Analyzer && !r.Value.Ignored)) + foreach (KeyValuePair registryKvp in registry.Where(r => !r.Value.Analyzer && !r.Value.Ignored)) { - var packageId = registryKvp.Key; + string packageId = registryKvp.Key; if (excludedPackagesRegex.IsMatch(packageId)) { continue; } - var versionRange = registryKvp.Value.Version; + VersionRange? versionRange = registryKvp.Value.Version; - var dependencyPackageMetas = await resource.GetMetadataAsync( + IEnumerable dependencyPackageMetas = await resource.GetMetadataAsync( packageId, includePrerelease: false, includeUnlisted: false, @@ -228,7 +230,7 @@ public async Task Ensure_Min_Version_Is_Correct_Ignoring_Analyzers_And_Native_Li logger, cancellationToken); - var packageIdentity = NuGetHelper.GetMinimumCompatiblePackageIdentity(dependencyPackageMetas, nuGetFrameworks, includeAny: false); + PackageIdentity? packageIdentity = NuGetHelper.GetMinimumCompatiblePackageIdentity(dependencyPackageMetas, nuGetFrameworks, includeAny: false); if (packageIdentity != null) { @@ -236,16 +238,16 @@ public async Task Ensure_Min_Version_Is_Correct_Ignoring_Analyzers_And_Native_Li } else { - var settings = Settings.LoadDefaultSettings(root: null); + ISettings settings = Settings.LoadDefaultSettings(root: null); - var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync( + DownloadResourceResult downloadResult = await PackageDownloader.GetDownloadResourceResultAsync( [repository], new PackageIdentity(registryKvp.Key, registryKvp.Value.Version!.MinVersion), new PackageDownloadContext(cache), SettingsUtility.GetGlobalPackagesFolder(settings), logger, cancellationToken); - var hasNativeLib = await NativeLibraries.GetSupportedNativeLibsAsync(downloadResult.PackageReader, logger).AnyAsync(); + bool hasNativeLib = await NativeLibraries.GetSupportedNativeLibsAsync(downloadResult.PackageReader, logger).AnyAsync(); if (hasNativeLib) { @@ -270,26 +272,26 @@ public async Task Ensure_Do_Not_Exceed_The_Maximum_Number_Of_Allowed_Versions() var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new FakeLoggerProvider()); - var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(registryOptions)); + var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(_registryOptions)); var logger = new NuGetConsoleTestLogger(); - var cancellationToken = CancellationToken.None; + CancellationToken cancellationToken = CancellationToken.None; await registry.StartAsync(cancellationToken); var cache = new SourceCacheContext(); - var repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json"); - var resource = await repository.GetResourceAsync(); + SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json"); + PackageMetadataResource resource = await repository.GetResourceAsync(); List<(string packageId, int versionCount)> packages = []; - foreach (var registryKvp in registry.Where(r => !r.Value.Analyzer && !r.Value.Ignored)) + foreach (KeyValuePair registryKvp in registry.Where(r => !r.Value.Analyzer && !r.Value.Ignored)) { - var packageId = registryKvp.Key; + string packageId = registryKvp.Key; - var versionRange = registryKvp.Value.Version; + VersionRange? versionRange = registryKvp.Value.Version; - var dependencyPackageMetas = await resource.GetMetadataAsync( + IEnumerable dependencyPackageMetas = await resource.GetMetadataAsync( packageId, includePrerelease: false, includeUnlisted: false, @@ -297,7 +299,7 @@ public async Task Ensure_Do_Not_Exceed_The_Maximum_Number_Of_Allowed_Versions() logger, cancellationToken); - var versions = dependencyPackageMetas.Where(v => versionRange!.Satisfies(v.Identity.Version)).ToArray(); + IPackageSearchMetadata[] versions = dependencyPackageMetas.Where(v => versionRange!.Satisfies(v.Identity.Version)).ToArray(); if (versions.Length > maxAllowedVersions) { @@ -307,7 +309,7 @@ public async Task Ensure_Do_Not_Exceed_The_Maximum_Number_Of_Allowed_Versions() StringBuilder stringBuilder = new(); - foreach (var (packageId, versionCount) in packages.OrderByDescending(p => p.versionCount)) + foreach ((string packageId, int versionCount) in packages.OrderByDescending(p => p.versionCount)) { stringBuilder.AppendLine($"{packageId} -> {versionCount}"); } diff --git a/src/UnityNuGet.Tests/UnityMetaTests.cs b/src/UnityNuGet.Tests/UnityMetaTests.cs index f36f5f59..0711c69d 100644 --- a/src/UnityNuGet.Tests/UnityMetaTests.cs +++ b/src/UnityNuGet.Tests/UnityMetaTests.cs @@ -5,15 +5,14 @@ namespace UnityNuGet.Tests { -#pragma warning disable CA1861 // Avoid constant arrays as arguments public class UnityMetaTests { [Test] public void GetMetaForDll_FormatsDefineConstraintsProperly_WithoutConstraints() { var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu); - var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs!, [], []); + PlatformDefinition? anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu); + string output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs!, [], []); Assert.That(output, Does.Not.Contain("defineConstraints")); // This is on the same line in the template, so ensure it's intact @@ -24,8 +23,8 @@ public void GetMetaForDll_FormatsDefineConstraintsProperly_WithoutConstraints() public void GetMetaForDll_FormatsLabelsProperly_WithoutLabels() { var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu); - var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs!, [], []); + PlatformDefinition? anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu); + string output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs!, [], []); Assert.That(output, Does.Not.Contain("labels")); // This is on the same line in the template, so ensure it's intact @@ -38,8 +37,8 @@ public void GetMetaForDll_FormatsDefineConstraintsProperly_WithConstraints( string[] constraints, string expected) { var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu); - var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs!, [], constraints); + PlatformDefinition? anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu); + string output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs!, [], constraints); Assert.That(output, Does.Contain(expected)); @@ -53,8 +52,8 @@ public void GetMetaForDll_FormatsLabelsProperly_WithLabels( string[] labels, string expected) { var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu); - var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs!, labels, []); + PlatformDefinition? anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu); + string output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs!, labels, []); Assert.That(output, Does.Contain(expected)); @@ -78,7 +77,7 @@ public void GetMetaForDll_FormatsAnyPlatformEnabledProperly(bool value, string e platformDef = new PlatformDefinition(UnityOs.AnyOs, UnityCpu.None, isEditorConfig: false); } - var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), platformDef!, [], []); + string output = UnityMeta.GetMetaForDll(Guid.NewGuid(), platformDef!, [], []); Assert.That(output, Does.Contain($"\n platformData:\n - first:\n Any:\n second:\n enabled: {expected}\n")); } @@ -87,8 +86,8 @@ public void GetMetaForDll_FormatsAnyPlatformEnabledProperly(bool value, string e public void GetMetaForDll_ContainsNoWindowsNewlines() { var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu); - var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs!, [], ["TEST"]); + PlatformDefinition? anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu); + string output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs!, [], ["TEST"]); Assert.That(output, Does.Not.Contain("\r")); } @@ -98,7 +97,7 @@ public void GetMetaForDll_ContainsNoWindowsNewlines() public void GetMetaForDll_NonEditor(UnityOs os, string platformName, string osName) { var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var output = UnityMeta.GetMetaForDll( + string output = UnityMeta.GetMetaForDll( Guid.NewGuid(), platformDefs.Find(os)!, [], @@ -106,7 +105,7 @@ public void GetMetaForDll_NonEditor(UnityOs os, string platformName, string osNa // There should be a single 'Exclude Android: 0' match var excludeRegex = new Regex("Exclude (.*): 0"); - var excludeMatches = excludeRegex.Matches(output); + MatchCollection excludeMatches = excludeRegex.Matches(output); Assert.That(excludeMatches, Is.Not.Null); Assert.That(excludeMatches, Has.Count.EqualTo(1)); Assert.Multiple(() => @@ -117,7 +116,7 @@ public void GetMetaForDll_NonEditor(UnityOs os, string platformName, string osNa // There should be a single 'enabled: 1' match var enableRegex = new Regex("enabled: 1"); - var enableMatches = enableRegex.Matches(output); + MatchCollection enableMatches = enableRegex.Matches(output); Assert.That(enableMatches, Is.Not.Null); Assert.Multiple(() => { @@ -133,8 +132,8 @@ public void GetMetaForDll_NonEditor(UnityOs os, string platformName, string osNa public void GetMetaForDll_Editor(UnityOs os, string[] osNames) { var platformDefs = PlatformDefinition.CreateAllPlatforms(); - var pDef = platformDefs.Find(os); - var output = UnityMeta.GetMetaForDll( + PlatformDefinition? pDef = platformDefs.Find(os); + string output = UnityMeta.GetMetaForDll( Guid.NewGuid(), pDef!, [], @@ -142,7 +141,7 @@ public void GetMetaForDll_Editor(UnityOs os, string[] osNames) // There should be only 'Exclude Editor: 0' and 'Exclude {{ osName }}: 0' matches var excludeRegex = new Regex("Exclude (.*): 0"); - var excludeMatches = excludeRegex.Matches(output); + MatchCollection excludeMatches = excludeRegex.Matches(output); Assert.That(excludeMatches, Is.Not.Null); var actualExcludes = excludeMatches .Select(match => match.Groups[1].Value) @@ -155,19 +154,18 @@ public void GetMetaForDll_Editor(UnityOs os, string[] osNames) // There should be as many 'enabled: 1' matches as exclude matches var enableRegex = new Regex("enabled: 1"); - var enableMatches = enableRegex.Matches(output); + MatchCollection enableMatches = enableRegex.Matches(output); Assert.Multiple(() => { Assert.That(enableMatches, Is.Not.Null); Assert.That(excludeMatches, Has.Count.EqualTo(enableMatches.Count)); }); - foreach (var osName in actualExcludes) + foreach (string? osName in actualExcludes) { - var platformName = (osName == "Editor") ? osName : "Standalone"; + string platformName = (osName == "Editor") ? osName : "Standalone"; Assert.That(output, Does.Contain($"- first:\n {platformName}: {osName}\n second:\n enabled: 1\n")); } } } -#pragma warning restore CA1861 // Avoid constant arrays as arguments } diff --git a/src/UnityNuGet/DotNetHelper.cs b/src/UnityNuGet/DotNetHelper.cs index baf4df53..083d8938 100644 --- a/src/UnityNuGet/DotNetHelper.cs +++ b/src/UnityNuGet/DotNetHelper.cs @@ -7,12 +7,12 @@ namespace UnityNuGet /// static class DotNetHelper { - public static bool IsNetStandard20Assembly(string packageId) => NetStandard20Assemblies.Contains(packageId); + public static bool IsNetStandard20Assembly(string packageId) => s_netStandard20Assemblies.Contains(packageId); - public static bool IsNetStandard21Assembly(string packageId) => NetStandard21Assemblies.Contains(packageId); + public static bool IsNetStandard21Assembly(string packageId) => s_netStandard21Assemblies.Contains(packageId); // Retrieved from NuGet package (/build/netstandard2.0/ref): https://www.nuget.org/packages/NETStandard.Library/2.0.3 - private static readonly HashSet NetStandard20Assemblies = [ + private static readonly HashSet s_netStandard20Assemblies = [ "Microsoft.Win32.Primitives", "System.AppContext", "System.Collections.Concurrent", @@ -127,7 +127,7 @@ static class DotNetHelper ]; // Retrieved from NuGet package (/ref/netstandard2.1): https://www.nuget.org/packages/NETStandard.Library.Ref - private static readonly HashSet NetStandard21Assemblies = [ + private static readonly HashSet s_netStandard21Assemblies = [ "Microsoft.Win32.Primitives", "System.AppContext", "System.Buffers", diff --git a/src/UnityNuGet/NativeLibraries.cs b/src/UnityNuGet/NativeLibraries.cs index b4725755..99a3fccf 100644 --- a/src/UnityNuGet/NativeLibraries.cs +++ b/src/UnityNuGet/NativeLibraries.cs @@ -1,72 +1,72 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading; -using NuGet.Common; -using NuGet.Packaging; - -namespace UnityNuGet -{ - static class NativeLibraries - { - public static async IAsyncEnumerable<(string file, string[] folders, UnityOs os, UnityCpu cpu)> GetSupportedNativeLibsAsync( - PackageReaderBase packageReader, - ILogger logger) - { - var versions = await packageReader.GetItemsAsync(PackagingConstants.Folders.Runtimes, CancellationToken.None); - var files = versions.SelectMany(v => v.Items); - - foreach (var file in files) - { - var folderPath = Path.GetDirectoryName(file)!; - var folders = folderPath.Split(Path.DirectorySeparatorChar); - - if (folders.Length != 3 || !folders[2].Equals("native", StringComparison.OrdinalIgnoreCase)) - { - logger.LogInformation($"Skipping non-native library file located in the runtimes folder: {file} ..."); - continue; - } - - var system = folders[1].Split('-'); - - if (system.Length != 2) - { - logger.LogInformation($"Skipping file located in the runtime folder that does not specify platform and architecture: {file} ..."); - continue; - } - - UnityOs? os = system[0][..3] switch - { - "lin" => UnityOs.Linux, - "osx" => UnityOs.OSX, - "win" => UnityOs.Windows, - "ios" => UnityOs.iOS, - _ => null - }; - - if (os is null) - { - logger.LogInformation($"Skipping file for unsupported OS: {file} ..."); - continue; - } - - UnityCpu? cpu = system[1] switch - { - "x86" => UnityCpu.X86, - "x64" => UnityCpu.X64, - "arm64" => UnityCpu.ARM64, - _ => null - }; - - if (cpu is null) - { - logger.LogInformation($"Skipping file for unsupported CPU: {file} ..."); - continue; - } - - yield return (file, folders, os.Value, cpu.Value); - } - } - } -} +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using NuGet.Common; +using NuGet.Packaging; + +namespace UnityNuGet +{ + static class NativeLibraries + { + public static async IAsyncEnumerable<(string file, string[] folders, UnityOs os, UnityCpu cpu)> GetSupportedNativeLibsAsync( + PackageReaderBase packageReader, + ILogger logger) + { + IEnumerable versions = await packageReader.GetItemsAsync(PackagingConstants.Folders.Runtimes, CancellationToken.None); + IEnumerable files = versions.SelectMany(v => v.Items); + + foreach (string? file in files) + { + string folderPath = Path.GetDirectoryName(file)!; + string[] folders = folderPath.Split(Path.DirectorySeparatorChar); + + if (folders.Length != 3 || !folders[2].Equals("native", StringComparison.OrdinalIgnoreCase)) + { + logger.LogInformation($"Skipping non-native library file located in the runtimes folder: {file} ..."); + continue; + } + + string[] system = folders[1].Split('-'); + + if (system.Length != 2) + { + logger.LogInformation($"Skipping file located in the runtime folder that does not specify platform and architecture: {file} ..."); + continue; + } + + UnityOs? os = system[0][..3] switch + { + "lin" => UnityOs.Linux, + "osx" => UnityOs.OSX, + "win" => UnityOs.Windows, + "ios" => UnityOs.iOS, + _ => null + }; + + if (os is null) + { + logger.LogInformation($"Skipping file for unsupported OS: {file} ..."); + continue; + } + + UnityCpu? cpu = system[1] switch + { + "x86" => UnityCpu.X86, + "x64" => UnityCpu.X64, + "arm64" => UnityCpu.ARM64, + _ => null + }; + + if (cpu is null) + { + logger.LogInformation($"Skipping file for unsupported CPU: {file} ..."); + continue; + } + + yield return (file, folders, os.Value, cpu.Value); + } + } + } +} diff --git a/src/UnityNuGet/Npm/NpmPackageListAllResponse.cs b/src/UnityNuGet/Npm/NpmPackageListAllResponse.cs index 34ac33f0..f45a1b76 100644 --- a/src/UnityNuGet/Npm/NpmPackageListAllResponse.cs +++ b/src/UnityNuGet/Npm/NpmPackageListAllResponse.cs @@ -31,7 +31,7 @@ private IDictionary AdditionalData get { var marshalPackages = new Dictionary(); - foreach (var packagePair in Packages) + foreach (KeyValuePair packagePair in Packages) { marshalPackages.Add(packagePair.Key, JObject.FromObject(packagePair.Value)); } diff --git a/src/UnityNuGet/Npm/NpmPackageRegistry.cs b/src/UnityNuGet/Npm/NpmPackageRegistry.cs index 4eb69f90..97aa5d43 100644 --- a/src/UnityNuGet/Npm/NpmPackageRegistry.cs +++ b/src/UnityNuGet/Npm/NpmPackageRegistry.cs @@ -22,9 +22,9 @@ public NpmPackageRegistry() public void AddPackage(NpmPackageCacheEntry entry, bool isListed) { - var package = entry.Package!; + NpmPackage package = entry.Package!; Packages.Add(package.Id!, package); - var packageInfos = isListed ? ListedPackageInfos : UnlistedPackageInfos; + NpmPackageListAllResponse packageInfos = isListed ? ListedPackageInfos : UnlistedPackageInfos; packageInfos.Packages.Add(package.Id!, entry.Info!); } diff --git a/src/UnityNuGet/NuGetHelper.cs b/src/UnityNuGet/NuGetHelper.cs index ff4f6111..01541bca 100644 --- a/src/UnityNuGet/NuGetHelper.cs +++ b/src/UnityNuGet/NuGetHelper.cs @@ -15,7 +15,7 @@ static partial class NuGetHelper private static partial Regex RoslynVersion(); // https://docs.unity3d.com/Manual/roslyn-analyzers.html - private static readonly Version unityRoslynSupportedVersion = new(3, 8, 0); + private static readonly Version s_unityRoslynSupportedVersion = new(3, 8, 0); // https://github.com/dotnet/sdk/blob/2838d93742658300698b2194882d57fd978fb168/src/Tasks/Microsoft.NET.Build.Tasks/NuGetUtils.NuGet.cs#L50 public static bool IsApplicableAnalyzer(string file) => IsApplicableAnalyzer(file, "C#"); @@ -64,12 +64,12 @@ bool IsResource() public static bool IsApplicableUnitySupportedRoslynVersionFolder(string file) { - var roslynVersionMatch = RoslynVersion().Match(file); + Match roslynVersionMatch = RoslynVersion().Match(file); bool hasRoslynVersionFolder = roslynVersionMatch.Success; bool hasUnitySupportedRoslynVersionFolder = hasRoslynVersionFolder && - int.Parse(roslynVersionMatch.Groups[1].Value) == unityRoslynSupportedVersion.Major && - int.Parse(roslynVersionMatch.Groups[2].Value) == unityRoslynSupportedVersion.Minor; + int.Parse(roslynVersionMatch.Groups[1].Value) == s_unityRoslynSupportedVersion.Major && + int.Parse(roslynVersionMatch.Groups[2].Value) == s_unityRoslynSupportedVersion.Minor; return !hasRoslynVersionFolder || hasUnitySupportedRoslynVersionFolder; } @@ -78,9 +78,9 @@ public static bool IsApplicableUnitySupportedRoslynVersionFolder(string file) { var result = new List<(FrameworkSpecificGroup, RegistryTargetFramework)>(); - foreach (var targetFramework in targetFrameworks) + foreach (RegistryTargetFramework targetFramework in targetFrameworks) { - var item = versions.Where(x => x.TargetFramework.Framework == targetFramework.Framework!.Framework && x.TargetFramework.Version <= targetFramework.Framework.Version).OrderByDescending(x => x.TargetFramework.Version) + FrameworkSpecificGroup? item = versions.Where(x => x.TargetFramework.Framework == targetFramework.Framework!.Framework && x.TargetFramework.Version <= targetFramework.Framework.Version).OrderByDescending(x => x.TargetFramework.Version) .FirstOrDefault(); if (item != null) @@ -99,9 +99,9 @@ public static IEnumerable GetCompatiblePackageDependency public static PackageIdentity? GetMinimumCompatiblePackageIdentity(IEnumerable packageSearchMetadataIt, IEnumerable targetFrameworks, bool includeAny = true) { - foreach (var packageSearchMetadata in packageSearchMetadataIt) + foreach (IPackageSearchMetadata packageSearchMetadata in packageSearchMetadataIt) { - var dependencyResolvedDependencyGroups = GetCompatiblePackageDependencyGroups(packageSearchMetadata.DependencySets, targetFrameworks, includeAny); + IEnumerable dependencyResolvedDependencyGroups = GetCompatiblePackageDependencyGroups(packageSearchMetadata.DependencySets, targetFrameworks, includeAny); if (dependencyResolvedDependencyGroups.Any()) { diff --git a/src/UnityNuGet/PlatformDefinition.cs b/src/UnityNuGet/PlatformDefinition.cs index 190431ec..caa83233 100644 --- a/src/UnityNuGet/PlatformDefinition.cs +++ b/src/UnityNuGet/PlatformDefinition.cs @@ -123,25 +123,13 @@ public static string GetName(this UnityCpu cpu) /// True if the Unity editor is available in this (os, cpu) tuple. internal class PlatformDefinition(UnityOs os, UnityCpu cpu, bool isEditorConfig) { - private readonly UnityOs _os = os; - private readonly UnityCpu _cpu = cpu; private readonly bool _isEditor = isEditorConfig; private readonly List _children = []; - private PlatformDefinition? _parent = null; - /// /// The parent that is a superset of this. /// - public PlatformDefinition? Parent - { - get => _parent; - - private set - { - _parent = value; - } - } + public PlatformDefinition? Parent { get; private set; } /// /// The child configurations this is a superset of. @@ -154,7 +142,7 @@ private set { _children.AddRange(value); - foreach (var child in _children) + foreach (PlatformDefinition child in _children) { child.Parent = this; } @@ -165,23 +153,21 @@ private set /// The distance from the root in the configuration tree. /// public int Depth - => (_parent == null) ? 0 : (1 + _parent.Depth); + => (Parent == null) ? 0 : (1 + Parent.Depth); /// /// The operating system. /// - public UnityOs Os - => _os; + public UnityOs Os { get; } = os; /// /// The CPU flavor. /// - public UnityCpu Cpu - => _cpu; + public UnityCpu Cpu { get; } = cpu; /// public override string ToString() - => $"{_os}.{_cpu}"; + => $"{Os}.{Cpu}"; /// /// Attempts to find a that matches the given @@ -193,7 +179,7 @@ public override string ToString() public PlatformDefinition? Find(UnityOs os, UnityCpu? cpu = default) { // Test self - if ((_os == os) && ((cpu == null) || (_cpu == cpu))) + if ((Os == os) && ((cpu == null) || (Cpu == cpu))) { return this; } @@ -246,7 +232,7 @@ public HashSet GetRemainingPlatforms(IReadOnlySet GetRemainingPlatforms(IReadOnlySet visitedPlatf return true; } - foreach (var c in _children) + foreach (PlatformDefinition c in _children) { if (c.HasVisitedDescendants(visitedPlatforms)) { @@ -345,20 +331,15 @@ private bool HasVisitedDescendants(IReadOnlySet visitedPlatf /// The platform the file is compatible with. internal class PlatformFile(string sourcePath, PlatformDefinition platform) { - private readonly PlatformDefinition _platform = platform; - private readonly string _sourcePath = sourcePath; - /// /// The full path of the file in the original NuGet package. /// - public string SourcePath - => _sourcePath; + public string SourcePath { get; } = sourcePath; /// /// The the file is compatible with. /// - public PlatformDefinition Platform - => _platform; + public PlatformDefinition Platform { get; } = platform; /// /// Returns the full path of the file in the UPM package. @@ -368,23 +349,23 @@ public PlatformDefinition Platform public string GetDestinationPath(string basePath) { // We start with just the base path - var fullPath = basePath; - var depth = _platform.Depth; + string fullPath = basePath; + int depth = Platform.Depth; if (depth > 0) { // Our configuration is not AnyOS, add a / to our full path - fullPath = Path.Combine(fullPath, _platform.Os.GetPathName()); + fullPath = Path.Combine(fullPath, Platform.Os.GetPathName()); } if (depth > 1) { // Our CPU os not AnyCPU, add a / to our full path - fullPath = Path.Combine(fullPath, _platform.Cpu.GetPathName()); + fullPath = Path.Combine(fullPath, Platform.Cpu.GetPathName()); } // Finally, append the file name and return - var fileName = Path.GetFileName(_sourcePath); + string fileName = Path.GetFileName(SourcePath); fullPath = Path.Combine(fullPath, fileName); return fullPath; } diff --git a/src/UnityNuGet/Registry.cs b/src/UnityNuGet/Registry.cs index aded6d54..17db7380 100644 --- a/src/UnityNuGet/Registry.cs +++ b/src/UnityNuGet/Registry.cs @@ -18,9 +18,9 @@ public sealed class Registry(IHostEnvironment hostEnvironment, ILoggerFactory lo { private IDictionary? _data; - private readonly IHostEnvironment hostEnvironment = hostEnvironment; - private readonly ILoggerFactory loggerFactory = loggerFactory; - private readonly RegistryOptions registryOptions = registryOptionsAccessor.Value; + private readonly IHostEnvironment _hostEnvironment = hostEnvironment; + private readonly ILoggerFactory _loggerFactory = loggerFactory; + private readonly RegistryOptions _registryOptions = registryOptionsAccessor.Value; public int Count => _data!.Count; @@ -34,13 +34,13 @@ public async Task StartAsync(CancellationToken cancellationToken) { string registryFilePath; - if (Path.IsPathRooted(registryOptions.RegistryFilePath)) + if (Path.IsPathRooted(_registryOptions.RegistryFilePath)) { - registryFilePath = registryOptions.RegistryFilePath; + registryFilePath = _registryOptions.RegistryFilePath; } else { - bool isDevelopment = hostEnvironment.IsDevelopment(); + bool isDevelopment = _hostEnvironment.IsDevelopment(); string currentDirectory; @@ -53,16 +53,16 @@ public async Task StartAsync(CancellationToken cancellationToken) currentDirectory = Directory.GetCurrentDirectory(); } - registryFilePath = Path.Combine(currentDirectory, registryOptions.RegistryFilePath!); + registryFilePath = Path.Combine(currentDirectory, _registryOptions.RegistryFilePath!); } - var logger = loggerFactory.CreateLogger("NuGet"); + ILogger logger = _loggerFactory.CreateLogger("NuGet"); logger.LogInformation("Using Unity registry file `{UnityRegistryFile}`", registryFilePath); string json = await File.ReadAllTextAsync(registryFilePath, cancellationToken); - var data = JsonConvert.DeserializeObject>(json, JsonCommonExtensions.Settings)!; + IDictionary data = JsonConvert.DeserializeObject>(json, JsonCommonExtensions.Settings)!; _data = new Dictionary(data, StringComparer.OrdinalIgnoreCase); } diff --git a/src/UnityNuGet/RegistryCache.cs b/src/UnityNuGet/RegistryCache.cs index 63899408..b3e657f8 100644 --- a/src/UnityNuGet/RegistryCache.cs +++ b/src/UnityNuGet/RegistryCache.cs @@ -35,7 +35,7 @@ public class RegistryCache // Change this version number if the content of the packages are changed by an update of this class private const string CurrentRegistryVersion = "1.8.0"; - private static readonly Encoding Utf8EncodingNoBom = new UTF8Encoding(false, false); + private static readonly Encoding s_utf8EncodingNoBom = new UTF8Encoding(false, false); private readonly Registry _registry; private readonly string _rootPersistentFolder; private readonly Uri _rootHttpUri; @@ -72,7 +72,7 @@ public RegistryCache(Registry registry, string rootPersistentFolder, Uri rootHtt // Force NuGet packages to be in the same directory to avoid storage full on Azure. if (IsRunningOnAzure) { - var nugetFolder = Path.Combine(_rootPersistentFolder, ".nuget"); + string nugetFolder = Path.Combine(_rootPersistentFolder, ".nuget"); Environment.SetEnvironmentVariable("NUGET_PACKAGES", nugetFolder); } @@ -82,7 +82,7 @@ public RegistryCache(Registry registry, string rootPersistentFolder, Uri rootHtt _logger = logger; // Initialize target framework - foreach (var registryTargetFramework in _targetFrameworks) + foreach (RegistryTargetFramework registryTargetFramework in _targetFrameworks) { registryTargetFramework.Framework = NuGetFramework.Parse(registryTargetFramework.Name!); } @@ -136,7 +136,7 @@ public NpmPackageListAllResponse All() public NpmPackage? GetPackage(string packageId) { ArgumentNullException.ThrowIfNull(packageId); - _npmPackageRegistry.Packages.TryGetValue(packageId, out var package); + _npmPackageRegistry.Packages.TryGetValue(packageId, out NpmPackage? package); return package; } @@ -149,7 +149,7 @@ public string GetPackageFilePath(string packageFileName) { ArgumentNullException.ThrowIfNull(packageFileName); packageFileName = packageFileName.Replace("/", packageFileName.Replace(".", string.Empty)); - var packageFilePath = Path.Combine(_rootPersistentFolder, packageFileName); + string packageFilePath = Path.Combine(_rootPersistentFolder, packageFileName); return packageFilePath; } @@ -170,11 +170,11 @@ public async Task Build() private async Task?> GetMetadataFromSources(string packageName) { - foreach (var source in _sourceRepositories) + foreach (SourceRepository source in _sourceRepositories) { - var packageMetadataResource = source.GetResource(); + PackageMetadataResource packageMetadataResource = source.GetResource(); - var result = await packageMetadataResource.GetMetadataAsync(packageName, includePrerelease: false, includeUnlisted: false, _sourceCacheContext, _logger, CancellationToken.None); + IEnumerable result = await packageMetadataResource.GetMetadataAsync(packageName, includePrerelease: false, includeUnlisted: false, _sourceCacheContext, _logger, CancellationToken.None); if (result.Any()) { @@ -200,8 +200,8 @@ private async Task GetPackageDownloadResourceResult(Pack /// private async Task BuildInternal() { - var versionPath = Path.Combine(_rootPersistentFolder, "version.txt"); - var forceUpdate = !File.Exists(versionPath) || await File.ReadAllTextAsync(versionPath) != CurrentRegistryVersion; + string versionPath = Path.Combine(_rootPersistentFolder, "version.txt"); + bool forceUpdate = !File.Exists(versionPath) || await File.ReadAllTextAsync(versionPath) != CurrentRegistryVersion; if (forceUpdate) { LogInformation($"Registry version changed to {CurrentRegistryVersion} - Regenerating all packages"); @@ -210,21 +210,21 @@ private async Task BuildInternal() _npmPackageRegistry.Reset(); } - var regexFilter = Filter != null ? new Regex(Filter, RegexOptions.IgnoreCase) : null; + Regex? regexFilter = Filter != null ? new Regex(Filter, RegexOptions.IgnoreCase) : null; if (Filter != null) { LogInformation($"Filtering with regex: {Filter}"); } - var onProgress = OnProgress; + Action? onProgress = OnProgress; - var globalPackagesFolder = SettingsUtility.GetGlobalPackagesFolder(_settings); + string globalPackagesFolder = SettingsUtility.GetGlobalPackagesFolder(_settings); - var progressCount = 0; - foreach (var packageDesc in _registry) + int progressCount = 0; + foreach (KeyValuePair packageDesc in _registry) { - var packageName = packageDesc.Key; - var packageEntry = packageDesc.Value; + string packageName = packageDesc.Key; + RegistryEntry packageEntry = packageDesc.Value; // Log progress count onProgress?.Invoke(++progressCount, _registry.Count); @@ -235,8 +235,8 @@ private async Task BuildInternal() continue; } - var packageId = packageName.ToLowerInvariant(); - var npmPackageId = $"{_unityScope}.{packageId}"; + string packageId = packageName.ToLowerInvariant(); + string npmPackageId = $"{_unityScope}.{packageId}"; NpmPackageCacheEntry? cacheEntry = null; NpmPackage? npmPackage = null; NpmPackageInfo? npmPackageInfo = null; @@ -251,13 +251,13 @@ private async Task BuildInternal() } } - var packageMetaIt = await GetMetadataFromSources(packageName); - var packageMetas = packageMetaIt != null ? packageMetaIt.ToArray() : []; - foreach (var packageMeta in packageMetas) + IEnumerable? packageMetaIt = await GetMetadataFromSources(packageName); + IPackageSearchMetadata[] packageMetas = packageMetaIt != null ? packageMetaIt.ToArray() : []; + foreach (IPackageSearchMetadata? packageMeta in packageMetas) { - var packageIdentity = packageMeta.Identity; + PackageIdentity packageIdentity = packageMeta.Identity; // Update latest version - var currentVersion = packageIdentity.Version; + NuGetVersion currentVersion = packageIdentity.Version; string npmCurrentVersion = GetNpmVersion(currentVersion); if (packageEntry.Version == null || !packageEntry.Version.Satisfies(packageMeta.Identity.Version)) @@ -266,7 +266,7 @@ private async Task BuildInternal() } // If the package id is cached already, we don't need to generate it again - if (npmPackage != null && npmPackage.Versions.TryGetValue(npmCurrentVersion, out var existingVersion)) + if (npmPackage != null && npmPackage.Versions.TryGetValue(npmCurrentVersion, out NpmPackageVersion? existingVersion)) { // If the package tgz exists, we don't need to regenerate it again var packageTgz = new FileInfo(GetUnityPackagePath(packageIdentity, existingVersion)); @@ -282,9 +282,9 @@ private async Task BuildInternal() if (!packageEntry.Analyzer && resolvedDependencyGroups.Count == 0) { - using var downloadResult = await GetPackageDownloadResourceResult(packageIdentity); + using DownloadResourceResult downloadResult = await GetPackageDownloadResourceResult(packageIdentity); - var hasNativeLib = await NativeLibraries.GetSupportedNativeLibsAsync(downloadResult.PackageReader, _logger).AnyAsync(); + bool hasNativeLib = await NativeLibraries.GetSupportedNativeLibsAsync(downloadResult.PackageReader, _logger).AnyAsync(); if (!hasNativeLib) { @@ -296,7 +296,7 @@ private async Task BuildInternal() npmPackage ??= new NpmPackage(); npmPackageInfo ??= new NpmPackageInfo(); - var update = !npmPackage.DistTags.TryGetValue("latest", out var latestVersion) + bool update = !npmPackage.DistTags.TryGetValue("latest", out string? latestVersion) || (currentVersion > NuGetVersion.Parse(latestVersion)) || forceUpdate; @@ -351,9 +351,9 @@ private async Task BuildInternal() npmPackage.Versions[npmVersion.Version] = npmVersion; bool hasDependencyErrors = false; - foreach (var resolvedDependencyGroup in resolvedDependencyGroups) + foreach (PackageDependencyGroup? resolvedDependencyGroup in resolvedDependencyGroups) { - foreach (var deps in resolvedDependencyGroup.Packages) + foreach (PackageDependency? deps in resolvedDependencyGroup.Packages) { if (DotNetHelper.IsNetStandard20Assembly(deps.Id)) { @@ -362,7 +362,7 @@ private async Task BuildInternal() PackageDependency resolvedDeps = deps; - if (!_registry.TryGetValue(deps.Id, out var packageEntryDep)) + if (!_registry.TryGetValue(deps.Id, out RegistryEntry? packageEntryDep)) { LogError($"The package `{packageIdentity}` has a dependency on `{deps.Id}` which is not in the registry. You must add this dependency to the registry.json file."); hasDependencyErrors = true; @@ -374,20 +374,20 @@ private async Task BuildInternal() } else if (!deps.VersionRange.IsSubSetOrEqualTo(packageEntryDep.Version)) { - var dependencyPackageMetaIt = await GetMetadataFromSources(deps.Id); - var dependencyPackageMetas = dependencyPackageMetaIt != null ? dependencyPackageMetaIt.ToArray() : []; + IEnumerable? dependencyPackageMetaIt = await GetMetadataFromSources(deps.Id); + IPackageSearchMetadata[] dependencyPackageMetas = dependencyPackageMetaIt != null ? dependencyPackageMetaIt.ToArray() : []; PackageDependency? packageDependency = null; - foreach (var dependencyPackageMeta in dependencyPackageMetas) + foreach (IPackageSearchMetadata? dependencyPackageMeta in dependencyPackageMetas) { - var dependencyResolvedDependencyGroups = NuGetHelper.GetCompatiblePackageDependencyGroups(dependencyPackageMeta.DependencySets, _targetFrameworks, includeAny: false); + IEnumerable dependencyResolvedDependencyGroups = NuGetHelper.GetCompatiblePackageDependencyGroups(dependencyPackageMeta.DependencySets, _targetFrameworks, includeAny: false); if (dependencyResolvedDependencyGroups.Any()) { - _registry.TryGetValue(dependencyPackageMeta.Identity.Id, out var registryEntry); + _registry.TryGetValue(dependencyPackageMeta.Identity.Id, out RegistryEntry? registryEntry); - var registryMinimumVersion = registryEntry?.Version?.MinVersion!; + NuGetVersion registryMinimumVersion = registryEntry?.Version?.MinVersion!; NuGetVersion newVersion = registryMinimumVersion > dependencyPackageMeta.Identity.Version ? registryMinimumVersion : dependencyPackageMeta.Identity.Version; @@ -420,8 +420,8 @@ private async Task BuildInternal() } // Otherwise add the package as a dependency - var depsId = resolvedDeps.Id.ToLowerInvariant(); - var key = $"{_unityScope}.{depsId}"; + string depsId = resolvedDeps.Id.ToLowerInvariant(); + string key = $"{_unityScope}.{depsId}"; if (!npmVersion.Dependencies.ContainsKey(key)) { npmVersion.Dependencies.Add(key, GetNpmVersion(resolvedDeps.VersionRange.MinVersion!)); @@ -523,17 +523,17 @@ private async Task ConvertNuGetPackageToUnity RegistryEntry packageEntry ) { - var unityPackageFileName = GetUnityPackageFileName(identity, npmPackageVersion); - var unityPackageFilePath = Path.Combine(_rootPersistentFolder, unityPackageFileName); + string unityPackageFileName = GetUnityPackageFileName(identity, npmPackageVersion); + string unityPackageFilePath = Path.Combine(_rootPersistentFolder, unityPackageFileName); LogInformation($"Converting NuGet package {identity} to Unity `{unityPackageFileName}`"); - using var downloadResult = await GetPackageDownloadResourceResult(identity); + using DownloadResourceResult downloadResult = await GetPackageDownloadResourceResult(identity); - using var packageReader = downloadResult.PackageReader; + using PackageReaderBase packageReader = downloadResult.PackageReader; // Update Repository metadata if necessary - var repoMeta = packageReader.NuspecReader.GetRepositoryMetadata(); + RepositoryMetadata repoMeta = packageReader.NuspecReader.GetRepositoryMetadata(); if (repoMeta != null && repoMeta.Url != null && repoMeta.Commit != null && repoMeta.Type != null) { npmPackageVersion.Repository = new NpmSourceRepository() @@ -552,7 +552,7 @@ RegistryEntry packageEntry { using var memStream = new MemoryStream(); - using (var outStream = File.Create(unityPackageFilePath)) + using (FileStream outStream = File.Create(unityPackageFilePath)) using (var gzoStream = new GZipOutputStream(outStream) { ModifiedTime = packageMeta.Published?.UtcDateTime @@ -560,15 +560,15 @@ RegistryEntry packageEntry using (var tarArchive = new TarOutputStream(gzoStream, Encoding.UTF8)) { // Select the framework version that is the closest or equal to the latest configured framework version - var versions = await packageReader.GetLibItemsAsync(CancellationToken.None); + IEnumerable versions = await packageReader.GetLibItemsAsync(CancellationToken.None); - var closestVersions = NuGetHelper.GetClosestFrameworkSpecificGroups(versions, _targetFrameworks); + IEnumerable<(FrameworkSpecificGroup, RegistryTargetFramework)> closestVersions = NuGetHelper.GetClosestFrameworkSpecificGroups(versions, _targetFrameworks); var collectedItems = new Dictionary>(); - foreach (var (item, targetFramework) in closestVersions) + foreach ((FrameworkSpecificGroup item, RegistryTargetFramework targetFramework) in closestVersions) { - if (!collectedItems.TryGetValue(item, out var frameworksPerGroup)) + if (!collectedItems.TryGetValue(item, out HashSet? frameworksPerGroup)) { frameworksPerGroup = []; collectedItems.Add(item, frameworksPerGroup); @@ -581,10 +581,10 @@ RegistryEntry packageEntry throw new InvalidOperationException($"The package does not contain a compatible .NET assembly {string.Join(",", _targetFrameworks.Select(x => x.Name))}"); } - var isPackageNetStandard21Assembly = DotNetHelper.IsNetStandard21Assembly(identity.Id); - var hasMultiNetStandard = collectedItems.Count > 1; - var hasOnlyNetStandard21 = collectedItems.Count == 1 && collectedItems.Values.First().All(x => x.Name == "netstandard2.1"); - var modTime = packageMeta.Published?.DateTime ?? DateTime.UnixEpoch; + bool isPackageNetStandard21Assembly = DotNetHelper.IsNetStandard21Assembly(identity.Id); + bool hasMultiNetStandard = collectedItems.Count > 1; + bool hasOnlyNetStandard21 = collectedItems.Count == 1 && collectedItems.Values.First().All(x => x.Name == "netstandard2.1"); + DateTime modTime = packageMeta.Published?.DateTime ?? DateTime.UnixEpoch; if (isPackageNetStandard21Assembly) { @@ -593,31 +593,31 @@ RegistryEntry packageEntry if (packageEntry.Analyzer) { - var packageFiles = await packageReader.GetItemsAsync(PackagingConstants.Folders.Analyzers, CancellationToken.None); + IEnumerable packageFiles = await packageReader.GetItemsAsync(PackagingConstants.Folders.Analyzers, CancellationToken.None); // https://learn.microsoft.com/en-us/nuget/guides/analyzers-conventions#analyzers-path-format - var analyzerFiles = packageFiles + string[] analyzerFiles = packageFiles .SelectMany(p => p.Items) .Where(p => NuGetHelper.IsApplicableUnitySupportedRoslynVersionFolder(p) && (NuGetHelper.IsApplicableAnalyzer(p) || NuGetHelper.IsApplicableAnalyzerResource(p))) .ToArray(); var createdDirectoryList = new List(); - foreach (var analyzerFile in analyzerFiles) + foreach (string? analyzerFile in analyzerFiles) { - var folderPrefix = $"{Path.GetDirectoryName(analyzerFile)!.Replace($"analyzers{Path.DirectorySeparatorChar}", string.Empty)}{Path.DirectorySeparatorChar}"; + string folderPrefix = $"{Path.GetDirectoryName(analyzerFile)!.Replace($"analyzers{Path.DirectorySeparatorChar}", string.Empty)}{Path.DirectorySeparatorChar}"; // Write folder meta if (!string.IsNullOrEmpty(folderPrefix)) { var directoryNameBuilder = new StringBuilder(); - foreach (var directoryName in folderPrefix.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries)) + foreach (string directoryName in folderPrefix.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries)) { directoryNameBuilder.Append(directoryName); directoryNameBuilder.Append(Path.DirectorySeparatorChar); - var processedDirectoryName = directoryNameBuilder.ToString()[0..^1]; + string processedDirectoryName = directoryNameBuilder.ToString()[0..^1]; if (createdDirectoryList.Any(d => d.Equals(processedDirectoryName))) { @@ -631,7 +631,7 @@ RegistryEntry packageEntry } } - var fileInUnityPackage = $"{folderPrefix}{Path.GetFileName(analyzerFile)}"; + string fileInUnityPackage = $"{folderPrefix}{Path.GetFileName(analyzerFile)}"; string? meta; string fileExtension = Path.GetExtension(fileInUnityPackage); @@ -668,9 +668,9 @@ RegistryEntry packageEntry memStream.Position = 0; memStream.SetLength(0); - using var stream = await packageReader.GetStreamAsync(analyzerFile, CancellationToken.None); + using Stream stream = await packageReader.GetStreamAsync(analyzerFile, CancellationToken.None); await stream.CopyToAsync(memStream); - var buffer = memStream.ToArray(); + byte[] buffer = memStream.ToArray(); // write content await WriteBufferToTar(tarArchive, fileInUnityPackage, buffer, modTime); @@ -681,14 +681,14 @@ RegistryEntry packageEntry // Write analyzer asmdef // Check Analyzer Scope section: https://docs.unity3d.com/Manual/roslyn-analyzers.html - var analyzerAsmdef = CreateAnalyzerAmsdef(identity); - var analyzerAsmdefAsJson = analyzerAsmdef.ToJson(); + UnityAsmdef analyzerAsmdef = CreateAnalyzerAmsdef(identity); + string analyzerAsmdefAsJson = analyzerAsmdef.ToJson(); string analyzerAsmdefFileName = $"{identity.Id}.asmdef"; await WriteTextFileToTar(tarArchive, analyzerAsmdefFileName, analyzerAsmdefAsJson, modTime); await WriteTextFileToTar(tarArchive, $"{analyzerAsmdefFileName}.meta", UnityMeta.GetMetaForExtension(GetStableGuid(identity, analyzerAsmdefFileName), ".asmdef")!, modTime); // Write empty script (Necessary to compile the asmdef file) - var emptyScriptContent = UnityScript.GetEmptyScript(); + string emptyScriptContent = UnityScript.GetEmptyScript(); const string emptyScriptFileName = "EmptyScript.cs"; await WriteTextFileToTar(tarArchive, emptyScriptFileName, emptyScriptContent, modTime); await WriteTextFileToTar(tarArchive, $"{emptyScriptFileName}.meta", UnityMeta.GetMetaForExtension(GetStableGuid(identity, emptyScriptFileName), ".cs")!, modTime); @@ -698,19 +698,19 @@ RegistryEntry packageEntry var platformDefs = PlatformDefinition.CreateAllPlatforms(); var packageFolders = new HashSet(StringComparer.OrdinalIgnoreCase); - foreach (var (item, frameworks) in collectedItems) + foreach ((FrameworkSpecificGroup item, HashSet frameworks) in collectedItems) { - var folderPrefix = hasMultiNetStandard ? $"{frameworks.First().Name}/" : ""; + string folderPrefix = hasMultiNetStandard ? $"{frameworks.First().Name}/" : ""; var filesToWrite = new List(); // Get any available runtime library groups - var runtimeLibs = await RuntimeLibraries + List<(string file, UnityOs, UnityCpu?)> runtimeLibs = await RuntimeLibraries .GetSupportedRuntimeLibsAsync(packageReader, item.TargetFramework, _logger) .ToListAsync(); // Mark-up the platforms of all runtime libraries var runtimePlatforms = new HashSet(); - foreach (var (file, os, cpu) in runtimeLibs) + foreach ((string file, UnityOs os, UnityCpu? cpu) in runtimeLibs) { // Reject resource dlls since Unity can't use them and we're not handling paths if (file.EndsWith(".resources.dll", StringComparison.OrdinalIgnoreCase)) @@ -718,7 +718,7 @@ RegistryEntry packageEntry continue; } - var platformDef = platformDefs.Find(os, cpu); + PlatformDefinition? platformDef = platformDefs.Find(os, cpu); if (platformDef == null) { LogInformation($"Failed to find a platform definition for: {os}, {cpu}"); @@ -731,12 +731,12 @@ RegistryEntry packageEntry } // Compute the set of platforms covered by the lib dlls - var libPlatforms = platformDefs.GetRemainingPlatforms(runtimePlatforms); + HashSet libPlatforms = platformDefs.GetRemainingPlatforms(runtimePlatforms); // Add the lib files - foreach (var libPlatform in libPlatforms) + foreach (PlatformDefinition libPlatform in libPlatforms) { - foreach (var file in item.Items) + foreach (string? file in item.Items) { // Reject resource dlls since Unity can't use them and we're not handling paths if (file.EndsWith(".resources.dll", StringComparison.OrdinalIgnoreCase)) @@ -749,10 +749,10 @@ RegistryEntry packageEntry } // Write the files - foreach (var file in filesToWrite) + foreach (PlatformFile file in filesToWrite) { // Get the destination path - var fileInUnityPackage = file.GetDestinationPath(folderPrefix); + string fileInUnityPackage = file.GetDestinationPath(folderPrefix); // Collect the folders' metas { @@ -770,7 +770,7 @@ RegistryEntry packageEntry string folder = string.Empty; - foreach (var relative in folders) + foreach (string relative in folders) { folder = Path.Combine(folder, relative); packageFolders.Add(folder); @@ -787,7 +787,7 @@ RegistryEntry packageEntry // We will use the define coming from the configuration file // Otherwise, it means that the assembly is compatible with whatever netstandard, and we can simply // use NET_STANDARD - var defineConstraints = hasMultiNetStandard + string[]? defineConstraints = hasMultiNetStandard || hasOnlyNetStandard21 || isPackageNetStandard21Assembly ? frameworks.First(x => x.Framework == item.TargetFramework).DefineConstraints : []; @@ -810,9 +810,9 @@ RegistryEntry packageEntry memStream.Position = 0; memStream.SetLength(0); - using var stream = await packageReader.GetStreamAsync(file.SourcePath, CancellationToken.None); + using Stream stream = await packageReader.GetStreamAsync(file.SourcePath, CancellationToken.None); await stream.CopyToAsync(memStream); - var buffer = memStream.ToArray(); + byte[] buffer = memStream.ToArray(); // write content await WriteBufferToTar(tarArchive, fileInUnityPackage, buffer, modTime); @@ -828,11 +828,11 @@ RegistryEntry packageEntry } // Write the native libraries - var nativeFiles = NativeLibraries.GetSupportedNativeLibsAsync(packageReader, _logger); + IAsyncEnumerable<(string file, string[] folders, UnityOs os, UnityCpu cpu)> nativeFiles = NativeLibraries.GetSupportedNativeLibsAsync(packageReader, _logger); - await foreach (var (file, folders, os, cpu) in nativeFiles) + await foreach ((string file, string[] folders, UnityOs os, UnityCpu cpu) in nativeFiles) { - var platformDef = platformDefs.Find(os, cpu); + PlatformDefinition? platformDef = platformDefs.Find(os, cpu); if (platformDef == null) { LogInformation($"Failed to find a platform definition for: {os}, {cpu}"); @@ -840,7 +840,7 @@ RegistryEntry packageEntry } string extension = Path.GetExtension(file); - var guid = GetStableGuid(identity, file); + Guid guid = GetStableGuid(identity, file); string? meta = extension switch { ".dll" or ".so" or ".dylib" => UnityMeta.GetMetaForDll(guid, platformDef!, [], []), @@ -854,9 +854,9 @@ RegistryEntry packageEntry } memStream.SetLength(0); - using var stream = await packageReader.GetStreamAsync(file, CancellationToken.None); + using Stream stream = await packageReader.GetStreamAsync(file, CancellationToken.None); await stream.CopyToAsync(memStream); - var buffer = memStream.ToArray(); + byte[] buffer = memStream.ToArray(); await WriteBufferToTar(tarArchive, file, buffer, modTime); await WriteTextFileToTar(tarArchive, $"{file}.meta", meta, modTime); @@ -864,21 +864,21 @@ RegistryEntry packageEntry // Remember all folders for meta files string folder = string.Empty; - foreach (var relative in folders) + foreach (string relative in folders) { folder = Path.Combine(folder, relative); packageFolders.Add(folder); } } - foreach (var folder in packageFolders) + foreach (string folder in packageFolders) { await WriteTextFileToTar(tarArchive, $"{folder}.meta", UnityMeta.GetMetaForFolder(GetStableGuid(identity, folder)), modTime); } // Write the package,json - var unityPackage = CreateUnityPackage(npmPackageInfo, npmPackageVersion); - var unityPackageAsJson = unityPackage.ToJson(); + UnityPackage unityPackage = CreateUnityPackage(npmPackageInfo, npmPackageVersion); + string unityPackageAsJson = unityPackage.ToJson(); const string packageJsonFileName = "package.json"; await WriteTextFileToTar(tarArchive, packageJsonFileName, unityPackageAsJson, modTime); await WriteTextFileToTar(tarArchive, $"{packageJsonFileName}.meta", UnityMeta.GetMetaForExtension(GetStableGuid(identity, packageJsonFileName), ".json")!, modTime); @@ -887,7 +887,7 @@ RegistryEntry packageEntry string? license = null; string? licenseUrlText = null; - var licenseUrl = packageMeta.LicenseMetadata?.LicenseUrl.ToString() ?? packageMeta.LicenseUrl?.ToString(); + string? licenseUrl = packageMeta.LicenseMetadata?.LicenseUrl.ToString() ?? packageMeta.LicenseUrl?.ToString(); if (!string.IsNullOrEmpty(licenseUrl)) { try @@ -940,9 +940,9 @@ RegistryEntry packageEntry } } - using (var stream = File.OpenRead(unityPackageFilePath)) + using (FileStream stream = File.OpenRead(unityPackageFilePath)) { - var sha1 = Sha1sum(stream); + string sha1 = Sha1sum(stream); await WriteUnityPackageSha1(identity, npmPackageVersion, sha1); npmPackageVersion.Distribution.Shasum = sha1; } @@ -1032,13 +1032,13 @@ private async Task WriteUnityPackageSha1(PackageIdentity identity, NpmPackageVer private bool TryReadPackageCacheEntry(string packageName, [NotNullWhen(true)] out NpmPackageCacheEntry? cacheEntry) { cacheEntry = null; - var path = GetUnityPackageDescPath(packageName); + string path = GetUnityPackageDescPath(packageName); if (!File.Exists(path)) return false; try { - var cacheEntryAsJson = File.ReadAllText(path); + string cacheEntryAsJson = File.ReadAllText(path); cacheEntry = JsonConvert.DeserializeObject(cacheEntryAsJson, JsonCommonExtensions.Settings); if (cacheEntry != null) { @@ -1056,8 +1056,8 @@ private bool TryReadPackageCacheEntry(string packageName, [NotNullWhen(true)] ou private async Task WritePackageCacheEntry(string packageName, NpmPackageCacheEntry cacheEntry) { - var path = GetUnityPackageDescPath(packageName); - var newJson = cacheEntry.ToJson(); + string path = GetUnityPackageDescPath(packageName); + string newJson = cacheEntry.ToJson(); // Only update if entry is different if (!string.Equals(newJson, cacheEntry.Json, StringComparison.InvariantCulture)) { @@ -1071,7 +1071,7 @@ private static async Task WriteTextFileToTar(TarOutputStream tarOut, string file ArgumentNullException.ThrowIfNull(filePath); ArgumentNullException.ThrowIfNull(content); - var buffer = Utf8EncodingNoBom.GetBytes(content); + byte[] buffer = s_utf8EncodingNoBom.GetBytes(content); await WriteBufferToTar(tarOut, filePath, buffer, modTime, cancellationToken); } @@ -1118,9 +1118,9 @@ private static UnityAsmdef CreateAnalyzerAmsdef(PackageIdentity packageIdentity) private static Guid StringToGuid(string text) { - var guid = new byte[16]; - var inputBytes = Encoding.UTF8.GetBytes(text); - var hash = SHA1.HashData(inputBytes); + byte[] guid = new byte[16]; + byte[] inputBytes = Encoding.UTF8.GetBytes(text); + byte[] hash = SHA1.HashData(inputBytes); Array.Copy(hash, 0, guid, 0, guid.Length); // Follow UUID for SHA1 based GUID @@ -1132,7 +1132,7 @@ private static Guid StringToGuid(string text) private static string Sha1sum(Stream stream) { - var hash = SHA1.HashData(stream); + byte[] hash = SHA1.HashData(stream); var sb = new StringBuilder(hash.Length * 2); foreach (byte b in hash) @@ -1172,7 +1172,7 @@ private static List SplitCommaSeparatedString(string input) char[] separators = [',', ';']; - foreach (var entry in input.Split(separators, StringSplitOptions.RemoveEmptyEntries)) + foreach (string entry in input.Split(separators, StringSplitOptions.RemoveEmptyEntries)) { list.Add(entry.Trim()); } diff --git a/src/UnityNuGet/RuntimeLibraries.cs b/src/UnityNuGet/RuntimeLibraries.cs index 0bb07daa..924954fa 100644 --- a/src/UnityNuGet/RuntimeLibraries.cs +++ b/src/UnityNuGet/RuntimeLibraries.cs @@ -1,99 +1,99 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading; -using NuGet.Common; -using NuGet.Frameworks; -using NuGet.Packaging; - -namespace UnityNuGet -{ - static class RuntimeLibraries - { - /// - /// Returns an enumerable over the files in the NuGet package's runtimes folder that are compatible with the - /// given target framework. - /// - /// The NuGet package reader. - /// The target framework the runtimes files should be compatible with. - /// The logger. - /// - /// The returned enumerable also includes platform information with each file item. The structure of a NuGet package with - /// runtimes is covered here. - /// - /// An enumerable over the files in the NuGet package's runtimes folder that are compatible with the - /// given target framework. - public static async IAsyncEnumerable<(string file, UnityOs, UnityCpu?)> GetSupportedRuntimeLibsAsync( - PackageReaderBase packageReader, - NuGetFramework targetFramework, - ILogger logger) - { - var versions = await packageReader.GetItemsAsync(PackagingConstants.Folders.Runtimes, CancellationToken.None); - var files = versions.SelectMany(v => v.Items); - - foreach (var file in files) - { - var folderPath = Path.GetDirectoryName(file)!; - var folders = folderPath.Split(Path.DirectorySeparatorChar); - - // We're looking for paths matching runtimes/{platform}-{architecture}/lib/{framework} - if (folders.Length != 4 || !folders[2].Equals("lib", StringComparison.OrdinalIgnoreCase)) - { - logger.LogInformation($"Skipping native library file located in the runtimes folder: {file} ..."); - continue; - } - - var framework = NuGetFramework.Parse(folders[3]); - if (framework != targetFramework) - { - logger.LogInformation($"Skipping runtime library targeting other frameworks: {file} ..."); - continue; - } - - var system = folders[1].Split('-'); - - if (system.Length < 1) - { - logger.LogInformation($"Skipping file located in the runtime folder that does not specify platform: {file} ..."); - continue; - } - - UnityOs? os = system[0][..3] switch - { - "lin" => UnityOs.Linux, - "osx" => UnityOs.OSX, - "win" => UnityOs.Windows, - "ios" => UnityOs.iOS, - _ => null - }; - - if (os is null) - { - logger.LogInformation($"Skipping runtime library for unsupported OS: {file} ..."); - continue; - } - - UnityCpu? cpu = null; - if (system.Length > 1) - { - cpu = system[1] switch - { - "x86" => UnityCpu.X86, - "x64" => UnityCpu.X64, - "arm64" => UnityCpu.ARM64, - _ => null - }; - - if (cpu is null) - { - logger.LogInformation($"Skipping runtime library for unsupported CPU: {file} ..."); - continue; - } - } - - yield return (file, os.Value, cpu); - } - } - } -} +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using NuGet.Common; +using NuGet.Frameworks; +using NuGet.Packaging; + +namespace UnityNuGet +{ + static class RuntimeLibraries + { + /// + /// Returns an enumerable over the files in the NuGet package's runtimes folder that are compatible with the + /// given target framework. + /// + /// The NuGet package reader. + /// The target framework the runtimes files should be compatible with. + /// The logger. + /// + /// The returned enumerable also includes platform information with each file item. The structure of a NuGet package with + /// runtimes is covered here. + /// + /// An enumerable over the files in the NuGet package's runtimes folder that are compatible with the + /// given target framework. + public static async IAsyncEnumerable<(string file, UnityOs, UnityCpu?)> GetSupportedRuntimeLibsAsync( + PackageReaderBase packageReader, + NuGetFramework targetFramework, + ILogger logger) + { + IEnumerable versions = await packageReader.GetItemsAsync(PackagingConstants.Folders.Runtimes, CancellationToken.None); + IEnumerable files = versions.SelectMany(v => v.Items); + + foreach (string? file in files) + { + string folderPath = Path.GetDirectoryName(file)!; + string[] folders = folderPath.Split(Path.DirectorySeparatorChar); + + // We're looking for paths matching runtimes/{platform}-{architecture}/lib/{framework} + if (folders.Length != 4 || !folders[2].Equals("lib", StringComparison.OrdinalIgnoreCase)) + { + logger.LogInformation($"Skipping native library file located in the runtimes folder: {file} ..."); + continue; + } + + var framework = NuGetFramework.Parse(folders[3]); + if (framework != targetFramework) + { + logger.LogInformation($"Skipping runtime library targeting other frameworks: {file} ..."); + continue; + } + + string[] system = folders[1].Split('-'); + + if (system.Length < 1) + { + logger.LogInformation($"Skipping file located in the runtime folder that does not specify platform: {file} ..."); + continue; + } + + UnityOs? os = system[0][..3] switch + { + "lin" => UnityOs.Linux, + "osx" => UnityOs.OSX, + "win" => UnityOs.Windows, + "ios" => UnityOs.iOS, + _ => null + }; + + if (os is null) + { + logger.LogInformation($"Skipping runtime library for unsupported OS: {file} ..."); + continue; + } + + UnityCpu? cpu = null; + if (system.Length > 1) + { + cpu = system[1] switch + { + "x86" => UnityCpu.X86, + "x64" => UnityCpu.X64, + "arm64" => UnityCpu.ARM64, + _ => null + }; + + if (cpu is null) + { + logger.LogInformation($"Skipping runtime library for unsupported CPU: {file} ..."); + continue; + } + } + + yield return (file, os.Value, cpu); + } + } + } +} diff --git a/src/UnityNuGet/UnityMeta.cs b/src/UnityNuGet/UnityMeta.cs index 74c5b2d9..89f1bc55 100644 --- a/src/UnityNuGet/UnityMeta.cs +++ b/src/UnityNuGet/UnityMeta.cs @@ -1,241 +1,241 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Scriban; - -namespace UnityNuGet -{ - /// - /// Helper methods to create Unity .meta files - /// - internal static class UnityMeta - { - public static string? GetMetaForExtension(Guid guid, string extension) - { - switch (extension) - { - case ".pdb": - break; - case ".asmdef": - case ".cs": - case ".json": - case ".md": - case ".txt": - case ".xml": - return GetMetaForText(guid); - } - - return null; - } - - public static string GetMetaForDll( - Guid guid, - PlatformDefinition platformDef, - IEnumerable labels, - IEnumerable defineConstraints) - { - const string text = @"fileFormatVersion: 2 -guid: {{ guid }} -{{ labels }}PluginImporter: - externalObjects: {} - serializedVersion: 2 - iconMap: {} - executionOrder: {} -{{ constraints }} isPreloaded: 0 - isOverridable: 0 - isExplicitlyReferenced: 0 - validateReferences: 1 - platformData:{{exclude_platforms}} - - first: - Any: - second: - enabled: {{ all_enabled }} - settings: {}{{ per_platform_settings }} - - first: - Windows Store Apps: WindowsStoreApps - second: - enabled: 0 - settings: - CPU: AnyCPU - userData: - assetBundleName: - assetBundleVariant: -"; - - var allLabels = labels.ToList(); - var allConstraints = defineConstraints.ToList(); - - static string FormatList(IEnumerable items) => string.Join( - string.Empty, - items.Select(d => $" - {d}\n")); - - string excludePlatforms = string.Empty; - string perPlatformSettings = @" - - first: - Editor: Editor - second: - enabled: 0 - settings: - DefaultValueInitialized: true"; - - // Render the per-platform settings - if (platformDef.Os != UnityOs.AnyOs) - { - // Determine which configurations are enabled - var platWin = platformDef.Find(UnityOs.Windows, UnityCpu.X86); - var platWin64 = platformDef.Find(UnityOs.Windows, UnityCpu.X64); - var platLinux64 = platformDef.Find(UnityOs.Linux, UnityCpu.X64); - var platOsx = platformDef.Find(UnityOs.OSX); - var platAndroid = platformDef.Find(UnityOs.Android); - var platWasm = platformDef.Find(UnityOs.WebGL); - var platIos = platformDef.Find(UnityOs.iOS); - var platEditor = platformDef.FindEditor(); - - var dict = new - { - enablesWin = (platWin != null) ? 1 : 0, - enablesWin64 = (platWin64 != null) ? 1 : 0, - enablesLinux64 = (platLinux64 != null) ? 1 : 0, - enablesOsx = (platOsx != null) ? 1 : 0, - enablesAndroid = (platAndroid != null) ? 1 : 0, - enablesWasm = (platWasm != null) ? 1 : 0, - enablesIos = (platIos != null) ? 1 : 0, - enablesEditor = (platEditor != null) ? 1 : 0, - - cpuWin = (platWin?.Cpu ?? UnityCpu.None).GetName(), - cpuWin64 = (platWin64?.Cpu ?? UnityCpu.None).GetName(), - cpuLinux64 = (platLinux64?.Cpu ?? UnityCpu.None).GetName(), - cpuOsx = (platOsx?.Cpu ?? UnityCpu.None).GetName(), - cpuAndroid = (platAndroid?.Cpu ?? UnityCpu.None).GetName(), - cpuIos = (platIos?.Cpu ?? UnityCpu.None).GetName(), - cpuEditor = (platEditor?.Cpu ?? UnityCpu.None).GetName(), - - osEditor = (platEditor?.Os ?? UnityOs.AnyOs).GetName(), - }; - - const string excludePlatformsText = @" - - first: - : Any - second: - enabled: 0 - settings: - Exclude Android: {{ 1 - enables_android }} - Exclude Editor: {{ 1 - enables_editor }} - Exclude Linux64: {{ 1 - enables_linux64 }} - Exclude OSXUniversal: {{ 1 - enables_osx }} - Exclude WebGL: {{ 1 - enables_wasm }} - Exclude Win: {{ 1 - enables_win }} - Exclude Win64: {{ 1 - enables_win64 }} - Exclude iOS: {{ 1 - enables_ios }}"; - - const string perPlatformSettingsText = @" - - first: - Android: Android - second: - enabled: {{ enables_android }} - settings: - CPU: {{ cpu_android }} - - first: - Editor: Editor - second: - enabled: {{ enables_editor }} - settings: - CPU: {{ cpu_editor }} - DefaultValueInitialized: true - OS: {{ os_editor }} - - first: - Standalone: Linux64 - second: - enabled: {{ enables_linux64 }} - settings: - CPU: {{ cpu_linux64 }} - - first: - Standalone: OSXUniversal - second: - enabled: {{ enables_osx }} - settings: - CPU: {{ cpu_osx }} - - first: - Standalone: Win - second: - enabled: {{ enables_win }} - settings: - CPU: {{ cpu_win }} - - first: - Standalone: Win64 - second: - enabled: {{ enables_win64 }} - settings: - CPU: {{ cpu_win64 }} - - first: - WebGL: WebGL - second: - enabled: {{ enables_wasm }} - settings: {} - - first: - iPhone: iOS - second: - enabled: {{ enables_ios }} - settings: - AddToEmbeddedBinaries: false - CPU: {{ cpu_ios }} - CompileFlags: - FrameworkDependencies: "; - - excludePlatforms = Template - .Parse(excludePlatformsText) - .Render(dict); - - perPlatformSettings = Template - .Parse(perPlatformSettingsText) - .Render(dict); - } - - bool allPlatformsEnabled = (platformDef.Os == UnityOs.AnyOs) && (platformDef.Cpu == UnityCpu.AnyCpu); - - return Template - .Parse(text) - .Render(new - { - excludePlatforms, - perPlatformSettings, - guid = guid.ToString("N"), - allEnabled = allPlatformsEnabled ? "1" : "0", - labels = allLabels.Count == 0 - ? string.Empty - : $"labels:\n{FormatList(allLabels)}", - constraints = allConstraints.Count == 0 - ? string.Empty - : $" defineConstraints:\n{FormatList(allConstraints)}" - }) - .StripWindowsNewlines(); - } - - public static string GetMetaForFolder(Guid guid) - { - return $@"fileFormatVersion: 2 -guid: {guid:N} -folderAsset: yes -DefaultImporter: - externalObjects: {{}} - userData: - assetBundleName: - assetBundleVariant: -".StripWindowsNewlines(); - } - - private static string GetMetaForText(Guid guid) - { - return $@"fileFormatVersion: 2 -guid: {guid:N} -TextScriptImporter: - externalObjects: {{}} - userData: - assetBundleName: - assetBundleVariant: -".StripWindowsNewlines(); - } - - private static string StripWindowsNewlines(this string input) => input.Replace("\r\n", "\n"); - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using Scriban; + +namespace UnityNuGet +{ + /// + /// Helper methods to create Unity .meta files + /// + internal static class UnityMeta + { + public static string? GetMetaForExtension(Guid guid, string extension) + { + switch (extension) + { + case ".pdb": + break; + case ".asmdef": + case ".cs": + case ".json": + case ".md": + case ".txt": + case ".xml": + return GetMetaForText(guid); + } + + return null; + } + + public static string GetMetaForDll( + Guid guid, + PlatformDefinition platformDef, + IEnumerable labels, + IEnumerable defineConstraints) + { + const string text = @"fileFormatVersion: 2 +guid: {{ guid }} +{{ labels }}PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} +{{ constraints }} isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData:{{exclude_platforms}} + - first: + Any: + second: + enabled: {{ all_enabled }} + settings: {}{{ per_platform_settings }} + - first: + Windows Store Apps: WindowsStoreApps + second: + enabled: 0 + settings: + CPU: AnyCPU + userData: + assetBundleName: + assetBundleVariant: +"; + + var allLabels = labels.ToList(); + var allConstraints = defineConstraints.ToList(); + + static string FormatList(IEnumerable items) => string.Join( + string.Empty, + items.Select(d => $" - {d}\n")); + + string excludePlatforms = string.Empty; + string perPlatformSettings = @" + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true"; + + // Render the per-platform settings + if (platformDef.Os != UnityOs.AnyOs) + { + // Determine which configurations are enabled + PlatformDefinition? platWin = platformDef.Find(UnityOs.Windows, UnityCpu.X86); + PlatformDefinition? platWin64 = platformDef.Find(UnityOs.Windows, UnityCpu.X64); + PlatformDefinition? platLinux64 = platformDef.Find(UnityOs.Linux, UnityCpu.X64); + PlatformDefinition? platOsx = platformDef.Find(UnityOs.OSX); + PlatformDefinition? platAndroid = platformDef.Find(UnityOs.Android); + PlatformDefinition? platWasm = platformDef.Find(UnityOs.WebGL); + PlatformDefinition? platIos = platformDef.Find(UnityOs.iOS); + PlatformDefinition? platEditor = platformDef.FindEditor(); + + var dict = new + { + enablesWin = (platWin != null) ? 1 : 0, + enablesWin64 = (platWin64 != null) ? 1 : 0, + enablesLinux64 = (platLinux64 != null) ? 1 : 0, + enablesOsx = (platOsx != null) ? 1 : 0, + enablesAndroid = (platAndroid != null) ? 1 : 0, + enablesWasm = (platWasm != null) ? 1 : 0, + enablesIos = (platIos != null) ? 1 : 0, + enablesEditor = (platEditor != null) ? 1 : 0, + + cpuWin = (platWin?.Cpu ?? UnityCpu.None).GetName(), + cpuWin64 = (platWin64?.Cpu ?? UnityCpu.None).GetName(), + cpuLinux64 = (platLinux64?.Cpu ?? UnityCpu.None).GetName(), + cpuOsx = (platOsx?.Cpu ?? UnityCpu.None).GetName(), + cpuAndroid = (platAndroid?.Cpu ?? UnityCpu.None).GetName(), + cpuIos = (platIos?.Cpu ?? UnityCpu.None).GetName(), + cpuEditor = (platEditor?.Cpu ?? UnityCpu.None).GetName(), + + osEditor = (platEditor?.Os ?? UnityOs.AnyOs).GetName(), + }; + + const string excludePlatformsText = @" + - first: + : Any + second: + enabled: 0 + settings: + Exclude Android: {{ 1 - enables_android }} + Exclude Editor: {{ 1 - enables_editor }} + Exclude Linux64: {{ 1 - enables_linux64 }} + Exclude OSXUniversal: {{ 1 - enables_osx }} + Exclude WebGL: {{ 1 - enables_wasm }} + Exclude Win: {{ 1 - enables_win }} + Exclude Win64: {{ 1 - enables_win64 }} + Exclude iOS: {{ 1 - enables_ios }}"; + + const string perPlatformSettingsText = @" + - first: + Android: Android + second: + enabled: {{ enables_android }} + settings: + CPU: {{ cpu_android }} + - first: + Editor: Editor + second: + enabled: {{ enables_editor }} + settings: + CPU: {{ cpu_editor }} + DefaultValueInitialized: true + OS: {{ os_editor }} + - first: + Standalone: Linux64 + second: + enabled: {{ enables_linux64 }} + settings: + CPU: {{ cpu_linux64 }} + - first: + Standalone: OSXUniversal + second: + enabled: {{ enables_osx }} + settings: + CPU: {{ cpu_osx }} + - first: + Standalone: Win + second: + enabled: {{ enables_win }} + settings: + CPU: {{ cpu_win }} + - first: + Standalone: Win64 + second: + enabled: {{ enables_win64 }} + settings: + CPU: {{ cpu_win64 }} + - first: + WebGL: WebGL + second: + enabled: {{ enables_wasm }} + settings: {} + - first: + iPhone: iOS + second: + enabled: {{ enables_ios }} + settings: + AddToEmbeddedBinaries: false + CPU: {{ cpu_ios }} + CompileFlags: + FrameworkDependencies: "; + + excludePlatforms = Template + .Parse(excludePlatformsText) + .Render(dict); + + perPlatformSettings = Template + .Parse(perPlatformSettingsText) + .Render(dict); + } + + bool allPlatformsEnabled = (platformDef.Os == UnityOs.AnyOs) && (platformDef.Cpu == UnityCpu.AnyCpu); + + return Template + .Parse(text) + .Render(new + { + excludePlatforms, + perPlatformSettings, + guid = guid.ToString("N"), + allEnabled = allPlatformsEnabled ? "1" : "0", + labels = allLabels.Count == 0 + ? string.Empty + : $"labels:\n{FormatList(allLabels)}", + constraints = allConstraints.Count == 0 + ? string.Empty + : $" defineConstraints:\n{FormatList(allConstraints)}" + }) + .StripWindowsNewlines(); + } + + public static string GetMetaForFolder(Guid guid) + { + return $@"fileFormatVersion: 2 +guid: {guid:N} +folderAsset: yes +DefaultImporter: + externalObjects: {{}} + userData: + assetBundleName: + assetBundleVariant: +".StripWindowsNewlines(); + } + + private static string GetMetaForText(Guid guid) + { + return $@"fileFormatVersion: 2 +guid: {guid:N} +TextScriptImporter: + externalObjects: {{}} + userData: + assetBundleName: + assetBundleVariant: +".StripWindowsNewlines(); + } + + private static string StripWindowsNewlines(this string input) => input.Replace("\r\n", "\n"); + } +}