WGet.NET 4.4.1
A WinGet wrapper library for .Net.
Loading...
Searching...
No Matches
WinGetDirectoryBuilder.cs
1//--------------------------------------------------//
2// Created by basicx-StrgV //
3// https://github.com/basicx-StrgV/ //
4//--------------------------------------------------//
5using System;
6using System.IO;
9
10namespace WGetNET.Builder
11{
15 internal class WinGetDirectoryBuilder : WinGetObjectBuilder<WinGetDirectory?>
16 {
17 private string _entryName = string.Empty;
18 private string _rawContent = string.Empty;
19 private bool _hasShortenedContent = false;
20 private DirectoryInfo? _directoryInfo = null;
21
25 public WinGetDirectoryBuilder()
26 {
27 // Provide empty constructor
28 }
29
34 public void AddEntryName(string entryName)
35 {
36 _entryName = entryName;
37 }
38
46 public void AddRawContent(string rawContent)
47 {
48 _hasShortenedContent = CheckShortenedValue(rawContent);
49
50 if (_hasShortenedContent)
51 {
52 // Remove the char at the end of the shortened content.
53#if NETCOREAPP3_1_OR_GREATER
54 _rawContent = rawContent[..^1];
55#elif NETSTANDARD2_0
56 _rawContent = rawContent.Remove(rawContent.Length - 1);
57#endif
58 }
59 else
60 {
61 _rawContent = rawContent;
62 }
63
64 SetDirectoryInfo(_rawContent, _hasShortenedContent);
65 }
66
73 public override WinGetDirectory? GetInstance()
74 {
75 if (_directoryInfo == null)
76 {
77 return null;
78 }
79
80 return new WinGetDirectory(_entryName, _rawContent, _hasShortenedContent, _directoryInfo);
81 }
82
84 public override void Clear()
85 {
86 _entryName = string.Empty;
87 _rawContent = string.Empty;
88 _hasShortenedContent = false;
89 _directoryInfo = null;
90 }
91
97 private void SetDirectoryInfo(string rawContent, bool hasShortenedContent)
98 {
99 try
100 {
101 string path = rawContent;
102
103#if NETCOREAPP3_1_OR_GREATER
104 if (path.StartsWith('%'))
105 {
106 path = Environment.ExpandEnvironmentVariables(path);
107 }
108#elif NETSTANDARD2_0
109 if (path.StartsWith("%"))
110 {
111 path = Environment.ExpandEnvironmentVariables(path);
112 }
113#endif
114
115 if (!hasShortenedContent)
116 {
117 _directoryInfo = new DirectoryInfo(path);
118 }
119
120 // Fallback for an incomplete directory path.
121 _directoryInfo = new DirectoryInfo(PathHelper.TrimLastPathPart(path, PathHelper.PathType.Directory));
122 }
123 catch
124 {
125 _directoryInfo = null;
126 }
127 }
128 }
129}
Represents a winget directory in the info set.