Skip to content

Commit

Permalink
Merge pull request #2390 from fzyzcjy/feat/12627
Browse files Browse the repository at this point in the history
  • Loading branch information
fzyzcjy authored Nov 5, 2024
2 parents ad1b295 + e632d27 commit 648c239
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
4 changes: 3 additions & 1 deletion frb_codegen/src/library/commands/dart_build_runner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::command_run;
use crate::commands::command_runner::call_shell;
use crate::library::commands::fvm::command_arg_maybe_fvm;
use crate::utils::dart_repository::dart_repo::DartRepository;
use anyhow::bail;
use log::debug;
Expand All @@ -9,9 +10,10 @@ use std::path::Path;
pub fn dart_build_runner(dart_root: &Path) -> anyhow::Result<()> {
debug!("Running build_runner at dart_root={dart_root:?}");

let repo = DartRepository::from_path(dart_root).unwrap();
let repo = DartRepository::from_path(dart_root)?;
let out = command_run!(
call_shell[Some(dart_root), Some(dart_run_extra_env())],
?command_arg_maybe_fvm(Some(dart_root)),
*repo.toolchain.as_run_command(),
*repo.command_extra_args(),
"run",
Expand Down
2 changes: 2 additions & 0 deletions frb_codegen/src/library/commands/dart_fix.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::command_run;
use crate::commands::command_runner::call_shell;
use crate::library::commands::command_runner::check_exit_code;
use crate::library::commands::fvm::command_arg_maybe_fvm;
use anyhow::Result;
use log::debug;
use std::path::Path;
Expand All @@ -11,6 +12,7 @@ pub fn dart_fix(base_path: &Path) -> Result<()> {

let res = command_run!(
call_shell[Some(base_path), None],
?command_arg_maybe_fvm(Some(base_path)),
"dart",
"fix",
"--apply",
Expand Down
2 changes: 2 additions & 0 deletions frb_codegen/src/library/commands/dart_format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::command_run;
use crate::commands::command_runner::call_shell;
use crate::library::commands::command_runner::check_exit_code;
use crate::library::commands::fvm::command_arg_maybe_fvm;
use crate::utils::path_utils::{normalize_windows_unc_path, path_to_string};
use anyhow::Context;
use itertools::Itertools;
Expand All @@ -24,6 +25,7 @@ pub fn dart_format(

let res = command_run!(
call_shell[Some(base_path), None],
?command_arg_maybe_fvm(Some(base_path)),
"dart",
"format",
"--line-length",
Expand Down
14 changes: 12 additions & 2 deletions frb_codegen/src/library/commands/flutter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::command_run;
use crate::commands::command_runner::call_shell;
use crate::library::commands::command_runner::check_exit_code;
use crate::library::commands::fvm::command_arg_maybe_fvm;
use crate::misc::Template;
use log::info;
use std::path::Path;
Expand All @@ -24,7 +25,11 @@ pub fn flutter_create(name: &str, org: &Option<String>, template: Template) -> a
"Execute `flutter create {}` (this may take a while)",
args.join(" ")
);
check_exit_code(&command_run!(call_shell[None, None], "flutter", "create", *args)?)
check_exit_code(&command_run!(
call_shell[None, None],
?command_arg_maybe_fvm(None),
"flutter", "create", *args
)?)
}

#[allow(clippy::vec_init_then_push)]
Expand All @@ -35,6 +40,7 @@ pub fn flutter_pub_add(items: &[String], pwd: Option<&Path>) -> anyhow::Result<(
);
check_exit_code(&command_run!(
call_shell[pwd, None],
?command_arg_maybe_fvm(pwd),
"flutter",
"pub",
"add",
Expand All @@ -45,5 +51,9 @@ pub fn flutter_pub_add(items: &[String], pwd: Option<&Path>) -> anyhow::Result<(
#[allow(clippy::vec_init_then_push)]
pub fn flutter_pub_get(path: &Path) -> anyhow::Result<()> {
info!("Execute `flutter pub get` inside {path:?} (this may take a while)");
check_exit_code(&command_run!(call_shell[Some(path), None], "flutter", "pub", "get")?)
check_exit_code(&command_run!(
call_shell[Some(path), None],
?command_arg_maybe_fvm(Some(path)),
"flutter", "pub", "get"
)?)
}
42 changes: 42 additions & 0 deletions frb_codegen/src/library/commands/fvm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::command_run;
use crate::library::commands::command_runner::call_shell;
use std::path::Path;

pub(crate) fn command_arg_maybe_fvm(pwd: Option<&Path>) -> Option<String> {
if should_use_fvm(pwd) {
Some("fvm".to_owned())
} else {
None
}
}

fn should_use_fvm(pwd: Option<&Path>) -> bool {
if pwd.is_some() && !has_fvmrc(pwd.unwrap()) {
false
} else {
let has_fvm_installation_output = has_fvm_installation();
if !has_fvm_installation_output {
log::info!("Has .fvmrc but no fvm binary installation, thus skip using fvm.");
}
has_fvm_installation_output
}
}

fn has_fvmrc(pwd: &Path) -> bool {
let mut directory = pwd;
loop {
if directory.join(".fvmrc").exists() {
return true;
}
if let Some(parent) = directory.parent() {
directory = parent;
} else {
return false;
}
}
}

fn has_fvm_installation() -> bool {
command_run!(call_shell[None, None], "fvm", "--version")
.map_or(false, |res| res.status.success())
}
1 change: 1 addition & 0 deletions frb_codegen/src/library/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pub(crate) mod ensure_tools_available;
pub(crate) mod ffigen;
pub(crate) mod flutter;
pub(crate) mod format_rust;
pub(crate) mod fvm;

0 comments on commit 648c239

Please sign in to comment.