forked from grandnode/grandnode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginFileParser.cs
More file actions
171 lines (157 loc) · 6.74 KB
/
Copy pathPluginFileParser.cs
File metadata and controls
171 lines (157 loc) · 6.74 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Grand.Core.Plugins
{
/// <summary>
/// Plugin files parser
/// </summary>
public static class PluginFileParser
{
public static IList<string> ParseInstalledPluginsFile(string filePath)
{
//read and parse the file
if (!File.Exists(filePath))
return new List<string>();
var text = File.ReadAllText(filePath);
if (String.IsNullOrEmpty(text))
return new List<string>();
var lines = new List<string>();
using (var reader = new StringReader(text))
{
string str;
while ((str = reader.ReadLine()) != null)
{
if (String.IsNullOrWhiteSpace(str))
continue;
lines.Add(str.Trim());
}
}
return lines;
}
public static async Task SaveInstalledPluginsFile(IList<String> pluginSystemNames, string filePath)
{
string result = "";
foreach (var sn in pluginSystemNames)
result += string.Format("{0}{1}", sn, Environment.NewLine);
await File.WriteAllTextAsync(filePath, result);
await Task.CompletedTask;
}
public static PluginDescriptor ParsePluginDescriptionFile(string filePath)
{
var descriptor = new PluginDescriptor();
var text = File.ReadAllText(filePath);
if (String.IsNullOrEmpty(text))
return descriptor;
var settings = new List<string>();
using (var reader = new StringReader(text))
{
string str;
while ((str = reader.ReadLine()) != null)
{
if (String.IsNullOrWhiteSpace(str))
continue;
settings.Add(str.Trim());
}
}
foreach (var setting in settings)
{
var separatorIndex = setting.IndexOf(':');
if (separatorIndex == -1)
{
continue;
}
string key = setting.Substring(0, separatorIndex).Trim();
string value = setting.Substring(separatorIndex + 1).Trim();
switch (key)
{
case "Group":
descriptor.Group = value;
break;
case "FriendlyName":
descriptor.FriendlyName = value;
break;
case "SystemName":
descriptor.SystemName = value;
break;
case "Version":
descriptor.Version = value;
break;
case "SupportedVersions":
{
//parse supported versions
descriptor.SupportedVersions = value.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim())
.ToList();
}
break;
case "Author":
descriptor.Author = value;
break;
case "DisplayOrder":
{
int displayOrder;
int.TryParse(value, out displayOrder);
descriptor.DisplayOrder = displayOrder;
}
break;
case "FileName":
descriptor.PluginFileName = value;
break;
case "LimitedToStores":
{
//parse list of store IDs
foreach (var str1 in value.Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim()))
{
descriptor.LimitedToStores.Add(str1);
}
}
break;
default:
break;
}
}
return descriptor;
}
public static void SavePluginDescriptionFile(PluginDescriptor plugin)
{
if (plugin == null)
throw new ArgumentException("plugin");
//get the Description.txt file path
if (plugin.OriginalAssemblyFile == null)
throw new Exception(string.Format("Cannot load original assembly path for {0} plugin.", plugin.SystemName));
var filePath = Path.Combine(plugin.OriginalAssemblyFile.Directory.FullName, "Description.txt");
if (!File.Exists(filePath))
throw new Exception(string.Format("Description file for {0} plugin does not exist. {1}", plugin.SystemName, filePath));
var keyValues = new List<KeyValuePair<string, string>>();
keyValues.Add(new KeyValuePair<string, string>("Group", plugin.Group));
keyValues.Add(new KeyValuePair<string, string>("FriendlyName", plugin.FriendlyName));
keyValues.Add(new KeyValuePair<string, string>("SystemName", plugin.SystemName));
keyValues.Add(new KeyValuePair<string, string>("Version", plugin.Version));
keyValues.Add(new KeyValuePair<string, string>("SupportedVersions", string.Join(",", plugin.SupportedVersions)));
keyValues.Add(new KeyValuePair<string, string>("Author", plugin.Author));
keyValues.Add(new KeyValuePair<string, string>("DisplayOrder", plugin.DisplayOrder.ToString()));
keyValues.Add(new KeyValuePair<string, string>("FileName", plugin.PluginFileName));
if (plugin.LimitedToStores.Any())
{
var storeList = string.Join(",", plugin.LimitedToStores);
keyValues.Add(new KeyValuePair<string, string>("LimitedToStores", storeList));
}
var sb = new StringBuilder();
for (int i = 0; i < keyValues.Count; i++)
{
var key = keyValues[i].Key;
var value = keyValues[i].Value;
sb.AppendFormat("{0}: {1}", key, value);
if (i != keyValues.Count -1)
sb.Append(Environment.NewLine);
}
//save the file
File.WriteAllText(filePath, sb.ToString());
}
}
}