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

Improve flutter_rust_bridge_codegen integrate #1566

Merged
merged 13 commits into from
Jan 1, 2024
14 changes: 12 additions & 2 deletions frb_codegen/src/library/integration/creator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::integration::integrator;
use crate::integration::integrator::IntegrateConfig;
use crate::library::commands::flutter::flutter_create;
use anyhow::ensure;
use log::{debug, info};
use std::path::Path;
use std::{env, fs};
Expand All @@ -14,11 +15,20 @@ pub struct CreateConfig {

/// Create a new Flutter + Rust project.
pub fn create(config: CreateConfig) -> anyhow::Result<()> {
debug!("create name={}", config.name);
let dart_root = env::current_dir()?.join(&config.name);
debug!("create name={} dart_root={dart_root:?}", config.name);

// This will stop the whole generator and tell the users, so we do not care about testing it
// frb-coverage:ignore-start
ensure!(
!dart_root.exists(),
"The target folder {:?} already exists. Please use the `integrate` command in this case",
dart_root,
);
// frb-coverage:ignore-end

flutter_create(&config.name)?;

let dart_root = env::current_dir()?.join(&config.name);
env::set_current_dir(&dart_root)?;

remove_unnecessary_files(&dart_root)?;
Expand Down
4 changes: 2 additions & 2 deletions frb_codegen/src/library/integration/integrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ fn modify_file(
let commented_existing_content = existing_content
.map(|x| {
format!(
"\n\n{}",
"// The original content is temporarily commented out to allow generating a self-contained demo - feel free to uncomment later.\n\n{}\n\n",
x.split('\n').map(|line| format!("// {line}")).join("\n")
)
})
.unwrap_or_default();
return Some((path, [&src, commented_existing_content.as_bytes()].concat()));
return Some((path, [commented_existing_content.as_bytes(), &src].concat()));
// We do not care about this warning
// frb-coverage:ignore-start
}
Expand Down
52 changes: 27 additions & 25 deletions frb_example/flutter_via_integrate/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter_via_integrate/src/rust/api/simple.dart';
import 'package:flutter_via_integrate/src/rust/frb_generated.dart';

Future<void> main() async {
await RustLib.init();
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('flutter_rust_bridge quickstart')),
body: Center(
child: Text(
'Action: Call Rust `greet("Tom")`\nResult: `${greet(name: "Tom")}`'),
),
),
);
}
}
// The original content is temporarily commented out to allow generating a self-contained demo - feel free to uncomment later.

// import 'package:flutter/material.dart';
//
Expand Down Expand Up @@ -150,3 +126,29 @@ class MyApp extends StatelessWidget {
// }
// }
//

import 'package:flutter/material.dart';
import 'package:flutter_via_integrate/src/rust/api/simple.dart';
import 'package:flutter_via_integrate/src/rust/frb_generated.dart';

Future<void> main() async {
await RustLib.init();
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('flutter_rust_bridge quickstart')),
body: Center(
child: Text(
'Action: Call Rust `greet("Tom")`\nResult: `${greet(name: "Tom")}`'),
),
),
);
}
}
Loading