Beispiel #1
0
        private List <Models.Type> LoadTypes(string basePath, Namespace ns)
        {
            string             nsFolder = Path.Combine(basePath, ns.Name);
            List <Models.Type> types    = new List <Models.Type>();

            foreach (var typeFile in ListFiles(nsFolder, "*.xml"))
            {
                try
                {
                    var t = LoadType(typeFile);
                    if (t != null)
                    {
                        t.Parent = ns;
                        types.Add(t);
                        if (typeFile.IsVirtual)
                        {
                            FallbackFiles.Add(typeFile.AbsolutePath);
                        }
                    }
                }
                catch (Exception ex)
                {
                    OPSLogger.LogUserError(LogCode.ECMA2Yaml_InternalError, typeFile.AbsolutePath, ex.ToString());
                    _errorFiles.Add(typeFile.AbsolutePath);
                }
            }
            return(types);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var opt = new CommandLineOptions();

            try
            {
                if (opt.Parse(args))
                {
                    if (!string.IsNullOrEmpty(opt.RepoRootPath))
                    {
                        OPSLogger.PathTrimPrefix = opt.RepoRootPath;
                    }
                    if (!opt.SDPMode)
                    {
                        OPSLogger.LogUserError(LogCode.ECMA2Yaml_SDP_MigrationNeeded, ".openpublishing.publish.config.json");
                    }
                    else
                    {
                        LoadAndConvert(opt);
                    }
                }
            }
            catch (Exception ex)
            {
                WriteLine(ex.ToString());
                OPSLogger.LogSystemError(LogCode.ECMA2Yaml_InternalError, null, ex.ToString());
            }
            finally
            {
                OPSLogger.Flush(opt.LogFilePath);
            }
        }
Beispiel #3
0
        public static void WriteCustomContentIfAny(string uid, Docs docs, string folder)
        {
            List <string> blocks = new List <string>();

            if (!string.IsNullOrEmpty(docs.ThreadSafety))
            {
                blocks.Add(GenerateOverwriteBlockForMarkup(uid, OPSMetadata.ThreadSafety, docs.ThreadSafety.TrimEnd()));
            }

            string fileName = null;

            if (blocks.Count > 0)
            {
                try
                {
                    fileName = Path.Combine(folder, TruncateUid(uid.Replace("*", "_")) + ".misc.md");
                    File.WriteAllLines(fileName, blocks);
                }
                catch (Exception ex)
                {
                    OPSLogger.LogUserError(LogCode.ECMA2Yaml_OverwriteMDFile_SaveFailed, null, $"{uid}: {ex}");
                    return;
                }
            }
        }
Beispiel #4
0
        public static (string, string) ParseCommentId(this string commentId)
        {
            var parts = commentId.Split(':');

            if (parts?.Length != 2)
            {
                OPSLogger.LogUserWarning(LogCode.ECMA2Yaml_CommentID_ParseFailed, null, commentId);
                return(null, null);
            }

            return(parts[0], parts[1]);
        }
Beispiel #5
0
        static Dictionary <string, Dictionary <string, object> > LoadFile(string path)
        {
            var rval    = new Dictionary <string, Dictionary <string, object> >();
            var text    = File.ReadAllText(path);
            var matches = YamlHeaderRegex.Matches(text);

            if (matches != null && matches.Count > 0)
            {
                foreach (Match match in matches)
                {
                    using (StringReader reader = new StringReader(match.Groups[1].Value))
                    {
                        Dictionary <string, object> result = null;
                        try
                        {
                            result = YamlUtility.Deserialize <Dictionary <string, object> >(reader);
                        }
                        catch (Exception ex)
                        {
                            OPSLogger.LogUserError(LogCode.ECMA2Yaml_YamlHeader_ParseFailed_WithException, path, ex);
                        }
                        if (result == null)
                        {
                            OPSLogger.LogUserError(LogCode.ECMA2Yaml_YamlHeader_ParseFailed, path, match.Value);
                        }
                        else if (!result.ContainsKey("uid"))
                        {
                            OPSLogger.LogUserError(LogCode.ECMA2Yaml_YamlHeader_ParseFailed, path, match.Value);
                        }
                        else
                        {
                            var uid = result["uid"].ToString();
                            result.Remove("uid");
                            if (rval.ContainsKey(uid))
                            {
                                OPSLogger.LogUserWarning(LogCode.ECMA2Yaml_Uid_Duplicated, path, uid);
                            }
                            else
                            {
                                rval[uid] = result;
                            }
                        }
                    }
                }
            }

            return(rval);
        }
