Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Adds dart fix when generating files #2182

Merged
merged 9 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions frb_codegen/src/library/codegen/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(crate) struct GeneratorProgressBarPack {
pub generate_ffigen: SimpleProgressBar,
pub polish: SimpleProgressBar,
pub polish_dart_build_runner: SimpleProgressBar,
pub polish_dart_fix: SimpleProgressBar,
pub polish_dart_formatter: SimpleProgressBar,
pub polish_rust_formatter: SimpleProgressBar,
pub polish_upgrade: SimpleProgressBar,
Expand All @@ -30,6 +31,7 @@ impl GeneratorProgressBarPack {
generate_ffigen: SimpleProgressBar::new("Run ffigen", 1),
polish: SimpleProgressBar::new("Polish", 0),
polish_dart_build_runner: SimpleProgressBar::new("Run Dart build_runner", 1),
polish_dart_fix: SimpleProgressBar::new("Run Dart fix", 1),
polish_dart_formatter: SimpleProgressBar::new("Run Dart formatter", 1),
polish_rust_formatter: SimpleProgressBar::new("Run Rust formatter", 1),
polish_upgrade: SimpleProgressBar::new("Auto upgrade", 1),
Expand Down
22 changes: 20 additions & 2 deletions frb_codegen/src/library/codegen/polisher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use crate::codegen::polisher::add_mod_to_lib::try_add_mod_to_lib;
use crate::codegen::polisher::internal_config::PolisherInternalConfig;
use crate::commands::format_rust::format_rust;
use crate::library::commands::dart_build_runner::dart_build_runner;
use crate::library::commands::format_dart::format_dart;
use crate::library::commands::dart_fix::dart_fix;
use crate::library::commands::dart_format::dart_format;
use crate::utils::dart_repository::dart_repo::{DartDependencyMode, DartRepository};
use crate::utils::path_utils::path_to_string;
use anyhow::Context;
Expand Down Expand Up @@ -33,6 +34,10 @@ pub(super) fn polish(
execute_build_runner(needs_freezed, config, progress_bar_pack),
"execute_build_runner",
);
warn_if_fail(
execute_dart_fix(config, output_paths, progress_bar_pack),
"execute_dart_fix",
);

// Even if formatting generated code fails, it is not a big problem, and our codegen should not fail.
warn_if_fail(
Expand Down Expand Up @@ -105,13 +110,26 @@ fn execute_build_runner(
dart_build_runner(&config.dart_root)
}

fn execute_dart_fix(
config: &PolisherInternalConfig,
output_paths: &[PathBuf],
progress_bar_pack: &GeneratorProgressBarPack,
) -> anyhow::Result<()> {
let _pb = progress_bar_pack.polish_dart_fix.start();
dart_fix(
&filter_paths_by_extension(output_paths, "dart"),
&config.dart_root,
&["g.dart", "freezed.dart"],
)
}

fn execute_dart_format(
config: &PolisherInternalConfig,
output_paths: &[PathBuf],
progress_bar_pack: &GeneratorProgressBarPack,
) -> anyhow::Result<()> {
let _pb = progress_bar_pack.polish_dart_formatter.start();
format_dart(
dart_format(
&filter_paths_by_extension(output_paths, "dart"),
&config.dart_root,
config.dart_format_line_length,
Expand Down
27 changes: 27 additions & 0 deletions frb_codegen/src/library/commands/dart_fix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::command_run;
use crate::commands::command_runner::call_shell;
use crate::library::commands::command_runner::check_exit_code;
use crate::library::commands::dart_format::prepare_paths;
use anyhow::Result;
use log::debug;
use std::path::{Path, PathBuf};

#[allow(clippy::vec_init_then_push)]
pub fn dart_fix(paths: &[PathBuf], base_path: &Path, extra_extensions: &[&str]) -> Result<()> {
if paths.is_empty() {
return Ok(());
}

let paths = prepare_paths(paths, base_path, extra_extensions)?;
debug!("execute dart_fix paths={paths:?}");

let res = command_run!(
call_shell[Some(base_path), None],
"dart",
"fix",
"--apply",
*paths
)?;
check_exit_code(&res)?;
Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ use crate::commands::command_runner::call_shell;
use crate::library::commands::command_runner::check_exit_code;
use crate::utils::path_utils::{normalize_windows_unc_path, path_to_string};
use anyhow::Context;
use anyhow::Result;
use itertools::Itertools;
use log::debug;
use pathdiff::diff_paths;
use std::path::{Path, PathBuf};

#[allow(clippy::vec_init_then_push)]
pub fn format_dart(
pub fn dart_format(
paths: &[PathBuf],
base_path: &Path,
line_length: u32,
Expand All @@ -21,7 +20,7 @@ pub fn format_dart(
}

let paths = prepare_paths(paths, base_path, extra_extensions)?;
debug!("execute format_dart paths={paths:?} line_length={line_length}");
debug!("execute dart_format paths={paths:?} line_length={line_length}");

let res = command_run!(
call_shell[Some(base_path), None],
Expand All @@ -43,21 +42,24 @@ pub(super) fn prepare_paths(
let base_path_str = path_to_string(base_path)?;
let normalized_base_path = normalize_windows_unc_path(&base_path_str);

Ok((paths.iter())
Ok(paths
.iter()
.map(|path| {
let mut path: PathBuf =
(normalize_windows_unc_path(&path_to_string(path)?).to_owned()).into();
let mut path: PathBuf = normalize_windows_unc_path(&path_to_string(path)?)
.to_owned()
.into();
path = diff_paths(path, normalized_base_path).context("diff path")?;
if path_to_string(&path)?.is_empty() {
path = ".".into();
}
Ok(path)
})
.collect::<Result<Vec<_>>>()?
.collect::<anyhow::Result<Vec<_>>>()?
.into_iter()
.flat_map(|path| {
(vec![path.clone()].into_iter()).chain(
(extra_extensions.iter())
vec![path.clone()].into_iter().chain(
extra_extensions
.iter()
.map(move |ext| with_extension(path.clone(), ext))
.filter(|path| path.exists()),
)
Expand Down
2 changes: 1 addition & 1 deletion frb_codegen/src/library/commands/format_rust.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::command_run;
use crate::library::commands::command_runner::{call_shell, check_exit_code};
use crate::library::commands::format_dart::prepare_paths;
use crate::library::commands::dart_format::prepare_paths;
use log::debug;
use std::path::{Path, PathBuf};

Expand Down
3 changes: 2 additions & 1 deletion frb_codegen/src/library/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ pub(crate) mod cargo_metadata;
pub(crate) mod cbindgen;
pub(crate) mod command_runner;
pub(crate) mod dart_build_runner;
pub(crate) mod dart_fix;
pub(crate) mod dart_format;
pub(crate) mod ensure_tools_available;
pub(crate) mod ffigen;
pub(crate) mod flutter;
pub(crate) mod format_dart;
pub(crate) mod format_rust;
14 changes: 10 additions & 4 deletions frb_codegen/src/library/integration/integrator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::integration::utils::{overlay_dir, replace_file_content};
use crate::library::commands::dart_fix::dart_fix;
use crate::library::commands::dart_format::dart_format;
use crate::library::commands::flutter::{flutter_pub_add, flutter_pub_get};
use crate::library::commands::format_dart::format_dart;
use crate::misc::Template;
use crate::utils::dart_repository::get_dart_package_name;
use crate::utils::path_utils::find_dart_package_dir;
Expand Down Expand Up @@ -28,8 +29,10 @@ pub fn integrate(config: IntegrateConfig) -> Result<()> {
debug!("integrate dart_root={dart_root:?}");

let dart_package_name = get_dart_package_name(&dart_root)?;
let rust_crate_name =
(config.rust_crate_name.clone()).unwrap_or(format!("rust_lib_{}", dart_package_name));
let rust_crate_name = config
.rust_crate_name
.clone()
.unwrap_or(format!("rust_lib_{}", dart_package_name));

info!("Overlay template onto project");
let replacements = compute_replacements(&config, &dart_package_name, &rust_crate_name);
Expand Down Expand Up @@ -73,8 +76,11 @@ pub fn integrate(config: IntegrateConfig) -> Result<()> {
info!("Setup cargokit dependencies");
setup_cargokit_dependencies(&dart_root, &config.template)?;

info!("Apply Dart fixes");
dart_fix(&[dart_root.clone()], &dart_root, &[])?;

info!("Format Dart code");
format_dart(&[dart_root.clone()], &dart_root, 80, &[])?;
dart_format(&[dart_root.clone()], &dart_root, 80, &[])?;

Ok(())
}
Expand Down
Loading