1- use std:: collections:: HashMap ;
1+ use std:: { collections:: HashMap , path :: PathBuf , vec } ;
22
33use libpkgx:: {
4+ env:: expand_moustaches, pantry_db,
45 platform_case_aware_env_key:: construct_platform_case_aware_env_key, types:: Installation ,
56} ;
7+ use serde:: Serialize ;
68use serde_json:: json;
79
810pub fn dump (
911 conn : rusqlite:: Connection ,
1012 installations : Vec < Installation > ,
11- env : HashMap < String , Vec < String > > ,
1213 flags : & crate :: args:: Flags ,
1314) -> Result < ( ) , Box < dyn std:: error:: Error > > {
14- if !flags. json {
15+ if let Some ( v) = flags. json {
16+ if v < 2 {
17+ let env = libpkgx:: env:: map ( & installations) ;
18+ let mut runtime_env = HashMap :: new ( ) ;
19+ for pkg in installations. clone ( ) {
20+ let pkg_runtime_env =
21+ libpkgx:: pantry_db:: runtime_env_for_project ( & pkg. pkg . project , & conn) ?;
22+ if !pkg_runtime_env. is_empty ( ) {
23+ runtime_env. insert ( pkg. pkg . project , pkg_runtime_env) ;
24+ }
25+ }
26+ let json = json ! ( {
27+ "pkgs" : installations,
28+ "env" : env,
29+ "runtime_env" : runtime_env
30+ } ) ;
31+ println ! ( "{}" , json) ;
32+ } else {
33+ let mut pkgs: HashMap < String , JsonV2Pkg > = HashMap :: new ( ) ;
34+ for installation in installations. clone ( ) {
35+ let env = libpkgx:: env:: map ( & vec ! [ installation. clone( ) ] ) ;
36+ let project = installation. pkg . project . clone ( ) ;
37+
38+ let mut runtime_env = libpkgx:: pantry_db:: runtime_env_for_project ( & project, & conn) ?;
39+
40+ for ( installation_key, installation_value) in runtime_env. clone ( ) {
41+ let installation_value =
42+ expand_moustaches ( & installation_value, & installation, & installations) ;
43+ runtime_env. insert ( installation_key, installation_value) ;
44+ }
45+
46+ let programs = pantry_db:: programs_for_project ( & project, & conn) ?;
47+ let companions = pantry_db:: companions_for_projects ( & [ project. clone ( ) ] , & conn) ?
48+ . iter ( )
49+ . map ( |c| c. to_string ( ) )
50+ . collect :: < Vec < String > > ( ) ;
51+
52+ let pkg = JsonV2Pkg {
53+ path : installation. path ,
54+ project,
55+ version : installation. pkg . version ,
56+ env,
57+ runtime_env,
58+ programs,
59+ companions,
60+ } ;
61+ pkgs. insert ( pkg. project . clone ( ) , pkg) ;
62+ }
63+
64+ let json = json ! ( {
65+ "pkgs" : pkgs, "env" : libpkgx:: env:: map( & installations)
66+ } ) ;
67+ println ! ( "{}" , json) ;
68+ }
69+ } else {
70+ let env = libpkgx:: env:: map ( & installations) ;
1571 let env = env
1672 . iter ( )
1773 . map ( |( k, v) | {
@@ -29,21 +85,21 @@ pub fn dump(
2985 value. replace( & format!( ":${}" , key) , & format!( "${{{}:+:${}}}" , key, key) )
3086 ) ;
3187 }
32- } else {
33- let mut runtime_env = HashMap :: new ( ) ;
34- for pkg in installations. clone ( ) {
35- let pkg_runtime_env =
36- libpkgx:: pantry_db:: runtime_env_for_project ( & pkg. pkg . project , & conn) ?;
37- if !pkg_runtime_env. is_empty ( ) {
38- runtime_env. insert ( pkg. pkg . project , pkg_runtime_env) ;
39- }
40- }
41- let json = json ! ( {
42- "pkgs" : installations,
43- "env" : env,
44- "runtime_env" : runtime_env
45- } ) ;
46- println ! ( "{}" , json) ;
4788 }
4889 Ok ( ( ) )
4990}
91+
92+ #[ derive( Serialize ) ]
93+ struct JsonV2Pkg {
94+ project : String ,
95+ version : libpkgx:: Version ,
96+ #[ serde( skip_serializing_if = "HashMap::is_empty" ) ]
97+ env : HashMap < String , Vec < String > > ,
98+ #[ serde( skip_serializing_if = "HashMap::is_empty" ) ]
99+ runtime_env : HashMap < String , String > ,
100+ path : PathBuf ,
101+ #[ serde( skip_serializing_if = "Vec::is_empty" ) ]
102+ programs : Vec < String > ,
103+ #[ serde( skip_serializing_if = "Vec::is_empty" ) ]
104+ companions : Vec < String > ,
105+ }
0 commit comments