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

Add The Ability To Generate Plugins From The CLI Tool #2144

Merged
merged 28 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fb35ce3
feat: Code changes for plugin project generation support
mcmah309 Jun 22, 2024
2df1f12
feat: Support file changes for plugin project generation support
mcmah309 Jun 22, 2024
1248ed7
chore: Update release script
mcmah309 Jun 22, 2024
1bce844
feat: Change type to template
mcmah309 Jun 23, 2024
cee548c
feat: Add integration test to template generation
mcmah309 Jun 23, 2024
0b3fcb8
test: Add tests for plugin creation
mcmah309 Jun 23, 2024
d86cc7c
chore: Update fvm to match ci version
mcmah309 Jun 23, 2024
8a907a8
chore: Remove unneeded fvm dot file
mcmah309 Jun 23, 2024
0fc7a31
chore: Update .gitignore for fvm dot file
mcmah309 Jun 23, 2024
6f20a74
Merge remote-tracking branch 'origin/add-plugin-cli' into add-plugin-cli
mcmah309 Jun 23, 2024
a6cb054
refactor: Move file after merge
mcmah309 Jun 23, 2024
c8c8859
chore: submodule move
mcmah309 Jun 23, 2024
f0341d2
chore: submodule move
mcmah309 Jun 23, 2024
a3ee2af
chore: Remove files removed before merge
mcmah309 Jun 23, 2024
8099455
test: Test plugin create
mcmah309 Jun 23, 2024
a3550ae
feat: Change generated plugin example files
mcmah309 Jun 23, 2024
f7f2476
chore: Update flutter_package example files
mcmah309 Jun 23, 2024
243e318
doc: Move old plugin set up to archive
mcmah309 Jun 23, 2024
97c5867
chore: lint fix
mcmah309 Jun 23, 2024
d460693
doc: Update package docs
mcmah309 Jun 23, 2024
b0181b1
refactor: Code review nits
mcmah309 Jun 24, 2024
c8ae116
Revert "doc: Move old plugin set up to archive"
mcmah309 Jun 24, 2024
a559975
doc: Doc code review chagnes
mcmah309 Jun 24, 2024
1dd09c2
feat: move path computation earlier
mcmah309 Jun 24, 2024
76f8238
refactor: Add template postfix back as per review comment
mcmah309 Jun 24, 2024
52b4865
refactor: Code review nit
mcmah309 Jun 24, 2024
4a99351
Merge branch 'master' into master
fzyzcjy Jun 25, 2024
40fd268
Merge branch 'feat/12413' into master
fzyzcjy Jun 25, 2024
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: 0 additions & 4 deletions .fvm/fvm_config.json
fzyzcjy marked this conversation as resolved.
Show resolved Hide resolved

This file was deleted.

4 changes: 4 additions & 0 deletions .fvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"flutter": "3.22.0",
"flavors": {}
}
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ jobs:
package:
- frb_example--flutter_via_create
- frb_example--flutter_via_integrate
- frb_example--flutter_package

steps:
# setup
Expand Down
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ launch.json
Makefile
*.log
.vscode
.fvm/flutter_sdk
coverage/
lcov.info
temp.txt
frb_example/my_app
frb_example/my_app

# FVM Version Cache
.fvm/
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "frb_codegen/assets/integration_template/cargokit"]
path = frb_codegen/assets/integration_template/rust_builder/cargokit
[submodule "frb_codegen/assets/integration_template/shared/rust_builder/cargokit"]
path = frb_codegen/assets/integration_template/shared/rust_builder/cargokit
url = https://github.com/irondash/cargokit
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:REPLACE_ME_DART_PACKAGE_NAME/REPLACE_ME_DART_PACKAGE_NAME.dart';
import 'package:integration_test/integration_test.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
setUpAll(() async => await RustLib.init());
test('Can call rust function', () async {
expect(greet(name: "Tom"), "Hello, Tom!");
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:flutter/material.dart';
import 'package:REPLACE_ME_DART_PACKAGE_NAME/REPLACE_ME_DART_PACKAGE_NAME.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")}`'),
),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library REPLACE_ME_DART_PACKAGE_NAME;

export 'src/rust/api/simple.dart';
export 'src/rust/frb_generated.dart' show RustLib;
Submodule cargokit deleted from 969c49
Submodule cargokit added at d56f1f
30 changes: 29 additions & 1 deletion frb_codegen/src/binary/commands.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::codegen::ConfigDumpContent;
use clap::{Args, Parser, Subcommand};
use clap::{Args, Parser, Subcommand, ValueEnum};
use lib_flutter_rust_bridge_codegen::misc::Template;
use std::path::PathBuf;

// The name `Cli`, `Commands` come from https://docs.rs/clap/latest/clap/_derive/_tutorial/chapter_0/index.html
Expand Down Expand Up @@ -184,6 +185,10 @@ pub(crate) struct CreateCommandArgs {

#[clap(flatten)]
pub common: CreateOrIntegrateCommandCommonArgs,

/// The template type to use to generate the flutter files.
#[clap(short, long, value_enum, default_value = "app")]
pub template: TemplateArg
}

