Skip to content

Commit

Permalink
Merge pull request #2021 from fzyzcjy/feat/12136
Browse files Browse the repository at this point in the history
Improve Dart import generation
  • Loading branch information
fzyzcjy authored Jun 3, 2024
2 parents 4765d07 + 583cf81 commit 66f3830
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
20 changes: 20 additions & 0 deletions frb_codegen/src/library/codegen/generator/api_dart/misc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::codegen::misc::THIRD_PARTY_DIR_NAME;
use crate::utils::crate_name::CrateName;
use crate::utils::namespace::Namespace;
use itertools::concat;
use std::path::{Path, PathBuf};

pub(crate) fn compute_path_from_namespace(
dart_decl_base_output_path: &Path,
namespace: &Namespace,
) -> PathBuf {
let raw_path = namespace.path();
let chunks = match raw_path[0] {
CrateName::SELF_CRATE => raw_path[1..].to_owned(),
_ => concat([vec![THIRD_PARTY_DIR_NAME], raw_path.clone()]),
};

let ans_without_extension =
(chunks.iter()).fold(dart_decl_base_output_path.to_owned(), |a, b| a.join(b));
ans_without_extension.with_extension("dart")
}
1 change: 1 addition & 0 deletions frb_codegen/src/library/codegen/generator/api_dart/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub(crate) mod internal_config;
pub(crate) mod misc;
pub(crate) mod spec_generator;
mod text_generator;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::codegen::generator::api_dart;
use crate::codegen::generator::api_dart::spec_generator::base::{
ApiDartGenerator, ApiDartGeneratorContext,
};
Expand All @@ -14,6 +15,7 @@ use crate::utils::path_utils::path_to_string;
use anyhow::Context;
use itertools::Itertools;
use pathdiff::diff_paths;
use std::path::PathBuf;

/// A trailing newline is included if comments is not empty.
pub(crate) fn generate_dart_comments(comments: &[MirComment]) -> String {
Expand Down Expand Up @@ -89,9 +91,15 @@ fn generate_imports_from_ty(
) -> anyhow::Result<String> {
let import_ty_itself = if let Some(ty_namespace) = ty.self_namespace() {
if &ty_namespace != current_file_namespace {
let dummy_base_path = PathBuf::from("/".to_owned());
let path_diff = diff_paths(
ty_namespace.to_pseudo_io_path("dart"),
(current_file_namespace.to_pseudo_io_path("dart").parent()).unwrap(),
api_dart::misc::compute_path_from_namespace(&dummy_base_path, &ty_namespace),
(api_dart::misc::compute_path_from_namespace(
&dummy_base_path,
current_file_namespace,
)
.parent())
.unwrap(),
)
.context("cannot diff path")?;
format!(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use crate::codegen::generator::api_dart::internal_config::GeneratorApiDartInternalConfig;
use crate::codegen::generator::api_dart::misc::compute_path_from_namespace;
use crate::codegen::generator::api_dart::spec_generator::function::ApiDartGeneratedFunction;
use crate::codegen::generator::api_dart::spec_generator::{
ApiDartOutputSpec, ApiDartOutputSpecItem,
};
use crate::codegen::generator::misc::target::TargetOrCommonMap;
use crate::codegen::generator::misc::{generate_code_header, PathText, PathTexts};
use crate::codegen::misc::THIRD_PARTY_DIR_NAME;
use crate::utils::basic_code::DartBasicHeaderCode;
use crate::utils::crate_name::CrateName;
use crate::utils::namespace::Namespace;
use crate::utils::path_utils::path_to_string;
use anyhow::Context;
use itertools::{concat, Itertools};
use itertools::Itertools;
use pathdiff::diff_paths;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -143,18 +141,3 @@ fn generate_function(func: &ApiDartGeneratedFunction) -> String {
} = &func;
format!("{func_comments}{func_expr} => {func_impl};")
}

fn compute_path_from_namespace(
dart_decl_base_output_path: &Path,
namespace: &Namespace,
) -> PathBuf {
let raw_path = namespace.path();
let chunks = match raw_path[0] {
CrateName::SELF_CRATE => raw_path[1..].to_owned(),
_ => concat([vec![THIRD_PARTY_DIR_NAME], raw_path.clone()]),
};

let ans_without_extension =
(chunks.iter()).fold(dart_decl_base_output_path.to_owned(), |a, b| a.join(b));
ans_without_extension.with_extension("dart")
}
23 changes: 11 additions & 12 deletions frb_codegen/src/library/utils/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use itertools::Itertools;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::borrow::ToOwned;
use std::fmt::{Display, Formatter};
use std::path::PathBuf;

/// The Rust files/modules/namespaces.
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Ord, PartialOrd, Default)]
Expand Down Expand Up @@ -66,9 +65,9 @@ impl Namespace {
// path
// }

pub fn to_pseudo_io_path(&self, extension: &str) -> PathBuf {
PathBuf::from(&format!("/{}.{extension}", self.path().join("/")))
}
// pub fn to_pseudo_io_path(&self, extension: &str) -> PathBuf {
// PathBuf::from(&format!("/{}.{extension}", self.path().join("/")))
// }

pub fn safe_ident(&self) -> String {
self.path().iter().join("__")
Expand Down Expand Up @@ -197,12 +196,12 @@ mod tests {
Ok(())
}

#[test]
pub fn test_to_pseudo_io_path() -> anyhow::Result<()> {
assert_eq!(
Namespace::new_raw("apple::orange".into()).to_pseudo_io_path("dart"),
PathBuf::from("/apple/orange.dart")
);
Ok(())
}
// #[test]
// pub fn test_to_pseudo_io_path() -> anyhow::Result<()> {
// assert_eq!(
// Namespace::new_raw("apple::orange".into()).to_pseudo_io_path("dart"),
// PathBuf::from("/apple/orange.dart")
// );
// Ok(())
// }
}

0 comments on commit 66f3830

Please sign in to comment.