Beispiel #6
0
        private TypedContent GetTypedContent(XElement ele, string filePath)
        {
            var cref = ele.Attribute("cref").Value;

            // Bug 211134: Ci should throw warning if exception cref is not prefixed with type (T:)
            if (cref.IndexOf(':') <= 0)
            {
                OPSLogger.LogUserWarning(LogCode.ECMA2Yaml_CrefTypePrefixMissing, filePath, cref, filePath);
            }
            return(new TypedContent
            {
                CommentId = cref,
                Description = NormalizeDocsElement(GetInnerXml(ele)),
                Uid = cref.Substring(cref.IndexOf(':') + 1).Replace('+', '.')
            });
        }
Beispiel #7
0
        private FrameworkIndex LoadFrameworks(string folder)
        {
            var            frameworkFolder = Path.Combine(folder, "FrameworksIndex");
            FrameworkIndex frameworkIndex  = new FrameworkIndex()
            {
                DocIdToFrameworkDict = new Dictionary <string, List <string> >(),
                FrameworkAssemblies  = new Dictionary <string, Dictionary <string, AssemblyInfo> >(),
                AllFrameworks        = new HashSet <string>()
            };

            foreach (var fxFile in ListFiles(frameworkFolder, "*.xml").OrderBy(f => Path.GetFileNameWithoutExtension(f.AbsolutePath)))
            {
                XDocument fxDoc  = XDocument.Load(fxFile.AbsolutePath);
                var       fxName = fxDoc.Root.Attribute("Name").Value;
                frameworkIndex.AllFrameworks.Add(fxName);
                foreach (var nsElement in fxDoc.Root.Elements("Namespace"))
                {
                    var ns = nsElement.Attribute("Name").Value;
                    frameworkIndex.DocIdToFrameworkDict.AddWithKey("N:" + ns, fxName);
                    foreach (var tElement in nsElement.Elements("Type"))
                    {
                        var t = tElement.Attribute("Id").Value;
                        frameworkIndex.DocIdToFrameworkDict.AddWithKey(t, fxName);
                        foreach (var mElement in tElement.Elements("Member"))
                        {
                            var m = mElement.Attribute("Id").Value;
                            frameworkIndex.DocIdToFrameworkDict.AddWithKey(m, fxName);
                        }
                    }
                }

                var assemblyNodes = fxDoc.Root.Element("Assemblies")?.Elements("Assembly")?.Select(ele => new AssemblyInfo()
                {
                    Name    = ele.Attribute("Name")?.Value,
                    Version = ele.Attribute("Version")?.Value
                }).ToList();
                if (assemblyNodes != null)
                {
                    frameworkIndex.FrameworkAssemblies.Add(fxName, assemblyNodes.ToDictionary(a => a.Name, a => a));
                }
                else
                {
                    OPSLogger.LogUserWarning(LogCode.ECMA2Yaml_Moniker_EmptyAssembly, null, fxFile.AbsolutePath);
                }
            }
            return(frameworkIndex);
        }
Beispiel #8
0
        private PackageInformationMapping LoadPackageInformationMapping(string folder)
        {
            var pkgInfoDir     = Path.Combine(folder, "PackageInformation");
            var pkgInfoMapping = new PackageInformationMapping();

            foreach (var file in ListFiles(pkgInfoDir, "*.json"))
            {
                try
                {
                    var mapping = JsonConvert.DeserializeObject <PackageInformationMapping>(_fileAccessor.ReadAllText(file.AbsolutePath));
                    pkgInfoMapping.Merge(mapping);
                }
                catch (Exception ex)
                {
                    OPSLogger.LogUserError(LogCode.ECMA2Yaml_PackageInformation_LoadFailed, file.AbsolutePath, ex);
                }
            }
            return(pkgInfoMapping);
        }
