From 090e480dcf81f8cf99be833e83cca9be95da9dcd Mon Sep 17 00:00:00 2001 From: Tris Shores <86677757+v-trisshores@users.noreply.github.com> Date: Thu, 21 Oct 2021 14:55:42 -0500 Subject: [PATCH] Modify redirects generator to update a specified _.openpublishing.redirection_ file (#1181) --- redirects_generator/readme.md | 4 +++- redirects_generator/redirects.cs | 33 ++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/redirects_generator/readme.md b/redirects_generator/readme.md index babe9ac..40f3ebe 100644 --- a/redirects_generator/readme.md +++ b/redirects_generator/readme.md @@ -1,4 +1,6 @@ -This tool uses the _definitions.json_ file to generate a _.openpublishing.redirection_ file. If a _.openpublishing.redirection_ file exists in the folder, it's updated and appended to. +This tool uses the _definitions.json_ file to generate a _.openpublishing.redirection_ file. If a _.openpublishing.redirection_ file exists in the folder, it's copied to the output folder and updated and appended to there. + +Pass in the file path of a _.openpublishing.redirection_ file, and it'll be updated in-place. e.g. redirs.exe "c:\repos\dotnet\docs-desktop\.openpublishing.redirection.json" The definitions generate two-way redirects between versioned content. The definition file is a json file that has some config at the start: diff --git a/redirects_generator/redirects.cs b/redirects_generator/redirects.cs index 962541c..b693037 100644 --- a/redirects_generator/redirects.cs +++ b/redirects_generator/redirects.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Text.Json; using System.Text.Json.Serialization; @@ -7,10 +8,22 @@ const string OutputFile = ".openpublishing.redirection.json"; JsonSerializerOptions options = new JsonSerializerOptions { - Converters = { new System.Text.Json.Serialization.JsonStringEnumConverter() }, - ReadCommentHandling = JsonCommentHandling.Skip + Converters = { new JsonStringEnumConverter() }, + ReadCommentHandling = JsonCommentHandling.Skip, + WriteIndented = true }; +// If a .openpublishing.redirection.json path is provided, +// then copy that file to the output folder. +if (args.Length == 1 && File.Exists(args[0])) +{ + // Get passed in .openpublishing.redirection.json path. + string srcRedirectFilePath = args[0]; + + // Copy .openpublishing.redirection.json to output folder. + File.Copy(srcRedirectFilePath, OutputFile, overwrite: true); +} + // Load the definitions Console.WriteLine("Loading 'definitions.json'"); Document document = JsonSerializer.Deserialize(System.IO.File.ReadAllText("definitions.json"), options); @@ -63,6 +76,22 @@ if (System.IO.File.Exists(OutputFile)) Console.WriteLine($"Writing '{OutputFile}'"); System.IO.File.WriteAllText(OutputFile, JsonSerializer.Serialize(redirects)); +// If a .openpublishing.redirection.json path is provided, +// then overwrite it with the formatted new content. +if (args.Length == 1 && File.Exists(args[0])) +{ + // Get passed in .openpublishing.redirection.json path. + string srcRedirectFilePath = args[0]; + + // Format new json content. + string formattedJson = JsonSerializer.Serialize(redirects, options); + formattedJson = formattedJson.Replace(" ", " "); // match existing indent. + formattedJson += "\r\n"; // add newline at end. + + // Save new content to source path. + File.WriteAllText(srcRedirectFilePath, formattedJson); +} + void CreateEntry(string sourceMoniker, string sourceUrl, string targetUrl) { // Opposite repo paths are used here according to moniker