Skip to content

Commit

Permalink
Merge pull request #291 from cunarist/check-pubspec
Browse files Browse the repository at this point in the history
Check `pubspec.yaml` while applying Rust template
  • Loading branch information
temeddix authored Feb 5, 2024
2 parents 6078025 + d6a127f commit 6bd4a4d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
66 changes: 38 additions & 28 deletions flutter_ffi_plugin/bin/src/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';
import 'package:package_config/package_config.dart';
import 'config.dart';
import 'message.dart';
import 'package:yaml/yaml.dart';

/// Creates new folders and files to an existing Flutter project folder.
Future<void> applyRustTemplate({
Expand All @@ -21,11 +22,17 @@ Future<void> applyRustTemplate({
);
final packagePath = package.root.toFilePath();

// Check if current folder is a Flutter project.
final mainFile = File('$flutterProjectPath/lib/main.dart');
final isFlutterProject = await mainFile.exists();
// Check if current folder is a Flutter app project.
final specFile = File('$flutterProjectPath/pubspec.yaml');
final isFlutterProject = await specFile.exists();
if (!isFlutterProject) {
print("\nThis folder doesn't look like a Flutter project. Aborting...\n");
print("This folder doesn't look like a Flutter project.");
return;
}
final pubspec = loadYaml(await specFile.readAsString());
final String? publishTo = pubspec['publish_to'];
if (publishTo != "none") {
print("Flutter package development is not supported by Rinf.");
return;
}

Expand Down Expand Up @@ -145,31 +152,34 @@ please refer to Rinf's [documentation](https://rinf.cunarist.com).
await Process.run('dart', ['pub', 'add', 'protobuf']);

// Modify `./lib/main.dart`
await Process.run('dart', ['format', './lib/main.dart']);
var mainText = await mainFile.readAsString();
if (!mainText.contains('messages/generated.dart')) {
final lines = mainText.split("\n");
final lastImportIndex = lines.lastIndexWhere(
(line) => line.startsWith('import '),
);
lines.insert(
lastImportIndex + 1,
"import './messages/generated.dart';",
);
mainText = lines.join("\n");
}
if (!mainText.contains('initializeRust()')) {
mainText = mainText.replaceFirst(
'main() {',
'main() async {',
);
mainText = mainText.replaceFirst(
'main() async {',
'main() async { await initializeRust();',
);
final mainFile = File('$flutterProjectPath/lib/main.dart');
if (await mainFile.exists()) {
await Process.run('dart', ['format', './lib/main.dart']);
var mainText = await mainFile.readAsString();
if (!mainText.contains('messages/generated.dart')) {
final lines = mainText.split("\n");
final lastImportIndex = lines.lastIndexWhere(
(line) => line.startsWith('import '),
);
lines.insert(
lastImportIndex + 1,
"import './messages/generated.dart';",
);
mainText = lines.join("\n");
}
if (!mainText.contains('initializeRust()')) {
mainText = mainText.replaceFirst(
'main() {',
'main() async {',
);
mainText = mainText.replaceFirst(
'main() async {',
'main() async { await initializeRust();',
);
}
await mainFile.writeAsString(mainText);
await Process.run('dart', ['format', './lib/main.dart']);
}
await mainFile.writeAsString(mainText);
await Process.run('dart', ['format', './lib/main.dart']);

await generateMessageCode(silent: true, messageConfig: messageConfig);

Expand Down
6 changes: 2 additions & 4 deletions rust_crate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ fn main() {
let dart_command_args: Vec<String> = env::args().skip(1).collect();

// Build the command to run the Dart script.
#[cfg(target_family = "windows")]
let mut command = process::Command::new("dart.bat");
#[cfg(target_family = "unix")]
let mut command = process::Command::new("dart");
let dart_path = which::which("dart").expect("Couldn't find Dart executable");
let mut command = process::Command::new(dart_path);
command.args(["run", "rinf"]);
command.args(&dart_command_args);

Expand Down

0 comments on commit 6bd4a4d

Please sign in to comment.