Example #1
0
        public static void Run(
            string xmlDirectory,
            string outputDirectory,
            string fallbackXmlDirectory    = null,
            string fallbackOutputDirectory = null,
            Action <LogItem> logWriter     = null,
            string logContentBaseDirectory = null,
            string sourceMapFilePath       = null,
            string publicGitRepoUrl        = null,
            string publicGitBranch         = null,
            ECMA2YamlRepoConfig config     = null)
        {
            if (xmlDirectory == null)
            {
                throw new ArgumentNullException(xmlDirectory);
            }
            if (!Directory.Exists(xmlDirectory))
            {
                throw new DirectoryNotFoundException($"{nameof(xmlDirectory)} {xmlDirectory} does not exist.");
            }
            if (outputDirectory == null)
            {
                throw new ArgumentNullException(outputDirectory);
            }
            if (!string.IsNullOrEmpty(fallbackXmlDirectory))
            {
                if (!Directory.Exists(fallbackXmlDirectory))
                {
                    throw new DirectoryNotFoundException($"{nameof(fallbackXmlDirectory)} {fallbackXmlDirectory} does not exist.");
                }
                if (string.IsNullOrEmpty(fallbackOutputDirectory))
                {
                    throw new ArgumentNullException(fallbackOutputDirectory,
                                                    $"{nameof(fallbackOutputDirectory)} cannot be empty if {nameof(fallbackXmlDirectory)} is present.");
                }
            }
            if (!string.IsNullOrEmpty(logContentBaseDirectory))
            {
                OPSLogger.PathTrimPrefix = logContentBaseDirectory.NormalizePath().AppendDirectorySeparator();
            }
            if (!string.IsNullOrEmpty(fallbackXmlDirectory))
            {
                OPSLogger.FallbackPathTrimPrefix = fallbackXmlDirectory.NormalizePath().AppendDirectorySeparator();
            }
            if (logWriter != null)
            {
                OPSLogger.WriteLogCallback = logWriter;
            }

            var        fileAccessor = new FileAccessor(xmlDirectory, fallbackXmlDirectory);
            ECMALoader loader       = new ECMALoader(fileAccessor);

            Console.WriteLine("Loading ECMAXML files...");
            var store = loader.LoadFolder("");

            if (store == null)
            {
                return;
            }

            Console.WriteLine("Building loaded files...");
            Console.WriteLine($"ECMA2YamlRepoConfig:{JsonConvert.SerializeObject(config)}");
            store.UWPMode = config?.UWP ?? false;
            store.Build();

            if (!string.IsNullOrEmpty(publicGitRepoUrl) && !string.IsNullOrEmpty(publicGitBranch))
            {
                store.TranlateContentSourceMeta(publicGitRepoUrl, publicGitBranch);
            }
            else
            {
                Console.WriteLine("Not enough information, unable to generate git url related metadata. -publicRepo {0}, -publicBranch {1}", publicGitRepoUrl, publicGitBranch);
            }

            var xmlYamlFileMapping = SDPYamlGenerator.Generate(store, outputDirectory, flatten: config?.Flatten ?? true, withVersioning: true);

            if (loader.FallbackFiles != null && loader.FallbackFiles.Any() && !string.IsNullOrEmpty(fallbackOutputDirectory))
            {
                if (!Directory.Exists(fallbackOutputDirectory))
                {
                    Directory.CreateDirectory(fallbackOutputDirectory);
                }
                foreach (var fallbackFile in loader.FallbackFiles)
                {
                    if (xmlYamlFileMapping.TryGetValue(fallbackFile, out var originalYamls))
                    {
                        foreach (var originalYaml in originalYamls)
                        {
                            var newYaml = originalYaml.Replace(outputDirectory, fallbackOutputDirectory);
                            File.Copy(originalYaml, newYaml, overwrite: true);
                            File.Delete(originalYaml);
                        }
                        xmlYamlFileMapping.Remove(fallbackFile);
                    }
                }
            }
            if (!string.IsNullOrEmpty(sourceMapFilePath))
            {
                WriteYamlXMLFileMap(sourceMapFilePath, xmlYamlFileMapping);
            }

            var toc = SDPTOCGenerator.Generate(store);
            var tocOutputDirectory = string.IsNullOrEmpty(config?.BatchId) ? outputDirectory : Path.Combine(outputDirectory, config.BatchId);

            if (!Directory.Exists(tocOutputDirectory))
            {
                Directory.CreateDirectory(tocOutputDirectory);
            }
            YamlUtility.Serialize(Path.Combine(tocOutputDirectory, "toc.yml"), toc, "YamlMime:TableOfContent");
        }
