Modify redirects generator to update a specified _.openpublishing.redirection_ file (#1181)

This commit is contained in:
Tris Shores
2021-10-21 12:55:42 -07:00
committed by GitHub
parent cb4acd590a
commit 090e480dcf
2 changed files with 34 additions and 3 deletions
+3 -1
View File
@@ -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:
+31 -2
View File
@@ -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<Document>(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