33using Core ::DwC_A ;
44using DwC_A . Config ;
55using DwC_A . Generator ;
6+ using DwC_A . Interactive . Extensions ;
67using DwC_A . Interactive . Mapping ;
78using Microsoft . DotNet . Interactive ;
89using Microsoft . DotNet . Interactive . Events ;
9- using Microsoft . DotNet . Interactive . ValueSharing ;
1010using System ;
1111using System . CommandLine ;
12- using System . CommandLine . Invocation ;
1312using System . IO ;
1413using System . Threading . Tasks ;
1514
@@ -20,47 +19,52 @@ internal class DwcaCodegenCommand : Command
2019 public DwcaCodegenCommand ( )
2120 : base ( "#!dwca-codegen" , "Generate strongly typed class files for Darwin Core Archive" )
2221 {
23- AddArgument ( new Argument < string > ( )
22+ var archivePathArg = new Argument < string > ( )
2423 {
2524 Name = "archivePath" ,
2625 Description = "Path to archive folder or zip file"
27- } ) ;
26+ } ;
27+ AddArgument ( archivePathArg ) ;
2828
29- AddOption ( new Option < string > (
30- aliases : new [ ] { "-c" , "--configName" } ,
29+ var cfgOption = new Option < string > (
30+ aliases : new [ ] { "-c" , "--configName" } ,
3131 description : "Name of configuration variable" ,
3232 getDefaultValue : ( ) => ""
33- ) ) ;
33+ ) ;
34+ AddOption ( cfgOption ) ;
3435
35- Handler = CommandHandler . Create < KernelInvocationContext , string , string > ( ( Func < KernelInvocationContext , string , string , Task > ) ( async ( context , archivePath , configName ) =>
36+ System . CommandLine . Handler . SetHandler ( this , async ( context ) =>
3637 {
38+ var archivePath = context . ParseResult . GetValueForArgument ( archivePathArg ) ;
39+ var configName = context . ParseResult . GetValueForOption ( cfgOption ) ;
3740 var archive = new ArchiveReader ( archivePath ) ;
3841
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" } ) ;
42+ var csharpKernel = KernelInvocationContext . Current . HandlingKernel . FindKernelByName ( "csharp" ) ;
43+ var ( success , value ) = await csharpKernel . TryRequestValueAsync ( configName ) ;
44+ IGeneratorConfiguration config = success ?
45+ value . Value as IGeneratorConfiguration :
46+ null ??
47+ new GeneratorConfigurationBuilder ( ) . Build ( ) ;
48+ KernelInvocationContext . Current . Display ( $ "Opening archive { archive . FileName } using configuration", new [ ] { "text/html" } ) ;
49+ KernelInvocationContext . Current . Display ( config , new [ ] { "text/html" } ) ;
4650
47- await GenerateClass ( context , archive . CoreFile , config ) ;
48- foreach ( var extension in archive . Extensions . GetFileReaders ( ) )
51+ await GenerateClass ( KernelInvocationContext . Current , archive . CoreFile , config ) ;
52+ foreach ( var extension in archive . Extensions . GetFileReaders ( ) )
4953 {
50- await GenerateClass ( context , extension , config ) ;
54+ await GenerateClass ( KernelInvocationContext . Current , extension , config ) ;
5155 }
52- } ) ) ;
56+ } ) ;
5357 }
5458
5559 private static async Task GenerateClass ( KernelInvocationContext context ,
5660 IFileReader fileReader ,
5761 IGeneratorConfiguration config )
5862 {
59- var classGenerator = new ClassGenerator ( ) ;
6063 var className = Path . GetFileNameWithoutExtension ( fileReader . FileName ) ;
61- className = char . ToUpper ( className [ 0 ] ) + className . Substring ( 1 ) ;
64+ className = char . ToUpper ( className [ 0 ] ) + className [ 1 .. ] ;
6265 context . Display ( $ "Generating class { className } ", new [ ] { "text/html" } ) ;
63- var source = classGenerator . GenerateFile ( fileReader . FileMetaData , config ) ;
66+ var source = new ClassGenerator ( )
67+ . GenerateFile ( fileReader . FileMetaData , config ) ;
6468 var result = await context . HandlingKernel . SubmitCodeAsync ( source ) ;
6569 result . KernelEvents . Subscribe ( ( ev ) => { } , ( ex ) =>
6670 {
0 commit comments