@@ -10,17 +10,17 @@ internal class RootCommand: System.CommandLine.RootCommand {
1010 /// <summary>
1111 /// The path to the input directory.
1212 /// </summary>
13- private readonly Argument < DirectoryInfo > inputArgument = new ( "input" ) {
13+ private readonly Argument < DirectoryInfo > inputArgument = new Argument < DirectoryInfo > ( "input" ) {
1414 Description = "The path to the input directory."
15- } ;
15+ } . AcceptExistingOnly ( ) ;
1616
1717 /// <summary>
1818 /// The path to the output directory.
1919 /// </summary>
20- private readonly Argument < DirectoryInfo ? > outputArgument = new ( "output" ) {
20+ private readonly Argument < DirectoryInfo ? > outputArgument = new Argument < DirectoryInfo ? > ( "output" ) {
2121 DefaultValueFactory = _ => null ,
2222 Description = "The path to the output directory."
23- } ;
23+ } . AcceptLegalFilePathsOnly ( ) ;
2424
2525 /// <summary>
2626 /// The path to the PHP executable.
@@ -46,11 +46,18 @@ internal class RootCommand: System.CommandLine.RootCommand {
4646 Description = "The operation mode of the minifier."
4747 } . AcceptOnlyFromAmong ( [ "fast" , "safe" ] ) ;
4848
49+ /// <summary>
50+ /// Value indicating whether to process the input directory recursively.
51+ /// </summary>
52+ private readonly Option < bool > recursiveOption = new ( "--recursive" , [ "-r" ] ) {
53+ Description = "Whether to process the input directory recursively."
54+ } ;
55+
4956 /// <summary>
5057 /// Value indicating whether to silence the minifier output.
5158 /// </summary>
52- private readonly Option < string > silentOption = new ( "--silent" , [ "-s" ] ) {
53- Description = "Value indicating whether to silence the minifier output."
59+ private readonly Option < bool > silentOption = new ( "--silent" , [ "-s" ] ) {
60+ Description = "Whether to silence the minifier output."
5461 } ;
5562
5663 /// <summary>
@@ -62,6 +69,7 @@ public RootCommand(): base("Minify PHP source code by removing comments and whit
6269 Options . Add ( binaryOption ) ;
6370 Options . Add ( extensionOption ) ;
6471 Options . Add ( modeOption ) ;
72+ Options . Add ( recursiveOption ) ;
6573 Options . Add ( silentOption ) ;
6674 SetAction ( InvokeAsync ) ;
6775 }
@@ -72,10 +80,24 @@ public RootCommand(): base("Minify PHP source code by removing comments and whit
7280 /// <param name="parseResult">The results of parsing the command line input.</param>
7381 /// <param name="cancellationToken">The token to cancel the operation.</param>
7482 /// <returns>The exit code.</returns>
75- public Task < int > InvokeAsync ( ParseResult parseResult , CancellationToken cancellationToken ) {
83+ public async Task < int > InvokeAsync ( ParseResult parseResult , CancellationToken cancellationToken ) {
84+ var binary = parseResult . GetRequiredValue ( binaryOption ) ;
7685 var input = parseResult . GetRequiredValue ( inputArgument ) ;
77- var output = parseResult . GetValue ( outputArgument ) ;
78- Console . WriteLine ( output ) ;
79- return Task . FromResult ( 0 ) ;
86+ var output = parseResult . GetValue ( outputArgument ) ?? input ;
87+ var silent = parseResult . GetValue ( silentOption ) ;
88+ ITransformer transformer = parseResult . GetRequiredValue ( modeOption ) == "fast" ? new FastTransformer ( binary ) : new SafeTransformer ( binary ) ;
89+
90+ var searchOption = parseResult . GetValue ( recursiveOption ) ? SearchOption . AllDirectories : SearchOption . TopDirectoryOnly ;
91+ foreach ( var file in input . EnumerateFiles ( $ "*.{ parseResult . GetRequiredValue ( extensionOption ) } ", searchOption ) ) {
92+ var relativePath = Path . GetRelativePath ( input . FullName , file . FullName ) ;
93+ if ( ! silent ) Console . WriteLine ( "Minifying: {0}" , relativePath ) ;
94+
95+ var script = await transformer . Transform ( file . FullName ) ;
96+ var target = Path . Join ( output . FullName , relativePath ) ;
97+ Directory . CreateDirectory ( Path . GetDirectoryName ( target ) ! ) ; // TODO try to remove the non-null assertion (i.e. "!")
98+ await File . WriteAllTextAsync ( target , script , cancellationToken ) ;
99+ }
100+
101+ return 0 ;
80102 }
81103}
0 commit comments