From 85f9d63572ecada745ca64bc0c3ee9935baf5af0 Mon Sep 17 00:00:00 2001 From: Micaiah Skolnick Date: Mon, 18 Sep 2023 17:30:42 -0400 Subject: [PATCH] continue migration --- asset/no_changed_dependencies_template.html | 3 + .../changelog_builder/changelog_builder2.dart | 162 ++++-------------- .../changelog_builder/dependency_pair.dart | 37 ++-- 3 files changed, 54 insertions(+), 148 deletions(-) create mode 100644 asset/no_changed_dependencies_template.html diff --git a/asset/no_changed_dependencies_template.html b/asset/no_changed_dependencies_template.html new file mode 100644 index 0000000..f0f67ee --- /dev/null +++ b/asset/no_changed_dependencies_template.html @@ -0,0 +1,3 @@ +
+No changed dependencies +
\ No newline at end of file diff --git a/lib/src/changelog_builder/changelog_builder2.dart b/lib/src/changelog_builder/changelog_builder2.dart index 7e8229e..06d9467 100644 --- a/lib/src/changelog_builder/changelog_builder2.dart +++ b/lib/src/changelog_builder/changelog_builder2.dart @@ -1,11 +1,10 @@ import 'dart:io'; import 'package:changelog_bubbler/src/change_manager.dart'; +import 'package:changelog_bubbler/src/change_type.dart'; import 'package:path/path.dart' as p; import 'package:changelog_bubbler/src/changelog_builder/dependency_pair.dart'; -import 'package:changelog_bubbler/src/dependency_type.dart'; -import 'package:changelog_bubbler/src/package_wrapper.dart'; import 'package:changelog_bubbler/src/templates/template_manager.dart'; class ChangelogBuilder2 { @@ -28,6 +27,9 @@ class ChangelogBuilder2 { /// Lowest level template used for templating an added or deleted dependency final TemplateManager depAddedOrRemovedTemplate; + /// Used when the app update had no dependency changes + final TemplateManager noChangedDependenciesTemplate; + ChangelogBuilder2({ required this.changeManager, required this.changelogName, @@ -35,6 +37,7 @@ class ChangelogBuilder2 { required this.depGroupTemplate, required this.depChangedTemplate, required this.depAddedOrRemovedTemplate, + required this.noChangedDependenciesTemplate, }); Future buildChangelogFromTemplates() async { @@ -54,10 +57,15 @@ class ChangelogBuilder2 { String _buildGroupsFromTemplate() { final stringBuffer = StringBuffer(); - for (final group in changeManager.groups) { + + if (changeManager.groups.isEmpty) { + return noChangedDependenciesTemplate.unalteredTemplate; + } + + for (final group in changeManager.groups.entries) { final dependencyListBuffer = StringBuffer(); for (final depPair in group.value) { - dependencyListBuffer.write(''); + dependencyListBuffer.write(_buildInvidualDepFromTemplate(depPair)); } final substitutedTemplate = depGroupTemplate.replaceAll({ '{{group_name}}': group.key, @@ -70,49 +78,33 @@ class ChangelogBuilder2 { return stringBuffer.toString(); } - String _buildMainAppSection() { - return _mainPackageDiffStr( - packageName: previous.pubspec.name, - previousVersion: previous.pubspec.version.toString(), - currentVersion: current.pubspec.version.toString(), - changelogDiff: _getChangelogDiff( - currentPath: current.repoPath, - previousPath: previous.repoPath, - ), - ); - } - - String _writeDepChangeTemplate({ - required MapEntry? previous, - required MapEntry? current, - }) { - assert(previous != null || current != null, - 'Either previous or current must not be null'); - if (previous == null) { - return _depAddedOrRemovedStr( - packageName: current!.key, - dependencyType: current.value.dependencyType.name, - changeType: 'ADDED', - ); - } - if (current == null) { - return _depAddedOrRemovedStr( - packageName: previous.key, - dependencyType: previous.value.dependencyType.name, - changeType: 'REMOVED', - ); + String _buildInvidualDepFromTemplate(DependencyPair depPair) { + final current = depPair.current; + final previous = depPair.previous; + + if (current != null && previous != null) { + return depChangedTemplate.replaceAll({ + '{{package_name}}': depPair.name, + '{{previous_version}}': previous.version.toString(), + '{{current_version}}': current.version.toString(), + '{{dependency_type}}': depPair.dependencyType.name, + '{{changelog_diff}}': _getChangelogDiff( + previousPath: previous.getPubCachePath(), + currentPath: current.getPubCachePath(), + ), + }); } - return _depDiffStr( - packageName: previous.key, - previousVersion: previous.value.version.toString(), - currentVersion: current.value.version.toString(), - dependencyType: current.value.dependencyType.name, - changelogDiff: _getChangelogDiff( - previousPath: previous.value.getPubCachePath(), - currentPath: current.value.getPubCachePath(), - ), - ); + switch (depPair.changeType) { + case ChangeType.added: + case ChangeType.removed: + return depAddedOrRemovedTemplate.replaceAll({ + '{{package_name}}': depPair.name, + '{{change_type}}': depPair.changeType.name, + '{{dependency_type}}': depPair.dependencyType.name, + }); + default: + } } String _getChangelogDiff({ @@ -134,84 +126,4 @@ class ChangelogBuilder2 { return diff; } - - String _fullOutputStr({ - required String mainApp, - required String dependencyGroups, - }) { - return ''' -$mainApp - -## Changed Dependencies -$dependencyGroups -'''; - } - - String _groupStr({ - required String groupName, - required String packagesInGroup, - }) { - return _collapsibleStr( - header: groupName, - body: packagesInGroup, - ); - } - - String _emptyDependencyStr() { - return _paddedDivStr(body: 'No changed dependencies'); - } - - String _mainPackageDiffStr({ - required String packageName, - required String previousVersion, - required String currentVersion, - required String changelogDiff, - }) { - return _collapsibleStr( - header: '$packageName | $previousVersion -> $currentVersion', - body: changelogDiff, - ); - } - - String _depDiffStr({ - required String packageName, - required String previousVersion, - required String currentVersion, - required String dependencyType, - required String changelogDiff, - }) { - return _collapsibleStr( - header: - '$packageName | $previousVersion -> $currentVersion | $dependencyType', - body: changelogDiff, - ); - } - - String _depAddedOrRemovedStr({ - required String packageName, - required String changeType, - required String dependencyType, - }) { - return '$packageName | $changeType | $dependencyType'; - } - - String _collapsibleStr({ - required String header, - required String body, - }) { - return ''' -
- $header - ${_paddedDivStr(body: body)} -
-'''; - } - - String _paddedDivStr({required String body}) { - return ''' -
- $body -
-'''; - } } diff --git a/lib/src/changelog_builder/dependency_pair.dart b/lib/src/changelog_builder/dependency_pair.dart index 9f5b532..a4e1f26 100644 --- a/lib/src/changelog_builder/dependency_pair.dart +++ b/lib/src/changelog_builder/dependency_pair.dart @@ -1,38 +1,29 @@ -import 'dart:io'; - +import 'package:changelog_bubbler/src/change_type.dart'; import 'package:changelog_bubbler/src/dependency_type.dart'; import 'package:changelog_bubbler/src/package_wrapper.dart'; -import 'package:path/path.dart' as p; class DependencyPair { final PackageWrapper? previous; final PackageWrapper? current; - // TODO add CHANGETYPE here - // eg added/removed/updated DependencyPair({ this.previous, this.current, - }); - - String? get trimmedUrl => previous?.trimmedUrl ?? current?.trimmedUrl; - DependencyType get dependencyType => - (previous?.dependencyType ?? current?.dependencyType)!; - - String getChangelogDiff() { - final previousChangelog = File(p.join(previous?.repoPath, changelogName)); - final currentChangelog = File(p.join(currentPath, changelogName)); + }) : assert(previous != null || current != null, + 'Either previous or current must not be null'); - if (!previousChangelog.existsSync() || !currentChangelog.existsSync()) { - return '$changelogName not found'; + ChangeType get changeType { + if (previous == null) { + return ChangeType.added; } - final previousString = previousChangelog.readAsStringSync(); - final currentString = currentChangelog.readAsStringSync(); - final diff = currentString.replaceFirst(previousString, ''); - if (diff.isEmpty) { - return '$changelogName did not contain changes'; + if (current == null) { + return ChangeType.removed; } - - return diff; + return ChangeType.updated; } + + String get name => (previous?.name ?? current?.name)!; + String? get trimmedUrl => previous?.trimmedUrl ?? current?.trimmedUrl; + DependencyType get dependencyType => + (previous?.dependencyType ?? current?.dependencyType)!; }