11use async_compression:: futures:: bufread:: ZstdDecoder ;
22use async_std:: { io:: ReadExt , stream:: StreamExt } ;
33use async_tar_wasm:: Archive ;
4+ use context:: bazel_bep:: parser:: BepParseResult ;
45use futures_io:: AsyncBufRead ;
5- use std:: io:: { Seek , Write } ;
6- use std:: path:: PathBuf ;
6+ use std:: {
7+ fs:: File ,
8+ io:: { Seek , Write } ,
9+ path:: PathBuf ,
10+ } ;
711#[ cfg( feature = "wasm" ) ]
812use tsify_next:: Tsify ;
913#[ cfg( feature = "wasm" ) ]
@@ -17,24 +21,25 @@ use crate::bundle_meta::{BundleMeta, VersionedBundle};
1721///
1822#[ cfg_attr( feature = "wasm" , derive( Tsify ) ) ]
1923pub struct BundlerUtil {
20- pub meta : BundleMeta ,
24+ meta : BundleMeta ,
25+ bep_result : Option < BepParseResult > ,
2126}
2227
2328const META_FILENAME : & ' static str = "meta.json" ;
2429
2530impl BundlerUtil {
2631 const ZSTD_COMPRESSION_LEVEL : i32 = 15 ; // This gives roughly 10x compression for text, 22 gives 11x.
2732
28- pub fn new ( meta : BundleMeta ) -> Self {
29- Self { meta }
33+ pub fn new ( meta : BundleMeta , bep_result : Option < BepParseResult > ) -> Self {
34+ Self { meta, bep_result }
3035 }
3136
3237 /// Writes compressed tarball to disk.
3338 ///
3439 pub fn make_tarball ( & self , bundle_path : & PathBuf ) -> anyhow:: Result < ( ) > {
3540 let mut total_bytes_in: u64 = 0 ;
3641
37- let tar_file = std :: fs :: File :: create ( bundle_path) ?;
42+ let tar_file = File :: create ( bundle_path) ?;
3843 let zstd_encoder = zstd:: Encoder :: new ( tar_file, Self :: ZSTD_COMPRESSION_LEVEL ) ?;
3944 let mut tar = tar:: Builder :: new ( zstd_encoder) ;
4045
@@ -56,7 +61,7 @@ impl BundlerUtil {
5661 . try_for_each ( |file_set| {
5762 file_set. files . iter ( ) . try_for_each ( |bundled_file| {
5863 let path = std:: path:: Path :: new ( & bundled_file. original_path ) ;
59- let mut file = std :: fs :: File :: open ( path) ?;
64+ let mut file = File :: open ( path) ?;
6065 tar. append_file ( & bundled_file. path , & mut file) ?;
6166 total_bytes_in += std:: fs:: metadata ( path) ?. len ( ) ;
6267 Ok :: < ( ) , anyhow:: Error > ( ( ) )
@@ -65,11 +70,28 @@ impl BundlerUtil {
6570 } ) ?;
6671
6772 if let Some ( CodeOwners { ref path, .. } ) = self . meta . base_props . codeowners {
68- let mut file = std :: fs :: File :: open ( path) ?;
73+ let mut file = File :: open ( path) ?;
6974 tar. append_file ( "CODEOWNERS" , & mut file) ?;
7075 total_bytes_in += std:: fs:: metadata ( path) ?. len ( ) ;
7176 }
7277
78+ if let Some ( bep_result) = self . bep_result . as_ref ( ) {
79+ let mut bep_events_file =
80+ bep_result
81+ . bep_test_events
82+ . iter ( )
83+ . fold ( tempfile:: tempfile ( ) ?, |f, event| {
84+ if let Err ( e) = serde_json:: to_writer ( & f, event) {
85+ log:: error!( "Failed to write BEP event: {}" , e) ;
86+ }
87+ f
88+ } ) ;
89+ bep_events_file. flush ( ) ?;
90+ bep_events_file. seek ( std:: io:: SeekFrom :: Start ( 0 ) ) ?;
91+ tar. append_file ( "bazel_bep.json" , & mut bep_events_file) ?;
92+ total_bytes_in += bep_events_file. seek ( std:: io:: SeekFrom :: End ( 0 ) ) ?;
93+ }
94+
7395 // Flush to disk.
7496 tar. into_inner ( ) ?. finish ( ) ?;
7597
0 commit comments