Add zip support
This commit is contained in:
1 parent
5d013ecf82
commit
c48345b86e
2 files changed
+41
-7
No files matched your search
@@ -39,11 +39,13 @@ hyper = "0.14.17"
|
|||||||
axum = { version = "0.5.1", features = ["ws", "headers"] }
|
axum = { version = "0.5.1", features = ["ws", "headers"] }
|
||||||
tower-http = { version = "0.2.2", features = ["fs", "trace"] }
|
tower-http = { version = "0.2.2", features = ["fs", "trace"] }
|
||||||
headers = "0.3.7"
|
headers = "0.3.7"
|
||||||
|
|
||||||
# tools download
|
# tools download
|
||||||
dirs = "4.0.0"
|
dirs = "4.0.0"
|
||||||
reqwest = { version = "0.11", features = ["rustls-tls", "stream", "trust-dns"] }
|
reqwest = { version = "0.11", features = ["rustls-tls", "stream", "trust-dns"] }
|
||||||
flate2 = "1.0.22"
|
flate2 = "1.0.22"
|
||||||
tar = "0.4.38"
|
tar = "0.4.38"
|
||||||
|
zip = "0.6.2"
|
||||||
tower = "0.4.12"
|
tower = "0.4.12"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
|
|||||||
+39
-7
@@ -1,6 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
fs::{create_dir_all, File},
|
fs::{create_dir_all, File},
|
||||||
path::PathBuf,
|
path::{Path, PathBuf},
|
||||||
process::Command,
|
process::Command,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -112,8 +112,9 @@ impl Tool {
|
|||||||
}
|
}
|
||||||
Self::Sass => {
|
Self::Sass => {
|
||||||
format!(
|
format!(
|
||||||
"https://github.com/sass/dart-sass/releases/download/1.51.0/dart-sass-1.51.0-{target}-x64.tar.gz",
|
"https://github.com/sass/dart-sass/releases/download/1.51.0/dart-sass-1.51.0-{target}-x64.{extension}",
|
||||||
target = self.target_platform()
|
target = self.target_platform(),
|
||||||
|
extension = self.extension()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -129,7 +130,7 @@ impl Tool {
|
|||||||
} else {
|
} else {
|
||||||
"tar.ge"
|
"tar.ge"
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,6 +170,8 @@ impl Tool {
|
|||||||
|
|
||||||
let dir_name = if self == &Tool::Binaryen {
|
let dir_name = if self == &Tool::Binaryen {
|
||||||
"binaryen-version_105"
|
"binaryen-version_105"
|
||||||
|
} else if self == &Tool::Sass {
|
||||||
|
"sass-version-1_51"
|
||||||
} else {
|
} else {
|
||||||
""
|
""
|
||||||
};
|
};
|
||||||
@@ -180,6 +183,8 @@ impl Tool {
|
|||||||
archive.unpack(&tool_path)?;
|
archive.unpack(&tool_path)?;
|
||||||
// println!("{:?} -> {:?}", tool_path.join(dir_name), tool_path.join(self.name()));
|
// println!("{:?} -> {:?}", tool_path.join(dir_name), tool_path.join(self.name()));
|
||||||
std::fs::rename(tool_path.join(dir_name), tool_path.join(self.name()))?;
|
std::fs::rename(tool_path.join(dir_name), tool_path.join(self.name()))?;
|
||||||
|
} else if self.extension() == "zip" {
|
||||||
|
// decompress the `zip` file
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -196,9 +201,7 @@ impl Tool {
|
|||||||
command.to_string()
|
command.to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Tool::Sass => {
|
Tool::Sass => command.to_string(),
|
||||||
command.to_string()
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if !bin_path.join(&command_file).is_file() {
|
if !bin_path.join(&command_file).is_file() {
|
||||||
@@ -215,3 +218,32 @@ impl Tool {
|
|||||||
Ok(output.stdout)
|
Ok(output.stdout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn extract_zip(file: &Path, target: &Path) -> anyhow::Result<()> {
|
||||||
|
let zip_file = std::fs::File::open(&file)?;
|
||||||
|
let mut zip = zip::ZipArchive::new(zip_file)?;
|
||||||
|
|
||||||
|
if !target.exists() {
|
||||||
|
let _ = std::fs::create_dir_all(target)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
for i in 0..zip.len() {
|
||||||
|
let mut file = zip.by_index(i)?;
|
||||||
|
if file.is_dir() {
|
||||||
|
// dir
|
||||||
|
let target = target.join(Path::new(&file.name().replace('\\', "")));
|
||||||
|
let _ = std::fs::create_dir_all(target)?;
|
||||||
|
} else {
|
||||||
|
// file
|
||||||
|
let file_path = target.join(Path::new(file.name()));
|
||||||
|
let mut target_file = if !file_path.exists() {
|
||||||
|
std::fs::File::create(file_path)?
|
||||||
|
} else {
|
||||||
|
std::fs::File::open(file_path)?
|
||||||
|
};
|
||||||
|
let _num = std::io::copy(&mut file, &mut target_file)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Reference in new issue
Block a user