package com.ankurm.dbmigrations; import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.time.Instant; /** * Writes a named transcript under {@code docs/output/} while the test that produced it runs. * Every console-line quoted in the companion article for this module comes from one of these * files, and the file is only ever written by a test assertion — never hand-typed. * * See docs/15-production-checklist.md for how this module's real output is regenerated. */ public final class Transcript { private static final Path OUTPUT_DIR = Paths.get("docs", "output"); private final StringBuilder buffer = new StringBuilder(); private final String name; private Transcript(String name) { this.name = name; } public static Transcript start(String name, String heading) { Transcript t = new Transcript(name); t.line("=".repeat(Math.min(78, heading.length() + 4))); t.line(heading); t.line("=".repeat(Math.min(78, heading.length() + 4))); t.line("captured: " + Instant.now()); t.line(""); return t; } public Transcript line(String text) { buffer.append(text).append(System.lineSeparator()); System.out.println(text); return this; } public Transcript section(String title) { line(""); line("-- " + title + " --"); return this; } public void write() { try { Files.createDirectories(OUTPUT_DIR); Path target = OUTPUT_DIR.resolve(name + ".txt"); Files.writeString(target, buffer.toString(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); } catch (IOException ex) { throw new UncheckedIOException(ex); } } }