Skip to content

Commit

Permalink
Replaced DotLiquid with Fluid.
Browse files Browse the repository at this point in the history
  • Loading branch information
reduckted committed Nov 20, 2024
1 parent e52bf79 commit 746862c
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 148 deletions.
4 changes: 2 additions & 2 deletions visual-studio/source/GitWebLinks/GitWebLinks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="DotLiquid">
<Version>2.2.692</Version>
<PackageReference Include="Fluid.Core">
<Version>2.12.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.SDK" Version="17.0.31902.203" ExcludeAssets="runtime">
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
48 changes: 25 additions & 23 deletions visual-studio/source/GitWebLinks/Services/DefinitionProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#nullable enable

using DotLiquid;
using Fluid;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -28,15 +28,17 @@ public static IReadOnlyCollection<HandlerDefinition> GetDefinitions() {
if (_definitions is null) {
Assembly container;
List<HandlerDefinition> definitions;
FluidParser parser;


definitions = new List<HandlerDefinition>();
container = typeof(LinkHandlerProvider).Assembly;
parser = new FluidParser();

foreach (string name in container.GetManifestResourceNames()) {
if (Path.GetExtension(name) == ".json") {
using (Stream stream = container.GetManifestResourceStream(name)) {
definitions.Add(LoadDefinition(stream));
definitions.Add(LoadDefinition(stream, parser));
}
}
}
Expand All @@ -48,7 +50,7 @@ public static IReadOnlyCollection<HandlerDefinition> GetDefinitions() {
}


private static HandlerDefinition LoadDefinition(Stream stream) {
private static HandlerDefinition LoadDefinition(Stream stream, FluidParser parser) {
JsonHandlerDefinition? json;


Expand All @@ -65,10 +67,10 @@ private static HandlerDefinition LoadDefinition(Stream stream) {
json.Name,
json.BranchRef,
json.SettingsKeys ?? Array.Empty<string>(),
Template.Parse(json.Url),
parser.Parse(json.Url),
json.Query is not null ? ParseQueryModifications(json.Query) : Array.Empty<QueryModification>(),
Template.Parse(json.Selection),
ParseReverseSettings(json.Reverse),
parser.Parse(json.Selection),
ParseReverseSettings(json.Reverse, parser),
json.Private
);

Expand All @@ -77,17 +79,17 @@ private static HandlerDefinition LoadDefinition(Stream stream) {
json.Name,
json.BranchRef,
json.SettingsKeys ?? Array.Empty<string>(),
Template.Parse(json.Url),
parser.Parse(json.Url),
json.Query is not null ? ParseQueryModifications(json.Query) : Array.Empty<QueryModification>(),
Template.Parse(json.Selection),
ParseReverseSettings(json.Reverse),
ParseServers(json.Server!)
parser.Parse(json.Selection),
ParseReverseSettings(json.Reverse, parser),
ParseServers(json.Server!, parser)
);
}
}


private static IReadOnlyList<IServer> ParseServers(IReadOnlyList<JsonServer> json) {
private static IReadOnlyList<IServer> ParseServers(IReadOnlyList<JsonServer> json, FluidParser parser) {
List<IServer> servers;


Expand All @@ -98,10 +100,10 @@ private static IReadOnlyList<IServer> ParseServers(IReadOnlyList<JsonServer> jso
servers.Add(
new DynamicServer(
new Regex(server.RemotePattern),
Template.Parse(server.Http),
Template.Parse(server.Ssh),
parser.Parse(server.Http),
parser.Parse(server.Ssh),
(server.WebPattern is not null) ? new Regex(server.WebPattern) : null,
(server.Web is not null) ? Template.Parse(server.Web) : null
(server.Web is not null) ? parser.Parse(server.Web) : null
)
);
} else {
Expand All @@ -118,21 +120,21 @@ private static IReadOnlyList<QueryModification> ParseQueryModifications(IReadOnl
}


private static ReverseSettings ParseReverseSettings(JsonReverseSettings json) {
private static ReverseSettings ParseReverseSettings(JsonReverseSettings json, FluidParser parser) {
return new ReverseSettings(
new Regex(json.Pattern),
Template.Parse(json.File),
parser.Parse(json.File),
json.FileMayStartWithBranch,
new ReverseServerSettings(
Template.Parse(json.Server.Http),
Template.Parse(json.Server.Ssh),
(json.Server.Web is not null) ? Template.Parse(json.Server.Web) : null
parser.Parse(json.Server.Http),
parser.Parse(json.Server.Ssh),
(json.Server.Web is not null) ? parser.Parse(json.Server.Web) : null
),
new ReverseSelectionSettings(
Template.Parse(json.Selection.StartLine),
json.Selection.StartColumn is not null ? Template.Parse(json.Selection.StartColumn) : null,
json.Selection.EndLine is not null ? Template.Parse(json.Selection.EndLine) : null,
json.Selection.EndColumn is not null ? Template.Parse(json.Selection.EndColumn) : null
parser.Parse(json.Selection.StartLine),
json.Selection.StartColumn is not null ? parser.Parse(json.Selection.StartColumn) : null,
json.Selection.EndLine is not null ? parser.Parse(json.Selection.EndLine) : null,
json.Selection.EndColumn is not null ? parser.Parse(json.Selection.EndColumn) : null
)
);
}
Expand Down
32 changes: 16 additions & 16 deletions visual-studio/source/GitWebLinks/Services/LinkHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#nullable enable

using DotLiquid;
using Fluid;
using Microsoft.VisualStudio;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -57,7 +57,7 @@ public async Task<CreateUrlResult> CreateUrlAsync(Repository repository, FileInf
string relativePath;
string selection;
TemplateData data;
Hash hash;
TemplateContext context;


// If a link type wasn't specified, then we'll use
Expand Down Expand Up @@ -111,11 +111,11 @@ public async Task<CreateUrlResult> CreateUrlAsync(Repository repository, FileInf
data.Add(key, await _settings.GetHandlerSettingAsync(key));
}

hash = data.ToHash();
url = _definition.Url.Render(hash);
context = data.AsTemplateContext();
url = _definition.Url.Render(context);

if (file.Selection is not null) {
selection = _definition.Selection.Render(hash);
selection = _definition.Selection.Render(context);
url += selection;
} else {
selection = "";
Expand Down Expand Up @@ -472,33 +472,33 @@ private static bool TryGetFinalPathNameByHandle(IntPtr handle, int bufferSize, o
match = _definition.Reverse.Pattern.Match(webUrl);

if (match.Success) {
Hash hash;
TemplateContext context;
string file;
StaticServer server;
PartialSelectedRange? selection;


hash = TemplateData
context = TemplateData
.Create()
.Add("http", address?.Http)
.Add("ssh", address?.Ssh)
.Add("web", address?.Web)
.Add(match)
.ToHash();
.AsTemplateContext();

file = _definition.Reverse.File.Render(hash);
file = _definition.Reverse.File.Render(context);

server = new StaticServer(
_definition.Reverse.Server.Http.Render(hash),
_definition.Reverse.Server.Ssh.Render(hash),
_definition.Reverse.Server.Web?.Render(hash)
_definition.Reverse.Server.Http.Render(context),
_definition.Reverse.Server.Ssh.Render(context),
_definition.Reverse.Server.Web?.Render(context)
);

selection = new PartialSelectedRange(
TryParseNumber(_definition.Reverse.Selection.StartLine.Render(hash)),
TryParseNumber(_definition.Reverse.Selection.StartColumn?.Render(hash)),
TryParseNumber(_definition.Reverse.Selection.EndLine?.Render(hash)),
TryParseNumber(_definition.Reverse.Selection.EndColumn?.Render(hash))
TryParseNumber(_definition.Reverse.Selection.StartLine.Render(context)),
TryParseNumber(_definition.Reverse.Selection.StartColumn?.Render(context)),
TryParseNumber(_definition.Reverse.Selection.EndLine?.Render(context)),
TryParseNumber(_definition.Reverse.Selection.EndColumn?.Render(context))
);

return new UrlInfo(file, server, selection);
Expand Down
14 changes: 7 additions & 7 deletions visual-studio/source/GitWebLinks/Services/TemplateData.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#nullable enable

using DotLiquid;
using Fluid;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
Expand All @@ -9,7 +9,7 @@ namespace GitWebLinks;

public class TemplateData {

private readonly Dictionary<string, object?> _data = new();
private readonly TemplateContext _context = new(TemplateEngine.Options);


public static TemplateData Create() {
Expand All @@ -21,7 +21,7 @@ private TemplateData() { }


public TemplateData Add(string key, object? value) {
_data[key] = value;
_context.SetValue(key, value);
return this;
}

Expand All @@ -42,18 +42,18 @@ public TemplateData Add(Match match) {
}

matchData["groups"] = groupsData;
_data["match"] = matchData;
_context.SetValue("match", matchData);

} else {
_data["match"] = match.Groups.OfType<Group>().Select((x) => x.Success ? x.Value : null).ToArray();
_context.SetValue("match", match.Groups.OfType<Group>().Select((x) => x.Success ? x.Value : null).ToArray());
}

return this;
}


public Hash ToHash() {
return Hash.FromDictionary(_data);
public TemplateContext AsTemplateContext() {
return _context;
}

}
76 changes: 59 additions & 17 deletions visual-studio/source/GitWebLinks/Services/TemplateEngine.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#nullable enable

using DotLiquid;
using Fluid;
using Fluid.Values;
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace GitWebLinks;

Expand All @@ -13,48 +14,89 @@ public static class TemplateEngine {
private static bool _initialized;


public static TemplateOptions Options = new();


public static void Initialize() {
if (!_initialized) {
Template.RegisterFilter(typeof(Filters));
Options.Filters.AddFilter("filename", Filters.FilenameAsync);
Options.Filters.AddFilter("encode_uri", Filters.EncodeUriAsync);
Options.Filters.AddFilter("encode_uri_component", Filters.EncodeUriComponentAsync);
Options.Filters.AddFilter("encode_uri_component_segments", Filters.EncodeUriComponentSegmentsAsync);
Options.Filters.AddFilter("decode_uri", Filters.DecodeUriAsync);
Options.Filters.AddFilter("decode_uri_component", Filters.DecodeUriComponentAsync);
Options.Filters.AddFilter("decode_uri_component_segments", Filters.DecodeUriComponentSegmentsAsync);
_initialized = true;
}
}


private class Filters {

public static string Filename(string value) {
return Path.GetFileName(value);
public static ValueTask<FluidValue> FilenameAsync(
FluidValue value,
FilterArguments arguments,
TemplateContext context
) {
return StringValue.Create(Path.GetFileName(value.ToStringValue()));
}


public static string EncodeUri(string value) {
return Uri.EscapeUriString(value);
public static ValueTask<FluidValue> EncodeUriAsync(
FluidValue value,
FilterArguments arguments,
TemplateContext context
) {
return StringValue.Create(Uri.EscapeUriString(value.ToStringValue()));
}


public static string EncodeUriComponent(string value) {
return Uri.EscapeDataString(value);
public static ValueTask<FluidValue> EncodeUriComponentAsync(
FluidValue value,
FilterArguments arguments,
TemplateContext context
) {
return StringValue.Create(Uri.EscapeDataString(value.ToStringValue()));
}


public static string EncodeUriComponentSegments(string value) {
return string.Join("/", value.Split('/').Select(Uri.EscapeDataString));
public static ValueTask<FluidValue> EncodeUriComponentSegmentsAsync(
FluidValue value,
FilterArguments arguments,
TemplateContext context
) {
return StringValue.Create(
string.Join("/", value.ToStringValue().Split('/').Select(Uri.EscapeDataString))
);
}


public static string DecodeUri(string value) {
return Uri.UnescapeDataString(value);
public static ValueTask<FluidValue> DecodeUriAsync(
FluidValue value,
FilterArguments arguments,
TemplateContext context
) {
return StringValue.Create(Uri.UnescapeDataString(value.ToStringValue()));
}


public static string DecodeUriComponent(string value) {
return Uri.UnescapeDataString(value);
public static ValueTask<FluidValue> DecodeUriComponentAsync(
FluidValue value,
FilterArguments arguments,
TemplateContext context
) {
return StringValue.Create(Uri.UnescapeDataString(value.ToStringValue()));
}


public static string DecodeUriComponentSegments(string value) {
return string.Join("/", value.Split('/').Select(Uri.UnescapeDataString));
public static ValueTask<FluidValue> DecodeUriComponentSegmentsAsync(
FluidValue value,
FilterArguments arguments,
TemplateContext context
) {
return StringValue.Create(
string.Join("/", value.ToStringValue().Split('/').Select(Uri.UnescapeDataString))
);
}
}

Expand Down
Loading

0 comments on commit 746862c

Please sign in to comment.