Beispiel #9
0
        public static void WriteOverload(Member overload, string folder)
        {
            var model = new Overload
            {
                Uid = overload.Uid
            };

            if (!string.IsNullOrEmpty(overload.Docs.Summary))
            {
                model.Summary = overload.Docs.Summary;
            }
            if (!string.IsNullOrEmpty(overload.Docs.Remarks))
            {
                model.Remarks = overload.Docs.Remarks;
            }
            if (!string.IsNullOrEmpty(overload.Docs.Examples))
            {
                model.Examples = new List <string> {
                    overload.Docs.Examples
                };
            }
            var fileContent = GenerateOverwriteBlock(model);

            string fileName = null;

            try
            {
                fileName = Path.Combine(folder, TruncateUid(overload.Uid.Replace("*", "_")) + ".md");
                File.WriteAllText(fileName, fileContent);
            }
            catch (Exception ex)
            {
                OPSLogger.LogUserError(LogCode.ECMA2Yaml_OverloadMDFile_SaveFailed, null, $"{overload.Uid}: {ex}");
                return;
            }
        }
Beispiel #10
0
        public ECMAStore LoadFolder(string sourcePath, bool isUWPMode = false)
        {
            //if (!System.IO.Directory.Exists(sourcePath))
            //{
            //    OPSLogger.LogUserWarning(string.Format("Source folder does not exist: {0}", sourcePath));
            //    return null;
            //}

            var filterStore      = LoadFilters(sourcePath);
            var typeMappingStore = LoadTypeMap(sourcePath);
            var pkgInfoMapping   = LoadPackageInformationMapping(sourcePath);

            ConcurrentBag <Namespace> namespaces = new ConcurrentBag <Namespace>();
            ParallelOptions           opt        = new ParallelOptions()
            {
                MaxDegreeOfParallelism = Environment.ProcessorCount
            };

            //foreach(var nsFile in ListFiles(sourcePath, "ns-*.xml"))
            Parallel.ForEach(ListFiles(sourcePath, "ns-*.xml"), opt, nsFile =>
            {
                var ns = LoadNamespace(sourcePath, nsFile);
                if (ns == null)
                {
                    OPSLogger.LogUserError(LogCode.ECMA2Yaml_Namespace_LoadFailed, nsFile.AbsolutePath);
                }
                else if (ns.Types == null)
                {
                    OPSLogger.LogUserWarning(LogCode.ECMA2Yaml_Namespace_NoTypes, nsFile.AbsolutePath);
                }
                else
                {
                    namespaces.Add(ns);
                    if (nsFile.IsVirtual)
                    {
                        FallbackFiles.Add(nsFile.AbsolutePath);
                    }
                }
            });

            if (namespaces.Count == 0)
            {
                return(null);
            }

            if (_errorFiles.Count > 0)
            {
                OPSLogger.LogUserError(LogCode.ECMA2Yaml_File_LoadFailed, null, _errorFiles.Count);
                return(null);
            }

            var frameworks = LoadFrameworks(sourcePath);

            if (frameworks == null || frameworks.DocIdToFrameworkDict.Count == 0)
            {
                OPSLogger.LogUserError(LogCode.ECMA2Yaml_Framework_NotFound, null, "any API, please check your FrameworkIndex folder");
                return(null);
            }

            var filteredNS = Filter(namespaces, filterStore, frameworks);
            var store      = new ECMAStore(filteredNS.OrderBy(ns => ns.Name).ToArray(), frameworks)
            {
                FilterStore      = filterStore,
                TypeMappingStore = isUWPMode ? typeMappingStore : null,
                PkgInfoMapping   = pkgInfoMapping,
                UWPMode          = isUWPMode
            };

            return(store);
        }
