Example #1
0
        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());
                }
            }

            //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).
            //var settings = text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            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);
        }