Skip to content

Commit

Permalink
Merge pull request #1566 from fzyzcjy/feat/11642
Browse files Browse the repository at this point in the history
Improve `flutter_rust_bridge_codegen integrate`
  • Loading branch information
fzyzcjy authored Jan 1, 2024
2 parents c006efe + b9363b6 commit 0ded428
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
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")}`'),
),
),
);
}
}

0 comments on commit 0ded428

Please sign in to comment.