Beispiel #11
0
        private Models.Type LoadType(FileItem typeFile)
        {
            string xmlContent = _fileAccessor.ReadAllText(typeFile.RelativePath);

            xmlContent = xmlContent.Replace("TextAntiAliasingQuality&nbsp;property.</summary>", "TextAntiAliasingQuality property.</summary>");
            xmlContent = xmlContent.Replace("DefaultValue('&#x0;')</AttributeName>", "DefaultValue('\\0')</AttributeName>");
            xmlContent = xmlContent.Replace("\0", "\\0");

            XDocument tDoc  = XDocument.Parse(xmlContent, LoadOptions.PreserveWhitespace);
            XElement  tRoot = tDoc.Root;

            if (tRoot.Name.LocalName != "Type")
            {
                return(null);
            }
            Models.Type t = new Models.Type();
            t.Name                = tRoot.Attribute("Name").Value.Replace('+', '.');
            t.FullName            = tRoot.Attribute("FullName").Value.Replace('+', '.');
            t.SourceFileLocalPath = typeFile.AbsolutePath;

            //TypeSignature
            t.Signatures = new VersionedSignatures(tRoot.Elements("TypeSignature"));
            t.Modifiers  = t.Signatures.CombinedModifiers;
            t.DocId      = t.Signatures.DocId;

            //AssemblyInfo
            t.AssemblyInfo = tRoot.Elements("AssemblyInfo")?.SelectMany(a => ParseAssemblyInfo(a)).ToList();

            //TypeParameters
            var tpElement = tRoot.Element("TypeParameters");

            if (tpElement != null)
            {
                t.TypeParameters = ParameterBase.LoadVersionedParameters <TypeParameter>(tpElement.Elements("TypeParameter"));
            }

            //Parameters
            var pElement = tRoot.Element("Parameters");

            if (pElement != null)
            {
                t.Parameters = ParameterBase.LoadVersionedParameters <Parameter>(pElement.Elements("Parameter"));
            }

            var rvalElement = tRoot.Element("ReturnValue");

            if (rvalElement != null)
            {
                t.ReturnValueType = MonikerizeReturnValue(rvalElement);
            }

            //BaseTypeName
            t.BaseTypes = LoadBaseType(tRoot.Element("Base"));

            //Interfaces
            var interfacesElement = tRoot.Element("Interfaces");

            if (interfacesElement != null)
            {
                t.Interfaces = interfacesElement.Elements("Interface")
                               .Select(i => new VersionedString(LoadFrameworkAlternate(i), i?.Element("InterfaceName")?.Value))
                               .ToList();
            }

            //Attributes
            var attrs = tRoot.Element("Attributes");

            if (attrs != null)
            {
                t.Attributes = attrs.Elements("Attribute").Select(a => LoadAttribute(a)).ToList();
            }

            //TypeForwardingChain
            var forwardingChain = tRoot.Element("TypeForwardingChain");

            if (forwardingChain != null)
            {
                var fwds = forwardingChain.Elements("TypeForwarding").Select(fwd => LoadTypeForwarding(fwd)).ToList();
                t.TypeForwardingChain = new TypeForwardingChain(fwds);
            }

            //Members
            var membersElement = tRoot.Element("Members");

            if (membersElement != null)
            {
                t.Members = membersElement.Elements("Member")?.Select(m => LoadMember(t, m)).ToList();
                t.Members.Sort((m1, m2) =>
                {
                    if (m1.IsEII == m2.IsEII)
                    {
                        return(string.Compare(m1.Name, m2.Name));
                    }
                    else
                    {
                        return(m1.IsEII ? 1 : -1); //non-EII first, EII later
                    }
                });
                if (t.Members != null)
                {
                    foreach (var m in t.Members)
                    {
                        m.SourceFileLocalPath = typeFile.AbsolutePath;
                    }
                }
                t.Overloads = membersElement.Elements("MemberGroup")?.Select(m => LoadMemberGroup(t, m)).ToList();
                if (t.Overloads != null)
                {
                    var distinctList = new List <Member>();
                    foreach (var og in t.Overloads.GroupBy(o => o.Name))
                    {
                        if (og.Count() > 1)
                        {
                            OPSLogger.LogUserWarning(LogCode.ECMA2Yaml_MemberGroup_Duplicated, typeFile.AbsolutePath, og.Key);
                        }
                        og.First().SourceFileLocalPath = typeFile.AbsolutePath;
                        distinctList.Add(og.First());
                    }
                    t.Overloads = distinctList;
                }
            }

            //Docs
            t.Docs = LoadDocs(tRoot.Element("Docs"), typeFile.AbsolutePath);

            //MemberType
            t.ItemType = InferTypeOfType(t);

            if (t.ItemType == ItemType.Enum && t.Members?.Count > 0)
            {
                foreach (var m in t.Members)
                {
                    if (!string.IsNullOrEmpty(m.Docs.Remarks))
                    {
                        OPSLogger.LogUserSuggestion(LogCode.ECMA2Yaml_Enum_NoRemarks, typeFile.AbsolutePath);
                    }
                }
            }

            // Metadata
            LoadMetadata(t, tRoot);

            return(t);
        }
Beispiel #12
0
 private void PrintUsage()
 {
     OPSLogger.LogUserError(LogCode.ECMA2Yaml_Command_Invalid, null);
     Console.WriteLine("Usage: ECMA2Yaml.exe <Options>");
     _options.WriteOptionDescriptions(Console.Out);
 }