feat: commit code

This commit is contained in:
mrxiaozhuox committed 2022-03-13 13:19:54 +08:00
1 parent 25ff6c5937
commit eea01f7f8c
5 files changed
+93 -4

No files matched your search

+2
View File
@@ -43,6 +43,8 @@ headers = "0.3.7"
# tools download
dirs = "4.0.0"
reqwest = { version = "0.11", features = ["rustls-tls", "stream", "trust-dns"] }
flate2 = "1.0.22"
tar = "0.4.38"
[[bin]]
path = "src/main.rs"
+4
View File
@@ -6,6 +6,7 @@ pub mod create;
pub mod serve;
pub mod translate;
pub mod tools;
pub mod tool;
use crate::{
cfg::{ConfigOptsBuild, ConfigOptsServe},
@@ -56,4 +57,7 @@ pub enum Commands {
/// Dioxus config file controls.
#[clap(subcommand)]
Config(config::Config),
/// Install & Manage tools for Dioxus-cli.
#[clap(subcommand)]
Tool(tool::Tool),
}
+40
View File
@@ -0,0 +1,40 @@
use super::*;
/// Build the Rust WASM app and all of its assets.
#[derive(Clone, Debug, Deserialize, Subcommand)]
#[clap(name = "tool")]
pub enum Tool {
/// Return all dioxus-cli support tools.
List {},
Add {
name: String
}
}
impl Tool {
pub async fn tool(self) -> Result<()> {
match self {
Tool::List {} => {
for item in tools::tool_list() {
if tools::Tool::from_str(item).unwrap().is_installed() {
println!("{item} [installed]");
} else {
println!("{item}");
}
}
}
Tool::Add { name } => {
let tool_list = tools::tool_list();
if !tool_list.contains(&name.as_str()) {
log::error!("Tool {name} not found.");
return Ok(());
}
let target_tool = tools::Tool::from_str(&name).unwrap();
println!("{:?}", target_tool.download_package().await);
}
}
Ok(())
}
}
+41 -4
View File
@@ -8,6 +8,10 @@ pub enum Tool {
WasmOpt,
}
pub fn tool_list() -> Vec<&'static str> {
vec!["wasm-opt"]
}
pub fn app_path() -> PathBuf {
let data_local = dirs::data_local_dir().unwrap();
let dioxus_dir = data_local.join("dioxus");
@@ -26,7 +30,24 @@ pub fn temp_path() -> PathBuf {
temp_path
}
pub fn tools_path(tool: &Tool) -> PathBuf {
let app_path = app_path();
let temp_path = app_path.join("tools");
if !temp_path.is_dir() {
create_dir_all(&temp_path).unwrap();
}
temp_path.join(tool.name())
}
#[allow(clippy::should_implement_trait)]
impl Tool {
pub fn from_str(name: &str) -> Option<Self> {
match name {
"wasm-opt" => Some(Self::WasmOpt),
_ => None,
}
}
pub fn name(&self) -> &str {
match self {
Self::WasmOpt => "wasm-opt",
@@ -72,11 +93,23 @@ impl Tool {
}
}
pub async fn download_package(&self) -> anyhow::Result<PathBuf> {
let temp_dir = temp_path();
let download_url = self.download_url();
pub fn extension(&self) -> &str {
match self {
Self::WasmOpt => "tar.gz",
}
}
let temp_out = temp_dir.join(format!("{}-tool.tmp", self.name()));
pub fn is_installed(&self) -> bool {
tools_path(self).is_dir()
}
pub fn temp_out_path(&self) -> PathBuf {
temp_path().join(format!("{}-tool.tmp", self.name()))
}
pub async fn download_package(&self) -> anyhow::Result<PathBuf> {
let download_url = self.download_url();
let temp_out = self.temp_out_path();
let mut file = tokio::fs::File::create(&temp_out)
.await
.context("failed creating temporary output file")?;
@@ -91,4 +124,8 @@ impl Tool {
Ok(temp_out)
}
pub async fn install_package() -> anyhow::Result<()> {
Ok(())
}
}
+6
View File
@@ -42,6 +42,12 @@ async fn main() -> Result<()> {
log::error!("config error: {}", e);
}
}
Commands::Tool(opts) => {
if let Err(e) = opts.tool().await {
log::error!("tool error: {}", e);
}
}
}
Ok(())