@@ -4,37 +4,60 @@ use std::{collections::BTreeMap, path::Path};
44
55use spin_manifest:: { schema:: v2, ManifestVersion } ;
66
7+ use crate :: deployment:: DeploymentTargets ;
8+
79/// Returns a map of component IDs to [`v2::ComponentBuildConfig`]s for the
810/// given (v1 or v2) manifest path. If the manifest cannot be loaded, the
911/// function attempts fallback: if fallback succeeds, result is Ok but the load error
1012/// is also returned via the second part of the return value tuple.
1113pub async fn component_build_configs (
1214 manifest_file : impl AsRef < Path > ,
13- ) -> Result < ( Vec < ComponentBuildInfo > , Option < spin_manifest:: Error > ) > {
15+ ) -> Result < (
16+ Vec < ComponentBuildInfo > ,
17+ DeploymentTargets ,
18+ Result < spin_manifest:: schema:: v2:: AppManifest , spin_manifest:: Error > ,
19+ ) > {
1420 let manifest = spin_manifest:: manifest_from_file ( & manifest_file) ;
1521 match manifest {
16- Ok ( manifest) => Ok ( ( build_configs_from_manifest ( manifest) , None ) ) ,
17- Err ( e) => fallback_load_build_configs ( & manifest_file)
18- . await
19- . map ( |bc| ( bc, Some ( e) ) ) ,
22+ Ok ( mut manifest) => {
23+ spin_manifest:: normalize:: normalize_manifest ( & mut manifest) ;
24+ let bc = build_configs_from_manifest ( & manifest) ;
25+ let dt = deployment_targets_from_manifest ( & manifest) ;
26+ Ok ( ( bc, dt, Ok ( manifest) ) )
27+ }
28+ Err ( e) => {
29+ let bc = fallback_load_build_configs ( & manifest_file) . await ?;
30+ let dt = fallback_load_deployment_targets ( & manifest_file) . await ?;
31+ Ok ( ( bc, dt, Err ( e) ) )
32+ }
2033 }
2134}
2235
2336fn build_configs_from_manifest (
24- mut manifest : spin_manifest:: schema:: v2:: AppManifest ,
37+ manifest : & spin_manifest:: schema:: v2:: AppManifest ,
2538) -> Vec < ComponentBuildInfo > {
26- spin_manifest:: normalize:: normalize_manifest ( & mut manifest) ;
27-
2839 manifest
2940 . components
30- . into_iter ( )
41+ . iter ( )
3142 . map ( |( id, c) | ComponentBuildInfo {
3243 id : id. to_string ( ) ,
33- build : c. build ,
44+ build : c. build . clone ( ) ,
3445 } )
3546 . collect ( )
3647}
3748
49+ fn deployment_targets_from_manifest (
50+ manifest : & spin_manifest:: schema:: v2:: AppManifest ,
51+ ) -> DeploymentTargets {
52+ let target_environments = manifest. application . targets . clone ( ) ;
53+ // let components = manifest
54+ // .components
55+ // .iter()
56+ // .map(|(id, c)| (id.to_string(), c.source.clone()))
57+ // .collect();
58+ DeploymentTargets :: new ( target_environments)
59+ }
60+
3861async fn fallback_load_build_configs (
3962 manifest_file : impl AsRef < Path > ,
4063) -> Result < Vec < ComponentBuildInfo > > {
@@ -57,6 +80,42 @@ async fn fallback_load_build_configs(
5780 } )
5881}
5982
83+ async fn fallback_load_deployment_targets (
84+ manifest_file : impl AsRef < Path > ,
85+ ) -> Result < DeploymentTargets > {
86+ // fn try_parse_component_source(c: (&String, &toml::Value)) -> Option<(String, spin_manifest::schema::v2::ComponentSource)> {
87+ // let (id, ctab) = c;
88+ // let cs = ctab.as_table()
89+ // .and_then(|c| c.get("source"))
90+ // .and_then(|cs| spin_manifest::schema::v2::ComponentSource::deserialize(cs.clone()).ok());
91+ // cs.map(|cs| (id.to_string(), cs))
92+ // }
93+ let manifest_text = tokio:: fs:: read_to_string ( manifest_file) . await ?;
94+ Ok ( match ManifestVersion :: detect ( & manifest_text) ? {
95+ ManifestVersion :: V1 => Default :: default ( ) ,
96+ ManifestVersion :: V2 => {
97+ let table: toml:: value:: Table = toml:: from_str ( & manifest_text) ?;
98+ let target_environments = table
99+ . get ( "application" )
100+ . and_then ( |a| a. as_table ( ) )
101+ . and_then ( |t| t. get ( "targets" ) )
102+ . and_then ( |arr| arr. as_array ( ) )
103+ . map ( |v| v. as_slice ( ) )
104+ . unwrap_or_default ( )
105+ . iter ( )
106+ . filter_map ( |t| t. as_str ( ) )
107+ . map ( |s| s. to_owned ( ) )
108+ . collect ( ) ;
109+ // let components = table
110+ // .get("component")
111+ // .and_then(|cs| cs.as_table())
112+ // .map(|table| table.iter().filter_map(try_parse_component_source).collect())
113+ // .unwrap_or_default();
114+ DeploymentTargets :: new ( target_environments)
115+ }
116+ } )
117+ }
118+
60119#[ derive( Deserialize ) ]
61120pub struct ComponentBuildInfo {
62121 #[ serde( default ) ]
0 commit comments