Example #2
0
        static void LoadAndConvert(CommandLineOptions opt)
        {
            var        rootPath     = Path.GetFullPath(opt.RepoRootPath ?? opt.SourceFolder);
            var        xmlFolder    = Path.GetFullPath(opt.SourceFolder).Replace(rootPath, "").Trim(Path.DirectorySeparatorChar);
            var        fileAccessor = new FileAccessor(rootPath, opt.FallbackRepoRoot);
            ECMALoader loader       = new ECMALoader(fileAccessor);

            WriteLine("Loading ECMAXML files...");
            var store = loader.LoadFolder(xmlFolder);

            if (store == null)
            {
                return;
            }
            store.StrictMode = opt.StrictMode;
            store.UWPMode    = opt.UWPMode;
            store.DemoMode   = opt.DemoMode;

            WriteLine("Building loaded files...");
            store.Build();
            if (OPSLogger.ErrorLogged)
            {
                return;
            }

            if (!string.IsNullOrEmpty(opt.RepoRootPath) &&
                !string.IsNullOrEmpty(opt.PublicRepoUrl) &&
                !string.IsNullOrEmpty(opt.PublicRepoBranch) &&
                !string.IsNullOrEmpty(opt.RepoUrl) &&
                !string.IsNullOrEmpty(opt.RepoBranch))
            {
                store.TranslateSourceLocation(opt.RepoRootPath, opt.RepoUrl, opt.RepoBranch, opt.PublicRepoUrl, opt.PublicRepoBranch);
            }
            else
            {
                WriteLine("Not enough information, unable to generate git url related metadata. -repoRoot {0}, -publicRepo {1}, -publicBranch {2}",
                          opt.RepoRootPath, opt.PublicRepoUrl, opt.PublicRepoBranch);
            }

            WriteLine("Loaded {0} namespaces.", store.Namespaces.Count);
            WriteLine("Loaded {0} types.", store.TypesByFullName.Count);
            WriteLine("Loaded {0} members.", store.MembersByUid.Count);
            WriteLine("Loaded {0} extension methods.", store.ExtensionMethodsByMemberDocId?.Values?.Count ?? 0);
            WriteLine("Loaded {0} attribute filters.", store.FilterStore?.AttributeFilters?.Count ?? 0);

            if (!string.IsNullOrEmpty(opt.UndocumentedApiReport))
            {
                UndocumentedApi.ReportGenerator.GenerateReport(store, opt.UndocumentedApiReport.NormalizePath(), opt.RepoBranch);
            }

            IDictionary <string, List <string> > xmlYamlFileMapping = null;

            if (opt.SDPMode)
            {
                xmlYamlFileMapping = SDPYamlGenerator.Generate(store, opt.OutputFolder, opt.Flatten, opt.Versioning);
                YamlUtility.Serialize(Path.Combine(opt.OutputFolder, "toc.yml"), SDPTOCGenerator.Generate(store), "YamlMime:TableOfContent");
            }

            //Translate change list
            if (opt.ChangeListFiles.Count > 0)
            {
                foreach (var changeList in opt.ChangeListFiles)
                {
                    if (File.Exists(changeList))
                    {
                        var count = ChangeListUpdater.TranslateChangeList(changeList, xmlYamlFileMapping);
                        WriteLine("Translated {0} file entries in {1}.", count, changeList);
                    }
                }
            }
            WriteYamlXMLFileMap(opt.YamlXMLMappingFile, opt.RepoRootPath, xmlYamlFileMapping);

            //Save fallback file list as skip publish
            if (!string.IsNullOrEmpty(opt.SkipPublishFilePath) && loader.FallbackFiles?.Count > 0)
            {
                List <string> list = new List <string>();
                if (File.Exists(opt.SkipPublishFilePath))
                {
                    var jsonContent = File.ReadAllText(opt.SkipPublishFilePath);
                    list = JsonConvert.DeserializeObject <List <string> >(jsonContent);
                    WriteLine("Read {0} entries in {1}.", list.Count, opt.SkipPublishFilePath);
                }
                list.AddRange(loader.FallbackFiles
                              .Where(path => xmlYamlFileMapping.ContainsKey(path))
                              .SelectMany(path => xmlYamlFileMapping[path].Select(p => p.Replace(opt.RepoRootPath, "").TrimStart('\\')))
                              );
                var jsonText = JsonConvert.SerializeObject(list, Formatting.Indented);
                File.WriteAllText(opt.SkipPublishFilePath, jsonText);
                WriteLine("Write {0} entries to {1}.", list.Count, opt.SkipPublishFilePath);
            }

            WriteLine("Done writing Yaml files.");
        }