コード例 #1
0
ファイル: Program.cs プロジェクト: qinezh/MarkdownMigration
        private static void Main(string[] args)
        {
            ServicePointManager.DefaultConnectionLimit = 64;
            var opt = new CommandLineOptions();

            try
            {
                var repoReport = new RepoReport();

                if (opt.Parse(args))
                {
                    switch (opt.RunMode)
                    {
                    case CommandLineOptions.Mode.Migration:
                        //sample local cmd: -m -c "path\repo" -p "**.md" -e "**/toc.md" -l -rule "Xref"
                        var migrationRule = opt.Rule ?? MigrationRule.All;
                        Console.WriteLine($"Using Migration Rules: {JsonConvert.SerializeObject(migrationRule, new StringEnumConverter())}");

                        EnvironmentContext.FileAbstractLayerImpl = FileAbstractLayerBuilder.Default
                                                                   .ReadFromRealFileSystem(opt.WorkingFolder)
                                                                   .WriteToRealFileSystem(opt.WorkingFolder).Create();
                        var tool = new MarkdownMigrateTool(opt.WorkingFolder, opt.UseLegacyMode, migrationRule);
                        if (!string.IsNullOrEmpty(opt.FilePath))
                        {
                            var input  = opt.FilePath;
                            var output = opt.Output;
                            if (string.IsNullOrEmpty(output))
                            {
                                output = input;
                            }
                            tool.MigrateFile(input, output);
                        }
                        else if (opt.Patterns.Count > 0)
                        {
                            tool.MigrateFromPattern(opt.WorkingFolder, opt.Patterns, opt.ExcludePatterns, opt.Output);
                        }

                        ReportUtility.Save(opt.WorkingFolder, "report.json", opt.DocsetFolder);
                        break;

                    case CommandLineOptions.Mode.Diff:
                        //sample local cmd: -d -j "path\dfm,path\markdig" -dbp

                        if (opt.DiffBuildPackage)
                        {
                            opt.JsonReportFile = "_output/report.json";
                            if (File.Exists(opt.JsonReportFile))
                            {
                                File.Delete(opt.JsonReportFile);
                            }
                        }

                        //ExtractHtml
                        var jsonfolders = opt.JsonFolders.Split(',');
                        ExtractHtml.ExtractHtml.ExtractHtmlFromJson(jsonfolders, opt.DocsetFolder, opt.DiffBuildPackage);

                        //Diff html
                        List <string>     allFiles;
                        List <DiffResult> differentResult;
                        HtmlCompare.HtmlCompare.CompareHtmlFromFolder(jsonfolders[0] + "-html", jsonfolders[1] + "-html", out differentResult, out allFiles, opt.DiffBuildPackage);

                        //Update report.json
                        var docsetReport = new DocsetReport();
                        if (File.Exists(opt.JsonReportFile))
                        {
                            docsetReport = JsonConvert.DeserializeObject <DocsetReport>(File.ReadAllText(opt.JsonReportFile));
                        }
                        UpdateMigrationReportWithDiffResult(differentResult, allFiles, docsetReport, opt.JsonReportFile, opt.BasePath, opt.DiffBuildPackage);

                        if (opt.DiffBuildPackage)
                        {
                            var _repoReport = new RepoReport()
                            {
                                Docsets = new List <DocsetReport>()
                                {
                                    docsetReport
                                }
                            };
                            var _excelGenerater = new ExcelGenerater(_repoReport, "_output/report.xlsx", "", "");
                            _excelGenerater.GenerateExcel();
                        }

                        break;

                    case CommandLineOptions.Mode.GenerateExcel:
                        try
                        {
                            repoReport = JsonConvert.DeserializeObject <RepoReport>(File.ReadAllText(opt.JsonReportFile));
                        }
                        catch (Exception)
                        {
                            throw new Exception("json file is not valid.");
                        }
                        var reportName     = string.IsNullOrEmpty(repoReport.RepoName) ? "repo_report.xlsx" : repoReport.RepoName + ".xlsx";
                        var excelGenerater = new ExcelGenerater(repoReport, Path.Combine(Path.GetDirectoryName(opt.JsonReportFile), reportName), opt.GitRepoUrl, opt.Branch);
                        excelGenerater.GenerateExcel();
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }