Skip to content

Commit

Permalink
Merge branch 'sdavids-users/sdavids/feat/issue-49-year' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
adoble committed Apr 22, 2024
2 parents 7a83c38 + 0a59e0a commit 9e943b9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and from version 4.0.0 this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

* Added year as template attribute (see [issue 49])(https://github.com/adoble/adr-j/issues/49). Many thanks to [Sebastian Davis](https://github.com/sdavids) for the PR.

## [3.3.0]

### Added
Expand Down
2 changes: 2 additions & 0 deletions doc/man/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ List of the currently set properties.

`author` Change the default name of the ADR author.

`authorEmail` Change the default email of the ADR author.

`dateFormat` Change how the dates in the ADRs are represented.

`docPath` Change the location where future ADRs are stored.
Expand Down
6 changes: 6 additions & 0 deletions doc/usage/Writing_Templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ Type: field

Description: The date the ADR was created

### `year`

Type: field

Description: The year the ADR was created

### `author`

Type: field
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/doble/adr/model/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.doble.adr.TemplateProvider;

public class Record {
private static final DateTimeFormatter YEAR_FORMATTER = DateTimeFormatter.ofPattern("yyyy");

private final Path docsPath; // Where the adr files are stored
private final Optional<String> template; // Using String type instead of Path as the default template is a resource.
// Resources are not correctly supported by Path.
Expand Down Expand Up @@ -135,6 +137,7 @@ public Path createPeristentRepresentation() throws ADRException {
.map(line -> line.replaceAll("\\{\\{author\\}\\}", author))
.map(line -> line.replaceAll("\\{\\{author.email\\}\\}", authorEmail))
.map(line -> line.replaceAll("\\{\\{date\\}\\}", dateFormatter.format(date)))
.map(line -> line.replaceAll("\\{\\{year\\}\\}", YEAR_FORMATTER.format(date)))
.filter(line -> !(line.contains("{{{link.id}}}") && linkFragments.size() == 0)) // Remove lines
// which will be
// blank
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/org/doble/adr/RecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,40 @@ public void testRecordConstructionWithGivenAuthorAndAuthorEmail() throws Excepti

assertEquals(expectedContents, actualContents);
}

@Test
@Order(12)
public void testRecordConstructionWithGivenAuthorAndAuthorEmailAndYear() throws Exception {
String expectedContents = """
<!--
SPDX-FileCopyrightText: © 2024 Andrew Doble <andrew.l.doble@gmail.com>
SPDX-License-Identifier: MIT
-->
# 69. ADR templates should support REUSE file header""";

// Build the record
Record record = new Record.Builder(docPath, dateFormatter)
.id(69)
.name("ADR templates should support REUSE file header")
.date(LocalDate.of(2024, 4, 12))
.author("Andrew Doble")
.authorEmail("andrew.l.doble@gmail.com")
.template("rsrc:template_with_REUSE_file_header.md")
.build();

record.createPeristentRepresentation();

// Check if the ADR file has been created
assertTrue(Files.exists(fileSystem.getPath("/test/0069-adr-templates-should-support-reuse-file-header.md")));

// Read in the file
Path adrFile = fileSystem.getPath("/test/0069-adr-templates-should-support-reuse-file-header.md");

String actualContents;
try (Stream<String> lines = Files.lines(adrFile)) {
actualContents = lines.collect(Collectors.joining("\n"));
}

assertEquals(expectedContents, actualContents);
}
}
5 changes: 5 additions & 0 deletions src/test/resources/template_with_REUSE_file_header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!--
SPDX-FileCopyrightText: © {{year}} {{author}} <{{author.email}}>
SPDX-License-Identifier: MIT
-->
# {{id}}. {{name}}

0 comments on commit 9e943b9

Please sign in to comment.