11use crate :: cargo_make:: CargoMake ;
2+ use crate :: common:: fs;
23use crate :: docker:: DockerContainer ;
34use crate :: project;
45use crate :: tools:: install_tools;
56use anyhow:: { Context , Result } ;
67use clap:: Parser ;
78use log:: debug;
8- use std:: fs;
99use std:: path:: { Path , PathBuf } ;
1010use tempfile:: TempDir ;
11- use tokio:: fs:: { remove_dir_all, remove_file} ;
1211
1312#[ derive( Debug , Parser ) ]
1413pub ( crate ) enum BuildCommand {
@@ -43,15 +42,16 @@ impl BuildVariant {
4342 let project = project:: load_or_find_project ( self . project_path . clone ( ) ) . await ?;
4443 let token = project. token ( ) ;
4544 let toolsdir = project. project_dir ( ) . join ( "build/tools" ) ;
46- tokio:: fs:: remove_dir_all ( & toolsdir) . await ?;
47- tokio:: fs:: create_dir_all ( & toolsdir) . await ?;
45+ // Ignore errors because we want to proceed even if the directory does not exist.
46+ let _ = fs:: remove_dir_all ( & toolsdir) . await ;
47+ fs:: create_dir_all ( & toolsdir) . await ?;
4848 install_tools ( & toolsdir) . await ?;
4949 let makefile_path = toolsdir. join ( "Makefile.toml" ) ;
5050 // A temporary directory in the `build` directory
5151 let build_temp_dir = TempDir :: new_in ( project. project_dir ( ) )
5252 . context ( "Unable to create a tempdir for Twoliter's build" ) ?;
5353 let packages_dir = build_temp_dir. path ( ) . join ( "sdk_rpms" ) ;
54- fs:: create_dir_all ( & packages_dir) ?;
54+ fs:: create_dir_all ( & packages_dir) . await ?;
5555
5656 let sdk_container = DockerContainer :: new (
5757 format ! ( "sdk-{}" , token) ,
@@ -70,12 +70,18 @@ impl BuildVariant {
7070 . await ?;
7171
7272 let rpms_dir = project. project_dir ( ) . join ( "build" ) . join ( "rpms" ) ;
73- fs:: create_dir_all ( & rpms_dir) ?;
73+ fs:: create_dir_all ( & rpms_dir) . await ?;
7474 debug ! ( "Moving rpms to build dir" ) ;
75- for maybe_file in fs:: read_dir ( packages_dir. join ( "rpms" ) ) ? {
76- let file = maybe_file?;
77- debug ! ( "Moving '{}'" , file. path( ) . display( ) ) ;
78- fs:: rename ( file. path ( ) , rpms_dir. join ( file. file_name ( ) ) ) ?;
75+ let rpms = packages_dir. join ( "rpms" ) ;
76+ let mut read_dir = tokio:: fs:: read_dir ( & rpms)
77+ . await
78+ . context ( format ! ( "Unable to read dir '{}'" , rpms. display( ) ) ) ?;
79+ while let Some ( entry) = read_dir. next_entry ( ) . await . context ( format ! (
80+ "Error while reading entries in dir '{}'" ,
81+ rpms. display( )
82+ ) ) ? {
83+ debug ! ( "Moving '{}'" , entry. path( ) . display( ) ) ;
84+ fs:: rename ( entry. path ( ) , rpms_dir. join ( entry. file_name ( ) ) ) . await ?;
7985 }
8086
8187 let mut created_files = Vec :: new ( ) ;
@@ -84,7 +90,7 @@ impl BuildVariant {
8490 if !sbkeys_dir. is_dir ( ) {
8591 // Create a sbkeys directory in the main project
8692 debug ! ( "sbkeys dir not found. Creating a temporary directory" ) ;
87- fs:: create_dir_all ( & sbkeys_dir) ?;
93+ fs:: create_dir_all ( & sbkeys_dir) . await ?;
8894 sdk_container
8995 . cp_out (
9096 Path :: new ( "twoliter/alpha/sbkeys/generate-local-sbkeys" ) ,
@@ -99,6 +105,7 @@ impl BuildVariant {
99105 if !models_dir. is_dir ( ) {
100106 debug ! ( "models source dir not found. Creating a temporary directory" ) ;
101107 fs:: create_dir_all ( & models_dir. join ( "src/variant" ) )
108+ . await
102109 . context ( "Unable to create models source directory" ) ?;
103110 created_files. push ( models_dir)
104111 }
@@ -120,9 +127,9 @@ impl BuildVariant {
120127 for file_name in created_files {
121128 let added = Path :: new ( & file_name) ;
122129 if added. is_file ( ) {
123- remove_file ( added) . await ?;
130+ fs :: remove_file ( added) . await ?;
124131 } else if added. is_dir ( ) {
125- remove_dir_all ( added) . await ?;
132+ fs :: remove_dir_all ( added) . await ?;
126133 }
127134 }
128135
0 commit comments