Skip to content

Commit

Permalink
w
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenWeatherford committed Feb 5, 2025
1 parent 08a2ca8 commit a5b7ace
Showing 1 changed file with 8 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ private enum ModuleCompletionPriority
FullPath = 2, // br:, ts:
}

// Direct reference to a full registry login server URI via br:<registry>
private static readonly Regex ModulePrefixWithFullPath = new(@"^br:(?<registry>(.*?))/", RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);

[GeneratedRegex(
"""
(?x) # Extended mode (allow comments and whitespace)
Expand Down Expand Up @@ -134,6 +131,8 @@ public Parts WithModulePath(string newResolvedModulePath)
};
}
}

public bool IsPublicRegistry => string.CompareOrdinal(ResolvedRegistry, PublicMcrRegistry) == 0;
}

private const string PublicMcrRegistry = LanguageConstants.BicepPublicMcrRegistry; // "mcr.microsoft.com"
Expand Down Expand Up @@ -392,37 +391,6 @@ private static bool TryGetValidModuleAlias(
return false;
}

private string? GetFirstMatch(Regex regex, string text, string group, bool allowEmpty)
{
var matches = regex.Matches(text);
if (!matches.Any())
{
return null;
}

string? value = matches[0].Groups[group].Value;
if (!allowEmpty && string.IsNullOrWhiteSpace(value))
{
return null;
}

return value;
}

/// <summary>
/// True if a direct reference to a private ACR registry (i.e. not pointing to the Microsoft public bicep registry)
/// Example:
/// "br:privateacr.azurecr.io/" => true
/// </summary>
/// <param name="text"></param>
/// <param name="registry">Won't be null with true return value, but could be empty</param>
/// <returns></returns>
private bool IsPrivateRegistryReference(string text, [NotNullWhen(true)] out string? registry)
{
registry = GetFirstMatch(ModulePrefixWithFullPath, text, "registry", allowEmpty: false);
return registry is not null && !registry.Equals(PublicMcrRegistry, StringComparison.Ordinal);
}

// Automatically adds completions for the base path portion of a private registry from an alias in bicepconfig.json.
// Example:
// bicepconfig.json:
Expand All @@ -441,22 +409,19 @@ private IEnumerable<CompletionItem> GetPartialPrivatePathCompletionsFromAliases(
{
if (ParseParts(trimmedText, rootConfiguration) is not Parts parts
|| !string.IsNullOrWhiteSpace(parts.ResolvedModulePath)
|| parts.HasVersionSeparator)
|| parts.HasVersionSeparator
|| parts.IsPublicRegistry
|| string.IsNullOrWhiteSpace(parts.ResolvedRegistry))
{
return [];
}

List<CompletionItem> completions = new();

if (!IsPrivateRegistryReference(trimmedText, out string? registry) || string.IsNullOrWhiteSpace(registry))
{
return completions;
}

var sentTelemetry = false;
foreach (var kvp in GetModuleAliases(rootConfiguration))
{
if (registry.Equals(kvp.Value.Registry, StringComparison.Ordinal))
if (parts.ResolvedRegistry.Equals(kvp.Value.Registry, StringComparison.Ordinal))
{
var modulePath = kvp.Value.ModulePath;

Expand All @@ -470,7 +435,7 @@ private IEnumerable<CompletionItem> GetPartialPrivatePathCompletionsFromAliases(
.WithSnippetEdit(context.ReplacementRange, insertText)
.WithFilterText(insertText)
.WithSortText(GetSortText(modulePath))
.WithResolveData(ModuleResolutionKey, new { Registry = registry, Module = modulePath })
.WithResolveData(ModuleResolutionKey, new { Registry = parts.ResolvedRegistry, Module = modulePath })
.WithFollowupCompletion("module path completion")
.Build();
completions.Add(completionItem);
Expand Down Expand Up @@ -681,7 +646,7 @@ private async Task<CompletionItem> ResolveVersionCompletionItem(CompletionItem c
if (registryModuleCatalog.TryGetCachedRegistry(registry) is IRegistryModuleMetadataProvider cachedRegistry
&& await cachedRegistry.TryGetModuleAsync(modulePath) is { } module
&& await module.TryGetVersionsAsync() is { } versions
&& versions.FirstOrDefault(v => v.Version.Equals(version, StringComparison.Ordinal)) is RegistryModuleVersionMetadata metadata/*asdfg does this work if not found?*/)
&& versions.FirstOrDefault(v => v.Version.Equals(version, StringComparison.Ordinal)) is RegistryModuleVersionMetadata metadata)
{
// Resolutions for MCR aren't very interesting because all the data is downloaded at once
if (!registry.Equals(LanguageConstants.BicepPublicMcrRegistry, StringComparison.Ordinal))
Expand Down

0 comments on commit a5b7ace

Please sign in to comment.