From d7ca45b0e989b1b03f2b3f4e70fb5b5a23dba41b Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Wed, 11 Sep 2024 20:28:59 +0800 Subject: [PATCH 01/15] refactor --- .../commands/ensure_tools_available.rs | 3 +-- .../utils/dart_repository/dart_repo.rs | 20 ++++++++----------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/frb_codegen/src/library/commands/ensure_tools_available.rs b/frb_codegen/src/library/commands/ensure_tools_available.rs index 373ce7cb51..b7d57b072b 100644 --- a/frb_codegen/src/library/commands/ensure_tools_available.rs +++ b/frb_codegen/src/library/commands/ensure_tools_available.rs @@ -1,5 +1,4 @@ use crate::utils::dart_repository::dart_repo::{DartDependencyMode, DartRepository}; -use crate::utils::path_utils::path_to_string; use anyhow::bail; use cargo_metadata::VersionReq; use lazy_static::lazy_static; @@ -15,7 +14,7 @@ pub fn ensure_tools_available( enable_deps_check: bool, needs_ffigen: bool, ) -> anyhow::Result<()> { - let repo = DartRepository::from_str(&path_to_string(dart_root)?)?; + let repo = DartRepository::from_path(&dart_root)?; if !repo.toolchain_available() { // This will stop the whole generator and tell the users, so we do not care about testing it // frb-coverage:ignore-start diff --git a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs index ca984710b6..af0172d647 100644 --- a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs +++ b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs @@ -1,11 +1,12 @@ use crate::utils::dart_repository::dart_toolchain::DartToolchain; use crate::utils::dart_repository::pubspec::*; +use crate::utils::path_utils::path_to_string; use anyhow::{anyhow, bail, Context}; use cargo_metadata::{Version, VersionReq}; use log::debug; use std::convert::TryFrom; use std::fmt::Display; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::str::FromStr; /// represents a dart / flutter repository @@ -14,33 +15,28 @@ pub(crate) struct DartRepository { pub(crate) toolchain: DartToolchain, } -// TODO it is from path, not from str -impl FromStr for DartRepository { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { +impl DartRepository { + pub(crate) fn from_path(path: &Path) -> anyhow::Result { debug!("Guessing toolchain the runner is run into"); let filename = DartToolchain::lock_filename(); - let lock_file = read_file(s, filename)?; + let lock_file = read_file(&path_to_string(path)?, filename)?; let lock_file: PubspecLock = serde_yaml::from_str(&lock_file) - .map_err(|e| anyhow!("unable to parse {filename} in {s}: {e:#}"))?; + .map_err(|e| anyhow!("unable to parse {filename} in {path:?}: {e:#}"))?; if lock_file .packages .contains_key(&DartToolchain::Flutter.to_string()) { return Ok(DartRepository { - at: PathBuf::from(s), + at: path.to_owned(), toolchain: DartToolchain::Flutter, }); } Ok(DartRepository { - at: PathBuf::from(s), + at: path.to_owned(), toolchain: DartToolchain::Dart, }) } -} -impl DartRepository { /// check whether the toolchain is available from the CLI pub(crate) fn toolchain_available(&self) -> bool { self.toolchain.available() From e97a4f579085d345a3ec8e23f62b8e0f68a962da Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Wed, 11 Sep 2024 20:29:57 +0800 Subject: [PATCH 02/15] compile --- frb_codegen/src/library/build_web/mod.rs | 2 +- frb_codegen/src/library/codegen/polisher/auto_upgrade.rs | 2 +- frb_codegen/src/library/codegen/polisher/mod.rs | 2 +- frb_codegen/src/library/commands/dart_build_runner.rs | 2 +- frb_codegen/src/library/commands/ffigen.rs | 2 +- frb_codegen/src/library/utils/dart_repository/mod.rs | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/frb_codegen/src/library/build_web/mod.rs b/frb_codegen/src/library/build_web/mod.rs index ffae6661c0..a4b4b9eff5 100644 --- a/frb_codegen/src/library/build_web/mod.rs +++ b/frb_codegen/src/library/build_web/mod.rs @@ -39,7 +39,7 @@ fn execute_dart_command( args: &[String], dart_coverage: bool, ) -> anyhow::Result<()> { - let repo = DartRepository::from_str(&path_to_string(dart_root)?)?; + let repo = DartRepository::from_path(&dart_root)?; let dart_run_args = { let mut ans = vec![ diff --git a/frb_codegen/src/library/codegen/polisher/auto_upgrade.rs b/frb_codegen/src/library/codegen/polisher/auto_upgrade.rs index 1863096010..d25eec33a3 100644 --- a/frb_codegen/src/library/codegen/polisher/auto_upgrade.rs +++ b/frb_codegen/src/library/codegen/polisher/auto_upgrade.rs @@ -35,7 +35,7 @@ struct DartUpgrader; impl Upgrader for DartUpgrader { fn check(base_dir: &Path) -> Result { - let repo = DartRepository::from_str(&path_to_string(base_dir)?)?; + let repo = DartRepository::from_path(base_dir)?; Ok(repo .has_specified_and_installed( "flutter_rust_bridge", diff --git a/frb_codegen/src/library/codegen/polisher/mod.rs b/frb_codegen/src/library/codegen/polisher/mod.rs index 2f59cd97ef..5e1ea768be 100644 --- a/frb_codegen/src/library/codegen/polisher/mod.rs +++ b/frb_codegen/src/library/codegen/polisher/mod.rs @@ -68,7 +68,7 @@ fn ensure_dependency_freezed( } if needs_freezed { - let repo = DartRepository::from_str(&path_to_string(&config.dart_root)?)?; + let repo = DartRepository::from_path(&config.dart_root)?; repo.has_specified_and_installed("freezed", DartDependencyMode::Dev, &ANY_REQUIREMENT)?; repo.has_specified_and_installed( "freezed_annotation", diff --git a/frb_codegen/src/library/commands/dart_build_runner.rs b/frb_codegen/src/library/commands/dart_build_runner.rs index a491ac458c..fddf93889d 100644 --- a/frb_codegen/src/library/commands/dart_build_runner.rs +++ b/frb_codegen/src/library/commands/dart_build_runner.rs @@ -11,7 +11,7 @@ use std::str::FromStr; pub fn dart_build_runner(dart_root: &Path) -> anyhow::Result<()> { debug!("Running build_runner at dart_root={dart_root:?}"); - let repo = DartRepository::from_str(&path_to_string(dart_root)?).unwrap(); + let repo = DartRepository::from_path(dart_root).unwrap(); let out = command_run!( call_shell[Some(dart_root), Some(dart_run_extra_env())], *repo.toolchain.as_run_command(), diff --git a/frb_codegen/src/library/commands/ffigen.rs b/frb_codegen/src/library/commands/ffigen.rs index 521e7f70c5..e915920c00 100644 --- a/frb_codegen/src/library/commands/ffigen.rs +++ b/frb_codegen/src/library/commands/ffigen.rs @@ -78,7 +78,7 @@ pub(crate) fn ffigen_raw(config: &FfigenCommandConfig, dart_root: &Path) -> anyh config_file.write_all(config.as_bytes())?; debug!("ffigen_raw config={config:?} config_file={config_file:?}"); - let repo = DartRepository::from_str(&path_to_string(dart_root)?).unwrap(); + let repo = DartRepository::from_path(dart_root).unwrap(); let res = command_run!( call_shell[Some(dart_root), Some(dart_run_extra_env())], *repo.toolchain.as_run_command(), diff --git a/frb_codegen/src/library/utils/dart_repository/mod.rs b/frb_codegen/src/library/utils/dart_repository/mod.rs index 15624cae3c..0542afdbf1 100644 --- a/frb_codegen/src/library/utils/dart_repository/mod.rs +++ b/frb_codegen/src/library/utils/dart_repository/mod.rs @@ -49,7 +49,7 @@ mod tests { } fn guess_toolchain_base(path: &Path, expect_toolchain: DartToolchain) { - let repo = DartRepository::from_str(&path.to_string_lossy()) + let repo = DartRepository::from_path(path) .unwrap_or_else(|_| panic!("can get toolchain from {}", path.to_string_lossy())); assert_eq!(repo.toolchain, expect_toolchain); } From e8749d700f10875f5b29c917ca906d26cb55ab07 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Wed, 11 Sep 2024 20:31:27 +0800 Subject: [PATCH 03/15] refactor more --- .../utils/dart_repository/dart_repo.rs | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs index af0172d647..9b02bc67c0 100644 --- a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs +++ b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs @@ -19,7 +19,7 @@ impl DartRepository { pub(crate) fn from_path(path: &Path) -> anyhow::Result { debug!("Guessing toolchain the runner is run into"); let filename = DartToolchain::lock_filename(); - let lock_file = read_file(&path_to_string(path)?, filename)?; + let lock_file = read_file(path, filename)?; let lock_file: PubspecLock = serde_yaml::from_str(&lock_file) .map_err(|e| anyhow!("unable to parse {filename} in {path:?}: {e:#}"))?; if lock_file @@ -63,14 +63,17 @@ impl DartRepository { manager: DartDependencyMode, requirement: &VersionReq, ) -> anyhow::Result<()> { - let at = self.at.to_str().unwrap(); - debug!("Checking presence of {} in {} at {}", package, manager, at); + let at = &self.at; + debug!( + "Checking presence of {} in {} at {:?}", + package, manager, at + ); let manifest_file = read_file(at, DartToolchain::manifest_filename())?; let manifest_file: PubspecYaml = serde_yaml::from_str(&manifest_file).map_err(|e| { // frb-coverage:ignore-start // This will stop the whole generator and tell the users, so we do not care about testing it anyhow!( - "unable to parse {} in {at}: {e:#}", + "unable to parse {} in {at:?}: {e:#}", DartToolchain::manifest_filename() ) // frb-coverage:ignore-end @@ -95,14 +98,17 @@ impl DartRepository { manager: DartDependencyMode, requirement: &VersionReq, ) -> anyhow::Result<()> { - let at = self.at.to_str().unwrap(); - debug!("Checking presence of {} in {} at {}", package, manager, at); + let at = &self.at; + debug!( + "Checking presence of {} in {} at {:?}", + package, manager, at + ); let lock_file = read_file(at, DartToolchain::lock_filename())?; let lock_file: PubspecLock = serde_yaml::from_str(&lock_file).map_err(|e| { // This will stop the whole generator and tell the users, so we do not care about testing it // frb-coverage:ignore-start anyhow!( - "unable to parse {} in {at}: {e:#}", + "unable to parse {} in {at:?}: {e:#}", DartToolchain::lock_filename() ) // frb-coverage:ignore-end @@ -219,16 +225,16 @@ impl Display for DartPackageVersion { } #[inline] -fn read_file(at: &str, filename: &str) -> anyhow::Result { - let file = PathBuf::from(at).join(filename); +fn read_file(at: &Path, filename: &str) -> anyhow::Result { + let file = at.join(filename); if !file.exists() { // This will stop the whole generator and tell the users, so we do not care about testing it // frb-coverage:ignore-start - bail!("missing {filename} in {at}"); + bail!("missing {filename} in {at:?}"); // frb-coverage:ignore-end } let content = std::fs::read_to_string(file) - .with_context(|| format!("unable to read {filename} in {at}"))?; + .with_context(|| format!("unable to read {filename} in {at:?}"))?; Ok(content) } From 1f5318f361c55a69767eee40fe8f65204f4d6dc3 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Wed, 11 Sep 2024 20:51:09 +0800 Subject: [PATCH 04/15] lint --- frb_codegen/src/library/build_web/mod.rs | 3 +-- frb_codegen/src/library/codegen/polisher/auto_upgrade.rs | 1 - frb_codegen/src/library/codegen/polisher/mod.rs | 2 -- frb_codegen/src/library/commands/dart_build_runner.rs | 2 -- frb_codegen/src/library/commands/ensure_tools_available.rs | 3 +-- frb_codegen/src/library/commands/ffigen.rs | 2 -- frb_codegen/src/library/integration/integrator.rs | 1 - frb_codegen/src/library/utils/dart_repository/dart_repo.rs | 2 -- frb_codegen/src/library/utils/dart_repository/mod.rs | 1 - 9 files changed, 2 insertions(+), 15 deletions(-) diff --git a/frb_codegen/src/library/build_web/mod.rs b/frb_codegen/src/library/build_web/mod.rs index a4b4b9eff5..57abf20d6b 100644 --- a/frb_codegen/src/library/build_web/mod.rs +++ b/frb_codegen/src/library/build_web/mod.rs @@ -9,7 +9,6 @@ use itertools::Itertools; use log::debug; use std::path::{Path, PathBuf}; use std::process::{Command, ExitStatus}; -use std::str::FromStr; use std::{env, fs}; // We make the core build-web logic in Dart, and Rust is just a wrapper. @@ -39,7 +38,7 @@ fn execute_dart_command( args: &[String], dart_coverage: bool, ) -> anyhow::Result<()> { - let repo = DartRepository::from_path(&dart_root)?; + let repo = DartRepository::from_path(dart_root)?; let dart_run_args = { let mut ans = vec![ diff --git a/frb_codegen/src/library/codegen/polisher/auto_upgrade.rs b/frb_codegen/src/library/codegen/polisher/auto_upgrade.rs index d25eec33a3..8c5a6bc3b6 100644 --- a/frb_codegen/src/library/codegen/polisher/auto_upgrade.rs +++ b/frb_codegen/src/library/codegen/polisher/auto_upgrade.rs @@ -2,7 +2,6 @@ use crate::codegen::misc::GeneratorProgressBarPack; use crate::integration::integrator::pub_add_dependency_frb; use crate::library::commands::cargo::cargo_add; use crate::utils::dart_repository::dart_repo::{DartDependencyMode, DartRepository}; -use crate::utils::path_utils::path_to_string; use anyhow::Result; use cargo_metadata::VersionReq; use std::path::Path; diff --git a/frb_codegen/src/library/codegen/polisher/mod.rs b/frb_codegen/src/library/codegen/polisher/mod.rs index 5e1ea768be..abbaa9636b 100644 --- a/frb_codegen/src/library/codegen/polisher/mod.rs +++ b/frb_codegen/src/library/codegen/polisher/mod.rs @@ -6,7 +6,6 @@ use crate::library::commands::dart_build_runner::dart_build_runner; 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; use cargo_metadata::VersionReq; use itertools::Itertools; @@ -14,7 +13,6 @@ use lazy_static::lazy_static; use log::warn; use std::fs; use std::path::{Path, PathBuf}; -use std::str::FromStr; pub(crate) mod add_mod_to_lib; mod auto_upgrade; diff --git a/frb_codegen/src/library/commands/dart_build_runner.rs b/frb_codegen/src/library/commands/dart_build_runner.rs index fddf93889d..f4cf21c201 100644 --- a/frb_codegen/src/library/commands/dart_build_runner.rs +++ b/frb_codegen/src/library/commands/dart_build_runner.rs @@ -1,12 +1,10 @@ use crate::command_run; use crate::commands::command_runner::call_shell; use crate::utils::dart_repository::dart_repo::DartRepository; -use crate::utils::path_utils::path_to_string; use anyhow::bail; use log::debug; use std::collections::HashMap; use std::path::Path; -use std::str::FromStr; pub fn dart_build_runner(dart_root: &Path) -> anyhow::Result<()> { debug!("Running build_runner at dart_root={dart_root:?}"); diff --git a/frb_codegen/src/library/commands/ensure_tools_available.rs b/frb_codegen/src/library/commands/ensure_tools_available.rs index b7d57b072b..20c4db6f01 100644 --- a/frb_codegen/src/library/commands/ensure_tools_available.rs +++ b/frb_codegen/src/library/commands/ensure_tools_available.rs @@ -3,7 +3,6 @@ use anyhow::bail; use cargo_metadata::VersionReq; use lazy_static::lazy_static; use std::path::Path; -use std::str::FromStr; lazy_static! { pub(crate) static ref FFIGEN_REQUIREMENT: VersionReq = VersionReq::parse(">= 8.0.0").unwrap(); @@ -14,7 +13,7 @@ pub fn ensure_tools_available( enable_deps_check: bool, needs_ffigen: bool, ) -> anyhow::Result<()> { - let repo = DartRepository::from_path(&dart_root)?; + let repo = DartRepository::from_path(dart_root)?; if !repo.toolchain_available() { // This will stop the whole generator and tell the users, so we do not care about testing it // frb-coverage:ignore-start diff --git a/frb_codegen/src/library/commands/ffigen.rs b/frb_codegen/src/library/commands/ffigen.rs index e915920c00..b165b7faee 100644 --- a/frb_codegen/src/library/commands/ffigen.rs +++ b/frb_codegen/src/library/commands/ffigen.rs @@ -2,7 +2,6 @@ use super::dart_build_runner::dart_run_extra_env; use crate::command_run; use crate::commands::command_runner::call_shell; use crate::utils::dart_repository::dart_repo::DartRepository; -use crate::utils::path_utils::path_to_string; use anyhow::bail; use itertools::Itertools; use log::{debug, warn}; @@ -11,7 +10,6 @@ use std::collections::HashMap; use std::fs; use std::io::Write; use std::path::{Path, PathBuf}; -use std::str::FromStr; pub(crate) struct FfigenArgs<'a> { pub c_file_content: &'a str, diff --git a/frb_codegen/src/library/integration/integrator.rs b/frb_codegen/src/library/integration/integrator.rs index 5b19bc07af..870971d8e0 100644 --- a/frb_codegen/src/library/integration/integrator.rs +++ b/frb_codegen/src/library/integration/integrator.rs @@ -1,5 +1,4 @@ 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::misc::Template; diff --git a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs index 9b02bc67c0..ecdc973ceb 100644 --- a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs +++ b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs @@ -1,13 +1,11 @@ use crate::utils::dart_repository::dart_toolchain::DartToolchain; use crate::utils::dart_repository::pubspec::*; -use crate::utils::path_utils::path_to_string; use anyhow::{anyhow, bail, Context}; use cargo_metadata::{Version, VersionReq}; use log::debug; use std::convert::TryFrom; use std::fmt::Display; use std::path::{Path, PathBuf}; -use std::str::FromStr; /// represents a dart / flutter repository pub(crate) struct DartRepository { diff --git a/frb_codegen/src/library/utils/dart_repository/mod.rs b/frb_codegen/src/library/utils/dart_repository/mod.rs index 0542afdbf1..84c181b574 100644 --- a/frb_codegen/src/library/utils/dart_repository/mod.rs +++ b/frb_codegen/src/library/utils/dart_repository/mod.rs @@ -37,7 +37,6 @@ mod tests { use std::{ collections::HashMap, path::{Path, PathBuf}, - str::FromStr, }; lazy_static! { From cc0fc976a7391cee757ee1254582c5bbf5852995 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Wed, 11 Sep 2024 21:58:15 +0800 Subject: [PATCH 05/15] fix --- frb_codegen/src/library/integration/integrator.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/frb_codegen/src/library/integration/integrator.rs b/frb_codegen/src/library/integration/integrator.rs index 363547f918..820bbe4323 100644 --- a/frb_codegen/src/library/integration/integrator.rs +++ b/frb_codegen/src/library/integration/integrator.rs @@ -1,4 +1,5 @@ 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::misc::Template; From dbf227fdb606568d7572a87143b9bc74ffa5c108 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Wed, 11 Sep 2024 22:40:05 +0800 Subject: [PATCH 06/15] extract --- .../utils/dart_repository/dart_repo.rs | 40 ++++++++----------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs index ecdc973ceb..401144940d 100644 --- a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs +++ b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs @@ -6,6 +6,7 @@ use log::debug; use std::convert::TryFrom; use std::fmt::Display; use std::path::{Path, PathBuf}; +use serde::de::DeserializeOwned; /// represents a dart / flutter repository pub(crate) struct DartRepository { @@ -17,9 +18,7 @@ impl DartRepository { pub(crate) fn from_path(path: &Path) -> anyhow::Result { debug!("Guessing toolchain the runner is run into"); let filename = DartToolchain::lock_filename(); - let lock_file = read_file(path, filename)?; - let lock_file: PubspecLock = serde_yaml::from_str(&lock_file) - .map_err(|e| anyhow!("unable to parse {filename} in {path:?}: {e:#}"))?; + let lock_file: PubspecLock = read_file_and_parse_yaml(path, filename)?; if lock_file .packages .contains_key(&DartToolchain::Flutter.to_string()) @@ -66,16 +65,8 @@ impl DartRepository { "Checking presence of {} in {} at {:?}", package, manager, at ); - let manifest_file = read_file(at, DartToolchain::manifest_filename())?; - let manifest_file: PubspecYaml = serde_yaml::from_str(&manifest_file).map_err(|e| { - // frb-coverage:ignore-start - // This will stop the whole generator and tell the users, so we do not care about testing it - anyhow!( - "unable to parse {} in {at:?}: {e:#}", - DartToolchain::manifest_filename() - ) - // frb-coverage:ignore-end - })?; + let manifest_file: PubspecYaml = + read_file_and_parse_yaml(at, DartToolchain::manifest_filename())?; let deps = match manager { DartDependencyMode::Main => manifest_file.dependencies.unwrap_or_default(), DartDependencyMode::Dev => manifest_file.dev_dependencies.unwrap_or_default(), @@ -101,16 +92,7 @@ impl DartRepository { "Checking presence of {} in {} at {:?}", package, manager, at ); - let lock_file = read_file(at, DartToolchain::lock_filename())?; - let lock_file: PubspecLock = serde_yaml::from_str(&lock_file).map_err(|e| { - // This will stop the whole generator and tell the users, so we do not care about testing it - // frb-coverage:ignore-start - anyhow!( - "unable to parse {} in {at:?}: {e:#}", - DartToolchain::lock_filename() - ) - // frb-coverage:ignore-end - })?; + let lock_file: PubspecLock = read_file_and_parse_yaml(at, DartToolchain::lock_filename())?; let dependency = lock_file.packages.get(package); let version = match dependency { Some(dependency) => { @@ -222,7 +204,6 @@ impl Display for DartPackageVersion { } } -#[inline] fn read_file(at: &Path, filename: &str) -> anyhow::Result { let file = at.join(filename); if !file.exists() { @@ -236,6 +217,17 @@ fn read_file(at: &Path, filename: &str) -> anyhow::Result { Ok(content) } +fn read_file_and_parse_yaml(at: &Path, filename: &str) -> anyhow::Result { + let file = read_file(at, filename)?; + let file: T = serde_yaml::from_str(&file).map_err(|e| { + // frb-coverage:ignore-start + // This will stop the whole generator and tell the users, so we do not care about testing it + anyhow!("Unable to parse {filename} in {at:?}: {e:#}") + // frb-coverage:ignore-end + })?; + Ok(file) +} + impl PubspecLockPackage { pub(crate) fn installed_in(&self) -> Option { match self.dependency.as_str() { From c0bab62b2fbed9248a77438ff0afeb84eab9ee08 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Wed, 11 Sep 2024 22:41:05 +0800 Subject: [PATCH 07/15] simp --- .../utils/dart_repository/dart_repo.rs | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs index 401144940d..73df78a552 100644 --- a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs +++ b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs @@ -3,10 +3,10 @@ use crate::utils::dart_repository::pubspec::*; use anyhow::{anyhow, bail, Context}; use cargo_metadata::{Version, VersionReq}; use log::debug; +use serde::de::DeserializeOwned; use std::convert::TryFrom; use std::fmt::Display; use std::path::{Path, PathBuf}; -use serde::de::DeserializeOwned; /// represents a dart / flutter repository pub(crate) struct DartRepository { @@ -17,20 +17,19 @@ pub(crate) struct DartRepository { impl DartRepository { pub(crate) fn from_path(path: &Path) -> anyhow::Result { debug!("Guessing toolchain the runner is run into"); - let filename = DartToolchain::lock_filename(); - let lock_file: PubspecLock = read_file_and_parse_yaml(path, filename)?; - if lock_file - .packages - .contains_key(&DartToolchain::Flutter.to_string()) - { - return Ok(DartRepository { - at: path.to_owned(), - toolchain: DartToolchain::Flutter, - }); - } + + let lock_file: PubspecLock = + read_file_and_parse_yaml(path, DartToolchain::lock_filename())?; + + let toolchain = if (lock_file.packages).contains_key(&DartToolchain::Flutter.to_string()) { + DartToolchain::Flutter + } else { + DartToolchain::Dart + }; + Ok(DartRepository { at: path.to_owned(), - toolchain: DartToolchain::Dart, + toolchain, }) } From 48651a18aa83572390716c24e8d621696a96ab14 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Wed, 11 Sep 2024 22:41:56 +0800 Subject: [PATCH 08/15] more --- .../src/library/utils/dart_repository/dart_repo.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs index 73df78a552..d88129b52b 100644 --- a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs +++ b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs @@ -18,14 +18,15 @@ impl DartRepository { pub(crate) fn from_path(path: &Path) -> anyhow::Result { debug!("Guessing toolchain the runner is run into"); - let lock_file: PubspecLock = + let lock_file: PubspecYaml = read_file_and_parse_yaml(path, DartToolchain::lock_filename())?; - let toolchain = if (lock_file.packages).contains_key(&DartToolchain::Flutter.to_string()) { - DartToolchain::Flutter - } else { - DartToolchain::Dart - }; + let toolchain = + if (lock_file.dependencies).contains_key(&DartToolchain::Flutter.to_string()) { + DartToolchain::Flutter + } else { + DartToolchain::Dart + }; Ok(DartRepository { at: path.to_owned(), From 4aa394f25ea2713f0207c98f676cb854e7a99bc4 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Wed, 11 Sep 2024 22:42:58 +0800 Subject: [PATCH 09/15] more --- .../src/library/utils/dart_repository/dart_repo.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs index d88129b52b..17fd008291 100644 --- a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs +++ b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs @@ -21,12 +21,15 @@ impl DartRepository { let lock_file: PubspecYaml = read_file_and_parse_yaml(path, DartToolchain::lock_filename())?; - let toolchain = - if (lock_file.dependencies).contains_key(&DartToolchain::Flutter.to_string()) { + let toolchain = if let Some(dependencies) = lock_file.dependencies.as_ref() { + if dependencies.contains_key(&DartToolchain::Flutter.to_string()) { DartToolchain::Flutter } else { DartToolchain::Dart - }; + } + } else { + DartToolchain::Dart + }; Ok(DartRepository { at: path.to_owned(), From a80ba47399bdd9d45383b13fc380fd6b9bf2dad6 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Wed, 11 Sep 2024 22:43:29 +0800 Subject: [PATCH 10/15] fix --- frb_codegen/src/library/utils/dart_repository/dart_repo.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs index 17fd008291..d89374573c 100644 --- a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs +++ b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs @@ -19,7 +19,7 @@ impl DartRepository { debug!("Guessing toolchain the runner is run into"); let lock_file: PubspecYaml = - read_file_and_parse_yaml(path, DartToolchain::lock_filename())?; + read_file_and_parse_yaml(path, DartToolchain::manifest_filename())?; let toolchain = if let Some(dependencies) = lock_file.dependencies.as_ref() { if dependencies.contains_key(&DartToolchain::Flutter.to_string()) { From 27b73964155740c42822c46dd22022e145ff1f0c Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Wed, 11 Sep 2024 22:44:38 +0800 Subject: [PATCH 11/15] refactor --- .../utils/dart_repository/dart_repo.rs | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs index d89374573c..c9860993d7 100644 --- a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs +++ b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs @@ -91,11 +91,10 @@ impl DartRepository { requirement: &VersionReq, ) -> anyhow::Result<()> { let at = &self.at; - debug!( - "Checking presence of {} in {} at {:?}", - package, manager, at - ); - let lock_file: PubspecLock = read_file_and_parse_yaml(at, DartToolchain::lock_filename())?; + let filename = DartToolchain::lock_filename(); + debug!("Checking presence of {} in {} at {:?}", package, manager, at); + + let lock_file: PubspecLock = read_file_and_parse_yaml(at, filename)?; let dependency = lock_file.packages.get(package); let version = match dependency { Some(dependency) => { @@ -111,9 +110,7 @@ impl DartRepository { // frb-coverage:ignore-start anyhow::Error::msg(format!( "unable to parse {} version in {}: {:#}", - package, - DartToolchain::lock_filename(), - e + package, filename, e )) // frb-coverage:ignore-end })? @@ -128,10 +125,9 @@ impl DartRepository { DartPackageVersion::Exact(ref v) if requirement.matches(v) => Ok(()), // This will stop the whole generator and tell the users, so we do not care about testing it // frb-coverage:ignore-start - DartPackageVersion::Range(_) => bail!( - "unexpected version range for {package} in {}", - DartToolchain::lock_filename() - ), + DartPackageVersion::Range(_) => { + bail!("unexpected version range for {package} in {}", filename) + } _ => Err(error_invalid_dep(package, manager, requirement)), // frb-coverage:ignore-end } From c73c4b5aea9dd47cfa62cb754b75fb6884028cdd Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Wed, 11 Sep 2024 22:45:39 +0800 Subject: [PATCH 12/15] impl --- .../src/library/utils/dart_repository/dart_repo.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs index c9860993d7..f22b0f4d60 100644 --- a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs +++ b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs @@ -92,7 +92,15 @@ impl DartRepository { ) -> anyhow::Result<()> { let at = &self.at; let filename = DartToolchain::lock_filename(); - debug!("Checking presence of {} in {} at {:?}", package, manager, at); + debug!("Checking presence of {package} in {manager} at {at:?}"); + + // We do not care about this branch + // frb-coverage:ignore-start + if !at.join(filename).exists() { + log::warn!("Skip checking presence of {package} in {manager} at {at:?} since {filename} does not exist. Please check manually."); + return Ok(()); + } + // frb-coverage:ignore-end let lock_file: PubspecLock = read_file_and_parse_yaml(at, filename)?; let dependency = lock_file.packages.get(package); From 857268c55f7caa9fc896febe032b999588ecebff Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Thu, 12 Sep 2024 08:56:20 +0800 Subject: [PATCH 13/15] more --- .../utils/dart_repository/dart_repo.rs | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs index f22b0f4d60..edc737bfdc 100644 --- a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs +++ b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs @@ -4,6 +4,7 @@ use anyhow::{anyhow, bail, Context}; use cargo_metadata::{Version, VersionReq}; use log::debug; use serde::de::DeserializeOwned; +use std::collections::HashMap; use std::convert::TryFrom; use std::fmt::Display; use std::path::{Path, PathBuf}; @@ -21,12 +22,11 @@ impl DartRepository { let lock_file: PubspecYaml = read_file_and_parse_yaml(path, DartToolchain::manifest_filename())?; - let toolchain = if let Some(dependencies) = lock_file.dependencies.as_ref() { - if dependencies.contains_key(&DartToolchain::Flutter.to_string()) { - DartToolchain::Flutter - } else { - DartToolchain::Dart - } + let toolchain = if option_hash_map_contains( + &lock_file.dependencies, + &DartToolchain::Flutter.to_string(), + ) { + DartToolchain::Flutter } else { DartToolchain::Dart }; @@ -64,10 +64,7 @@ impl DartRepository { requirement: &VersionReq, ) -> anyhow::Result<()> { let at = &self.at; - debug!( - "Checking presence of {} in {} at {:?}", - package, manager, at - ); + debug!("Checking presence of {package} in {manager} at {at:?}"); let manifest_file: PubspecYaml = read_file_and_parse_yaml(at, DartToolchain::manifest_filename())?; let deps = match manager { @@ -235,6 +232,14 @@ fn read_file_and_parse_yaml(at: &Path, filename: &str) -> a Ok(file) } +fn option_hash_map_contains(map: &Option>, key: &K) -> bool { + if let Some(map) = map.as_ref() { + map.contains_key(key) + } else { + false + } +} + impl PubspecLockPackage { pub(crate) fn installed_in(&self) -> Option { match self.dependency.as_str() { From 1c32cb10d4070b667ca46ca256249d8aa9165ad1 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Thu, 12 Sep 2024 08:56:36 +0800 Subject: [PATCH 14/15] rename --- frb_codegen/src/library/utils/dart_repository/dart_repo.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs index edc737bfdc..2376ccd566 100644 --- a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs +++ b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs @@ -19,11 +19,11 @@ impl DartRepository { pub(crate) fn from_path(path: &Path) -> anyhow::Result { debug!("Guessing toolchain the runner is run into"); - let lock_file: PubspecYaml = + let manifest_file: PubspecYaml = read_file_and_parse_yaml(path, DartToolchain::manifest_filename())?; let toolchain = if option_hash_map_contains( - &lock_file.dependencies, + &manifest_file.dependencies, &DartToolchain::Flutter.to_string(), ) { DartToolchain::Flutter From afb76c36b817e28f2b2b328a57d92e7aa1c66ec2 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Thu, 12 Sep 2024 08:56:59 +0800 Subject: [PATCH 15/15] fix --- frb_codegen/src/library/utils/dart_repository/dart_repo.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs index 2376ccd566..1738fdc7c4 100644 --- a/frb_codegen/src/library/utils/dart_repository/dart_repo.rs +++ b/frb_codegen/src/library/utils/dart_repository/dart_repo.rs @@ -7,6 +7,7 @@ use serde::de::DeserializeOwned; use std::collections::HashMap; use std::convert::TryFrom; use std::fmt::Display; +use std::hash::Hash; use std::path::{Path, PathBuf}; /// represents a dart / flutter repository @@ -232,7 +233,7 @@ fn read_file_and_parse_yaml(at: &Path, filename: &str) -> a Ok(file) } -fn option_hash_map_contains(map: &Option>, key: &K) -> bool { +fn option_hash_map_contains(map: &Option>, key: &K) -> bool { if let Some(map) = map.as_ref() { map.contains_key(key) } else {