mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-10 06:34:20 +00:00
Workspace support for dx fmt
(#2745)
This commit is contained in:
parent
74e51b7e0d
commit
ef0202f999
1 changed files with 43 additions and 19 deletions
|
@ -1,8 +1,9 @@
|
|||
use super::*;
|
||||
use crate::DioxusCrate;
|
||||
use build::TargetArgs;
|
||||
use dioxus_autofmt::{IndentOptions, IndentType};
|
||||
use rayon::prelude::*;
|
||||
use std::{fs, path::Path, process::exit};
|
||||
|
||||
use super::*;
|
||||
use std::{borrow::Cow, fs, path::Path, process::exit};
|
||||
|
||||
// For reference, the rustfmt main.rs file
|
||||
// https://github.com/rust-lang/rustfmt/blob/master/src/bin/main.rs
|
||||
|
@ -30,6 +31,10 @@ pub struct Autoformat {
|
|||
/// Split attributes in lines or not
|
||||
#[clap(short, long, default_value = "false")]
|
||||
pub split_line_attributes: bool,
|
||||
|
||||
/// The package to build
|
||||
#[clap(short, long)]
|
||||
pub package: Option<String>,
|
||||
}
|
||||
|
||||
impl Autoformat {
|
||||
|
@ -43,15 +48,11 @@ impl Autoformat {
|
|||
..
|
||||
} = self;
|
||||
|
||||
// Default to formatting the project
|
||||
if raw.is_none() && file.is_none() {
|
||||
if let Err(e) = autoformat_project(check, split_line_attributes, format_rust_code) {
|
||||
eprintln!("error formatting project: {}", e);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(raw) = raw {
|
||||
if let Some(file) = file {
|
||||
// Format a single file
|
||||
refactor_file(file, split_line_attributes, format_rust_code)?;
|
||||
} else if let Some(raw) = raw {
|
||||
// Format raw text.
|
||||
let indent = indentation_for(".", self.split_line_attributes)?;
|
||||
if let Some(inner) = dioxus_autofmt::fmt_block(&raw, 0, indent) {
|
||||
println!("{}", inner);
|
||||
|
@ -60,11 +61,32 @@ impl Autoformat {
|
|||
eprintln!("error formatting codeblock");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Default to formatting the project.
|
||||
let crate_dir = if let Some(package) = self.package {
|
||||
// TODO (matt): Do we need to use the entire `DioxusCrate` here?
|
||||
let target_args = TargetArgs {
|
||||
package: Some(package),
|
||||
..Default::default()
|
||||
};
|
||||
let dx_crate = match DioxusCrate::new(&target_args) {
|
||||
Ok(x) => x,
|
||||
Err(error) => {
|
||||
eprintln!("failed to parse crate graph: {error}");
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
Cow::Owned(dx_crate.crate_dir())
|
||||
} else {
|
||||
Cow::Borrowed(Path::new("."))
|
||||
};
|
||||
|
||||
// Format single file
|
||||
if let Some(file) = file {
|
||||
refactor_file(file, split_line_attributes, format_rust_code)?;
|
||||
if let Err(e) =
|
||||
autoformat_project(check, split_line_attributes, format_rust_code, crate_dir)
|
||||
{
|
||||
eprintln!("error formatting project: {}", e);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -108,9 +130,9 @@ fn refactor_file(
|
|||
}
|
||||
|
||||
use std::ffi::OsStr;
|
||||
fn get_project_files() -> Vec<PathBuf> {
|
||||
fn get_project_files(dir: impl AsRef<Path>) -> Vec<PathBuf> {
|
||||
let mut files = Vec::new();
|
||||
for result in ignore::Walk::new("./") {
|
||||
for result in ignore::Walk::new(dir) {
|
||||
let path = result.unwrap().into_path();
|
||||
if let Some(ext) = path.extension() {
|
||||
if ext == OsStr::new("rs") {
|
||||
|
@ -161,8 +183,9 @@ fn autoformat_project(
|
|||
check: bool,
|
||||
split_line_attributes: bool,
|
||||
format_rust_code: bool,
|
||||
dir: impl AsRef<Path>,
|
||||
) -> Result<()> {
|
||||
let files_to_format = get_project_files();
|
||||
let files_to_format = get_project_files(dir);
|
||||
|
||||
if files_to_format.is_empty() {
|
||||
return Ok(());
|
||||
|
@ -285,6 +308,7 @@ async fn test_auto_fmt() {
|
|||
raw: Some(test_rsx),
|
||||
file: None,
|
||||
split_line_attributes: false,
|
||||
package: None,
|
||||
};
|
||||
|
||||
fmt.autoformat().unwrap();
|
||||
|
|
Loading…
Reference in a new issue