66using DwC_A . Interactive . Mapping ;
77using Microsoft . DotNet . Interactive ;
88using Microsoft . DotNet . Interactive . Events ;
9- using Microsoft . DotNet . Interactive . ValueSharing ;
109using System ;
1110using System . CommandLine ;
12- using System . CommandLine . Invocation ;
1311using System . IO ;
1412using System . Threading . Tasks ;
1513
@@ -20,36 +18,41 @@ internal class DwcaCodegenCommand : Command
2018 public DwcaCodegenCommand ( )
2119 : base ( "#!dwca-codegen" , "Generate strongly typed class files for Darwin Core Archive" )
2220 {
23- AddArgument ( new Argument < string > ( )
21+ var archivePathArg = new Argument < string > ( )
2422 {
2523 Name = "archivePath" ,
2624 Description = "Path to archive folder or zip file"
27- } ) ;
25+ } ;
26+ AddArgument ( archivePathArg ) ;
2827
29- AddOption ( new Option < string > (
30- aliases : new [ ] { "-c" , "--configName" } ,
28+ var cfgOption = new Option < string > (
29+ aliases : new [ ] { "-c" , "--configName" } ,
3130 description : "Name of configuration variable" ,
3231 getDefaultValue : ( ) => ""
33- ) ) ;
32+ ) ;
33+ AddOption ( cfgOption ) ;
3434
35- Handler = CommandHandler . Create < KernelInvocationContext , string , string > ( ( Func < KernelInvocationContext , string , string , Task > ) ( async ( context , archivePath , configName ) =>
35+ System . CommandLine . Handler . SetHandler ( this , async ( context ) =>
3636 {
37+ var archivePath = context . ParseResult . GetValueForArgument ( archivePathArg ) ;
38+ var configName = context . ParseResult . GetValueForOption ( cfgOption ) ;
3739 var archive = new ArchiveReader ( archivePath ) ;
3840
39- var csharpKernel = ( ISupportGetValue ) context . HandlingKernel . FindKernel ( "csharp" ) ;
40- if ( ! csharpKernel . TryGetValue < IGeneratorConfiguration > ( configName , out IGeneratorConfiguration config ) )
41- {
42- config = new GeneratorConfigurationBuilder ( ) . Build ( ) ;
43- }
44- context . Display ( $ "Opening archive { archive . FileName } using configuration", new [ ] { "text/html" } ) ;
45- context . Display ( config , new [ ] { "text/html" } ) ;
41+ var csharpKernel = KernelInvocationContext . Current . HandlingKernel . FindKernelByName ( "csharp" ) ;
42+ var ( success , value ) = await csharpKernel . TryRequestValueAsync ( configName ) ;
43+ IGeneratorConfiguration config = success ?
44+ value . Value as IGeneratorConfiguration :
45+ null ??
46+ new GeneratorConfigurationBuilder ( ) . Build ( ) ;
47+ KernelInvocationContext . Current . Display ( $ "Opening archive { archive . FileName } using configuration", new [ ] { "text/html" } ) ;
48+ KernelInvocationContext . Current . Display ( config , new [ ] { "text/html" } ) ;
4649
47- await GenerateClass ( context , archive . CoreFile , config ) ;
48- foreach ( var extension in archive . Extensions . GetFileReaders ( ) )
50+ await GenerateClass ( KernelInvocationContext . Current , archive . CoreFile , config ) ;
51+ foreach ( var extension in archive . Extensions . GetFileReaders ( ) )
4952 {
50- await GenerateClass ( context , extension , config ) ;
53+ await GenerateClass ( KernelInvocationContext . Current , extension , config ) ;
5154 }
52- } ) ) ;
55+ } ) ;
5356 }
5457
5558 private static async Task GenerateClass ( KernelInvocationContext context ,
0 commit comments