Skip to content
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
4 changes: 2 additions & 2 deletions rust_crate/src/interface.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::AppError;

#[cfg(not(target_family = "wasm"))]
use super::interface_os::{send_rust_signal_real, start_rust_logic_real};
use crate::interface_os::{send_rust_signal_real, start_rust_logic_real};
#[cfg(target_family = "wasm")]
use super::interface_web::{send_rust_signal_real, start_rust_logic_real};
use crate::interface_web::{send_rust_signal_real, start_rust_logic_real};

/// This contains a message from Dart.
/// Optionally, a custom binary called `binary` can also be included.
Expand Down
14 changes: 10 additions & 4 deletions rust_crate_cli/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use std::process::ExitCode;

#[cfg(not(target_family = "wasm"))]
fn main() {
fn main() -> ExitCode {
use owo_colors::OwoColorize;
let result = rinf_cli::run_command();
if let Err(err) = result {
eprintln!("Error: {}", err);
}
eprintln!("{}", format!("Error: {}", err).red());
return ExitCode::FAILURE;
};
ExitCode::SUCCESS
}

#[cfg(target_family = "wasm")]
fn main() {
fn main() -> ExitCode {
// This is a dummy function to make Clippy happy.
ExitCode::SUCCESS
}
38 changes: 22 additions & 16 deletions rust_crate_cli/src/tool/common.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
use owo_colors::OwoColorize;

use crate::SetupError;
use std::ffi::OsStr;
use std::net::ToSocketAddrs;
use std::path::PathBuf;
use std::process::{Command, Stdio};

pub fn run_dart_command(args: &[&str]) -> Result<(), SetupError> {
#[cfg(target_family = "windows")]
let cmd = "dart.bat";
#[cfg(target_family = "unix")]
let cmd = "dart";
run_subprocess(cmd, args)
}
use std::process::Output;

pub fn run_subprocess(cmd: &str, args: &[&str]) -> Result<(), SetupError> {
Command::new(cmd)
.args(args)
.stdout(Stdio::null())
.output()?;
Ok(())
}
#[cfg(target_family = "windows")]
pub static DART_BIN: &str = "dart.bat";
#[cfg(target_family = "unix")]
pub static DART_BIN: &str = "dart";

pub fn check_internet_connection() -> bool {
"pub.dev:80"
Expand Down Expand Up @@ -48,3 +39,18 @@ impl CleanFileName for PathBuf {
Ok(file_name)
}
}

pub trait CaptureError {
fn capture_err(self) -> Result<(), SetupError>;
}

impl CaptureError for Output {
fn capture_err(self) -> Result<(), SetupError> {
if self.status.success() {
Ok(())
} else {
eprintln!("{}", String::from_utf8_lossy(&self.stderr).red());
Err(SetupError::SubprocessError)
}
}
}
6 changes: 5 additions & 1 deletion rust_crate_cli/src/tool/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum SetupError {
TemplateApplied,
DuplicatedSignal(String),
CodeSyntax(String),
SubprocessError,
}

impl Error for SetupError {
Expand Down Expand Up @@ -54,7 +55,7 @@ impl Display for SetupError {
write!(f, "Invalid `pubspec.yaml` config: {}", s)
}
Self::BadFilePath(p) => {
write!(f, "Not a valid file path: `{}`", p.display())
write!(f, "Invalid file path: `{}`", p.display())
}
Self::NotFlutterApp => {
write!(f, "This is not a Flutter app project")
Expand All @@ -68,6 +69,9 @@ impl Display for SetupError {
Self::CodeSyntax(n) => {
write!(f, "Invalid syntax in file `{}`", n)
}
Self::SubprocessError => {
write!(f, "A subprocess did not exit successfully")
}
}
}
}
Expand Down
31 changes: 23 additions & 8 deletions rust_crate_cli/src/tool/template.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::tool::{
RinfConfig, SetupError, generate_dart_code, run_dart_command, run_subprocess,
CaptureError, CleanFileName, DART_BIN, RinfConfig, SetupError,
generate_dart_code,
};
use include_dir::{Dir, include_dir};
use std::fs::{create_dir_all, read_to_string, write};
use std::path::Path;

use super::CleanFileName;
use std::process::Command;

static TEMPLATE_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/template");

Expand All @@ -28,14 +28,23 @@ pub fn apply_rust_template(
update_readme(root_dir)?;

// Add Dart dependencies.
run_dart_command(&["pub", "add", "meta"])?;
run_dart_command(&["pub", "add", "tuple"])?;
Command::new(DART_BIN)
.args(["pub", "add", "meta"])
.output()?
.capture_err()?;
Command::new(DART_BIN)
.args(["pub", "add", "tuple"])
.output()?
.capture_err()?;

// Modify `lib/main.dart`
update_main_dart(root_dir)?;

// Format Rust code.
run_subprocess("cargo", &["fmt"])?;
Command::new("cargo")
.args(["fmt"])
.output()?
.capture_err()?;

// Generate Dart code.
generate_dart_code(root_dir, rinf_config)?;
Expand Down Expand Up @@ -161,7 +170,10 @@ please refer to Rinf's [documentation](https://rinf.cunarist.com).
fn update_main_dart(root_dir: &Path) -> Result<(), SetupError> {
let main_path = root_dir.join("lib/main.dart");
if main_path.exists() {
run_dart_command(&["format", "lib/main.dart"])?;
Command::new(DART_BIN)
.args(["format", "lib/main.dart"])
.output()?
.capture_err()?;
let mut content = read_to_string(&main_path)?;
if !content.contains("messages/all.dart") {
content = content.replacen(
Expand All @@ -179,7 +191,10 @@ fn update_main_dart(root_dir: &Path) -> Result<(), SetupError> {
);
}
write(main_path, content)?;
run_dart_command(&["format", "lib/main.dart"])?;
Command::new(DART_BIN)
.args(["format", "lib/main.dart"])
.output()?
.capture_err()?;
}
Ok(())
}
47 changes: 29 additions & 18 deletions rust_crate_cli/src/tool/webassembly.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::dimmedln;
use crate::tool::{SetupError, run_subprocess};
use crate::tool::{CaptureError, SetupError};
use std::path::Path;
use std::process::Command;

Expand Down Expand Up @@ -46,15 +46,30 @@ pub fn build_webassembly(
}

fn install_wasm_toolchain() -> Result<(), SetupError> {
run_subprocess("rustup", &["toolchain", "install", "nightly"])?;
run_subprocess("rustup", &["+nightly", "component", "add", "rust-src"])?;
run_subprocess(
"rustup",
&["+nightly", "target", "add", "wasm32-unknown-unknown"],
)?;
run_subprocess("rustup", &["target", "add", "wasm32-unknown-unknown"])?;
run_subprocess("cargo", &["install", "wasm-pack"])?;
run_subprocess("cargo", &["install", "wasm-bindgen-cli"])?;
Command::new("rustup")
.args(["toolchain", "install", "nightly"])
.output()?
.capture_err()?;
Command::new("rustup")
.args(["+nightly", "component", "add", "rust-src"])
.output()?
.capture_err()?;
Command::new("rustup")
.args(["+nightly", "target", "add", "wasm32-unknown-unknown"])
.output()?
.capture_err()?;
Command::new("rustup")
.args(["target", "add", "wasm32-unknown-unknown"])
.output()?
.capture_err()?;
Command::new("cargo")
.args(["install", "wasm-pack"])
.output()?
.capture_err()?;
Command::new("cargo")
.args(["install", "wasm-bindgen-cli"])
.output()?
.capture_err()?;
Ok(())
}

Expand All @@ -78,25 +93,21 @@ fn compile_wasm(
"--target",
"web",
"--",
"-Z",
"build-std=std,panic_abort",
"-Zbuild-std=std,panic_abort",
];
if !is_release_mode {
wasm_pack_args.insert(7, "--dev");
}

let status = Command::new("wasm-pack")
Command::new("wasm-pack")
.args(&wasm_pack_args)
.env("RUSTUP_TOOLCHAIN", "nightly")
.env(
"RUSTFLAGS",
"-C target-feature=+atomics,+bulk-memory,+mutable-globals",
)
.status()?;

if !status.success() {
panic!("Wasm compilation failed");
}
.output()?
.capture_err()?;

Ok(())
}
Loading