Skip to content

Commit 4479202

Browse files
authored
Merge pull request #131 from webern/common-fs
fix build variant remove_dir_all and use crate::common::fs
2 parents 9ed19b7 + 3314e83 commit 4479202

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

twoliter/src/cmd/build.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use crate::cargo_make::CargoMake;
2+
use crate::common::fs;
23
use crate::docker::DockerContainer;
34
use crate::project;
45
use crate::tools::install_tools;
56
use anyhow::{Context, Result};
67
use clap::Parser;
78
use log::debug;
8-
use std::fs;
99
use std::path::{Path, PathBuf};
1010
use tempfile::TempDir;
11-
use tokio::fs::{remove_dir_all, remove_file};
1211

1312
#[derive(Debug, Parser)]
1413
pub(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

twoliter/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub(crate) mod fs {
148148

149149
pub(crate) async fn remove_file(path: impl AsRef<Path>) -> Result<()> {
150150
fs::remove_file(path.as_ref()).await.context(format!(
151-
"Unable to XXSOMETHINGXX '{}'",
151+
"Unable to remove file '{}'",
152152
path.as_ref().display()
153153
))
154154
}

0 commit comments

Comments
 (0)