33import 'package:args/command_runner.dart' ;
44import 'package:changelog_cli/src/model/model.dart' ;
55import 'package:changelog_cli/src/printers/printers.dart' ;
6+ import 'package:changelog_cli/src/processors/preprocessor.dart' ;
67import 'package:conventional_commit/conventional_commit.dart' ;
78import 'package:git/git.dart' ;
89import 'package:mason_logger/mason_logger.dart' ;
@@ -20,12 +21,12 @@ class GenerateCommand extends Command<int> {
2021 argParser.addOption (
2122 'start' ,
2223 abbr: 's' ,
23- help: 'Start git reference (e.g. commit SHA)' ,
24+ help: 'Start git reference (e.g. commit SHA or tag )' ,
2425 );
2526 argParser.addOption (
2627 'end' ,
2728 abbr: 'e' ,
28- help: 'End git reference (e.g. commit SHA)' ,
29+ help: 'End git reference (e.g. commit SHA or tag )' ,
2930 );
3031 argParser.addMultiOption (
3132 'include' ,
@@ -50,7 +51,7 @@ class GenerateCommand extends Command<int> {
5051 argParser.addOption (
5152 'version' ,
5253 abbr: 'v' ,
53- help: 'Manually specify version' ,
54+ help: 'Manually specify version printed in the header of the changelog ' ,
5455 defaultsTo: '' ,
5556 );
5657 argParser.addOption (
@@ -78,6 +79,29 @@ class GenerateCommand extends Command<int> {
7879 'slack-markdown' ,
7980 ],
8081 );
82+ argParser.addOption (
83+ 'group-by' ,
84+ abbr: 'g' ,
85+ help: 'Group entries by type' ,
86+ allowed: [
87+ 'date-asc' ,
88+ 'date-desc' ,
89+ 'scope-asc' ,
90+ 'scope-desc' ,
91+ ],
92+ );
93+ argParser.addOption (
94+ 'date-format' ,
95+ help: 'Date format, providing empty skips date formatting. \n Needs '
96+ 'to be valid ISO date format e.g. yyyy-MM-dd, yyyy-MM-dd HH:mm:ss. '
97+ 'By default it does not print any dates. Uses system-default locale.' ,
98+ defaultsTo: '' ,
99+ );
100+ argParser.addOption (
101+ 'date-format-locale' ,
102+ help: 'Date format passed to the date formatting, expected format: xx_XX' ,
103+ defaultsTo: 'en_US' ,
104+ );
81105 }
82106
83107 @override
@@ -91,25 +115,25 @@ class GenerateCommand extends Command<int> {
91115
92116 @override
93117 Future <int > run () async {
94- final start = argResults ? [ 'start' ] as String ? ;
95- final end = argResults ? [ 'end' ] as String ? ;
96- final include = argResults ? [ 'include' ] as List < String > ? ?? [];
97- final version = argResults ? [ 'version' ] as String ;
98- final limit = int . tryParse (argResults? [ 'limit' ] as String ? ?? '' );
118+ if (argResults == null ) {
119+ return ExitCode .usage.code ;
120+ }
121+
122+ final configuration = GenerateConfiguration . fromArgs (argResults! );
99123
100124 final path = await getGitPath ();
101125
102126 if (path != null ) {
103127 final String ? startRef;
104- if (argResults ? [ ' auto' ] == true ) {
128+ if (configuration. auto) {
105129 startRef = await getLastTag (path: path);
106130 } else {
107- startRef = start;
131+ startRef = configuration. start;
108132 }
109133
110134 final commits = await getCommits (
111135 start: startRef,
112- end: end,
136+ end: configuration. end,
113137 path: path,
114138 );
115139 if (commits.isEmpty) {
@@ -122,29 +146,28 @@ class GenerateCommand extends Command<int> {
122146 final conventionalCommit = ConventionalCommit .tryParse (v.value.message);
123147
124148 if (conventionalCommit != null ) {
125- if (include.contains (conventionalCommit.type)) {
149+ if (configuration. include.contains (conventionalCommit.type)) {
126150 list.add (
127151 ChangelogEntry (
128152 conventionalCommit: conventionalCommit,
129153 ref: v.key,
130154 commit: v.value,
155+ date: parseDate (v.value.author),
131156 ),
132157 );
133158 }
134159 }
135160 }
136161 _logger.detail ('Found ${list .length } conventional commits' );
137162
138- final printer = getPrinter (argResults ? [ 'printer' ] as String ? );
163+ final processedList = Preprocessor . processGitHistory (list, configuration );
139164
140- final output = printer.print (
141- entries: list,
142- version: version,
143- types: include,
144- );
165+ final printer = getPrinter (configuration);
145166
146- if (limit != null && limit > 0 ) {
147- final limitClamped = limit.clamp (0 , output.length);
167+ final output = printer.print (entries: processedList);
168+
169+ if (configuration.limit > 0 ) {
170+ final limitClamped = configuration.limit.clamp (0 , output.length);
148171 _logger.info (output.substring (0 , limitClamped));
149172 } else {
150173 _logger.info (output);
@@ -214,14 +237,25 @@ class GenerateCommand extends Command<int> {
214237 }
215238 }
216239
217- Printer getPrinter (String ? argResult ) {
218- switch (argResult ) {
219- case ' markdown' :
220- return MarkdownPrinter ();
221- case 'slack-markdown' :
222- return SlackMarkdownPrinter ();
223- default :
224- return SimplePrinter ();
240+ Printer getPrinter (GenerateConfiguration configuration ) {
241+ switch (configuration.printer ) {
242+ case PrinterType . markdown:
243+ return MarkdownPrinter (configuration : configuration );
244+ case PrinterType .slackMarkdown :
245+ return SlackMarkdownPrinter (configuration : configuration );
246+ case PrinterType .simple :
247+ return SimplePrinter (configuration : configuration );
225248 }
226249 }
227250}
251+
252+ DateTime ? parseDate (String gitAuthor) {
253+ final author = gitAuthor.split (' ' );
254+ final oneBeforeLast = author.length - 2 ;
255+ final date = author[oneBeforeLast];
256+ final seconds = int .tryParse (date);
257+ if (seconds != null ) {
258+ return DateTime .fromMillisecondsSinceEpoch (seconds * 1000 );
259+ }
260+ return null ;
261+ }
0 commit comments