1- use futures:: TryFutureExt ;
21use miette:: Diagnostic ;
32use prodash:: { messages:: MessageLevel , tree:: Item } ;
43use tokio:: { task:: JoinSet , time:: Instant } ;
@@ -43,26 +42,18 @@ impl Output {
4342 }
4443}
4544
46- pub struct Context < T > {
45+ pub struct Context {
4746 pub service_name : String ,
4847 pub platform : String ,
49- pub input : T ,
48+ pub progress : Item ,
5049}
5150
52- impl < T > Context < T > {
53- pub fn new ( service_name : String , platform : String , input : T ) -> Self {
51+ impl Context {
52+ pub fn new ( service_name : String , platform : String , progress : Item ) -> Self {
5453 Self {
5554 service_name,
5655 platform,
57- input,
58- }
59- }
60-
61- pub fn with < I > ( self , input : I ) -> Context < I > {
62- Context {
63- input,
64- platform : self . platform ,
65- service_name : self . service_name ,
56+ progress,
6657 }
6758 }
6859}
@@ -74,22 +65,18 @@ pub trait Builder: Clone {
7465 fn try_init ( ) -> Result < Self , Self :: Error >
7566 where
7667 Self : Sized ;
77- async fn build (
78- self ,
79- progress : Item ,
80- input : Context < Self :: Input > ,
81- ) -> Result < Output , Self :: Error > ;
68+ async fn build ( self , ctx : Context , input : Self :: Input ) -> Result < Output , Self :: Error > ;
8269}
8370
8471type ErrorOf < T > = <T as Builder >:: Error ;
8572
86- use std:: { collections:: HashMap , sync :: Arc } ;
73+ use std:: collections:: HashMap ;
8774
8875fn run_builder < B > (
8976 var : & mut Option < B > ,
90- progress : Item ,
91- ctx : Context < B :: Input > ,
92- ) -> Result < impl Future < Output = Result < Output , < B as Builder > :: Error > > + use < B > , BuildError >
77+ ctx : Context ,
78+ input : B :: Input ,
79+ ) -> Result < impl Future < Output = Result < Output , BuildError > > + use < B > , BuildError >
9380where
9481 B : Builder ,
9582 BuildError : From < <B as Builder >:: Error > ,
@@ -103,19 +90,19 @@ where
10390 }
10491 } ?;
10592
106- Ok ( builder. build ( progress , ctx ) )
93+ Ok ( async { Ok ( builder. build ( ctx , input ) . await ? ) } )
10794}
10895
10996pub struct MetaBuild {
110- config : Arc < Config > ,
97+ config : Config ,
11198 ko : Option < KoBuilder > ,
11299 bazel : Option < BazelBuilder > ,
113100 docker : Option < DockerBuilder > ,
114101 nix : Option < NixBuilder > ,
115102}
116103
117104impl MetaBuild {
118- pub fn new ( config : Arc < Config > ) -> Self {
105+ pub fn new ( config : Config ) -> Self {
119106 Self {
120107 config,
121108 ko : None ,
@@ -127,40 +114,27 @@ impl MetaBuild {
127114
128115 pub async fn build ( mut self , mut pb : Item , platform : & str ) -> Result < Output , BuildError > {
129116 let instant = Instant :: now ( ) ;
130- let config = Arc :: clone ( & self . config ) ;
131117 let mut set = JoinSet :: default ( ) ;
132118
133- pb. init ( Some ( config. build . len ( ) ) , None ) ;
119+ pb. init ( Some ( self . config . build . len ( ) ) , None ) ;
134120 pb. message ( MessageLevel :: Info , format ! ( "detected platform: {platform}" ) ) ;
135121
136- for ( name, build) in config. build . iter ( ) {
137- let progress = pb. add_child ( name) ;
138- let ctx = Context :: new ( name. clone ( ) , platform. to_string ( ) , ( ) ) ;
122+ for ( name, build) in self . config . build {
123+ let progress = pb. add_child ( & name) ;
124+ let ctx = Context :: new ( name, platform. to_string ( ) , progress ) ;
139125
140- match & build {
126+ match build {
141127 Build :: Ko ( ko) => {
142- set. spawn (
143- run_builder ( & mut self . ko , progress, ctx. with ( ko. clone ( ) ) ) ?
144- . map_err ( BuildError :: Ko ) ,
145- ) ;
128+ set. spawn ( run_builder ( & mut self . ko , ctx, ko) ?) ;
146129 }
147130 Build :: Bazel ( bazel) => {
148- set. spawn (
149- run_builder ( & mut self . bazel , progress, ctx. with ( bazel. clone ( ) ) ) ?
150- . map_err ( BuildError :: Bazel ) ,
151- ) ;
131+ set. spawn ( run_builder ( & mut self . bazel , ctx, bazel) ?) ;
152132 }
153133 Build :: Docker ( docker) => {
154- set. spawn (
155- run_builder ( & mut self . docker , progress, ctx. with ( docker. clone ( ) ) ) ?
156- . map_err ( BuildError :: Docker ) ,
157- ) ;
134+ set. spawn ( run_builder ( & mut self . docker , ctx, docker) ?) ;
158135 }
159136 Build :: Nix ( nix) => {
160- set. spawn (
161- run_builder ( & mut self . nix , progress, ctx. with ( nix. clone ( ) ) ) ?
162- . map_err ( BuildError :: Nix ) ,
163- ) ;
137+ set. spawn ( run_builder ( & mut self . nix , ctx, nix) ?) ;
164138 }
165139 } ;
166140 }
0 commit comments