forked from namidaco/namida
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chlog.dart
82 lines (73 loc) · 2.53 KB
/
chlog.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// ignore_for_file: avoid_print
import 'dart:io';
import 'package:intl/intl.dart';
void main(List<String> argumentsPre) async {
final changelog = File('CHANGELOG.md');
DateTime? startDatetime;
DateTime? stopDatetime;
if (argumentsPre.isNotEmpty) {
startDatetime = DateTime.tryParse(argumentsPre.first);
if (argumentsPre.length == 2) stopDatetime = DateTime.tryParse(argumentsPre.last);
}
final fileLines = changelog.readAsLinesSync();
if (startDatetime == null) {
// fetching the latest date found inside [ChANGELOG.md]
// format has to be [dd/MM/yyyy] if not found then it
// should be at least parsable.
for (final l in fileLines) {
final line = l.split('#').last;
final splitted = line.split('/');
if (splitted.length == 3) {
final y = int.tryParse(splitted[2]);
final m = int.tryParse(splitted[1]);
final d = int.tryParse(splitted[0]);
if (y != null && m != null && d != null) {
final datetime = DateTime(y, m, d);
startDatetime = datetime;
break;
}
}
}
}
if (startDatetime == null) {
for (final l in fileLines) {
final line = l.split('#').last;
final datetime = DateTime.tryParse(line);
if (datetime != null) {
startDatetime = datetime;
break;
}
}
}
if (startDatetime == null) {
print('Warning: first date isn\'t provided, fetching all changes...');
}
final gitOutput = await Process.run('git', <String>[
'log',
'--oneline',
'--decorate',
// '--pretty=%s',
// '--first-parent',
if (startDatetime != null) '--after="$startDatetime"',
if (stopDatetime != null) '--before="$stopDatetime"',
]);
final outputLines = (gitOutput.stdout as String).split('\n');
final map = <String, List<String>>{};
for (final line in outputLines) {
final parts = line.split(':');
final firstPart = parts.first.toLowerCase().trim().split(' ');
final hash = firstPart.first;
final key = firstPart.last;
final val = "#$hash: ${parts.last.trim()}";
if (map[key] == null) {
map[key] = [val];
} else {
map[key]!.add(val);
}
}
final stringy = map.entries.map((e) => '- ${e.key}:\n${e.value.map((e) => ' - $e').join('\n')}');
final dateText = DateFormat('dd/MM/yyyy').format(stopDatetime ?? DateTime.now());
const title = '# Namida Changelog';
final finalString = "$title\n\n## $dateText\n${stringy.join('\n')}\n${changelog.readAsStringSync().replaceFirst(title, '')}";
changelog.writeAsStringSync(finalString);
}