11use structopt:: StructOpt ;
22
33use std:: {
4- path:: { PathBuf , Path } , collections:: HashMap , fs:: { File , OpenOptions , self } , io:: { Read , Write } ,
5- process:: Command
4+ collections:: HashMap ,
5+ fs:: { self , File , OpenOptions } ,
6+ io:: { Read , Write } ,
7+ path:: { Path , PathBuf } ,
8+ process:: Command ,
69} ;
710
811use glob;
@@ -40,11 +43,9 @@ fn find_cargo_tomls(path: PathBuf) -> Vec<PathBuf> {
4043 let glob = glob:: glob ( & path) . expect ( "Generates globbing pattern" ) ;
4144
4245 let mut result = Vec :: new ( ) ;
43- glob. into_iter ( ) . for_each ( |file| {
44- match file {
45- Ok ( file) => result. push ( file) ,
46- Err ( e) => println ! ( "{:?}" , e) ,
47- }
46+ glob. into_iter ( ) . for_each ( |file| match file {
47+ Ok ( file) => result. push ( file) ,
48+ Err ( e) => println ! ( "{:?}" , e) ,
4849 } ) ;
4950
5051 if result. is_empty ( ) {
@@ -78,30 +79,44 @@ fn get_git_commit_id(path: &Path) -> String {
7879/// Parse the given `Cargo.toml` into a `HashMap`
7980fn parse_cargo_toml ( file : & Path ) -> CargoToml {
8081 let mut content = String :: new ( ) ;
81- File :: open ( file) . expect ( "Cargo.toml exists" ) . read_to_string ( & mut content) . expect ( "Reads file" ) ;
82+ File :: open ( file)
83+ . expect ( "Cargo.toml exists" )
84+ . read_to_string ( & mut content)
85+ . expect ( "Reads file" ) ;
8286 toml:: from_str ( & content) . expect ( "Cargo.toml is a valid toml file" )
8387}
8488
8589/// Replaces all substrate path dependencies with a git dependency.
86- fn replace_path_dependencies_with_git ( cargo_toml_path : & Path , commit_id : & str , cargo_toml : & mut CargoToml ) {
90+ fn replace_path_dependencies_with_git (
91+ cargo_toml_path : & Path ,
92+ commit_id : & str ,
93+ cargo_toml : & mut CargoToml ,
94+ ) {
8795 let mut cargo_toml_path = cargo_toml_path. to_path_buf ( ) ;
8896 // remove `Cargo.toml`
8997 cargo_toml_path. pop ( ) ;
9098
9199 for & table in & [ "dependencies" , "build-dependencies" , "dev-dependencies" ] {
92- let mut dependencies: toml:: value:: Table = match cargo_toml
93- . remove ( table)
94- . and_then ( |v| v. try_into ( ) . ok ( ) ) {
95- Some ( deps) => deps,
96- None => continue ,
97- } ;
100+ let mut dependencies: toml:: value:: Table =
101+ match cargo_toml. remove ( table) . and_then ( |v| v. try_into ( ) . ok ( ) ) {
102+ Some ( deps) => deps,
103+ None => continue ,
104+ } ;
98105
99106 let deps_rewritten = dependencies
100107 . iter ( )
101- . filter_map ( |( k, v) | v. clone ( ) . try_into :: < toml:: value:: Table > ( ) . ok ( ) . map ( move |v| ( k, v) ) )
102- . filter ( |t| t. 1 . contains_key ( "path" ) && {
103- // if the path does not exists, we need to add this as git dependency
104- t. 1 . get ( "path" ) . unwrap ( ) . as_str ( ) . map ( |path| !cargo_toml_path. join ( path) . exists ( ) ) . unwrap_or ( false )
108+ . filter_map ( |( k, v) | {
109+ v. clone ( ) . try_into :: < toml:: value:: Table > ( ) . ok ( ) . map ( move |v| ( k, v) )
110+ } )
111+ . filter ( |t| {
112+ t. 1 . contains_key ( "path" ) && {
113+ // if the path does not exists, we need to add this as git dependency
114+ t. 1 . get ( "path" )
115+ . unwrap ( )
116+ . as_str ( )
117+ . map ( |path| !cargo_toml_path. join ( path) . exists ( ) )
118+ . unwrap_or ( false )
119+ }
105120 } )
106121 . map ( |( k, mut v) | {
107122 // remove `path` and add `git` and `rev`
@@ -110,7 +125,8 @@ fn replace_path_dependencies_with_git(cargo_toml_path: &Path, commit_id: &str, c
110125 v. insert ( "rev" . into ( ) , commit_id. into ( ) ) ;
111126
112127 ( k. clone ( ) , v. into ( ) )
113- } ) . collect :: < HashMap < _ , _ > > ( ) ;
128+ } )
129+ . collect :: < HashMap < _ , _ > > ( ) ;
114130
115131 dependencies. extend ( deps_rewritten. into_iter ( ) ) ;
116132
@@ -135,8 +151,9 @@ fn update_top_level_cargo_toml(
135151
136152 cargo_toml. insert ( "profile" . into ( ) , profile. into ( ) ) ;
137153
138- let members = workspace_members. iter ( )
139- . map ( |p|
154+ let members = workspace_members
155+ . iter ( )
156+ . map ( |p| {
140157 p. strip_prefix ( node_template_path)
141158 . expect ( "Workspace member is a child of the node template path!" )
142159 . parent ( )
@@ -145,7 +162,7 @@ fn update_top_level_cargo_toml(
145162 . expect ( "The given path ends with `Cargo.toml` as file name!" )
146163 . display ( )
147164 . to_string ( )
148- )
165+ } )
149166 . collect :: < Vec < _ > > ( ) ;
150167
151168 let mut members_section = toml:: value:: Table :: new ( ) ;
@@ -163,24 +180,20 @@ fn write_cargo_toml(path: &Path, cargo_toml: CargoToml) {
163180/// Build and test the generated node-template
164181fn build_and_test ( path : & Path , cargo_tomls : & [ PathBuf ] ) {
165182 // Build node
166- assert ! (
167- Command :: new( "cargo" )
168- . args( & [ "build" , "--all" ] )
169- . current_dir( path)
170- . status( )
171- . expect( "Compiles node" )
172- . success( )
173- ) ;
183+ assert ! ( Command :: new( "cargo" )
184+ . args( & [ "build" , "--all" ] )
185+ . current_dir( path)
186+ . status( )
187+ . expect( "Compiles node" )
188+ . success( ) ) ;
174189
175190 // Test node
176- assert ! (
177- Command :: new( "cargo" )
178- . args( & [ "test" , "--all" ] )
179- . current_dir( path)
180- . status( )
181- . expect( "Tests node" )
182- . success( )
183- ) ;
191+ assert ! ( Command :: new( "cargo" )
192+ . args( & [ "test" , "--all" ] )
193+ . current_dir( path)
194+ . status( )
195+ . expect( "Tests node" )
196+ . success( ) ) ;
184197
185198 // Remove all `target` directories
186199 for toml in cargo_tomls {
@@ -189,7 +202,8 @@ fn build_and_test(path: &Path, cargo_tomls: &[PathBuf]) {
189202 target_path = target_path. join ( "target" ) ;
190203
191204 if target_path. exists ( ) {
192- fs:: remove_dir_all ( & target_path) . expect ( & format ! ( "Removes `{}`" , target_path. display( ) ) ) ;
205+ fs:: remove_dir_all ( & target_path)
206+ . expect ( & format ! ( "Removes `{}`" , target_path. display( ) ) ) ;
193207 }
194208 }
195209}
@@ -219,7 +233,10 @@ fn main() {
219233 // Check if top level Cargo.toml exists. If not, create one in the destination
220234 if !cargo_tomls. contains ( & top_level_cargo_toml_path) {
221235 // create the top_level_cargo_toml
222- OpenOptions :: new ( ) . create ( true ) . write ( true ) . open ( top_level_cargo_toml_path. clone ( ) )
236+ OpenOptions :: new ( )
237+ . create ( true )
238+ . write ( true )
239+ . open ( top_level_cargo_toml_path. clone ( ) )
223240 . expect ( "Create root level `Cargo.toml` failed." ) ;
224241
225242 // push into our data structure
@@ -233,20 +250,30 @@ fn main() {
233250 // Check if this is the top level `Cargo.toml`, as this requires some special treatments.
234251 if top_level_cargo_toml_path == * t {
235252 // All workspace member `Cargo.toml` file paths.
236- let workspace_members = cargo_tomls. iter ( )
237- . filter ( |p| * * p != top_level_cargo_toml_path)
238- . collect ( ) ;
253+ let workspace_members =
254+ cargo_tomls. iter ( ) . filter ( |p| * * p != top_level_cargo_toml_path) . collect ( ) ;
239255
240256 update_top_level_cargo_toml ( & mut cargo_toml, workspace_members, & node_template_path) ;
241257 }
242258
243259 write_cargo_toml ( & t, cargo_toml) ;
244260 } ) ;
245261
262+ // adding root rustfmt to node template build path
263+ let node_template_rustfmt_toml_path = node_template_path. join ( "rustfmt.toml" ) ;
264+ let root_rustfmt_toml =
265+ & options. node_template . join ( "../../rustfmt.toml" ) ;
266+ if root_rustfmt_toml. exists ( ) {
267+ fs:: copy ( & root_rustfmt_toml, & node_template_rustfmt_toml_path)
268+ . expect ( "Copying rustfmt.toml." ) ;
269+ }
270+
246271 build_and_test ( & node_template_path, & cargo_tomls) ;
247272
248- let output = GzEncoder :: new ( File :: create ( & options. output )
249- . expect ( "Creates output file" ) , Compression :: default ( ) ) ;
273+ let output = GzEncoder :: new (
274+ File :: create ( & options. output ) . expect ( "Creates output file" ) ,
275+ Compression :: default ( ) ,
276+ ) ;
250277 let mut tar = tar:: Builder :: new ( output) ;
251278 tar. append_dir_all ( "substrate-node-template" , node_template_path)
252279 . expect ( "Writes substrate-node-template archive" ) ;
0 commit comments