Skip to content

Commit

Permalink
feat: changelogFormat configuration, add includeDate boolean (#720)
Browse files Browse the repository at this point in the history
* feat: changelogFormat configuration, add includeDate boolean

* fix new line

* add test

* docs

* fix json

* fmt

* use intl

* internal annot

* eol
  • Loading branch information
mzdm authored Jun 5, 2024
1 parent 590cf90 commit fed343b
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 4 deletions.
19 changes: 18 additions & 1 deletion docs/configuration/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,21 @@ Whether to include commit bodies in the changelog. Defaults to `false`.

#### onlyBreaking

Whether to include only breaking changes in the changelog. Defaults to `true`.
Whether to include only breaking changes in the changelog. Defaults to `true`.

### changelogFormat

Configure the format of the generated CHANGELOG.md.

```yaml
command:
version:
changelogFormat:
includeDate: true
```

#### includeDate

Whether to include the date in the generated CHANGELOG.md. Defaults to `false`.

With enabled, changelog entry header will include the date in the `yyyy-MM-dd` format.
28 changes: 26 additions & 2 deletions packages/melos/lib/src/command_configs/version.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class VersionCommandConfigs {
List<AggregateChangelogConfig>? aggregateChangelogs,
this.fetchTags = true,
this.hooks = VersionLifecycleHooks.empty,
this.includeDateInChangelogEntry = false,
}) : _aggregateChangelogs = aggregateChangelogs;

factory VersionCommandConfigs.fromYaml(
Expand Down Expand Up @@ -156,6 +157,19 @@ class VersionCommandConfigs {
path: 'command/version/changelogCommitBodies',
);

final changelogFormat = assertKeyIsA<Map<Object?, Object?>?>(
key: 'changelogFormat',
map: yaml,
path: 'command/version',
) ??
const {};

final includeDate = assertKeyIsA<bool?>(
key: 'includeDate',
map: changelogFormat,
path: 'command/version/changelogFormat',
);

return VersionCommandConfigs(
branch: branch,
message: message,
Expand All @@ -169,6 +183,7 @@ class VersionCommandConfigs {
aggregateChangelogs: aggregateChangelogs,
fetchTags: fetchTags ?? true,
hooks: hooks,
includeDateInChangelogEntry: includeDate ?? false,
);
}

Expand Down Expand Up @@ -217,6 +232,9 @@ class VersionCommandConfigs {
/// Lifecycle hooks for this command.
final VersionLifecycleHooks hooks;

/// Whether to include the date in the changelog entry in format `yyyy-MM-dd`.
final bool includeDateInChangelogEntry;

Map<String, Object?> toJson() {
return {
if (branch != null) 'branch': branch,
Expand All @@ -229,6 +247,9 @@ class VersionCommandConfigs {
aggregateChangelogs.map((config) => config.toJson()).toList(),
'fetchTags': fetchTags,
'hooks': hooks.toJson(),
'changelogFormat': {
'includeDate': includeDateInChangelogEntry,
},
};
}

Expand All @@ -246,7 +267,8 @@ class VersionCommandConfigs {
const DeepCollectionEquality()
.equals(other.aggregateChangelogs, aggregateChangelogs) &&
other.fetchTags == fetchTags &&
other.hooks == hooks;
other.hooks == hooks &&
other.includeDateInChangelogEntry == includeDateInChangelogEntry;

@override
int get hashCode =>
Expand All @@ -260,7 +282,8 @@ class VersionCommandConfigs {
releaseUrl.hashCode ^
const DeepCollectionEquality().hash(aggregateChangelogs) ^
fetchTags.hashCode ^
hooks.hashCode;
hooks.hashCode ^
includeDateInChangelogEntry.hashCode;

@override
String toString() {
Expand All @@ -276,6 +299,7 @@ VersionCommandConfigs(
aggregateChangelogs: $aggregateChangelogs,
fetchTags: $fetchTags,
hooks: $hooks,
includeDateInChangelogEntry: $includeDateInChangelogEntry,
)''';
}
}
24 changes: 23 additions & 1 deletion packages/melos/lib/src/common/changelog.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:intl/intl.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';

Expand Down Expand Up @@ -93,9 +95,20 @@ extension MarkdownStringBufferExtension on StringBuffer {

extension ChangelogStringBufferExtension on StringBuffer {
void writePackageChangelog(MelosPendingPackageUpdate update) {
final config = update.workspace.config;
final includeDate = config.commands.version.includeDateInChangelogEntry;

// Changelog entry header.
write('## ');
writeln(update.nextVersion);
if (includeDate) {
final now = DateTime.now();

write(update.nextVersion);
write(' - ');
writeln(now.toFormattedString());
} else {
writeln(update.nextVersion);
}
writeln();

if (update.reason == PackageUpdateReason.dependency) {
Expand Down Expand Up @@ -230,3 +243,12 @@ extension on String {
});
}
}

extension DateTimeExt on DateTime {
/// Returns a formatted string in the format `yyyy-MM-dd`.
@internal
String toFormattedString() {
final format = DateFormat('yyyy-MM-dd');
return format.format(this);
}
}
1 change: 1 addition & 0 deletions packages/melos/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies:
glob: ^2.1.2
graphs: ^2.3.1
http: ^1.1.0
intl: ^0.19.0
meta: ^1.10.0
mustache_template: ^2.0.0
path: ^1.8.3
Expand Down
32 changes: 32 additions & 0 deletions packages/melos/test/changelog_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,35 @@ void main() {
);
});

group('includeDateInChangelogEntry', () {
test('should not include date by default', () {
final changelogEntryDate = DateTime.now().toFormattedString();

final workspace = buildWorkspaceWithRepository();
final package = workspace.allPackages['test_pkg']!;
final commit = testCommit(message: 'feat: a');

expect(
renderCommitPackageUpdate(workspace, package, commit),
isNot(contains(changelogEntryDate)),
);
});

test('should include date when enabled', () {
final changelogEntryDate = DateTime.now().toFormattedString();

final workspace =
buildWorkspaceWithRepository(includeDateInChangelogEntry: true);
final package = workspace.allPackages['test_pkg']!;
final commit = testCommit(message: 'feat: a');

expect(
renderCommitPackageUpdate(workspace, package, commit),
contains(changelogEntryDate),
);
});
});

test('when repository is specified, adds links to referenced issues/PRs', () {
final workspace = buildWorkspaceWithRepository();
final package = workspace.allPackages['test_pkg']!;
Expand All @@ -137,6 +166,7 @@ MelosWorkspace buildWorkspaceWithRepository({
bool includeScopes = false,
bool linkToCommits = false,
bool includeCommitId = false,
bool includeDateInChangelogEntry = false,
}) {
final workspaceBuilder = VirtualWorkspaceBuilder(
'''
Expand All @@ -146,6 +176,8 @@ MelosWorkspace buildWorkspaceWithRepository({
includeScopes: $includeScopes
includeCommitId: $includeCommitId
linkToCommits: $linkToCommits
changelogFormat:
includeDate: $includeDateInChangelogEntry
''',
)..addPackage(
'''
Expand Down

0 comments on commit fed343b

Please sign in to comment.