-
Notifications
You must be signed in to change notification settings - Fork 727
/
LocalModuleRegistry.cs
99 lines (79 loc) · 4.41 KB
/
LocalModuleRegistry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Immutable;
using Bicep.Core.Diagnostics;
using Bicep.Core.Extensions;
using Bicep.Core.FileSystem;
using Bicep.Core.Modules;
using Bicep.Core.Semantics;
using Bicep.Core.SourceCode;
using Bicep.Core.Utils;
namespace Bicep.Core.Registry
{
public class LocalModuleRegistry : ArtifactRegistry<LocalModuleReference>
{
private readonly IFileResolver fileResolver;
private readonly Uri parentModuleUri;
private readonly BicepCompiler? bicepCompiler;
public LocalModuleRegistry(IFileResolver fileResolver, Uri parentModuleUri, BicepCompiler? bicepCompiler)
{
this.fileResolver = fileResolver;
this.parentModuleUri = parentModuleUri;
this.bicepCompiler = bicepCompiler;
}
public override string Scheme => ArtifactReferenceSchemes.Local;
public override RegistryCapabilities GetCapabilities(LocalModuleReference reference) => RegistryCapabilities.Default;
public override ResultWithDiagnostic<ArtifactReference> TryParseArtifactReference(ArtifactType artifactType, string? alias, string reference)
{
if (artifactType != ArtifactType.Module && artifactType != ArtifactType.Provider)
{
return new(x => x.UnsupportedArtifactType(artifactType));
}
if (!LocalModuleReference.TryParse(reference, parentModuleUri).IsSuccess(out var @ref, out var failureBuilder))
{
return new(failureBuilder);
}
return new(@ref);
}
public override ResultWithDiagnostic<Uri> TryGetLocalArtifactEntryPointUri(LocalModuleReference reference)
{
var localUri = fileResolver.TryResolveFilePath(reference.ParentModuleUri, reference.Path);
if (localUri is null)
{
return new(x => x.FilePathCouldNotBeResolved(reference.Path, reference.ParentModuleUri.LocalPath));
}
return new(localUri);
}
public override Task<IDictionary<ArtifactReference, DiagnosticBuilder.ErrorBuilderDelegate>> RestoreArtifacts(IEnumerable<LocalModuleReference> references)
{
// local modules are already present on the file system
// and do not require init
return Task.FromResult<IDictionary<ArtifactReference, DiagnosticBuilder.ErrorBuilderDelegate>>(ImmutableDictionary<ArtifactReference, DiagnosticBuilder.ErrorBuilderDelegate>.Empty);
}
public override Task<IDictionary<ArtifactReference, DiagnosticBuilder.ErrorBuilderDelegate>> InvalidateArtifactsCache(IEnumerable<LocalModuleReference> references)
{
// local modules are already present on the file system, there's no cache concept for this one
// we do nothing
return Task.FromResult<IDictionary<ArtifactReference, DiagnosticBuilder.ErrorBuilderDelegate>>(ImmutableDictionary<ArtifactReference, DiagnosticBuilder.ErrorBuilderDelegate>.Empty);
}
public override bool IsArtifactRestoreRequired(LocalModuleReference reference) => false;
public override Task PublishModule(LocalModuleReference moduleReference, BinaryData compiledArmTemplate, BinaryData? bicepSources, string? documentationUri, string? description)
=> throw new NotSupportedException("Local modules cannot be published.");
public override Task PublishProvider(LocalModuleReference reference, BinaryData typesTgz)
=> throw new NotSupportedException("Local providers cannot be published.");
public override Task<bool> CheckArtifactExists(LocalModuleReference reference) => throw new NotSupportedException("Local modules cannot be published.");
public override string? TryGetDocumentationUri(LocalModuleReference moduleReference) => null;
public override Task<string?> TryGetModuleDescription(ModuleSymbol module, LocalModuleReference moduleReference)
{
if (module.TryGetSemanticModel().TryUnwrap() is { } model)
{
return Task.FromResult(DescriptionHelper.TryGetFromSemanticModel(model));
}
return Task.FromResult<string?>(null);
}
public override ResultWithException<SourceArchive> TryGetSource(LocalModuleReference reference)
{
return new(new SourceNotAvailableException());
}
}
}