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 dioxus_autofmt::{IndentOptions, IndentType};
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use std::{fs, path::Path, process::exit};
|
use std::{borrow::Cow, fs, path::Path, process::exit};
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
// For reference, the rustfmt main.rs file
|
// For reference, the rustfmt main.rs file
|
||||||
// https://github.com/rust-lang/rustfmt/blob/master/src/bin/main.rs
|
// 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
|
/// Split attributes in lines or not
|
||||||
#[clap(short, long, default_value = "false")]
|
#[clap(short, long, default_value = "false")]
|
||||||
pub split_line_attributes: bool,
|
pub split_line_attributes: bool,
|
||||||
|
|
||||||
|
/// The package to build
|
||||||
|
#[clap(short, long)]
|
||||||
|
pub package: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Autoformat {
|
impl Autoformat {
|
||||||
|
@ -43,15 +48,11 @@ impl Autoformat {
|
||||||
..
|
..
|
||||||
} = self;
|
} = self;
|
||||||
|
|
||||||
// Default to formatting the project
|
if let Some(file) = file {
|
||||||
if raw.is_none() && file.is_none() {
|
// Format a single file
|
||||||
if let Err(e) = autoformat_project(check, split_line_attributes, format_rust_code) {
|
refactor_file(file, split_line_attributes, format_rust_code)?;
|
||||||
eprintln!("error formatting project: {}", e);
|
} else if let Some(raw) = raw {
|
||||||
exit(1);
|
// Format raw text.
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(raw) = raw {
|
|
||||||
let indent = indentation_for(".", self.split_line_attributes)?;
|
let indent = indentation_for(".", self.split_line_attributes)?;
|
||||||
if let Some(inner) = dioxus_autofmt::fmt_block(&raw, 0, indent) {
|
if let Some(inner) = dioxus_autofmt::fmt_block(&raw, 0, indent) {
|
||||||
println!("{}", inner);
|
println!("{}", inner);
|
||||||
|
@ -60,11 +61,32 @@ impl Autoformat {
|
||||||
eprintln!("error formatting codeblock");
|
eprintln!("error formatting codeblock");
|
||||||
exit(1);
|
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 Err(e) =
|
||||||
if let Some(file) = file {
|
autoformat_project(check, split_line_attributes, format_rust_code, crate_dir)
|
||||||
refactor_file(file, split_line_attributes, format_rust_code)?;
|
{
|
||||||
|
eprintln!("error formatting project: {}", e);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -108,9 +130,9 @@ fn refactor_file(
|
||||||
}
|
}
|
||||||
|
|
||||||
use std::ffi::OsStr;
|
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();
|
let mut files = Vec::new();
|
||||||
for result in ignore::Walk::new("./") {
|
for result in ignore::Walk::new(dir) {
|
||||||
let path = result.unwrap().into_path();
|
let path = result.unwrap().into_path();
|
||||||
if let Some(ext) = path.extension() {
|
if let Some(ext) = path.extension() {
|
||||||
if ext == OsStr::new("rs") {
|
if ext == OsStr::new("rs") {
|
||||||
|
@ -161,8 +183,9 @@ fn autoformat_project(
|
||||||
check: bool,
|
check: bool,
|
||||||
split_line_attributes: bool,
|
split_line_attributes: bool,
|
||||||
format_rust_code: bool,
|
format_rust_code: bool,
|
||||||
|
dir: impl AsRef<Path>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let files_to_format = get_project_files();
|
let files_to_format = get_project_files(dir);
|
||||||
|
|
||||||
if files_to_format.is_empty() {
|
if files_to_format.is_empty() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -285,6 +308,7 @@ async fn test_auto_fmt() {
|
||||||
raw: Some(test_rsx),
|
raw: Some(test_rsx),
|
||||||
file: None,
|
file: None,
|
||||||
split_line_attributes: false,
|
split_line_attributes: false,
|
||||||
|
package: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
fmt.autoformat().unwrap();
|
fmt.autoformat().unwrap();
|
||||||
|
|
Loading…
Reference in a new issue