PluginFileParser.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace GreenTree.Nachtragsmanagement.Core.Plugins
  8. {
  9. /// <summary>
  10. /// Plugin files parser
  11. /// </summary>
  12. public static class PluginFileParser
  13. {
  14. public static IList<string> ParseInstalledPluginsFile(string filePath)
  15. {
  16. //read and parse the file
  17. if (!File.Exists(filePath))
  18. return new List<string>();
  19. var text = File.ReadAllText(filePath);
  20. if (String.IsNullOrEmpty(text))
  21. return new List<string>();
  22. //Old way of file reading. This leads to unexpected behavior when a user's FTP program transfers these files as ASCII (\r\n becomes \n).
  23. //var lines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  24. var lines = new List<string>();
  25. using (var reader = new StringReader(text))
  26. {
  27. string str;
  28. while ((str = reader.ReadLine()) != null)
  29. {
  30. if (String.IsNullOrWhiteSpace(str))
  31. continue;
  32. lines.Add(str.Trim());
  33. }
  34. }
  35. return lines;
  36. }
  37. public static void SaveInstalledPluginsFile(IList<String> pluginSystemNames, string filePath)
  38. {
  39. string result = "";
  40. foreach (var sn in pluginSystemNames)
  41. result += string.Format("{0}{1}", sn, Environment.NewLine);
  42. File.WriteAllText(filePath, result);
  43. }
  44. public static PluginDescriptor ParsePluginDescriptionFile(string filePath)
  45. {
  46. var descriptor = new PluginDescriptor();
  47. var text = File.ReadAllText(filePath);
  48. if (String.IsNullOrEmpty(text))
  49. return descriptor;
  50. var settings = new List<string>();
  51. using (var reader = new StringReader(text))
  52. {
  53. string str;
  54. while ((str = reader.ReadLine()) != null)
  55. {
  56. if (String.IsNullOrWhiteSpace(str))
  57. continue;
  58. settings.Add(str.Trim());
  59. }
  60. }
  61. //Old way of file reading. This leads to unexpected behavior when a user's FTP program transfers these files as ASCII (\r\n becomes \n).
  62. //var settings = text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  63. foreach (var setting in settings)
  64. {
  65. var separatorIndex = setting.IndexOf(':');
  66. if (separatorIndex == -1)
  67. {
  68. continue;
  69. }
  70. string key = setting.Substring(0, separatorIndex).Trim();
  71. string value = setting.Substring(separatorIndex + 1).Trim();
  72. switch (key)
  73. {
  74. case "Group":
  75. descriptor.Group = value;
  76. break;
  77. case "FriendlyName":
  78. descriptor.FriendlyName = value;
  79. break;
  80. case "SystemName":
  81. descriptor.SystemName = value;
  82. break;
  83. case "Version":
  84. descriptor.Version = value;
  85. break;
  86. case "SupportedVersions":
  87. {
  88. //parse supported versions
  89. descriptor.SupportedVersions = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
  90. .Select(x => x.Trim())
  91. .ToList();
  92. }
  93. break;
  94. case "Author":
  95. descriptor.Author = value;
  96. break;
  97. case "DisplayOrder":
  98. {
  99. int displayOrder;
  100. int.TryParse(value, out displayOrder);
  101. descriptor.DisplayOrder = displayOrder;
  102. }
  103. break;
  104. case "FileName":
  105. descriptor.PluginFileName = value;
  106. break;
  107. case "LimitedToStores":
  108. {
  109. //parse list of store IDs
  110. foreach (var str1 in value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
  111. .Select(x => x.Trim()))
  112. {
  113. int storeId;
  114. if (int.TryParse(str1, out storeId))
  115. {
  116. descriptor.LimitedToStores.Add(storeId);
  117. }
  118. }
  119. }
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125. //nopCommerce 2.00 didn't have 'SupportedVersions' parameter
  126. //so let's set it to "2.00"
  127. if (descriptor.SupportedVersions.Count == 0)
  128. descriptor.SupportedVersions.Add("2.00");
  129. return descriptor;
  130. }
  131. public static void SavePluginDescriptionFile(PluginDescriptor plugin)
  132. {
  133. if (plugin == null)
  134. throw new ArgumentException("plugin");
  135. //get the Description.txt file path
  136. if (plugin.OriginalAssemblyFile == null)
  137. throw new Exception(string.Format("Cannot load original assembly path for {0} plugin.", plugin.SystemName));
  138. var filePath = Path.Combine(plugin.OriginalAssemblyFile.Directory.FullName, "Description.txt");
  139. if (!File.Exists(filePath))
  140. throw new Exception(string.Format("Description file for {0} plugin does not exist. {1}", plugin.SystemName, filePath));
  141. var keyValues = new List<KeyValuePair<string, string>>();
  142. keyValues.Add(new KeyValuePair<string, string>("Group", plugin.Group));
  143. keyValues.Add(new KeyValuePair<string, string>("FriendlyName", plugin.FriendlyName));
  144. keyValues.Add(new KeyValuePair<string, string>("SystemName", plugin.SystemName));
  145. keyValues.Add(new KeyValuePair<string, string>("Version", plugin.Version));
  146. keyValues.Add(new KeyValuePair<string, string>("SupportedVersions", string.Join(",", plugin.SupportedVersions)));
  147. keyValues.Add(new KeyValuePair<string, string>("Author", plugin.Author));
  148. keyValues.Add(new KeyValuePair<string, string>("DisplayOrder", plugin.DisplayOrder.ToString()));
  149. keyValues.Add(new KeyValuePair<string, string>("FileName", plugin.PluginFileName));
  150. if (plugin.LimitedToStores.Count > 0)
  151. {
  152. var storeList = "";
  153. for (int i = 0; i < plugin.LimitedToStores.Count; i++)
  154. {
  155. storeList += plugin.LimitedToStores[i];
  156. if (i != plugin.LimitedToStores.Count - 1)
  157. storeList += ",";
  158. }
  159. keyValues.Add(new KeyValuePair<string, string>("LimitedToStores", storeList));
  160. }
  161. var sb = new StringBuilder();
  162. for (int i = 0; i < keyValues.Count; i++)
  163. {
  164. var key = keyValues[i].Key;
  165. var value = keyValues[i].Value;
  166. sb.AppendFormat("{0}: {1}", key, value);
  167. if (i != keyValues.Count - 1)
  168. sb.Append(Environment.NewLine);
  169. }
  170. //save the file
  171. File.WriteAllText(filePath, sb.ToString());
  172. }
  173. }
  174. }