Пример #1
0
        /// <summary>
        /// Processes the copied files recursively.
        /// </summary>
        /// <param name="baseDir">The base dir.</param>
        /// <param name="baseUrl">The base URL.</param>
        /// <param name="baseEnUsUrl">The base en-us URL.</param>
        /// <param name="baseWoExtUrl">The base URL for the en-us links without .md extension.</param>
        /// <param name="externalText">The external text.</param>
        /// <param name="enRepository">The en repository.</param>
        /// <param name="copiedFiles">The copied files.</param>
        private static void ProcessCopiedFiles(string baseDir, string baseUrl, string baseEnUsUrl, string baseWoExtUrl, string externalText, string enRepository, ImmutableArray <string> copiedFiles)
        {
            foreach (string file in copiedFiles)
            {
                string filePath = Path.Combine(baseDir, file.TrimStart('~').TrimStart('\\').TrimStart('/'));
                if (!Path.GetExtension(filePath).Equals(".md", StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }
                string            content    = File.ReadAllText(filePath);
                StringBuilder     newContent = new StringBuilder();
                LinkProcessorBase proc       = new ConceptualLinkProcessor(Logger, baseDir, baseUrl, baseEnUsUrl, baseWoExtUrl, externalText, file.TrimStart('~').TrimStart('\\').TrimStart('/'), enRepository, content, newContent);
                bool hasModified             = proc.ProcessContentLinks();

                if (hasModified)
                {
                    File.WriteAllText(filePath, newContent.ToString());
                }

                AppendTextLog(LogType.NotExistingFiles, proc.GetLogContent(LogType.NotExistingFiles));
                AppendTextLog(LogType.NormalFiles, proc.GetLogContent(LogType.NormalFiles));
                AppendTextLog(LogType.CopiedFiles, proc.GetLogContent(LogType.CopiedFiles));
                AppendTextLog(LogType.ReplacedLinks, proc.GetLogContent(LogType.ReplacedLinks));
                AppendTextLog(LogType.ReplacedEnUsLinks, proc.GetLogContent(LogType.ReplacedEnUsLinks));
                AppendRemovedFiles(proc.GetFilesToRemove());
                AppendIgnoredFiles(proc.GetFilesToIgnore());

                if (proc.GetCopiedFiles().Length > 0)
                {
                    ProcessCopiedFiles(baseDir, baseUrl, baseEnUsUrl, baseWoExtUrl, externalText, enRepository, proc.GetCopiedFiles());
                }
            }
        }
Пример #2
0
        /// <summary>
        /// The entry point.
        /// </summary>
        /// <param name="parameters"></param>
        private static void RunMain(CommandLineArguments parameters)
        {
            InitialRun(parameters);

            if (!parameters.DoNotClone)
            {
                CheckFreeSpace(parameters.Repo, parameters.Out);
            }

            string clonedRepoPath = Path.Combine(parameters.Out, ".git");

            if (!parameters.DoNotClone)
            {
                clonedRepoPath = CloneRepository(parameters.Repo, parameters.Out);
            }

            string enClonedRepoPath = null;

            if (!String.IsNullOrEmpty(parameters.EnOut))
            {
                enClonedRepoPath = Path.Combine(parameters.EnOut, ".git");
                if (!String.IsNullOrEmpty(parameters.EnRepo))
                {
                    enClonedRepoPath = CloneRepository(parameters.EnRepo, parameters.EnOut);
                }
            }

            if (!RepoHelper.TryToCheckClonedRepo(parameters.Out, parameters.Json, out string pathToDocFxJson))
            {
                Logger.LogError($"{Error}: File {pathToDocFxJson} does not exist");
                Exit(ExitCodeEnum.InvalidRepo);
            }

            string pathToEnDocFxJson = null;

            if (enClonedRepoPath != null && !RepoHelper.TryToCheckClonedRepo(parameters.EnOut, parameters.Json, out pathToEnDocFxJson))
            {
                Logger.LogError($"{Error}: File {pathToEnDocFxJson} does not exist in en-US repository");
                Exit(ExitCodeEnum.InvalidRepo);
            }

            string repoLocalRoot     = Path.GetDirectoryName(pathToDocFxJson);
            string repoEnUsLocalRoot = Path.GetDirectoryName(pathToEnDocFxJson);

            if (!DocFxJsonHelper.IsDocFxJsonCorrect(pathToDocFxJson))
            {
                Logger.LogError(DocFxJsonIncorrect);
                Exit(ExitCodeEnum.InvalidRepo);
            }

            FilesCollector filesCollector = new FilesCollector(Logger);

            Logger.LogInfo("Collecting files, it may take awhile...");
            string[] files = filesCollector.FindAllFiles(repoLocalRoot, "*.md|*.yml", (fname, count) =>
            {
                Console.SetCursorPosition(0, Console.CursorTop);
                Console.CursorVisible = false;
                string file           = Path.GetFileName(fname);
                if (file != null && file.Length > 30)
                {
                    file = file.Substring(file.Length - 30, 30);
                }
                Console.Write($@"Found {count} files: {file}");
            });
            Console.SetCursorPosition(0, Console.CursorTop);
            Console.Write(new char[80]);
            Console.SetCursorPosition(0, Console.CursorTop);
            Logger.LogInfo($"Found {files.Length} files.");

            Console.WriteLine(@"Normalizing files...");
            foreach (string f in files)
            {
                string content = File.ReadAllText(f);
                content = Regex.Replace(content, "ms.contentlocale:.*", "ms.contentlocale: " + parameters.Lng);
                File.WriteAllText(f, content);
            }

            string externalText = parameters.ExternalText;

            if (!String.IsNullOrEmpty(externalText))
            {
                externalText = " " + externalText;
            }

            if (!parameters.ReplaceUrl.EndsWith("/"))
            {
                parameters.ReplaceUrl = $"{parameters.ReplaceUrl}/";
            }

            string baseEnUsUrl  = parameters.ReplaceUrl;
            string baseWoExtUrl = null;

            if (Uri.TryCreate(parameters.ReplaceUrl, UriKind.Absolute, out var uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps))
            {
                baseWoExtUrl = $"{uriResult.Scheme}://{uriResult.Host}";
            }

            foreach (string f in files)
            {
                string            content    = File.ReadAllText(f);
                StringBuilder     newContent = new StringBuilder();
                LinkProcessorBase proc       = new ConceptualLinkProcessor(Logger, repoLocalRoot, parameters.ReplaceUrl, baseEnUsUrl, baseWoExtUrl, externalText, repoLocalRoot != null ? f.Replace(repoLocalRoot, "").TrimStart('\\') : "", repoEnUsLocalRoot, content, newContent);
                bool hasModified             = proc.ProcessContentLinks();
                if (hasModified)
                {
                    File.WriteAllText(f, newContent.ToString());
                }

                AppendTextLog(LogType.NotExistingFiles, proc.GetLogContent(LogType.NotExistingFiles));
                AppendTextLog(LogType.NormalFiles, proc.GetLogContent(LogType.NormalFiles));
                AppendTextLog(LogType.CopiedFiles, proc.GetLogContent(LogType.CopiedFiles));
                AppendTextLog(LogType.ReplacedLinks, proc.GetLogContent(LogType.ReplacedLinks));
                AppendTextLog(LogType.ReplacedEnUsLinks, proc.GetLogContent(LogType.ReplacedEnUsLinks));
                AppendRemovedFiles(proc.GetFilesToRemove());
                AppendIgnoredFiles(proc.GetFilesToIgnore());

                if (proc.GetCopiedFiles().Length > 0)
                {
                    ProcessCopiedFiles(repoLocalRoot, parameters.ReplaceUrl, baseEnUsUrl, baseWoExtUrl, externalText, repoEnUsLocalRoot, proc.GetCopiedFiles());
                }
            }

            LogFiles logFiles = new LogFiles(parameters.LogsDir);

            StringBuilder sb = new StringBuilder();

            foreach (string file in LRemovedFiles)
            {
                if (!LIgnoredFiles.Contains(file))
                {
                    sb.AppendLine(file);
                    string path = Path.Combine(repoLocalRoot, file);
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                }
            }
            WriteLog(sb.ToString(), logFiles.RemovedFilesLog, "The files which have been removed");

            StringBuilder sbIgnored = new StringBuilder();

            foreach (string file in LIgnoredFiles)
            {
                sbIgnored.AppendLine(file);
            }

            WriteLog(SbNormalFiles.ToString(), logFiles.NormalFilesLog, "The normal files");
            WriteLog(SbNotExistingFiles.ToString(), logFiles.NotExistentFilesLog, "Not Existing Link;Source File");
            WriteLog(SbCopiedFiles.ToString(), logFiles.CopiedFilesLog, "The files which have been copied from en-US repository");
            WriteLog(SbReplacedLinks.ToString(), logFiles.ReplacedLinksLog, "Source file,Link,Title,New Link,New Title");
            WriteLog(SbReplacedEnUsLinks.ToString(), logFiles.ReplacedLanguageLinksLog, "Source file,Link,Title,New Link,New Title");
            //WriteLog(sbIgnored.ToString(), "logs\\ignored files.txt", "The ignored files");

            string tempDocFxZipFile = SaveToTempFile(Properties.Resources.docfx, DocFxZip);
            string tempDocFxDir     = ExtractZip(tempDocFxZipFile, DocFxZip);

            CopyCustomPlugin(pathToDocFxJson);
            string pathToTemplateZip = SaveToTempFile(Properties.Resources.mstemplate, MsTemplateZip);
            string templateTempDir   = ExtractZip(pathToTemplateZip, MsTemplateZip);

            UtilityHelper.CopyDirectory(templateTempDir, Path.GetDirectoryName(pathToDocFxJson));

            if (String.IsNullOrEmpty(parameters.Lng))
            {
                parameters.Lng = "en-us";
            }
            Logger.LogInfo($"Setting language {parameters.Lng} for template...", false);
            SetLanguage(parameters.Lng, Path.GetDirectoryName(pathToDocFxJson));
            Logger.LogInfo(Completed);
            DocFxJsonHelper.ModifyDocfxJson(pathToDocFxJson, CustomPluginName, parameters.Rtl);

            string       docfxexe      = Path.Combine(tempDocFxDir, DocFxExe);
            int          exitCodeDocFx = RunDocFx(docfxexe, pathToDocFxJson);
            ExitCodeEnum exitCode      = ExitCodeEnum.Success;

            if (exitCodeDocFx != 0)
            {
                exitCode = ExitCodeEnum.DocFxError;
                Logger.LogError($"{Error}: exit code {exitCode}");
            }

            DocFxJsonHelper.RevertDocfxJson(pathToDocFxJson, CustomPluginName);
            RepoHelper.CleanRepo(parameters.RemoveGitFolder ? clonedRepoPath : null, pathToDocFxJson);
            string docfxDir = Path.GetDirectoryName(pathToDocFxJson);

            RemoveTemp(new[]
            {
                tempDocFxDir, tempDocFxZipFile, pathToTemplateZip, templateTempDir,
                (docfxDir != null ? Path.Combine(docfxDir, CustomPluginName) : null)
            });

            string consoleOutput = Logger.GetLogContent();

            File.WriteAllText(logFiles.ConsoleLog, consoleOutput);
            Exit(exitCode);
        }