|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025, 2025 Hannes Wellmann and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Hannes Wellmann - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | + |
| 15 | +final Locale LOCAL = Locale.ENGLISH; |
| 16 | +final DateTimeFormatter LONG = DateTimeFormatter.ofPattern("EEEE, MMMM d, yyyy", LOCAL); |
| 17 | +final DateTimeFormatter SHORT = DateTimeFormatter.ofPattern("MM/dd", LOCAL); |
| 18 | + |
| 19 | +private record SimRelDates(LocalDate M1, LocalDate M2, LocalDate M3, LocalDate RC1, LocalDate RC2, LocalDate GA) { |
| 20 | +} |
| 21 | + |
| 22 | +void main(String[] args) throws IOException { |
| 23 | + String simRelName = readSimRelName(args); |
| 24 | + Path templateFile = Path.of("wiki/SimRel/releaseDetails.template_md"); |
| 25 | + Path overviewFile = templateFile.resolveSibling(simRelName + ".md"); |
| 26 | + |
| 27 | + SimRelDates dates = parseDatesJSON(Path.of("wiki/SimRel/" + simRelName + "_dates.json")); |
| 28 | + performSanityChecks(simRelName, dates); |
| 29 | + |
| 30 | + String simRelMonthYear = DateTimeFormatter.ofPattern("MMMM yyyy", LOCAL).format(dates.GA); |
| 31 | + String previousSimRelName = DateTimeFormatter.ofPattern("yyyy-MM", LOCAL).format(dates.GA.minusMonths(3)); |
| 32 | + if (!Files.isRegularFile(overviewFile.resolveSibling(previousSimRelName + ".md"))) { |
| 33 | + throw new IllegalStateException("Previous SimRel " + previousSimRelName + " not found."); |
| 34 | + } |
| 35 | + var calendarDate = DateTimeFormatter.ofPattern("yyyyMMdd", LOCAL); |
| 36 | + String calendarPeriodStart = calendarDate.format(dates.M1.minusWeeks(2)); |
| 37 | + String calendarPeriodEnd = calendarDate.format(dates.GA.plusDays(1)); |
| 38 | + |
| 39 | + String content = Files.readString(templateFile); |
| 40 | + content = content.replace("${SIMREL_NAME}", simRelName) // |
| 41 | + .replace("${PREVIOUS_SIMREL_NAME}", previousSimRelName) // |
| 42 | + .replace("${SIMREL_FULL_MONTH_YEAR}", simRelMonthYear) |
| 43 | + .replace("${QUIET_PERIOD_START_SHORT}", SHORT.format(dates.RC2)) |
| 44 | + .replace("${QUIET_PERIOD_END_SHORT}", SHORT.format(dates.GA.minusDays(1))) |
| 45 | + .replace("${CALENDAR_PERIOD}", calendarPeriodStart + "%2F" + calendarPeriodEnd); |
| 46 | + content = resolveDateVariables(content, "M1", dates.M1); |
| 47 | + content = resolveDateVariables(content, "M2", dates.M2); |
| 48 | + content = resolveDateVariables(content, "M3", dates.M3); |
| 49 | + content = resolveDateVariables(content, "RC1", dates.RC1); |
| 50 | + content = resolveDateVariables(content, "RC2", dates.RC2); |
| 51 | + content = resolveDateVariables(content, "GA", dates.GA); |
| 52 | + |
| 53 | + Files.writeString(overviewFile, content); |
| 54 | + |
| 55 | + IO.println("Overview for " + simRelName + " generated successfully: " + overviewFile); |
| 56 | +} |
| 57 | + |
| 58 | +String readSimRelName(String[] args) { |
| 59 | + if (args.length < 1) { |
| 60 | + throw new IllegalArgumentException("Expected SimRel name argument is missing"); |
| 61 | + } |
| 62 | + String simRelName = args[0]; |
| 63 | + if (!Pattern.compile("(?<year>\\d{4})-(?<month>\\d{2})").matcher(simRelName).matches()) { |
| 64 | + throw new IllegalArgumentException("SimRel name has unexpected format: " + simRelName); |
| 65 | + } |
| 66 | + return simRelName; |
| 67 | +} |
| 68 | + |
| 69 | +SimRelDates parseDatesJSON(Path file) throws IOException { |
| 70 | + // Use built-in JSON parser once the JDK provides one |
| 71 | + String content = Files.readString(file).trim(); |
| 72 | + if (!content.startsWith("{") || !content.endsWith("}")) { |
| 73 | + throw new IllegalArgumentException("Unexpected content of" + file.getFileName()); |
| 74 | + } |
| 75 | + var jsonDateObject = Pattern.compile("\"(?<name>\\w+)\"\\s*:\\s*\"(?<date>[^\"]+)\"\\s*(,|})"); |
| 76 | + var dates = jsonDateObject.matcher(content).results() |
| 77 | + .collect(Collectors.toMap(m -> m.group("name"), m -> LocalDate.parse(m.group("date")))); |
| 78 | + return new SimRelDates(getDate(dates, "M1"), getDate(dates, "M2"), getDate(dates, "M3"), // |
| 79 | + getDate(dates, "RC1"), getDate(dates, "RC2"), getDate(dates, "GA")); |
| 80 | +} |
| 81 | + |
| 82 | +LocalDate getDate(Map<String, LocalDate> dates, String key) { |
| 83 | + return Objects.requireNonNull(dates.get(key), key + " date not specified"); |
| 84 | +} |
| 85 | + |
| 86 | +void performSanityChecks(String simRelName, SimRelDates dates) { |
| 87 | + var allDates = Arrays.stream(SimRelDates.class.getRecordComponents()).map(RecordComponent::getAccessor).map(m -> { |
| 88 | + try { |
| 89 | + return m.invoke(dates); |
| 90 | + } catch (IllegalAccessException | InvocationTargetException e) { |
| 91 | + throw new IllegalStateException(e); |
| 92 | + } |
| 93 | + }).map(LocalDate.class::cast).toList(); |
| 94 | + for (int i = 1; i < allDates.size(); i++) { |
| 95 | + if (allDates.get(i - 1).compareTo(allDates.get(i)) >= 0) { |
| 96 | + throw new IllegalArgumentException("Dates are not in strictly ascending order: " + allDates); |
| 97 | + } |
| 98 | + } |
| 99 | + if (!allDates.subList(0, allDates.size() - 1).stream().allMatch(d -> d.getDayOfWeek() == DayOfWeek.FRIDAY)) { |
| 100 | + throw new IllegalArgumentException("Not all milestones and RCs are Fridays: " + dates); |
| 101 | + } |
| 102 | + if (dates.GA.getDayOfWeek() != DayOfWeek.WEDNESDAY) { |
| 103 | + throw new IllegalArgumentException("GA release day is not a Wednesday: " + dates.GA); |
| 104 | + } |
| 105 | + if (!simRelName.equals(String.format("%d-%02d", dates.GA.getYear(), dates.GA.getMonthValue()))) { |
| 106 | + throw new IllegalArgumentException("GA date does not match the SimRel name (" + simRelName + "): " + dates.GA); |
| 107 | + } |
| 108 | + var milestoneLengths = Set.of(Period.ofWeeks(2), Period.ofWeeks(3), Period.ofWeeks(4)); |
| 109 | + if (!milestoneLengths.contains(dates.M1.until(dates.M2)) || !milestoneLengths.contains(dates.M2.until(dates.M3))) { |
| 110 | + // The number of weeks between M1 and M2 may be subject to adjustments in order |
| 111 | + // to adapt to years not having exactly 52 years and correspondingly increase or |
| 112 | + // decrease the offset of M1 from M2 as necessary. Usually both are three weeks |
| 113 | + // apart, just like M2 and M3 (which is usually not adapted). |
| 114 | + throw new IllegalArgumentException( |
| 115 | + "M1, M2 and M3 are further apart than usual: " + dates.M1 + ", " + dates.M2 + ", " + dates.M3); |
| 116 | + } |
| 117 | + if (dates.M3.until(dates.RC1, ChronoUnit.DAYS) != 7 || dates.RC1.until(dates.RC2, ChronoUnit.DAYS) != 7) { |
| 118 | + throw new IllegalArgumentException("M3, RC1 and RC2 are not in strictly one week apart: " + dates.M3 + ", " |
| 119 | + + dates.RC1 + ", " + dates.RC2); |
| 120 | + } |
| 121 | + if (dates.RC2.until(dates.GA, ChronoUnit.DAYS) >= 7) { |
| 122 | + throw new IllegalArgumentException( |
| 123 | + "RC2 and GA are not in strictly within one week: " + dates.RC2 + ", " + dates.GA); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +String resolveDateVariables(String str, String name, LocalDate date) { |
| 128 | + return str.replace("${" + name + "_DATE_LONG}", LONG.format(date)) |
| 129 | + .replace("${" + name + "_START_SHORT}", SHORT.format(date.minusDays(7))) |
| 130 | + .replace("${" + name + "_END_SHORT}", SHORT.format(date)); |
| 131 | +} |
0 commit comments