Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Rinf commands work in offline environments #369

Merged
merged 1 commit into from
Jun 11, 2024
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
2 changes: 2 additions & 0 deletions flutter_ffi_plugin/bin/rinf.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'src/config.dart';
import 'src/helpers.dart';
import 'src/message.dart';
import 'src/internet.dart';

Future<void> main(List<String> args) async {
if (args.isEmpty) {
Expand All @@ -10,6 +11,7 @@ Future<void> main(List<String> args) async {
}

final rinfConfig = await loadVerifiedRinfConfig("pubspec.yaml");
await checkConnectivity();

switch (args[0]) {
case "config":
Expand Down
70 changes: 46 additions & 24 deletions flutter_ffi_plugin/bin/src/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:yaml/yaml.dart';
import 'config.dart';
import 'message.dart';
import 'common.dart';
import 'internet.dart';

/// Creates new folders and files to an existing Flutter project folder.
Future<void> applyRustTemplate({
Expand Down Expand Up @@ -197,31 +198,52 @@ Future<void> copyDirectory(Uri source, Uri destination) async {
}

Future<void> buildWebassembly({bool isReleaseMode = false}) async {
// Verify Rust toolchain.
print("Verifying Rust toolchain for the web." +
"\nThis might take a while if there are new updates.");
await Process.run("rustup", ["toolchain", "install", "nightly"]);
await Process.run("rustup", [
"+nightly",
"component",
"add",
"rust-src",
]);
await Process.run("rustup", [
"+nightly",
"target",
"add",
"wasm32-unknown-unknown",
]); // For actual compilation
await Process.run("rustup", [
"target",
"add",
"wasm32-unknown-unknown",
]); // For Rust-analyzer
await Process.run("cargo", ["install", "wasm-pack"]);
await Process.run("cargo", ["install", "wasm-bindgen-cli"]);
// Ensure Rust toolchain.
if (isInternetConnected) {
print("Ensuring Rust toolchain for the web." +
"\nThis is done by installing it globally on the system.");
final processResults = <ProcessResult>[];
processResults.add(await Process.run("rustup", [
"toolchain",
"install",
"nightly",
]));
processResults.add(await Process.run("rustup", [
"+nightly",
"component",
"add",
"rust-src",
]));
processResults.add(await Process.run("rustup", [
"+nightly",
"target",
"add",
"wasm32-unknown-unknown",
])); // For actual compilation
processResults.add(await Process.run("rustup", [
"target",
"add",
"wasm32-unknown-unknown",
])); // For Rust-analyzer
processResults.add(await Process.run("cargo", [
"install",
"wasm-pack",
]));
processResults.add(await Process.run("cargo", [
"install",
"wasm-bindgen-cli",
]));
processResults.forEach((processResult) {
if (processResult.exitCode != 0) {
print(processResult.stderr.toString().trim());
throw Exception('Cannot globally install Rust toolchain for the web.');
}
});
} else {
print("Skipping ensurement of Rust toolchain for the web.");
}

// Verify Flutter SDK web server's response headers.
// Patch Flutter SDK web server's response headers.
try {
await patchServerHeaders();
print("Patched Flutter SDK's web server with CORS HTTP headers.");
Expand Down
7 changes: 7 additions & 0 deletions flutter_ffi_plugin/bin/src/internet.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:internet_connection_checker/internet_connection_checker.dart';

var isInternetConnected = false;

Future<void> checkConnectivity() async {
isInternetConnected = await InternetConnectionChecker().hasConnection;
}
65 changes: 39 additions & 26 deletions flutter_ffi_plugin/bin/src/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:path/path.dart';
import 'package:watcher/watcher.dart';
import 'config.dart';
import 'common.dart';
import 'internet.dart';

enum MarkType {
dartSignal,
Expand Down Expand Up @@ -46,7 +47,7 @@ Future<void> generateMessageCode({
resourcesInFolders,
);

// Verify `package` statement in `.proto` files.
// Include `package` statement in `.proto` files.
// Package name should be the same as the filename
// because Rust filenames are written with package name
// and Dart filenames are written with the `.proto` filename.
Expand Down Expand Up @@ -76,18 +77,24 @@ Future<void> generateMessageCode({
}

// Generate Rust message files.
if (!silent) {
print("Verifying `protoc-gen-prost` for Rust." +
"\nThis might take a while if there are new updates.");
}
final cargoInstallCommand = await Process.run('cargo', [
'install',
'protoc-gen-prost',
...(messageConfig.rustSerde ? ['protoc-gen-prost-serde'] : [])
]);
if (cargoInstallCommand.exitCode != 0) {
print(cargoInstallCommand.stderr.toString().trim());
throw Exception('Cannot globally install `protoc-gen-prost` Rust crate');
if (isInternetConnected) {
if (!silent) {
print("Ensuring `protoc-gen-prost` for Rust." +
"\nThis is done by installing it globally on the system.");
}
final cargoInstallCommand = await Process.run('cargo', [
'install',
'protoc-gen-prost',
...(messageConfig.rustSerde ? ['protoc-gen-prost-serde'] : [])
]);
if (cargoInstallCommand.exitCode != 0) {
print(cargoInstallCommand.stderr.toString().trim());
throw Exception('Cannot globally install `protoc-gen-prost` Rust crate');
}
} else {
if (!silent) {
print("Skipping ensurement of `protoc-gen-prost` for Rust.");
}
}
for (final entry in resourcesInFolders.entries) {
final subPath = entry.key;
Expand Down Expand Up @@ -164,19 +171,25 @@ Future<void> generateMessageCode({
}

// Generate Dart message files.
if (!silent) {
print("Verifying `protoc_plugin` for Dart." +
"\nThis might take a while if there are new updates.");
}
final pubGlobalActivateCommand = await Process.run('dart', [
'pub',
'global',
'activate',
'protoc_plugin',
]);
if (pubGlobalActivateCommand.exitCode != 0) {
print(pubGlobalActivateCommand.stderr.toString().trim());
throw Exception('Cannot globally install `protoc_plugin` Dart package');
if (isInternetConnected) {
if (!silent) {
print("Ensuring `protoc_plugin` for Dart." +
"\nThis is done by installing it globally on the system.");
}
final pubGlobalActivateCommand = await Process.run('dart', [
'pub',
'global',
'activate',
'protoc_plugin',
]);
if (pubGlobalActivateCommand.exitCode != 0) {
print(pubGlobalActivateCommand.stderr.toString().trim());
throw Exception('Cannot globally install `protoc_plugin` Dart package');
}
} else {
if (!silent) {
print("Skipping ensurement of `protoc_plugin` for Dart.");
}
}
for (final entry in resourcesInFolders.entries) {
final subPath = entry.key;
Expand Down
1 change: 1 addition & 0 deletions flutter_ffi_plugin/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:
watcher: ^1.1.0
ffi: ^2.1.0
yaml: ^3.1.2
internet_connection_checker: ^1.0.0

dev_dependencies:
lints: ">=3.0.0 <5.0.0"
Expand Down
2 changes: 1 addition & 1 deletion rust_crate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() -> Result<()> {
use std::path;
use std::process;

// Verify Protobuf compiler.
// Ensure Protobuf compiler.
let protoc_path = if let Ok(installed) = which::which("protoc") {
// Get the path of Protobuf compiler that's already installed.
println!("Detected `protoc`, skipping auto installation.");
Expand Down