|
| 1 | +//.title |
| 2 | +// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ |
| 3 | +// |
| 4 | +// Dart/Flutter (DF) Packages by dev-cetera.com & contributors. The use of this |
| 5 | +// source code is governed by an MIT-style license described in the LICENSE |
| 6 | +// file located in this project's root directory. |
| 7 | +// |
| 8 | +// See: https://opensource.org/license/mit |
| 9 | +// |
| 10 | +// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ |
| 11 | +//.title~ |
| 12 | + |
| 13 | +import 'dart:io'; |
| 14 | + |
| 15 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 16 | + |
| 17 | +void main(List<String> args) { |
| 18 | + final version = args.isNotEmpty ? args[0] : '0.1.0'; |
| 19 | + final comitMesssage = args.length > 1 ? args[1].replaceFirst('+', '') : ''; |
| 20 | + final changelogPath = 'CHANGELOG.md'; |
| 21 | + final file = File(changelogPath); |
| 22 | + if (!file.existsSync()) { |
| 23 | + print('$changelogPath does not exist.'); |
| 24 | + exit(1); |
| 25 | + } |
| 26 | + var contents = file.readAsStringSync(); |
| 27 | + contents = contents.replaceAll('# Changelog', '').trim(); |
| 28 | + final sections = extractSections(contents); |
| 29 | + final versionExist = sections.where((e) => e.version == version).isNotEmpty; |
| 30 | + if (versionExist) { |
| 31 | + sections.where((e) => e.version == version).forEach((e) { |
| 32 | + e.addUpdate(comitMesssage); |
| 33 | + }); |
| 34 | + } else { |
| 35 | + sections.add( |
| 36 | + _VersionSection( |
| 37 | + version: version, |
| 38 | + releasedAt: DateTime.now().toUtc(), |
| 39 | + updates: {comitMesssage}, |
| 40 | + ), |
| 41 | + ); |
| 42 | + } |
| 43 | + contents = '# Changelog\n\n${(sections.toList()..sort((a, b) { |
| 44 | + return compareVersions(b.version, a.version); |
| 45 | + })).map((e) => e.toString()).join('\n')}'; |
| 46 | + |
| 47 | + file.writeAsStringSync(contents); |
| 48 | + print('Changelog updated with version $version.'); |
| 49 | +} |
| 50 | + |
| 51 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 52 | + |
| 53 | +Set<_VersionSection> extractSections(String contents) { |
| 54 | + final headerPattern = RegExp(r'## \[\d+\.\d+\.\d+(\+\d+)?\]'); |
| 55 | + final allVersionMatches = headerPattern.allMatches(contents).toList(); |
| 56 | + final results = <_VersionSection>{}; |
| 57 | + for (var i = 0; i < allVersionMatches.length; i++) { |
| 58 | + final start = allVersionMatches[i].end; |
| 59 | + final end = i + 1 < allVersionMatches.length ? allVersionMatches[i + 1].start : contents.length; |
| 60 | + final sectionContents = contents.substring(start, end).trim(); |
| 61 | + final lines = sectionContents.split('\n').where((line) => line.isNotEmpty).toList(); |
| 62 | + final version = |
| 63 | + allVersionMatches[i].group(0)!.substring(4, allVersionMatches[i].group(0)!.length - 1); |
| 64 | + var releasedAt = DateTime.now().toUtc(); |
| 65 | + final updates = <String>{}; |
| 66 | + final old = lines |
| 67 | + .map((e) => e.trim()) |
| 68 | + .where((e) => e.isNotEmpty) |
| 69 | + .map((e) => e.startsWith('-') ? e.substring(1) : e) |
| 70 | + .map((e) => e.trim()) |
| 71 | + .where((e) => e.isNotEmpty); |
| 72 | + for (var line in old) { |
| 73 | + if (line.contains('Released @')) { |
| 74 | + releasedAt = parseReleaseDate(line); |
| 75 | + } else { |
| 76 | + updates.add(line); |
| 77 | + } |
| 78 | + } |
| 79 | + results.add(_VersionSection( |
| 80 | + version: version, |
| 81 | + releasedAt: releasedAt, |
| 82 | + updates: updates, |
| 83 | + )); |
| 84 | + } |
| 85 | + |
| 86 | + return results; |
| 87 | +} |
| 88 | + |
| 89 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 90 | + |
| 91 | +class _VersionSection { |
| 92 | + // |
| 93 | + // |
| 94 | + // |
| 95 | + |
| 96 | + String version; |
| 97 | + DateTime releasedAt; |
| 98 | + Set<String> updates; |
| 99 | + |
| 100 | + // |
| 101 | + // |
| 102 | + // |
| 103 | + |
| 104 | + _VersionSection({ |
| 105 | + required this.version, |
| 106 | + required this.releasedAt, |
| 107 | + Set<String>? updates, |
| 108 | + }) : this.updates = updates ?? {}; |
| 109 | + |
| 110 | + // |
| 111 | + // |
| 112 | + // |
| 113 | + |
| 114 | + void addUpdate(String update) { |
| 115 | + updates.add(update); |
| 116 | + releasedAt = DateTime.now().toUtc(); |
| 117 | + } |
| 118 | + |
| 119 | + // |
| 120 | + // |
| 121 | + // |
| 122 | + |
| 123 | + @override |
| 124 | + String toString() { |
| 125 | + final updatesString = updates.map((update) => '- $update').join('\n'); |
| 126 | + return '## [$version]\n\n- Released @ ${releasedAt.month}/${releasedAt.year} (UTC)\n$updatesString\n'; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 131 | + |
| 132 | +int compareVersions(String version1, String version2) { |
| 133 | + List<int> parseVersion(String version) { |
| 134 | + // Split by the '+' first to handle the build number |
| 135 | + final parts = version.split('+'); |
| 136 | + final versionParts = parts[0].split('.').map(int.tryParse).map((e) => e ?? 0).toList(); |
| 137 | + // Add the build number as the last part (if it exists) |
| 138 | + if (parts.length > 1) { |
| 139 | + versionParts.add(int.tryParse(parts[1]) ?? 0); |
| 140 | + } |
| 141 | + return versionParts; |
| 142 | + } |
| 143 | + |
| 144 | + final v1 = parseVersion(version1); |
| 145 | + final v2 = parseVersion(version2); |
| 146 | + final maxLength = v1.length > v2.length ? v1.length : v2.length; |
| 147 | + for (var i = 0; i < maxLength; i++) { |
| 148 | + final part1 = i < v1.length ? v1[i] : 0; |
| 149 | + final part2 = i < v2.length ? v2[i] : 0; |
| 150 | + if (part1 > part2) return 1; |
| 151 | + if (part1 < part2) return -1; |
| 152 | + } |
| 153 | + return 0; |
| 154 | +} |
| 155 | + |
| 156 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 157 | + |
| 158 | +DateTime parseReleaseDate(String line) { |
| 159 | + if (line.contains('Released @')) { |
| 160 | + final temp = line.split('Released @').last.trim().replaceAll(' (UTC)', ''); |
| 161 | + final parts = temp.split('/'); |
| 162 | + if (parts.length == 2) { |
| 163 | + final month = int.tryParse(parts[0]) ?? 1; |
| 164 | + final year = int.tryParse(parts[1]) ?? DateTime.now().year; |
| 165 | + return DateTime.utc(year, month); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return DateTime.now().toUtc(); |
| 170 | +} |
0 commit comments