Skip to content

Commit

Permalink
Merge pull request #613 from Roms1383/chore/improve-version-checking
Browse files Browse the repository at this point in the history
Chore/improve version checking
  • Loading branch information
fzyzcjy authored Aug 4, 2022
2 parents ed258cf + ebea31c commit 45a2f11
Show file tree
Hide file tree
Showing 8 changed files with 495 additions and 246 deletions.
3 changes: 3 additions & 0 deletions frb_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ enum_dispatch = "0.3.8"
thiserror = "1"
cbindgen = "0.24"

[dev-dependencies]
semver = "1.0.12"

[profile.release]
strip = "debuginfo"
lto = "thin"
Expand Down
60 changes: 27 additions & 33 deletions frb_codegen/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ use std::fmt::Write;
use std::path::Path;
use std::process::Command;
use std::process::Output;
use std::str::FromStr;

use crate::error::{Error, Result};
use crate::utils::PackageManager;
use crate::utils::{ensure_dependencies, guess_toolchain, DartToolchain};
use crate::tools::DartRepository;
use crate::tools::PackageManager;
use crate::tools::FFIGEN_REQUIREMENT;
use crate::tools::FFI_REQUIREMENT;
use log::{debug, info, warn};

#[must_use]
fn call_shell(cmd: &str) -> Output {
pub(crate) fn call_shell(cmd: &str) -> Output {
#[cfg(windows)]
return execute_command("powershell", &["-noprofile", "-c", cmd], None);

Expand All @@ -18,32 +21,25 @@ fn call_shell(cmd: &str) -> Output {
}

pub fn ensure_tools_available(dart_root: &str) -> Result {
let toolchain = guess_toolchain(dart_root).unwrap();
if toolchain == DartToolchain::Dart && !call_shell("dart --version").status.success() {
return Err(Error::MissingExe("dart".to_string()));
} else if toolchain == DartToolchain::Flutter
&& !call_shell("flutter --version").status.success()
{
return Err(Error::MissingExe("flutter".to_string()));
let repo =
DartRepository::from_str(dart_root).map_err(|e| Error::StringError(e.to_string()))?;
if !repo.toolchain_available() {
return Err(Error::MissingExe(repo.toolchain.to_string()));
}

let ffi = ensure_dependencies(dart_root, "ffi", PackageManager::Dependencies).unwrap();
if ffi.is_none() {
return Err(Error::MissingDep {
name: "ffi".into(),
context: PackageManager::Dependencies,
version: "^2.0.1".into(),
});
}
repo.has_specified("ffi", PackageManager::Dependencies, &FFI_REQUIREMENT)?;
repo.has_installed("ffi", PackageManager::Dependencies, &FFI_REQUIREMENT)?;

let ffigen = ensure_dependencies(dart_root, "ffigen", PackageManager::DevDependencies).unwrap();
if ffigen.is_none() {
return Err(Error::MissingDep {
name: "ffigen".into(),
context: PackageManager::DevDependencies,
version: "^6.0.1".into(),
});
}
repo.has_specified(
"ffigen",
PackageManager::DevDependencies,
&FFIGEN_REQUIREMENT,
)?;
repo.has_installed(
"ffigen",
PackageManager::DevDependencies,
&FFIGEN_REQUIREMENT,
)?;

Ok(())
}
Expand Down Expand Up @@ -233,10 +229,8 @@ fn ffigen(
std::io::Write::write_all(&mut config_file, config.as_bytes())?;
debug!("ffigen config_file: {:?}", config_file);

let cmd = format!(
"{} run",
guess_toolchain(dart_root).unwrap().as_run_command()
);
let repo = DartRepository::from_str(dart_root).unwrap();
let cmd = format!("{} run", repo.toolchain.as_run_command());
let res = call_shell(&format!(
"cd {}{}{} ffigen --config \"{}\"",
dart_root,
Expand Down Expand Up @@ -288,18 +282,18 @@ pub fn format_dart(path: &str, line_length: i32) -> Result {

pub fn build_runner(dart_root: &str) -> Result {
info!("Running build_runner at {}", dart_root);
let toolchain = guess_toolchain(dart_root).unwrap();
let repo = DartRepository::from_str(dart_root).unwrap();
let out = if cfg!(windows) {
call_shell(&format!(
"cd \"{}\"; {} run build_runner build --delete-conflicting-outputs",
dart_root,
toolchain.as_run_command()
repo.toolchain.as_run_command()
))
} else {
call_shell(&format!(
"cd \"{}\" && {} run build_runner build --delete-conflicting-outputs",
dart_root,
toolchain.as_run_command()
repo.toolchain.as_run_command()
))
};
if !out.status.success() {
Expand Down
25 changes: 17 additions & 8 deletions frb_codegen/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use thiserror::Error;

use crate::utils::PackageManager;
use crate::tools::PackageManager;

pub type Result = std::result::Result<(), Error>;

#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum Error {
#[error("rustfmt failed: {0}")]
Rustfmt(String),
Expand All @@ -21,17 +21,17 @@ pub enum Error {
MissingExe(String),
#[error("{0}")]
StringError(String),
#[error("please add {name} to your {context}. (version {version})")]
#[error("please add {name} to your {manager}. (version {requirement})")]
MissingDep {
name: String,
context: PackageManager,
version: String,
manager: PackageManager,
requirement: String,
},
#[error("please update version of {name} in your {context}. (version {version})")]
#[error("please update version of {name} in your {manager}. (version {requirement})")]
InvalidDep {
name: String,
context: PackageManager,
version: String,
manager: PackageManager,
requirement: String,
},
}

Expand All @@ -44,3 +44,12 @@ impl Error {
Self::StringError(msg)
}
}

impl From<anyhow::Error> for Error {
fn from(e: anyhow::Error) -> Self {
if let Some(e) = e.downcast_ref::<Self>() {
return e.clone();
}
Error::StringError(e.to_string())
}
}
1 change: 1 addition & 0 deletions frb_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::others::*;
use crate::utils::*;

mod config;
mod tools;

pub use crate::commands::ensure_tools_available;
pub use crate::config::parse as config_parse;
Expand Down
Loading

0 comments on commit 45a2f11

Please sign in to comment.