fix(xtask): 下载时创建父目录

This commit is contained in:
YdrMaster 2022-05-11 09:51:42 +08:00
parent 9f991c6b4c
commit 64bf09c596
2 changed files with 20 additions and 8 deletions

View File

@ -1,6 +1,9 @@
//! 操作目录。
use std::path::{Path, PathBuf};
use std::{
fs,
path::{Path, PathBuf},
};
/// 删除指定路径。
///
@ -10,9 +13,17 @@ pub fn rm(path: impl AsRef<Path>) -> std::io::Result<()> {
if !path.exists() {
Ok(())
} else if path.is_dir() {
std::fs::remove_dir_all(path)
fs::remove_dir_all(path)
} else {
std::fs::remove_file(path)
fs::remove_file(path)
}
}
/// 创建 `path` 的父目录。
pub fn create_parent(path: impl AsRef<Path>) -> std::io::Result<()> {
match path.as_ref().parent() {
Some(parent) => fs::create_dir_all(parent),
None => Ok(()),
}
}

View File

@ -15,9 +15,10 @@ pub(crate) fn wget(url: impl AsRef<OsStr>, dst: impl AsRef<Path>) {
.status()
.unwrap();
if status.success() {
fs::rename(&tmp, dst).unwrap();
dir::create_parent(&dst).unwrap();
fs::rename(tmp, dst).unwrap();
} else {
dir::rm(&tmp).unwrap();
dir::rm(tmp).unwrap();
panic!(
"Failed with code {}: wget {:?}",
status.code().unwrap(),
@ -39,10 +40,10 @@ pub(crate) fn git_clone(repo: impl AsRef<OsStr>, dst: impl AsRef<Path>) {
let mut git = Git::clone(repo, Some(&tmp));
let status = git.status();
if status.success() {
dir::clear(dst).unwrap();
fs::rename(&tmp, dst).unwrap();
dir::create_parent(&dst).unwrap();
fs::rename(tmp, dst).unwrap();
} else {
dir::rm(&tmp).unwrap();
dir::rm(tmp).unwrap();
panic!(
"Failed with code {}: {:?}",
status.code().unwrap(),