Skip to content

Commit 69f2a6b

Browse files
bas-iemockersf
authored andcommitted
Support creating asset directories (#16220)
# Objective Exposes a means to create an asset directory (and its parent directories). Wasn't sure whether we also wanted the variant to create directories without the parent (i.e. `mkdir` instead of `mkdir -p`)? Fixes bevyengine/bevy_editor_prototypes#144
1 parent b182452 commit 69f2a6b

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

crates/bevy_asset/src/io/file/file_asset.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ impl AssetWriter for FileAssetWriter {
164164
Ok(())
165165
}
166166

167+
async fn create_directory<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> {
168+
let full_path = self.root_path.join(path);
169+
async_fs::create_dir_all(full_path).await?;
170+
Ok(())
171+
}
172+
167173
async fn remove_directory<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> {
168174
let full_path = self.root_path.join(path);
169175
async_fs::remove_dir_all(full_path).await?;

crates/bevy_asset/src/io/file/sync_file_asset.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ impl AssetWriter for FileAssetWriter {
205205
Ok(())
206206
}
207207

208+
async fn create_directory<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> {
209+
let full_path = self.root_path.join(path);
210+
std::fs::create_dir_all(full_path)?;
211+
Ok(())
212+
}
213+
208214
async fn remove_directory<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> {
209215
let full_path = self.root_path.join(path);
210216
std::fs::remove_dir_all(full_path)?;

crates/bevy_asset/src/io/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,12 @@ pub trait AssetWriter: Send + Sync + 'static {
384384
old_path: &'a Path,
385385
new_path: &'a Path,
386386
) -> impl ConditionalSendFuture<Output = Result<(), AssetWriterError>>;
387+
/// Creates a directory at the given path, including all parent directories if they do not
388+
/// already exist.
389+
fn create_directory<'a>(
390+
&'a self,
391+
path: &'a Path,
392+
) -> impl ConditionalSendFuture<Output = Result<(), AssetWriterError>>;
387393
/// Removes the directory at the given path, including all assets _and_ directories in that directory.
388394
fn remove_directory<'a>(
389395
&'a self,
@@ -460,6 +466,12 @@ pub trait ErasedAssetWriter: Send + Sync + 'static {
460466
old_path: &'a Path,
461467
new_path: &'a Path,
462468
) -> BoxedFuture<'a, Result<(), AssetWriterError>>;
469+
/// Creates a directory at the given path, including all parent directories if they do not
470+
/// already exist.
471+
fn create_directory<'a>(
472+
&'a self,
473+
path: &'a Path,
474+
) -> BoxedFuture<'a, Result<(), AssetWriterError>>;
463475
/// Removes the directory at the given path, including all assets _and_ directories in that directory.
464476
fn remove_directory<'a>(
465477
&'a self,
@@ -523,6 +535,12 @@ impl<T: AssetWriter> ErasedAssetWriter for T {
523535
) -> BoxedFuture<'a, Result<(), AssetWriterError>> {
524536
Box::pin(Self::rename_meta(self, old_path, new_path))
525537
}
538+
fn create_directory<'a>(
539+
&'a self,
540+
path: &'a Path,
541+
) -> BoxedFuture<'a, Result<(), AssetWriterError>> {
542+
Box::pin(Self::create_directory(self, path))
543+
}
526544
fn remove_directory<'a>(
527545
&'a self,
528546
path: &'a Path,

0 commit comments

Comments
 (0)