init subcommand (#1840)

* `init` subcommand

* clippy
This commit is contained in:
hem1t authored and GitHub committed 2024-01-16 10:25:07 -06:00
1 parent 1db8bd7edd
commit c6e1b0f728
4 files changed
+50 -3

No files matched your search

+7 -2
View File
@@ -21,9 +21,15 @@ impl Create {
let path = cargo_generate::generate(args)?; let path = cargo_generate::generate(args)?;
post_create(&path)
}
}
// being also used by `init`
pub fn post_create(path: &PathBuf) -> Result<()> {
// first run cargo fmt // first run cargo fmt
let mut cmd = Command::new("cargo"); let mut cmd = Command::new("cargo");
let cmd = cmd.arg("fmt").current_dir(&path); let cmd = cmd.arg("fmt").current_dir(path);
let output = cmd.output().expect("failed to execute process"); let output = cmd.output().expect("failed to execute process");
if !output.status.success() { if !output.status.success() {
log::error!("cargo fmt failed"); log::error!("cargo fmt failed");
@@ -61,7 +67,6 @@ impl Create {
log::info!("Generated project at {}", path.display()); log::info!("Generated project at {}", path.display());
Ok(()) Ok(())
}
} }
fn remove_triple_newlines(string: &str) -> String { fn remove_triple_newlines(string: &str) -> String {
+33
View File
@@ -0,0 +1,33 @@
use super::*;
use cargo_generate::{GenerateArgs, TemplatePath};
#[derive(Clone, Debug, Default, Deserialize, Parser)]
#[clap(name = "init")]
pub struct Init {
/// Template path
#[clap(default_value = "gh:dioxuslabs/dioxus-template", long)]
template: String,
}
impl Init {
pub fn init(self) -> Result<()> {
// get dir name
let name = std::env::current_dir()?
.file_name()
.map(|f| f.to_str().unwrap().to_string());
let args = GenerateArgs {
template_path: TemplatePath {
auto_path: Some(self.template),
..Default::default()
},
name,
init: true,
..Default::default()
};
let path = cargo_generate::generate(args)?;
create::post_create(&path)
}
}
+6 -1
View File
@@ -6,6 +6,7 @@ pub mod check;
pub mod clean; pub mod clean;
pub mod config; pub mod config;
pub mod create; pub mod create;
pub mod init;
pub mod plugin; pub mod plugin;
pub mod serve; pub mod serve;
pub mod translate; pub mod translate;
@@ -56,9 +57,12 @@ pub enum Commands {
/// Build, watch & serve the Rust WASM app and all of its assets. /// Build, watch & serve the Rust WASM app and all of its assets.
Serve(serve::Serve), Serve(serve::Serve),
/// Init a new project for Dioxus. /// Create a new project for Dioxus.
Create(create::Create), Create(create::Create),
/// Init a new project for Dioxus
Init(init::Init),
/// Clean output artifacts. /// Clean output artifacts.
Clean(clean::Clean), Clean(clean::Clean),
@@ -94,6 +98,7 @@ impl Display for Commands {
Commands::Translate(_) => write!(f, "translate"), Commands::Translate(_) => write!(f, "translate"),
Commands::Serve(_) => write!(f, "serve"), Commands::Serve(_) => write!(f, "serve"),
Commands::Create(_) => write!(f, "create"), Commands::Create(_) => write!(f, "create"),
Commands::Init(_) => write!(f, "init"),
Commands::Clean(_) => write!(f, "clean"), Commands::Clean(_) => write!(f, "clean"),
Commands::Config(_) => write!(f, "config"), Commands::Config(_) => write!(f, "config"),
Commands::Version(_) => write!(f, "version"), Commands::Version(_) => write!(f, "version"),
+4
View File
@@ -52,6 +52,10 @@ async fn main() -> anyhow::Result<()> {
.create() .create()
.map_err(|e| anyhow!("🚫 Creating new project failed: {}", e)), .map_err(|e| anyhow!("🚫 Creating new project failed: {}", e)),
Init(opts) => opts
.init()
.map_err(|e| anyhow!("🚫 Initialising a new project failed: {}", e)),
Config(opts) => opts Config(opts) => opts
.config() .config()
.map_err(|e| anyhow!("🚫 Configuring new project failed: {}", e)), .map_err(|e| anyhow!("🚫 Configuring new project failed: {}", e)),