WGet.NET 4.4.1
A WinGet wrapper library for .Net.
Loading...
Searching...
No Matches
WinGetLinkBuilder.cs
1//--------------------------------------------------//
2// Created by basicx-StrgV //
3// https://github.com/basicx-StrgV/ //
4//--------------------------------------------------//
5using System;
8
9namespace WGetNET.Builder
10{
14 internal class WinGetLinkBuilder : WinGetObjectBuilder<WinGetLink?>
15 {
16 private string _entryName = string.Empty;
17 private string _rawContent = string.Empty;
18 private bool _hasShortenedContent = false;
19 private Uri? _url = null;
20
24 public WinGetLinkBuilder()
25 {
26 // Provide empty constructor
27 }
28
33 public void AddEntryName(string entryName)
34 {
35 _entryName = entryName;
36 }
37
45 public void AddRawContent(string rawContent)
46 {
47 _hasShortenedContent = CheckShortenedValue(rawContent);
48
49 if (_hasShortenedContent)
50 {
51 // Remove the char at the end of the shortened content.
52#if NETCOREAPP3_1_OR_GREATER
53 _rawContent = rawContent[..^1];
54#elif NETSTANDARD2_0
55 _rawContent = rawContent.Remove(rawContent.Length - 1);
56#endif
57 }
58 else
59 {
60 _rawContent = rawContent;
61 }
62
63 SetUri(_rawContent, _hasShortenedContent);
64 }
65
72 public override WinGetLink? GetInstance()
73 {
74 if (_url == null)
75 {
76 return null;
77 }
78
79 return new WinGetLink(_entryName, _rawContent, _hasShortenedContent, _url);
80 }
81
83 public override void Clear()
84 {
85 _entryName = string.Empty;
86 _rawContent = string.Empty;
87 _hasShortenedContent = false;
88 _url = null;
89 }
90
96 private void SetUri(string rawContent, bool hasShortenedContent)
97 {
98 if (!hasShortenedContent)
99 {
100 Uri.TryCreate(rawContent, UriKind.Absolute, out Uri? uri);
101 _url = uri;
102 }
103
104 // Fallback for an incomplete uri.
105 Uri.TryCreate(PathHelper.TrimLastPathPart(rawContent, PathHelper.PathType.URI), UriKind.Absolute, out Uri? shortenedUri);
106 _url = shortenedUri;
107 }
108 }
109}
Represents a winget link in the info set.
Definition WinGetLink.cs:14