ILRepacking.Steps.SourceServerData.HttpSourceServerDescriptor.TryParse C# (CSharp) Method

TryParse() public static method

public static TryParse ( string rawSrcSrv, HttpSourceServerDescriptor &descriptor ) : bool
rawSrcSrv string
descriptor HttpSourceServerDescriptor
return bool
        public static bool TryParse(string rawSrcSrv, out HttpSourceServerDescriptor descriptor)
        {
            string currentSection = "";
            int version = 0;
            string versionControl = "";
            string target = "";
            var sources = new List<SourceFileDescriptor>();
            
            foreach (var line in rawSrcSrv.GetLines())
            {
                if (new[] { InitSection, VariablesSection, SourceFilesSection, EndSection }.Contains(line))
                {
                    currentSection = line;
                }
                else
                {
                    switch (currentSection)
                    {
                        case InitSection:
                        case VariablesSection:
                            var groups = VariablesRegex.Match(line).Groups;
                            var key = groups[1].Value;
                            var value = groups[2].Value;
                            switch (key)
                            {
                                case VersionKey:
                                    version = int.Parse(value);
                                    break;
                                case VersionControlKey:
                                    versionControl = value;
                                    break;
                                case TargetKey:
                                    target = value;
                                    break;
                            }
                            break;
                        case SourceFilesSection:
                            sources.Add(SourceFileDescriptor.Parse(line));
                            break;
                    }
                }
            }

            descriptor = new[] { "http", "https" }.Contains(versionControl)
                ? new HttpSourceServerDescriptor(version, versionControl, target, sources.ToArray())
                : null;
            return descriptor != null;
        }

Usage Example

        public void Perform()
        {
            var srcsrvValues = _assemblyFiles
                               .Select(PdbPath)
                               .Where(File.Exists)
                               .Select(_pdbStr.Read)
                               .ToArray();
            var descriptors = srcsrvValues.Select(srcsrv =>
            {
                HttpSourceServerDescriptor descriptor;
                return(new
                {
                    Valid = HttpSourceServerDescriptor.TryParse(srcsrv, out descriptor),
                    Descriptor = descriptor
                });
            })
                              .Where(tuple => tuple.Valid)
                              .Select(tuple => tuple.Descriptor)
                              .ToArray();

            _srcSrv = descriptors.Any()
                ? descriptors.First().MergeWith(descriptors.Skip(1)).ToString()
                : srcsrvValues.FirstOrDefault();
        }