@@ -11,159 +11,104 @@ use std::path::PathBuf;
1111type Dir = String ;
1212type PackageName = String ;
1313type AbsolutePath = String ;
14- type PackagePath = String ;
14+ type Pkg = ( PackageName , AbsolutePath ) ;
1515
1616#[ derive( Serialize , Debug , Clone , PartialEq , Hash ) ]
17- pub struct SourceDirs {
18- pub dirs : Vec < Dir > ,
19- pub pkgs : Vec < ( PackageName , AbsolutePath ) > ,
20- pub generated : Vec < String > ,
17+ pub struct SourceDirs < ' a > {
18+ pub dirs : & ' a Vec < Dir > ,
19+ pub pkgs : & ' a Vec < Pkg > ,
20+ pub generated : & ' a Vec < String > ,
2121}
2222
23- pub fn print ( buildstate : & BuildState ) {
24- // Take all packages apart from the root package
25- let child_packages = buildstate
26- . packages
27- . par_iter ( )
28- . filter ( |( _name, package) | !package. is_root )
29- . map ( |( _name, package) | {
30- let path = package. get_build_path ( ) ;
31-
32- let dirs = package
33- . dirs
34- . to_owned ( )
35- . unwrap_or ( AHashSet :: new ( ) )
36- . iter ( )
37- . filter_map ( |path| path. to_str ( ) . map ( String :: from) )
38- . collect :: < AHashSet < String > > ( ) ;
39-
40- fn deps_to_pkgs < ' a > (
41- packages : & ' a AHashMap < String , Package > ,
42- dependencies : & ' a Option < Vec < String > > ,
43- ) -> AHashSet < ( String , PackagePath ) > {
44- dependencies
45- . as_ref ( )
46- . unwrap_or ( & vec ! [ ] )
47- . iter ( )
48- . filter_map ( |name| {
49- packages
50- . get ( & name. to_owned ( ) )
51- . map ( |package| ( name. clone ( ) , package. path . clone ( ) ) )
52- } )
53- . collect :: < AHashSet < ( String , PackagePath ) > > ( )
54- }
55-
56- let pinned_dependencies =
57- deps_to_pkgs ( & buildstate. packages , & package. bsconfig . pinned_dependencies ) ;
58- let bs_dependencies = deps_to_pkgs ( & buildstate. packages , & package. bsconfig . bs_dependencies ) ;
59- let bs_dev_dependencies =
60- deps_to_pkgs ( & buildstate. packages , & package. bsconfig . bs_dev_dependencies ) ;
61-
62- let mut pkgs = AHashMap :: new ( ) ;
63- pkgs. extend ( pinned_dependencies) ;
64- pkgs. extend ( bs_dependencies) ;
65- pkgs. extend ( bs_dev_dependencies) ;
66-
67- let name = path + "/.sourcedirs.json" ;
68- let _ = File :: create ( & name) . map ( |mut file| {
69- let source_files = SourceDirs {
70- dirs : dirs. clone ( ) . into_iter ( ) . collect :: < Vec < Dir > > ( ) ,
71- pkgs : pkgs
72- . clone ( )
73- . into_iter ( )
74- . collect :: < Vec < ( PackageName , AbsolutePath ) > > ( ) ,
75- generated : vec ! [ ] ,
76- } ;
77-
78- file. write ( json ! ( source_files) . to_string ( ) . as_bytes ( ) )
79- } ) ;
80- let _ = std:: fs:: copy ( package. get_bs_build_path ( ) , package. get_build_path ( ) ) ;
23+ fn package_to_dirs < ' a > ( package : & ' a Package , root_package_path : & String ) -> AHashSet < Dir > {
24+ let relative_path = PathBuf :: from ( & package. path )
25+ . strip_prefix ( PathBuf :: from ( & root_package_path) )
26+ . unwrap ( )
27+ . to_string_lossy ( )
28+ . to_string ( ) ;
29+
30+ package
31+ . dirs
32+ . as_ref ( )
33+ . unwrap_or ( & AHashSet :: new ( ) )
34+ . iter ( )
35+ . filter_map ( |path| path. to_str ( ) . map ( |path| format ! ( "{relative_path}/{path}" ) ) )
36+ . collect :: < AHashSet < String > > ( )
37+ }
8138
82- ( & package. path , dirs, pkgs)
39+ fn deps_to_pkgs < ' a > (
40+ packages : & ' a AHashMap < String , Package > ,
41+ dependencies : & ' a Option < Vec < String > > ,
42+ ) -> AHashSet < Pkg > {
43+ dependencies
44+ . as_ref ( )
45+ . unwrap_or ( & vec ! [ ] )
46+ . iter ( )
47+ . filter_map ( |name| {
48+ packages
49+ . get ( name)
50+ . map ( |package| ( name. to_owned ( ) , package. path . to_owned ( ) ) )
8351 } )
84- . collect :: < Vec < (
85- & PackagePath ,
86- AHashSet < String > ,
87- AHashMap < PackageName , AbsolutePath > ,
88- ) > > ( ) ;
52+ . collect :: < AHashSet < Pkg > > ( )
53+ }
8954
90- let mut all_dirs = AHashSet :: new ( ) ;
91- let mut all_pkgs: AHashMap < PackageName , AbsolutePath > = AHashMap :: new ( ) ;
55+ fn write_sourcedirs_files ( path : String , source_dirs : & SourceDirs ) -> Result < usize , std:: io:: Error > {
56+ let mut source_dirs_json = File :: create ( path + "/.sourcedirs.json" ) ?;
57+ source_dirs_json. write ( json ! ( source_dirs) . to_string ( ) . as_bytes ( ) )
58+ }
9259
60+ pub fn print ( buildstate : & BuildState ) {
9361 // Find Root Package
9462 let ( _name, root_package) = buildstate
9563 . packages
9664 . iter ( )
9765 . find ( |( _name, package) | package. is_root )
9866 . expect ( "Could not find root package" ) ;
9967
100- child_packages. iter ( ) . for_each ( |( package_path, dirs, pkgs) | {
101- let relative_filename = PathBuf :: from ( & package_path)
102- . strip_prefix ( PathBuf :: from ( & root_package. path ) )
103- . unwrap ( )
104- . to_string_lossy ( )
105- . to_string ( ) ;
106-
107- dirs. iter ( ) . for_each ( |dir| {
108- all_dirs. insert ( format ! ( "{relative_filename}/{dir}" ) ) ;
109- } ) ;
110-
111- all_pkgs. extend ( pkgs. to_owned ( ) ) ;
112- } ) ;
113-
114- let path = root_package. get_bs_build_path ( ) ;
115- let name = path + "/.sourcedirs.json" ;
116-
117- let _ = File :: create ( name. clone ( ) ) . map ( |mut file| {
118- let all_source_files = SourceDirs {
119- dirs : all_dirs. into_iter ( ) . collect :: < Vec < String > > ( ) ,
120- pkgs : all_pkgs. into_iter ( ) . collect :: < Vec < ( PackageName , AbsolutePath ) > > ( ) ,
121- generated : vec ! [ ] ,
122- } ;
123- file. write ( json ! ( all_source_files) . to_string ( ) . as_bytes ( ) )
124- } ) ;
125-
126- let _ = std:: fs:: copy ( root_package. get_bs_build_path ( ) , root_package. get_build_path ( ) ) ;
127- }
128-
129- /*
130- {
131- "dirs": [
132- "/Users/rwjpeelen/Git/rewatch/testrepo/packages/dep02/src",
133- "/Users/rwjpeelen/Git/rewatch/testrepo/packages/main/src",
134- "/Users/rwjpeelen/Git/rewatch/testrepo/packages/new-namespace/src",
135- "/Users/rwjpeelen/Git/rewatch/testrepo/packages/dep01/src"
136- ],
137- "generated": [],
138- "pkgs": [
139- [
140- "@testrepo/new-namespace",
141- "/Users/rwjpeelen/Git/rewatch/testrepo/packages/new-namespace"
142- ],
143- ["@testrepo/dep01", "/Users/rwjpeelen/Git/rewatch/testrepo/packages/dep01"],
144- ["@testrepo/dep02", "/Users/rwjpeelen/Git/rewatch/testrepo/packages/dep02"]
145- ]
146- }
147- */
148-
149- /*
150- {
151- "dirs":[
152- "src",
153- "src/assets"
154- ],
155- "pkgs":[
156- [
157- "@rescript/core",
158- "/Users/rwjpeelen/Git/walnut/test-reanalyze/node_modules/@rescript/core"
159- ],
160- [
161- "@rescript/react",
162- "/Users/rwjpeelen/Git/walnut/test-reanalyze/node_modules/@rescript/react"
163- ]
164- ],
165- "generated":[
166-
167- ]
68+ // Take all packages apart from the root package
69+ let ( dirs, pkgs) : ( Vec < AHashSet < Dir > > , Vec < AHashMap < PackageName , AbsolutePath > > ) = buildstate
70+ . packages
71+ . par_iter ( )
72+ . filter ( |( _name, package) | !package. is_root )
73+ . map ( |( _name, package) | {
74+ // Extract Directories
75+ let dirs = package_to_dirs ( & package, & root_package. path ) ;
76+
77+ // Extract Pkgs
78+ let pkgs = [
79+ & package. bsconfig . pinned_dependencies ,
80+ & package. bsconfig . bs_dependencies ,
81+ & package. bsconfig . bs_dev_dependencies ,
82+ ]
83+ . into_iter ( )
84+ . map ( |dependencies| deps_to_pkgs ( & buildstate. packages , dependencies) ) ;
85+
86+ // Write sourcedirs.json
87+ write_sourcedirs_files (
88+ package. get_build_path ( ) ,
89+ & SourceDirs {
90+ dirs : & dirs. clone ( ) . into_iter ( ) . collect :: < Vec < Dir > > ( ) ,
91+ pkgs : & pkgs. clone ( ) . flatten ( ) . collect :: < Vec < Pkg > > ( ) ,
92+ generated : & vec ! [ ] ,
93+ } ,
94+ )
95+ . expect ( "Could not write sourcedirs.json" ) ;
96+
97+ (
98+ dirs,
99+ pkgs. flatten ( ) . collect :: < AHashMap < PackageName , AbsolutePath > > ( ) ,
100+ )
101+ } )
102+ . unzip ( ) ;
103+
104+ // Write sourcedirs.json
105+ write_sourcedirs_files (
106+ root_package. get_bs_build_path ( ) ,
107+ & SourceDirs {
108+ dirs : & dirs. into_iter ( ) . flatten ( ) . collect :: < Vec < Dir > > ( ) ,
109+ pkgs : & pkgs. into_iter ( ) . flatten ( ) . collect :: < Vec < Pkg > > ( ) ,
110+ generated : & vec ! [ ] ,
111+ } ,
112+ )
113+ . expect ( "Could not write sourcedirs.json" ) ;
168114}
169- * */
0 commit comments