@@ -115,6 +115,10 @@ impl PkgExporter {
115115 }
116116 }
117117
118+ fn new_standalone_variant_exporter ( ) -> Self {
119+ Self :: new_module_exporter ( "" , "" , None :: < String > , "" , vec ! [ ] )
120+ }
121+
118122 pub async fn export_as_bytes ( & mut self , ctx : & DalContext ) -> PkgResult < Vec < u8 > > {
119123 match self . kind {
120124 SiPkgKind :: Module => info ! ( "Building module package" ) ,
@@ -145,40 +149,19 @@ impl PkgExporter {
145149 }
146150
147151 let in_change_set = std_model_change_set_matches ( change_set_pk, schema) ;
148- let is_deleted = schema. visibility ( ) . is_deleted ( ) ;
152+ let schema_is_deleted = schema. visibility ( ) . is_deleted ( ) ;
149153
150154 let default_variant_id = schema. default_schema_variant_id ( ) . copied ( ) ;
151155 let mut default_variant_unique_id = None ;
152156
153157 for variant in & variants {
154- let related_funcs = SchemaVariant :: all_funcs ( ctx, * variant. id ( ) ) . await ?;
155-
156- for func in & related_funcs {
157- if change_set_pk. is_some ( )
158- && change_set_pk. as_ref ( ) . expect ( "some is ensured" ) != & ChangeSetPk :: NONE
159- && self . func_map . get ( ChangeSetPk :: NONE , func. id ( ) ) . is_none ( )
160- && func. visibility ( ) . change_set_pk == ChangeSetPk :: NONE
161- {
162- let ( func_spec, _) =
163- self . export_func ( ctx, Some ( ChangeSetPk :: NONE ) , func) . await ?;
164- self . func_map
165- . insert ( ChangeSetPk :: NONE , * func. id ( ) , func_spec. to_owned ( ) ) ;
166- head_funcs. push ( func_spec) ;
167- } else {
168- let ( func_spec, include) = self . export_func ( ctx, change_set_pk, func) . await ?;
169- self . func_map . insert (
170- change_set_pk. unwrap_or ( ChangeSetPk :: NONE ) ,
171- * func. id ( ) ,
172- func_spec. to_owned ( ) ,
173- ) ;
174-
175- if include {
176- funcs. push ( func_spec) ;
177- }
178- }
179- }
158+ let ( variant_funcs, variant_head_funcs) = self
159+ . export_funcs_for_variant ( ctx, change_set_pk, * variant. id ( ) )
160+ . await ?;
161+ funcs. extend ( variant_funcs) ;
162+ head_funcs. extend ( variant_head_funcs) ;
180163
181- if !is_deleted {
164+ if !schema_is_deleted {
182165 let variant_spec = self . export_variant ( ctx, change_set_pk, variant) . await ?;
183166 self . variant_map . insert (
184167 change_set_pk. unwrap_or ( ChangeSetPk :: NONE ) ,
@@ -196,7 +179,7 @@ impl PkgExporter {
196179 }
197180 }
198181
199- if in_change_set && is_deleted {
182+ if in_change_set && schema_is_deleted {
200183 schema_spec_builder. deleted ( true ) ;
201184 } else if in_change_set {
202185 let mut data_builder = SchemaSpecData :: builder ( ) ;
@@ -222,6 +205,23 @@ impl PkgExporter {
222205 Ok ( ( schema_spec, funcs, head_funcs) )
223206 }
224207
208+ /// Exports just a single schema variant and the functions connected to it.
209+ /// Visiblity is taken from the context, so this will export the schema
210+ /// variant according to the normal rules of visibility.
211+ pub async fn export_variant_standalone (
212+ ctx : & DalContext ,
213+ variant : & SchemaVariant ,
214+ ) -> PkgResult < ( SchemaVariantSpec , Vec < FuncSpec > ) > {
215+ let mut exporter = Self :: new_standalone_variant_exporter ( ) ;
216+ let ( funcs, _) = exporter
217+ . export_funcs_for_variant ( ctx, None , * variant. id ( ) )
218+ . await ?;
219+
220+ let variant_spec = exporter. export_variant ( ctx, None , variant) . await ?;
221+
222+ Ok ( ( variant_spec, funcs) )
223+ }
224+
225225 async fn export_variant (
226226 & self ,
227227 ctx : & DalContext ,
@@ -1672,6 +1672,43 @@ impl PkgExporter {
16721672
16731673 Ok ( pkg)
16741674 }
1675+
1676+ async fn export_funcs_for_variant (
1677+ & mut self ,
1678+ ctx : & DalContext ,
1679+ change_set_pk : Option < ChangeSetPk > ,
1680+ schema_variant_id : SchemaVariantId ,
1681+ ) -> PkgResult < ( Vec < FuncSpec > , Vec < FuncSpec > ) > {
1682+ let related_funcs = SchemaVariant :: all_funcs ( ctx, schema_variant_id) . await ?;
1683+ let mut head_funcs = vec ! [ ] ;
1684+ let mut funcs = vec ! [ ] ;
1685+
1686+ for func in & related_funcs {
1687+ if change_set_pk. is_some ( )
1688+ && change_set_pk. as_ref ( ) . expect ( "some is ensured" ) != & ChangeSetPk :: NONE
1689+ && self . func_map . get ( ChangeSetPk :: NONE , func. id ( ) ) . is_none ( )
1690+ && func. visibility ( ) . change_set_pk == ChangeSetPk :: NONE
1691+ {
1692+ let ( func_spec, _) = self . export_func ( ctx, Some ( ChangeSetPk :: NONE ) , func) . await ?;
1693+ self . func_map
1694+ . insert ( ChangeSetPk :: NONE , * func. id ( ) , func_spec. to_owned ( ) ) ;
1695+ head_funcs. push ( func_spec) ;
1696+ } else {
1697+ let ( func_spec, include) = self . export_func ( ctx, change_set_pk, func) . await ?;
1698+ self . func_map . insert (
1699+ change_set_pk. unwrap_or ( ChangeSetPk :: NONE ) ,
1700+ * func. id ( ) ,
1701+ func_spec. to_owned ( ) ,
1702+ ) ;
1703+
1704+ if include {
1705+ funcs. push ( func_spec) ;
1706+ }
1707+ }
1708+ }
1709+
1710+ Ok ( ( funcs, head_funcs) )
1711+ }
16751712}
16761713
16771714fn remove_duplicate_func_specs ( func_specs : & [ FuncSpec ] ) -> Vec < FuncSpec > {
0 commit comments