#[derive(Debug, Args)]
Expand All @@ -194,6 +199,19 @@ pub(crate) struct IntegrateCommandArgs {

#[clap(flatten)]
pub common: CreateOrIntegrateCommandCommonArgs,

/// The template type to use for integration. This should usually match the type of flutter project
/// being integrating with.
#[clap(short, long, value_enum, default_value = "app")]
pub template: TemplateArg
}

#[derive(Debug,Clone,ValueEnum)]
pub(crate) enum TemplateArg {
/// (default) a Flutter application
App,
/// A shareable Flutter project that can be used across multiple Flutter applictions.
Plugin,
}

#[derive(Debug, Args)]
Expand Down Expand Up @@ -229,3 +247,13 @@ pub(crate) struct BuildWebCommandArgs {

#[derive(Debug, Args)]
pub(crate) struct InternalGenerateCommandArgs {}


impl From<TemplateArg> for Template {
fn from(value: TemplateArg) -> Self {
match value {
TemplateArg::App => Template::App,
TemplateArg::Plugin => Template::Plugin,
}
}
}
7 changes: 6 additions & 1 deletion frb_codegen/src/library/commands/flutter.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
use crate::command_run;
use crate::commands::command_runner::call_shell;
use crate::library::commands::command_runner::check_exit_code;
use crate::misc::Template;
use log::info;
use std::path::Path;

#[allow(clippy::vec_init_then_push)]
pub fn flutter_create(name: &str, org: &Option<String>) -> anyhow::Result<()> {
pub fn flutter_create(name: &str, org: &Option<String>, template: Template) -> anyhow::Result<()> {
let mut args = vec![name];
if let Some(o) = org {
args.extend(["--org", o]);
}
match template {
Template::App => args.extend(["--template","app"]),
Template::Plugin => args.extend(["--template","plugin","--platforms", "android,ios,linux,macos,windows,web"]),
}
info!(
"Execute `flutter create {}` (this may take a while)",
args.join(" ")
Expand Down
2 changes: 1 addition & 1 deletion frb_codegen/src/library/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pub(crate) mod ensure_tools_available;
pub(crate) mod ffigen;
pub(crate) mod flutter;
pub(crate) mod format_dart;
pub(crate) mod format_rust;
pub(crate) mod format_rust;
55 changes: 48 additions & 7 deletions frb_codegen/src/library/integration/creator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::integration::integrator;
use crate::integration::integrator::IntegrateConfig;
use crate::library::commands::flutter::flutter_create;
use anyhow::ensure;
use crate::misc::Template;
use anyhow::{bail, ensure};
use log::{debug, info};
use std::path::Path;
use std::{env, fs};
Expand All @@ -12,6 +13,7 @@ pub struct CreateConfig {
pub enable_local_dependency: bool,
pub rust_crate_name: Option<String>,
pub rust_crate_dir: String,
pub template: Template,
}

/// Create a new Flutter + Rust project.
Expand All @@ -31,26 +33,65 @@ pub fn create(config: CreateConfig) -> anyhow::Result<()> {
);
// frb-coverage:ignore-end

flutter_create(&config.name, &config.org)?;
flutter_create(&config.name, &config.org, config.template)?;

env::set_current_dir(&dart_root)?;

remove_unnecessary_files(&dart_root)?;

match &config.template {
Template::App => remove_unnecessary_app_files(&dart_root)?,
Template::Plugin => remove_unnecessary_plugin_files(&dart_root)?,
}

info!("Step: Inject flutter_rust_bridge related code");
integrator::integrate(IntegrateConfig {
enable_integration_test: true,
enable_local_dependency: config.enable_local_dependency,
rust_crate_name: config.rust_crate_name,
rust_crate_dir: config.rust_crate_dir,
template: config.template,
})
}

// the function signature is not covered while the whole body is covered - looks like a bug in coverage tool
// frb-coverage:ignore-start
fn remove_unnecessary_files(dart_root: &Path) -> anyhow::Result<()> {
fn remove_unnecessary_app_files(dart_root: &Path) -> anyhow::Result<()> {
// frb-coverage:ignore-end
let lib_dir = dart_root.join("lib");
let test_dir = dart_root.join("test");

remove_files_in_dir(&lib_dir)?;
remove_files_in_dir(&test_dir)?;

Ok(())
}

// the function signature is not covered while the whole body is covered - looks like a bug in coverage tool
// frb-coverage:ignore-start
fn remove_unnecessary_plugin_files(dart_root: &Path) -> anyhow::Result<()> {
// frb-coverage:ignore-end
fs::remove_file(dart_root.join("test").join("widget_test.dart"))?;
fs::remove_file(dart_root.join("lib").join("main.dart"))?;
let lib_dir = dart_root.join("lib");
let test_dir = dart_root.join("test");
let example_lib_dir = dart_root.join("example").join("lib");
let example_integration_test_dir = dart_root.join("example").join("integration_test");

remove_files_in_dir(&lib_dir)?;
remove_files_in_dir(&test_dir)?;
remove_files_in_dir(&example_lib_dir)?;
remove_files_in_dir(&example_integration_test_dir)?;

Ok(())
}


fn remove_files_in_dir(dir: &Path) -> anyhow::Result<()> {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_file() {
fs::remove_file(&path)?;
} else if path.is_dir() {
bail!("An unexpected directory was encountered: {:?}", path);
}
}
Ok(())
}
Loading
Loading