1- use std:: { io:: Write as _, path:: PathBuf } ;
1+ use std:: {
2+ io:: { IsTerminal as _, Write as _} ,
3+ path:: PathBuf ,
4+ } ;
25
36use anyhow:: { bail, Context as _, Result } ;
47use clap:: Args ;
@@ -19,6 +22,10 @@ pub struct PlugCommand {
1922 #[ clap( value_name = "SOCKET_PATH" , required = true ) ]
2023 pub socket : PathBuf ,
2124
25+ /// Whether to emit the WebAssembly text format.
26+ #[ clap( long, short = 't' ) ]
27+ pub wat : bool ,
28+
2229 /// The path to write the output to.
2330 ///
2431 /// If not specified, the output will be written to stdout.
@@ -43,21 +50,28 @@ impl PlugCommand {
4350 let socket = graph. register_package ( socket) ?;
4451 let socket_instantiation = graph. instantiate ( socket) ;
4552
53+ // Collect the plugs by their names
54+ let mut plugs_by_name = std:: collections:: HashMap :: < _ , Vec < _ > > :: new ( ) ;
55+ for plug in self . plugs . iter ( ) {
56+ let name = plug
57+ . file_stem ( )
58+ . map ( |fs| fs. to_string_lossy ( ) )
59+ . with_context ( || format ! ( "path to plug '{}' was not a file" , plug. display( ) ) ) ?;
60+ // TODO(rylev): sanitize the name to ensure it's a valid package identifier.
61+ plugs_by_name. entry ( name) . or_default ( ) . push ( plug) ;
62+ }
63+
4664 // Plug each plug into the socket.
47- for ( i, plug) in self . plugs . iter ( ) . enumerate ( ) {
48- let plug = std:: fs:: read ( & plug) . with_context ( || {
49- format ! (
50- "failed to read plug component `{path}`" ,
51- path = plug. display( )
52- )
53- } ) ?;
54- plug_into_socket (
55- & format ! ( "plug{i}" ) ,
56- plug,
57- socket,
58- socket_instantiation,
59- & mut graph,
60- ) ?;
65+ for ( name, plug_paths) in plugs_by_name {
66+ for ( i, plug_path) in plug_paths. iter ( ) . enumerate ( ) {
67+ let mut name = format ! ( "plug:{name}" ) ;
68+ // If there's more than one plug with the same name, append an index to the name.
69+ if plug_paths. len ( ) > 1 {
70+ use core:: fmt:: Write ;
71+ write ! ( & mut name, "{i}" ) . unwrap ( ) ;
72+ }
73+ plug_into_socket ( & name, plug_path, socket, socket_instantiation, & mut graph) ?;
74+ }
6175 }
6276
6377 // Check we've actually done any plugging.
@@ -66,7 +80,7 @@ impl PlugCommand {
6680 . next ( )
6781 . is_none ( )
6882 {
69- bail ! ( "no plugs were used to plug into the socket component " )
83+ bail ! ( "the socket component had no matching imports for the plugs that were provided " )
7084 }
7185
7286 // Export all exports from the socket component.
@@ -80,7 +94,18 @@ impl PlugCommand {
8094 graph. export ( export, & name) ?;
8195 }
8296
83- let bytes = graph. encode ( EncodeOptions :: default ( ) ) ?;
97+ let binary_output_to_terminal =
98+ !self . wat && self . output . is_none ( ) && std:: io:: stdout ( ) . is_terminal ( ) ;
99+ if binary_output_to_terminal {
100+ bail ! ( "cannot print binary wasm output to a terminal; pass the `-t` flag to print the text format instead" ) ;
101+ }
102+
103+ let mut bytes = graph. encode ( EncodeOptions :: default ( ) ) ?;
104+ if self . wat {
105+ bytes = wasmprinter:: print_bytes ( & bytes)
106+ . context ( "failed to convert binary wasm output to text" ) ?
107+ . into_bytes ( ) ;
108+ }
84109 match & self . output {
85110 Some ( path) => {
86111 std:: fs:: write ( & path, bytes) . context ( format ! (
@@ -92,6 +117,10 @@ impl PlugCommand {
92117 std:: io:: stdout ( )
93118 . write_all ( & bytes)
94119 . context ( "failed to write to stdout" ) ?;
120+
121+ if self . wat {
122+ println ! ( ) ;
123+ }
95124 }
96125 }
97126 Ok ( ( ) )
@@ -101,12 +130,12 @@ impl PlugCommand {
101130/// Take the exports of the plug component and plug them into the socket component.
102131fn plug_into_socket (
103132 name : & str ,
104- plug : Vec < u8 > ,
133+ plug_path : & std :: path :: Path ,
105134 socket : PackageId ,
106135 socket_instantiation : NodeId ,
107136 graph : & mut CompositionGraph ,
108137) -> Result < ( ) , anyhow:: Error > {
109- let plug = Package :: from_bytes ( name, None , plug , graph. types_mut ( ) ) ?;
138+ let plug = Package :: from_file ( name, None , plug_path , graph. types_mut ( ) ) ?;
110139 let plug = graph. register_package ( plug) ?;
111140
112141 let mut plugs = Vec :: new ( ) ;
0 commit comments