-
Notifications
You must be signed in to change notification settings - Fork 2k
Add simple file-based app for rendering frequent announcement templates #6930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+243
−1
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f0edff7
Add Alpine floating tag announcement template
lbussell 03c3e4a
Use PascalCase template parameters
lbussell d8dbc2c
Make interactive prompts a static factory class for the template para…
lbussell 1fdae2a
Add simple command line argument support
lbussell 3984cf9
Move to eng dir
lbussell ec8b864
Fix DateOnly doc comment
lbussell 6ebe9ee
Remove unused variable in DisplayPatchTuesdayReferenceText method
lbussell d7b9b2c
Add helper for template context with better error handling
lbussell f4647eb
Add helper for checking supported template up front
lbussell b88ccd7
Rename FloatingTagTemplateParameters to AlpineFloatingTagTemplatePara…
lbussell 290abba
Ignore template files in markdown linter
lbussell f8c650d
Exclude announcement-templates directory from linkspector checks
lbussell 1f66e2a
Merge branch 'nightly' into announcement-templates
lbussell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| #!/usr/bin/env dotnet | ||
| #:package [email protected] | ||
| #:package [email protected] | ||
| #:package [email protected] | ||
|
|
||
| using System.CommandLine; | ||
| using Fluid; | ||
| using Spectre.Console; | ||
|
|
||
| var renderCommand = new RenderTemplateCommand(templateFileInfo => | ||
| { | ||
| if (!templateFileInfo.Exists) | ||
| { | ||
| Console.Error.WriteLine($"Template file not found: {templateFileInfo.FullName}"); | ||
| return 1; | ||
| } | ||
|
|
||
| if (!TemplateDefinitions.TemplateIsSupported(templateFileInfo)) | ||
| { | ||
| Console.Error.WriteLine($"Unsupported template file: {templateFileInfo.Name}"); | ||
| return 1; | ||
| } | ||
|
|
||
| // Parse the template first, so any errors are caught before prompting for input | ||
| var template = TemplateDefinitions.ParseTemplate(templateFileInfo, out var parseError); | ||
| if (template is null) | ||
| { | ||
| Console.Error.WriteLine($"Failed to parse template: {parseError}"); | ||
| return 1; | ||
| } | ||
|
|
||
| // Display some helpful reference information right before prompting the user for input. | ||
| DisplayPatchTuesdayReferenceText(); | ||
|
|
||
| // This will prompt the user for the template parameters. | ||
| TemplateContext context = TemplateDefinitions.GetTemplateContext(templateFileInfo); | ||
|
|
||
| // Finally, render the template with the provided parameters. | ||
| var result = template.Render(context); | ||
|
|
||
| AnsiConsole.WriteLine(); | ||
| AnsiConsole.Write(new Rule("[green]Generated Announcement[/]")); | ||
| AnsiConsole.WriteLine(); | ||
| Console.WriteLine(result); | ||
|
|
||
| return 0; | ||
| }); | ||
|
|
||
| var parseResult = renderCommand.Parse(args); | ||
| return parseResult.Invoke(); | ||
|
|
||
|
|
||
| static void DisplayPatchTuesdayReferenceText() | ||
| { | ||
| AnsiConsole.WriteLine(); | ||
| AnsiConsole.MarkupLine("[grey]Patch Tuesdays Reference:[/]"); | ||
| for (int i = -4; i <= 4; i++) | ||
| { | ||
| var pt = DateOnly.GetPatchTuesday(i); | ||
| var label = i == 0 ? "this month" : i.ToString("+0;-0"); | ||
| AnsiConsole.MarkupLine($"[grey] {label,11}: {pt:yyyy-MM-dd}[/]"); | ||
| } | ||
|
|
||
| AnsiConsole.WriteLine(); | ||
| } | ||
|
|
||
| static class TemplateDefinitions | ||
| { | ||
| // All supported templates and their associated context factories. | ||
| private static readonly Dictionary<string, Func<TemplateContext>> s_templateContexts = new() | ||
| { | ||
| ["alpine-floating-tag-update.md"] = AlpineFloatingTagTemplateParameters.ContextFactory, | ||
| }; | ||
|
|
||
| public static TemplateContext GetTemplateContext(FileInfo templateFileInfo) | ||
| { | ||
| var contextFactory = s_templateContexts[templateFileInfo.Name]; | ||
| var templateContext = contextFactory(); | ||
| return templateContext; | ||
| } | ||
|
|
||
| public static IFluidTemplate? ParseTemplate(FileInfo templateFile, out string? error) | ||
| { | ||
| var parser = new FluidParser(); | ||
| var templateText = File.ReadAllText(templateFile.FullName); | ||
|
|
||
| if (!parser.TryParse(templateText, out var template, out string? internalError)) | ||
| { | ||
| error = internalError; | ||
| return null; | ||
| } | ||
|
|
||
| error = null; | ||
| return template; | ||
| } | ||
|
|
||
| public static bool TemplateIsSupported(FileInfo templateFile) => | ||
| s_templateContexts.ContainsKey(templateFile.Name); | ||
| } | ||
|
|
||
| sealed class RenderTemplateCommand : RootCommand | ||
| { | ||
| public RenderTemplateCommand(Func<FileInfo, int> handler) : base("Render announcement template") | ||
| { | ||
| var templateFileArgument = new Argument<FileInfo>("templateFile") | ||
| { | ||
| Description = "The template file to read and display on the console", | ||
| }; | ||
| Arguments.Add(templateFileArgument); | ||
|
|
||
| SetAction(parseResult => | ||
| { | ||
| var templateFileResult = parseResult.GetValue(templateFileArgument); | ||
| if (parseResult.Errors.Count == 0 && templateFileResult is FileInfo validTemplateFile) | ||
| { | ||
| return handler(validTemplateFile); | ||
| } | ||
|
|
||
| if (parseResult.Errors.Count > 0) | ||
| { | ||
| foreach (var error in parseResult.Errors) | ||
| Console.Error.WriteLine(error.Message); | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| // Show help text | ||
| Parse("-h").Invoke(); | ||
|
|
||
| return 0; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| sealed record AlpineFloatingTagTemplateParameters( | ||
| string NewVersion, | ||
| string OldVersion, | ||
| DateTime PublishDate, | ||
| DateTime ReleaseDate, | ||
| DateTime EolDate, | ||
| string PublishDiscussionUrl, | ||
| string DotnetExampleVersion) | ||
| { | ||
| public static Func<TemplateContext> ContextFactory { get; } = () => | ||
| { | ||
| var model = PromptForInput(); | ||
| return new TemplateContext(model); | ||
| }; | ||
|
|
||
| public static AlpineFloatingTagTemplateParameters PromptForInput() | ||
| { | ||
| var newVersion = AnsiConsole.Prompt( | ||
| new TextPrompt<string>("New Alpine version:") | ||
| .DefaultValue("3.XX")); | ||
|
|
||
| var oldVersion = AnsiConsole.Prompt( | ||
| new TextPrompt<string>("Previous Alpine version:") | ||
| .DefaultValue("3.XX")); | ||
|
|
||
| var publishDate = AnsiConsole.Prompt( | ||
| new TextPrompt<DateOnly>($"When was Alpine {newVersion} published?") | ||
| .DefaultValue(DateOnly.GetPatchTuesday(-1))); | ||
|
|
||
| const string DiscussionQueryLink = "https://github.com/dotnet/dotnet-docker/discussions/categories/announcements?discussions_q=is%3Aopen+category%3AAnnouncements+alpine"; | ||
| var publishDiscussionUrl = AnsiConsole.Prompt( | ||
| new TextPrompt<string>($"Link to announcement for publishing Alpine {newVersion} images (see {DiscussionQueryLink}):")); | ||
|
|
||
| var releaseDate = AnsiConsole.Prompt( | ||
| new TextPrompt<DateOnly>($"When were floating tags moved from Alpine {oldVersion} to {newVersion}?") | ||
| .DefaultValue(DateOnly.GetPatchTuesday(0))); | ||
|
|
||
| var eolDate = AnsiConsole.Prompt( | ||
| new TextPrompt<DateOnly>($"When will we stop publishing Alpine {oldVersion} images?") | ||
| .DefaultValue(DateOnly.GetPatchTuesday(3))); | ||
|
|
||
| var dotnetExampleVersion = AnsiConsole.Prompt( | ||
| new TextPrompt<string>(".NET example version for tags:") | ||
| .DefaultValue("10.0")); | ||
|
|
||
| return new AlpineFloatingTagTemplateParameters( | ||
| newVersion, | ||
| oldVersion, | ||
| publishDate.ToDateTime(TimeOnly.MinValue), | ||
| releaseDate.ToDateTime(TimeOnly.MinValue), | ||
| eolDate.ToDateTime(TimeOnly.MinValue), | ||
| publishDiscussionUrl, | ||
| dotnetExampleVersion); | ||
| } | ||
| } | ||
|
|
||
| internal static class DateOnlyExtensions | ||
| { | ||
| extension(DateOnly date) | ||
| { | ||
| /// <summary> | ||
| /// Gets the Patch Tuesday (second Tuesday of the month) for a month | ||
| /// relative to the current month. | ||
| /// </summary> | ||
| /// <param name="offset"> | ||
| /// The number of months from the current month. | ||
| /// 0 = this month, 1 = next month, -3 = three months ago. | ||
| /// </param> | ||
| /// <returns>The date of Patch Tuesday for the target month.</returns> | ||
| public static DateOnly GetPatchTuesday(int offset = 0) | ||
| { | ||
| var today = DateOnly.FromDateTime(DateTime.Today); | ||
| var targetMonth = today.AddMonths(offset); | ||
| var firstOfMonth = new DateOnly(targetMonth.Year, targetMonth.Month, 1); | ||
|
|
||
| // Find the first Tuesday | ||
| var daysUntilTuesday = ((int)DayOfWeek.Tuesday - (int)firstOfMonth.DayOfWeek + 7) % 7; | ||
| var firstTuesday = firstOfMonth.AddDays(daysUntilTuesday); | ||
|
|
||
| // Second Tuesday is 7 days later | ||
| return firstTuesday.AddDays(7); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Alpine Floating Tags Updated to Alpine {{ NewVersion }} | ||
|
|
||
| In {{ PublishDate | date: "%B %Y" }}, [Alpine {{ NewVersion }} container images were published]({{ PublishDiscussionUrl }}). For today's {{ ReleaseDate | date: "%B %Y" }} .NET release, all Alpine floating tags now point to Alpine {{ NewVersion }} instead of Alpine {{ OldVersion }} according to our [tagging policy](https://github.com/dotnet/dotnet-docker/blob/main/documentation/supported-tags.md). | ||
|
|
||
| Per the [.NET Docker platform support policy](https://github.com/dotnet/dotnet-docker/blob/main/documentation/supported-platforms.md#linux), Alpine {{ OldVersion }} images will no longer be maintained starting on {{ EolDate | date: "%B %-d, %Y" }} (3 months after Alpine {{ NewVersion }} images were released). | ||
|
|
||
| ## Details | ||
|
|
||
| Please review the [Alpine {{ NewVersion }} changelog](https://alpinelinux.org/posts/Alpine-{{ NewVersion }}.0-released.html) for more details on changes that were made in this version of Alpine. | ||
|
|
||
| The affected floating tags use this naming pattern: | ||
|
|
||
| * `<version>-alpine` (e.g. `{{ DotnetExampleVersion }}-alpine`) | ||
| * `<version>-alpine-<variant>` (e.g. `{{ DotnetExampleVersion }}-alpine-extra`) | ||
| * `<version>-alpine-<arch>` (e.g. `{{ DotnetExampleVersion }}-alpine-amd64`) | ||
| * `<version>-alpine-<variant>-<arch>` (e.g. `{{ DotnetExampleVersion }}-alpine-extra-amd64`) | ||
|
|
||
| The following image repos have been updated: | ||
|
|
||
| * dotnet/sdk - [Microsoft Artifact Registry](https://mcr.microsoft.com/product/dotnet/sdk/about) | ||
| * dotnet/aspnet - [Microsoft Artifact Registry](https://mcr.microsoft.com/product/dotnet/aspnet/about) | ||
| * dotnet/runtime - [Microsoft Artifact Registry](https://mcr.microsoft.com/product/dotnet/runtime/about) | ||
| * dotnet/runtime-deps - [Microsoft Artifact Registry](https://mcr.microsoft.com/product/dotnet/runtime-deps/about) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.