Skip to content

Commit

Permalink
continue migration
Browse files Browse the repository at this point in the history
  • Loading branch information
m-skolnick committed Sep 18, 2023
1 parent 737a4a9 commit 85f9d63
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 148 deletions.
3 changes: 3 additions & 0 deletions asset/no_changed_dependencies_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div style="padding-left: 2em; padding-bottom: 1em">
No changed dependencies
</div>
162 changes: 37 additions & 125 deletions lib/src/changelog_builder/changelog_builder2.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -28,13 +27,17 @@ 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,
required this.changelogTemplate,
required this.depGroupTemplate,
required this.depChangedTemplate,
required this.depAddedOrRemovedTemplate,
required this.noChangedDependenciesTemplate,
});

Future<String> buildChangelogFromTemplates() async {
Expand All @@ -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,
Expand All @@ -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<String, PackageWrapper>? previous,
required MapEntry<String, PackageWrapper>? 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({
Expand All @@ -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 '''
<details>
<summary>$header</summary>
${_paddedDivStr(body: body)}
</details>
''';
}

String _paddedDivStr({required String body}) {
return '''
<div style="padding-left: 2em; padding-bottom: 1em">
$body
</div>
''';
}
}
37 changes: 14 additions & 23 deletions lib/src/changelog_builder/dependency_pair.dart
Original file line number Diff line number Diff line change
@@ -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)!;
}

0 comments on commit 85f9d63

Please sign in to comment.