### Install Application Executable Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Installs the main application executable to the root of the installation bundle. ```CMake install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime) ``` -------------------------------- ### Configure Installation Directories Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/CMakeLists.txt This section configures the installation paths for the application. It sets the build bundle directory to the target file's location, makes the 'install' step default for Visual Studio, and defines data and library installation directories relative to the install prefix. ```CMake set(BUILD_BUNDLE_DIR "$") set(CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD 1) if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE) endif() set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data") set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}") ``` -------------------------------- ### Install Application Executable and Core Files Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/CMakeLists.txt This snippet defines installation rules for the application's runtime components. It installs the main executable, Flutter ICU data file, and the Flutter library to their respective bundle directories. It also conditionally installs bundled plugin libraries if they exist. ```CMake install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime) install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime) install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) if(PLUGIN_BUNDLED_LIBRARIES) install(FILES "${PLUGIN_BUNDLED_LIBRARIES}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) endif() ``` -------------------------------- ### Install Bundled Plugin Libraries Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Iterates through a list of bundled plugin libraries and installs each one to the library subdirectory of the installation bundle. ```CMake foreach(bundled_library ${PLUGIN_BUNDLED_LIBRARIES}) install(FILES "${bundled_library}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) endforeach(bundled_library) ``` -------------------------------- ### Install Flutter Engine Library Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Installs the core Flutter engine library to the library subdirectory of the installation bundle. ```CMake install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) ``` -------------------------------- ### Define Installation Bundle Subdirectories Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Sets variables for the standard data and library subdirectories within the installation bundle, based on the defined CMake install prefix. ```CMake set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data") set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib") ``` -------------------------------- ### Initialize CMake Project and Set Binary Name Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/CMakeLists.txt This snippet sets the minimum required CMake version, defines the project name 'example' with CXX language support, and specifies the executable's on-disk name using the BINARY_NAME variable. ```CMake cmake_minimum_required(VERSION 3.14) project(example LANGUAGES CXX) set(BINARY_NAME "example") ``` -------------------------------- ### Install Flutter Assets and AOT Library Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/CMakeLists.txt This snippet handles the installation of Flutter assets and the AOT (Ahead-of-Time) compiled library. It ensures that the `flutter_assets` directory is fully re-copied on each build by first removing existing assets, then installing the new ones. The AOT library is installed only for 'Profile' and 'Release' configurations. ```CMake set(FLUTTER_ASSET_DIR_NAME "flutter_assets") install(CODE "\n file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\")\n " COMPONENT Runtime) install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime) install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" CONFIGURATIONS Profile;Release COMPONENT Runtime) ``` -------------------------------- ### Configure Installation Bundle Directory Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Defines the directory where the relocatable application bundle will be built. If the CMake install prefix is at its default value, it is forced to this bundle directory. ```CMake set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle") if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE) endif() ``` -------------------------------- ### Clean Build Bundle Directory on Install Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Adds an installation step that recursively removes all contents of the build bundle directory before installation, ensuring a clean state and preventing stale files from previous builds. ```CMake install(CODE " file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\") " COMPONENT Runtime) ``` -------------------------------- ### Install AOT Library for Non-Debug Builds Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Installs the Ahead-Of-Time (AOT) compiled library to the bundle's library directory, but only for build configurations that are not 'Debug'. ```CMake if(NOT CMAKE_BUILD_TYPE MATCHES "Debug") install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) endif() ``` -------------------------------- ### Frx Generator Installation Source: https://github.com/cogivn/frx/blob/main/frx_generator/README.md Instructions for adding `frx_annotation`, `frx_generator`, `build_runner`, and `freezed` to your Dart project's `pubspec.yaml` for dependency management. ```yaml dependencies: frx_annotation: ^1.0.1 dev_dependencies: frx_generator: ^1.0.5 build_runner: ^2.4.0 freezed: ^3.0.0 # Updated to use Freezed 3.x ``` -------------------------------- ### Install Flutter ICU Data File Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Installs the International Components for Unicode (ICU) data file required by Flutter to the data subdirectory of the installation bundle. ```CMake install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime) ``` -------------------------------- ### Install Native Assets Directory Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/CMakeLists.txt This snippet sets the source directory for native assets generated by `build.dart` and configures CMake to install this directory's contents into the application's library bundle, ensuring native dependencies are present at runtime. ```CMake set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/windows/") install(DIRECTORY "${NATIVE_ASSETS_DIR}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) ``` -------------------------------- ### Apply @frx or @FrxAnnotation for Code Generation Source: https://github.com/cogivn/frx/blob/main/frx_annotation/README.md Examples of applying the `@frx` or `@FrxAnnotation()` to a `sealed class` to enable pattern matching generation. It also shows how to use custom options like `generateAllFields`. ```dart @frx sealed class Result { // ... } // Equivalent to: @FrxAnnotation() sealed class Result { // ... } // With custom options: @FrxAnnotation(generateAllFields: false) sealed class Result { // ... } ``` -------------------------------- ### Copy Native Assets to Bundle Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Copies the directory containing native assets, generated by the `build.dart` script from various packages, to the library subdirectory of the installation bundle. ```CMake set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/linux/") install(DIRECTORY "${NATIVE_ASSETS_DIR}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) ``` -------------------------------- ### Define Union Type with @frx Annotation Source: https://github.com/cogivn/frx/blob/main/frx_annotation/README.md Example of defining a `sealed class` as a union type using the `@frx` annotation, including several `final class` implementations that extend it. This setup enables code generation for pattern matching. ```dart import 'package:frx_annotation/frx_annotation.dart'; part 'example.g.dart'; @frx sealed class Union { const Union(); } final class _First extends Union { final String value; const _First(this.value); } final class _Second extends Union { const _Second(); } final class _Third extends Union { const _Third(); } ``` -------------------------------- ### Install FRX Dependencies in pubspec.yaml Source: https://github.com/cogivn/frx/blob/main/README.md This snippet shows how to add `frx_annotation` and `frx_generator` to your `pubspec.yaml` file, along with `build_runner` and `freezed` as development dependencies. These are essential for enabling FRX's code generation capabilities in your Dart or Flutter project. ```yaml dependencies: frx_annotation: ^1.0.2 dev_dependencies: frx_generator: ^1.0.5 build_runner: ^2.4.0 freezed: ^3.0.0 # Updated minimum version requirement ``` -------------------------------- ### Define Flutter Library and Headers Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/flutter/CMakeLists.txt This section defines the Flutter engine library path and its associated ICU data file, making them available to parent scopes for installation. It also lists and transforms Flutter library headers, then defines an INTERFACE library for Flutter, linking against the engine and adding necessary include directories and build dependencies. ```CMake # === Flutter Library === set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/flutter_windows.dll") # Published to parent scope for install step. set(FLUTTER_LIBRARY ${FLUTTER_LIBRARY} PARENT_SCOPE) set(FLUTTER_ICU_DATA_FILE "${EPHEHEMERAL_DIR}/icudtl.dat" PARENT_SCOPE) set(PROJECT_BUILD_DIR "${PROJECT_DIR}/build/" PARENT_SCOPE) set(AOT_LIBRARY "${PROJECT_DIR}/build/windows/app.so" PARENT_SCOPE) list(APPEND FLUTTER_LIBRARY_HEADERS "flutter_export.h" "flutter_windows.h" "flutter_messenger.h" "flutter_plugin_registrar.h" "flutter_texture_registrar.h" ) list(TRANSFORM FLUTTER_LIBRARY_HEADERS PREPEND "${EPHEMERAL_DIR}/") add_library(flutter INTERFACE) target_include_directories(flutter INTERFACE "${EPHEMERAL_DIR}" ) target_link_libraries(flutter INTERFACE "${FLUTTER_LIBRARY}.lib") add_dependencies(flutter flutter_assemble) ``` -------------------------------- ### Set Runtime Path for Bundled Libraries Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Configures the runtime path (RPATH) for the installed application to ensure that bundled libraries located in a 'lib/' directory relative to the executable are found correctly. ```CMake set(CMAKE_INSTALL_RPATH "$ORIGIN/lib") ``` -------------------------------- ### Freezed Private Constructors and Mapping Methods Source: https://github.com/cogivn/frx/blob/main/frx_generator/README.md This snippet demonstrates how Freezed's behavior changes for private factory constructors (prefixed with an underscore). Unlike public constructors, private ones do not generate 'map', 'maybeMap', or 'mapOrNull' methods, limiting them to 'when' and 'maybeWhen' for pattern matching. The example shows both public and private constructor definitions and their respective usage limitations. ```Dart @freezed @frx class UserState with _$UserState { // Public constructors - all methods will be generated (when, map, etc.) const factory UserState.initial() = Initial; const factory UserState.loading() = Loading; // Private constructors - only pattern matching methods will be generated (when, maybeWhen, whenOrNull) // No mapping methods (map, maybeMap, mapOrNull) will be generated const factory UserState._authenticated(User user) = _Authenticated; const factory UserState._error(String message) = _Error; } // Usage example void example(UserState state) { // This works for all constructors (public and private) final message = state.when( initial: () => 'Initial state', loading: () => 'Loading...', _authenticated: (user) => 'Welcome, ${user.name}', _error: (message) => 'Error: $message', ); // This only works for public constructors // Private constructors (_authenticated and _error) are not included final widget = state.map( initial: (state) => Text('Initial'), loading: (state) => CircularProgressIndicator(), // No _authenticated or _error cases here ); } ``` -------------------------------- ### Ignoring Freezed Factory Constructors with @frxIgnore Source: https://github.com/cogivn/frx/blob/main/frx_generator/README.md This example illustrates the use of the `@frxIgnore` annotation to exclude specific factory constructors from the pattern matching generation process. This is particularly useful for utility constructors, such as deserialization methods (e.g., `fromJson`), that should not be part of the primary pattern matching logic. ```Dart import 'package:frx_annotation/frx_annotation.dart'; part 'api_client.g.dart'; @frx sealed class ApiResult { const ApiResult(); factory ApiResult.success(T data) = Success; factory ApiResult.error(String message) = Error; // This constructor will be ignored in pattern matching @frxIgnore factory ApiResult.fromJson(Map json) { // ... custom deserialization logic return ApiResult.success(data); } } ``` -------------------------------- ### Define CMake Project and Application Executable for Flutter Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/runner/CMakeLists.txt This section initializes the CMake project, sets the minimum required version, and defines the main application executable. It specifies the source files required for the Flutter desktop runner, including C++ files, resource files, and the generated plugin registrant. ```CMake cmake_minimum_required(VERSION 3.14) project(runner LANGUAGES CXX) # Define the application target. To change its name, change BINARY_NAME in the # top-level CMakeLists.txt, not the value here, or `flutter run` will no longer # work. # # Any new source files that you add to the application should be added here. add_executable(${BINARY_NAME} WIN32 "flutter_window.cpp" "main.cpp" "utils.cpp" "win32_window.cpp" "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc" "Runner.rc" "runner.exe.manifest" ) ``` -------------------------------- ### Link Libraries and Include Directories for Flutter Application Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/runner/CMakeLists.txt This section specifies the libraries that the application executable needs to link against, including Flutter's core libraries and `dwmapi.lib` for desktop window management. It also adds the source directory to the include paths. ```CMake target_link_libraries(${BINARY_NAME} PRIVATE flutter flutter_wrapper_app) target_link_libraries(${BINARY_NAME} PRIVATE "dwmapi.lib") target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}") ``` -------------------------------- ### Configure Flutter Desktop Application Runner with CMake Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/runner/CMakeLists.txt This CMake script sets up the build environment for a Flutter desktop application. It defines the minimum CMake version, names the project, specifies the executable target with its source files, applies standard build settings, adds preprocessor definitions for the application ID, and links essential libraries such as Flutter and GTK for GUI support. ```CMake cmake_minimum_required(VERSION 3.13) project(runner LANGUAGES CXX) # Define the application target. To change its name, change BINARY_NAME in the # top-level CMakeLists.txt, not the value here, or `flutter run` will no longer # work. # # Any new source files that you add to the application should be added here. add_executable(${BINARY_NAME} "main.cc" "my_application.cc" "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc" ) # Apply the standard set of build settings. This can be removed for applications # that need different build settings. apply_standard_settings(${BINARY_NAME}) # Add preprocessor definitions for the application ID. add_definitions(-DAPPLICATION_ID="${APPLICATION_ID}") # Add dependency libraries. Add any application-specific dependencies here. target_link_libraries(${BINARY_NAME} PRIVATE flutter) target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::GTK) target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}") ``` -------------------------------- ### Initialize CMake and Define Core Directories Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/flutter/CMakeLists.txt This snippet sets the minimum required CMake version, defines the ephemeral build directory for generated files, and specifies the root for the C++ client wrapper. It also includes a generated configuration file and sets a fallback for the Flutter target platform. ```CMake cmake_minimum_required(VERSION 3.14) set(EPHEMERAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ephemeral") # Configuration provided via flutter tool. include(${EPHEMERAL_DIR}/generated_config.cmake) # TODO: Move the rest of this into files in ephemeral. See # https://github.com/flutter/flutter/issues/57146. set(WRAPPER_ROOT "${EPHEMERAL_DIR}/cpp_client_wrapper") # Set fallback configurations for older versions of the flutter tool. if (NOT DEFINED FLUTTER_TARGET_PLATFORM) set(FLUTTER_TARGET_PLATFORM "windows-x64") endif() ``` -------------------------------- ### Configure Flutter Wrapper Application Library Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/flutter/CMakeLists.txt This snippet defines a static library, `flutter_wrapper_app`, which includes core and application-specific C++ wrapper sources. It applies standard settings, links against the `flutter` interface library, and adds necessary include directories and build dependencies for the main application runner. ```CMake # Wrapper sources needed for the runner. add_library(flutter_wrapper_app STATIC ${CPP_WRAPPER_SOURCES_CORE} ${CPP_WRAPPER_SOURCES_APP} ) apply_standard_settings(flutter_wrapper_app) target_link_libraries(flutter_wrapper_app PUBLIC flutter) target_include_directories(flutter_wrapper_app PUBLIC "${WRAPPER_ROOT}/include" ) add_dependencies(flutter_wrapper_app flutter_assemble) ``` -------------------------------- ### Find System-Level Dependencies Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Locates and checks for required system-level dependencies using PkgConfig, specifically ensuring that GTK 3.0 is available and imported as a target. ```CMake find_package(PkgConfig REQUIRED) pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) ``` -------------------------------- ### Apply Standard Build Settings to Flutter Executable Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/runner/CMakeLists.txt This command applies a predefined set of standard build settings to the application executable. It's a common practice in Flutter desktop projects to ensure consistent build configurations. ```CMake apply_standard_settings(${BINARY_NAME}) ``` -------------------------------- ### Add Application Subdirectory and Dependencies Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Includes the 'runner' subdirectory, which contains application-specific build rules, and establishes a dependency for the main binary on the `flutter_assemble` target to ensure Flutter assets are built. ```CMake add_subdirectory("runner") add_dependencies(${BINARY_NAME} flutter_assemble) ``` -------------------------------- ### Ensure Flutter Tool Build Steps are Executed Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/runner/CMakeLists.txt This command ensures that the Flutter tool's build steps, such as asset bundling and code generation, are executed as a dependency before compiling the application executable. This is crucial for a successful Flutter desktop build. ```CMake add_dependencies(${BINARY_NAME} flutter_assemble) ``` -------------------------------- ### Include Flutter Build Rules Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Sets the path to the Flutter managed directory and includes its subdirectory, which contains the necessary CMake rules for building Flutter libraries and tools. ```CMake set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter") add_subdirectory(${FLUTTER_MANAGED_DIR}) ``` -------------------------------- ### Configure Flutter Wrapper Plugin Library Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/flutter/CMakeLists.txt This section defines a static library, `flutter_wrapper_plugin`, which includes core and plugin-specific C++ wrapper sources. It applies standard settings, sets target properties for position-independent code and hidden CXX visibility, links against the `flutter` interface library, and adds necessary include directories and build dependencies. ```CMake # Wrapper sources needed for a plugin. add_library(flutter_wrapper_plugin STATIC ${CPP_WRAPPER_SOURCES_CORE} ${CPP_WRAPPER_SOURCES_PLUGIN} ) apply_standard_settings(flutter_wrapper_plugin) set_target_properties(flutter_wrapper_plugin PROPERTIES POSITION_INDEPENDENT_CODE ON) set_target_properties(flutter_wrapper_plugin PROPERTIES CXX_VISIBILITY_PRESET hidden) target_link_libraries(flutter_wrapper_plugin PUBLIC flutter) target_include_directories(flutter_wrapper_plugin PUBLIC "${WRAPPER_ROOT}/include" ) add_dependencies(flutter_wrapper_plugin flutter_assemble) ``` -------------------------------- ### Pattern Matching with Freezed Classes using Frx Source: https://github.com/cogivn/frx/blob/main/frx_generator/README.md Illustrates integrating Frx with Freezed classes (`UserState`) to generate pattern matching methods. Shows how to define different states and use the `when` method to render UI components based on the state. ```dart import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:frx_annotation/frx_annotation.dart'; part 'user_state.freezed.dart'; part 'user_state.g.dart'; @freezed @frx class UserState with _$UserState { const factory UserState.initial() = Initial; const factory UserState.loading() = Loading; const factory UserState.loaded(List users) = Loaded; const factory UserState.error(String message) = Error; } // Using the generated extension methods void example(UserState state) { final widget = state.when( initial: () => Text('No data yet'), loading: () => CircularProgressIndicator(), loaded: (users) => ListView.builder( itemCount: users.length, itemBuilder: (context, index) => UserTile(users[index]), ), error: (message) => Text('Error: $message'), ); } ``` -------------------------------- ### Configure Basic CMake Project Settings Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Sets the minimum required CMake version, defines the project name and language, specifies the executable's on-disk name, and sets the unique GTK application identifier. It also explicitly opts into modern CMake behaviors to avoid warnings. ```CMake cmake_minimum_required(VERSION 3.13) project(runner LANGUAGES CXX) set(BINARY_NAME "example") set(APPLICATION_ID "com.example.example") cmake_policy(SET CMP0063 NEW) ``` -------------------------------- ### Include Flutter and Application Subdirectories Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/CMakeLists.txt This snippet sets the path to the Flutter managed directory, then includes it and the 'runner' subdirectory, which contains the application's build rules. It also includes `generated_plugins.cmake` for managing Flutter plugins. ```CMake set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter") add_subdirectory(${FLUTTER_MANAGED_DIR}) add_subdirectory("runner") include(flutter/generated_plugins.cmake) ``` -------------------------------- ### Discover System Dependencies for Flutter Library Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/flutter/CMakeLists.txt Identifies and imports system-level dependencies required for the Flutter Linux GTK library, including PkgConfig, GTK, GLIB, and GIO, ensuring they are available for linking. ```CMake find_package(PkgConfig REQUIRED) pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) pkg_check_modules(GLIB REQUIRED IMPORTED_TARGET glib-2.0) pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0) ``` -------------------------------- ### Define C++ Wrapper Source Files Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/flutter/CMakeLists.txt This snippet defines lists of C++ source files for different components of the Flutter C++ client wrapper: core implementations, plugin-specific sources, and application-specific sources. It prepends the wrapper root path to each source file. ```CMake # === Wrapper === list(APPEND CPP_WRAPPER_SOURCES_CORE "core_implementations.cc" "standard_codec.cc" ) list(TRANSFORM CPP_WRAPPER_SOURCES_CORE PREPEND "${WRAPPER_ROOT}/") list(APPEND CPP_WRAPPER_SOURCES_PLUGIN "plugin_registrar.cc" ) list(TRANSFORM CPP_WRAPPER_SOURCES_PLUGIN PREPEND "${WRAPPER_ROOT}/") list(APPEND CPP_WRAPPER_SOURCES_APP "flutter_engine.cc" "flutter_view_controller.cc" ) list(TRANSFORM CPP_WRAPPER_SOURCES_APP PREPEND "${WRAPPER_ROOT}/") ``` -------------------------------- ### Configure Sysroot for Cross-Building Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Establishes the CMake sysroot and defines find root path modes when cross-building for a specific Flutter target platform, ensuring that programs, packages, libraries, and includes are searched correctly within the sysroot. ```CMake if(FLUTTER_TARGET_PLATFORM_SYSROOT) set(CMAKE_SYSROOT ${FLUTTER_TARGET_PLATFORM_SYSROOT}) set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT}) set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) endif() ``` -------------------------------- ### Define Custom Command for Flutter Tool Backend Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/flutter/CMakeLists.txt This section sets up a custom command and target (`flutter_assemble`) to invoke the Flutter tool backend. It uses a phony output file to force the command to run every time, ensuring that Flutter's build artifacts (like the engine library, headers, and wrapper sources) are always up-to-date by calling the Flutter tool's `tool_backend.bat` script. ```CMake # === Flutter tool backend === # _phony_ is a non-existent file to force this command to run every time, # since currently there's no way to get a full input/output list from the # flutter tool. set(PHONY_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/_phony_") set_source_files_properties("${PHONY_OUTPUT}" PROPERTIES SYMBOLIC TRUE) add_custom_command( OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS} ${CPP_WRAPPER_SOURCES_CORE} ${CPP_WRAPPER_SOURCES_PLUGIN} ${CPP_WRAPPER_SOURCES_APP} ${PHONY_OUTPUT} COMMAND ${CMAKE_COMMAND} -E env ${FLUTTER_TOOL_ENVIRONMENT} "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.bat" ${FLUTTER_TARGET_PLATFORM} $ VERBATIM ) add_custom_target(flutter_assemble DEPENDS "${FLUTTER_LIBRARY}" ${FLUTTER_LIBRARY_HEADERS} ${CPP_WRAPPER_SOURCES_CORE} ${CPP_WRAPPER_SOURCES_PLUGIN} ${CPP_WRAPPER_SOURCES_APP} ) ``` -------------------------------- ### Execute Flutter Tool Backend Custom Command Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/flutter/CMakeLists.txt Defines a custom build command that executes the Flutter tool backend script, responsible for generating the Flutter library and its associated header files. A non-existent `_phony_` file is used as an output to force this command to run on every build, compensating for the Flutter tool's lack of a full input/output list. ```CMake add_custom_command( OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS} ${CMAKE_CURRENT_BINARY_DIR}/_phony_ COMMAND ${CMAKE_COMMAND} -E env ${FLUTTER_TOOL_ENVIRONMENT} "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh" ${FLUTTER_TARGET_PLATFORM} ${CMAKE_BUILD_TYPE} VERBATIM ) ``` -------------------------------- ### Include Generated Flutter Plugin Rules Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Includes the CMake file generated by Flutter that manages the building and integration of all detected plugins into the application. ```CMake include(flutter/generated_plugins.cmake) ``` -------------------------------- ### Define Profile Build Mode Flags Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/CMakeLists.txt This snippet configures the linker and compiler flags for the 'Profile' build mode to mirror those of the 'Release' build mode, ensuring consistent optimization and linking behavior for profiling builds. ```CMake set(CMAKE_EXE_LINKER_FLAGS_PROFILE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}") set(CMAKE_SHARED_LINKER_FLAGS_PROFILE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE}") set(CMAKE_C_FLAGS_PROFILE "${CMAKE_C_FLAGS_RELEASE}") set(CMAKE_CXX_FLAGS_PROFILE "${CMAKE_CXX_FLAGS_RELEASE}") ``` -------------------------------- ### Apply FRX Pattern Matching to Freezed Unions Source: https://github.com/cogivn/frx/blob/main/README.md Illustrates integrating FRX with Freezed unions. It defines an `ApiResult` Freezed class with `success` and `error` factories, showcasing how to use `when` for exhaustive handling, `maybeWhen` for optional handling with a fallback, and `whenOrNull` for cases where no match returns null. The `@frxIgnore` annotation is also demonstrated to exclude specific constructors from pattern matching. ```dart import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:frx_annotation/frx_annotation.dart'; part 'api_result.freezed.dart'; part 'api_result.g.dart'; @freezed @frx class ApiResult with _$ApiResult { const factory ApiResult.success(T data) = _Success; const factory ApiResult.error(String message) = _Error; @frxIgnore // This constructor will be excluded from pattern matching factory ApiResult.fromJson(Map json) => _$ApiResultFromJson(json); } // Usage void handle(ApiResult result) { // Handle all cases final widget = result.when( success: (data) => UserProfile(user: data), error: (message) => ErrorDisplay(message: message), ); // Handle specific cases with a fallback final message = result.maybeWhen( success: (data) => 'Welcome, ${data.name}', orElse: () => 'Something went wrong', ); // Handle only cases you care about (returns null if none match) final errorMsg = result.whenOrNull( error: (message) => message, ); } ``` -------------------------------- ### Execute frx_annotation Code Generator Source: https://github.com/cogivn/frx/blob/main/frx_annotation/README.md Command to run the `build_runner` tool, which processes the `@frx` annotations and generates the necessary pattern matching extension methods for the defined union types. ```bash dart run build_runner build ``` -------------------------------- ### Implement Pattern Matching with Dart Sealed Classes using FRX Source: https://github.com/cogivn/frx/blob/main/README.md Demonstrates how to use FRX with Dart's `sealed class` feature. It defines a `Result` sealed class with `Success` and `Error` subclasses, then illustrates how to generate and utilize the `when` method for exhaustive pattern matching on instances of `Result`, allowing for type-safe handling of different states. ```dart import 'package:frx_annotation/frx_annotation.dart'; part 'result.g.dart'; @frx sealed class Result { const Result(); } final class Success extends Result { final String value; const Success(this.value); } final class Error extends Result { final String message; const Error(this.message); } // Using pattern matching void example() { final result = Success("Data loaded"); final message = result.when( success: (value) => 'Success: $value', error: (message) => 'Error: $message', ); } ``` -------------------------------- ### Add Preprocessor Definitions for Flutter Version Information Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/runner/CMakeLists.txt This section adds preprocessor definitions to the application target, embedding Flutter version details (major, minor, patch, build) directly into the compiled binary. This is useful for runtime version checks or displaying application version. ```CMake target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION=\"${FLUTTER_VERSION}\"") target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION_MAJOR=${FLUTTER_VERSION_MAJOR}") target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION_MINOR=${FLUTTER_VERSION_MINOR}") target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION_PATCH=${FLUTTER_VERSION_PATCH}") target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION_BUILD=${FLUTTER_VERSION_BUILD}") ``` -------------------------------- ### FRX Generated Pattern Matching and Mapping Methods Source: https://github.com/cogivn/frx/blob/main/README.md This section details the six extension methods automatically generated by FRX for annotated classes. It distinguishes between exhaustive pattern matching methods (`when`, `maybeWhen`, `whenOrNull`) and mapping methods (`map`, `maybeMap`, `mapOrNull`), explaining their purpose and behavior for handling different cases of sealed classes or Freezed unions. ```APIDOC Pattern Matching Methods (for all constructors): - when: Exhaustive pattern matching (requires handlers for all cases) - maybeWhen: Pattern matching with an orElse fallback for unspecified cases - whenOrNull: Returns null for unmatched cases (all handlers are optional) Mapping Methods (only for public constructors): - map: Maps each case to a new value through type-safe instance access - maybeMap: Maps with an orElse fallback - mapOrNull: Returns null for unmatched cases ``` -------------------------------- ### Apply Standard Compilation Settings to Target Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Defines a CMake function `APPLY_STANDARD_SETTINGS` that applies common compilation features and options to a specified target. This includes enabling C++14, setting Wall/Werror, and applying optimization flags (-O3) and NDEBUG for non-Debug builds. ```CMake function(APPLY_STANDARD_SETTINGS TARGET) target_compile_features(${TARGET} PUBLIC cxx_std_14) target_compile_options(${TARGET} PRIVATE -Wall -Werror) target_compile_options(${TARGET} PRIVATE "$<$>:-O3>") target_compile_definitions(${TARGET} PRIVATE "$<$>:NDEBUG>") endfunction() ``` -------------------------------- ### Distinguish Pattern Matching and Mapping Method Generation Source: https://github.com/cogivn/frx/blob/main/frx_annotation/README.md Demonstrates how `frx_generator` generates both pattern matching (`when`, `maybeWhen`, `whenOrNull`) and mapping (`map`, `maybeMap`, `mapOrNull`) methods for public constructors, but only pattern matching methods for private constructors. ```dart @frx sealed class Result { // Public constructor - gets both pattern matching and mapping methods const factory Result.success(String value) = Success; // Private constructor - only gets pattern matching methods (when, maybeWhen, whenOrNull) const factory Result._failure(String message) = _Failure; } ``` -------------------------------- ### Control Parameter Extraction in FRX Pattern Matching Source: https://github.com/cogivn/frx/blob/main/README.md Shows how to use `@frxParam` and `generateAllFields: false` with `@FrxAnnotation` to selectively include parameters in the generated `when` methods. This allows for cleaner pattern matching by only exposing relevant fields from a Freezed union or sealed class, improving readability and reducing boilerplate. ```dart @FrxAnnotation(generateAllFields: false) @freezed class NetworkResponse with _$NetworkResponse { const factory NetworkResponse.success( @frxParam String data, int statusCode, Map headers, ) = _Success; const factory NetworkResponse.error( @frxParam String message, @frxParam int code, ) = _Error; } // Usage with selective parameters void example(NetworkResponse response) { // Only selected parameters are included final result = response.when( success: (data) => 'Got: $data', // statusCode and headers excluded error: (message, code) => 'Error $code: $message', ); } ``` -------------------------------- ### Configure Flutter Library Paths and Headers Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/flutter/CMakeLists.txt Configures essential paths for the Flutter library, ICU data, project build directory, and AOT library, making them available in the parent scope. It also compiles a comprehensive list of Flutter C++ header files and prepends their full ephemeral directory path. ```CMake set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/libflutter_linux_gtk.so") # Published to parent scope for install step. set(FLUTTER_LIBRARY ${FLUTTER_LIBRARY} PARENT_SCOPE) set(FLUTTER_ICU_DATA_FILE "${EPHEMERAL_DIR}/icudtl.dat" PARENT_SCOPE) set(PROJECT_BUILD_DIR "${PROJECT_DIR}/build/" PARENT_SCOPE) set(AOT_LIBRARY "${PROJECT_DIR}/build/lib/libapp.so" PARENT_SCOPE) list(APPEND FLUTTER_LIBRARY_HEADERS "fl_basic_message_channel.h" "fl_binary_codec.h" "fl_binary_messenger.h" "fl_dart_project.h" "fl_engine.h" "fl_json_message_codec.h" "fl_json_method_codec.h" "fl_message_codec.h" "fl_method_call.h" "fl_method_channel.h" "fl_method_codec.h" "fl_method_response.h" "fl_plugin_registrar.h" "fl_plugin_registry.h" "fl_standard_message_codec.h" "fl_standard_method_codec.h" "fl_string_codec.h" "fl_value.h" "fl_view.h" "flutter_linux.h" ) list_prepend(FLUTTER_LIBRARY_HEADERS "${EPHEMERAL_DIR}/flutter_linux/") ``` -------------------------------- ### Enable Unicode Support Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/CMakeLists.txt This line adds preprocessor definitions to enable Unicode support for all projects compiled within this CMake configuration, ensuring proper handling of wide characters. ```CMake add_definitions(-DUNICODE -D_UNICODE) ``` -------------------------------- ### Set Executable Output Directory Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Configures the runtime output directory for the main executable to a subdirectory named 'intermediates_do_not_run'. This prevents users from attempting to run the unbundled executable directly, as it requires resources in specific relative locations. ```CMake set_target_properties(${BINARY_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/intermediates_do_not_run" ) ``` -------------------------------- ### Add frx_annotation Dependencies Source: https://github.com/cogivn/frx/blob/main/frx_annotation/README.md Instructions on how to add the necessary `frx_annotation`, `build_runner`, and `frx_generator` packages to a Dart project's `pubspec.yaml` file for development and runtime. ```yaml dependencies: frx_annotation: ^1.0.0 dev_dependencies: build_runner: ^2.4.0 frx_generator: ^1.0.0 ``` -------------------------------- ### Define Standard Compilation Settings Function Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/CMakeLists.txt This CMake function, `APPLY_STANDARD_SETTINGS`, applies common compilation settings to a specified target. It enforces C++17 standard, sets warning levels, disables exceptions, and defines `_DEBUG` for Debug configurations. It's intended for most targets, but caution is advised when adding new options here. ```CMake function(APPLY_STANDARD_SETTINGS TARGET) target_compile_features(${TARGET} PUBLIC cxx_std_17) target_compile_options(${TARGET} PRIVATE /W4 /WX /wd"4100") target_compile_options(${TARGET} PRIVATE /EHsc) target_compile_definitions(${TARGET} PRIVATE "_HAS_EXCEPTIONS=0") target_compile_definitions(${TARGET} PRIVATE "$<$:_DEBUG>") endfunction() ``` -------------------------------- ### Utilize Generated Pattern Matching Methods Source: https://github.com/cogivn/frx/blob/main/frx_annotation/README.md Demonstrates how to use the `when` and `maybeWhen` extension methods generated by `frx_annotation` for pattern matching on a union type instance. It shows handling different cases and providing fallback options. ```dart void example() { final union = _First('hello'); // Using when pattern final result = union.when( first: (value) => 'First: $value', second: () => 'Second', third: () => 'Third', ); // Using maybeWhen pattern final maybeResult = union.maybeWhen( first: (value) => 'First: $value', orElse: () => 'Other case', ); } ``` -------------------------------- ### Re-copy Flutter Assets to Bundle Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Ensures that the Flutter assets directory is completely refreshed on each build. It first removes any existing assets in the bundle's data directory and then copies the latest assets from the build directory. ```CMake set(FLUTTER_ASSET_DIR_NAME "flutter_assets") install(CODE " file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\") " COMPONENT Runtime) install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime) ``` -------------------------------- ### MIT License for frx_annotation Source: https://github.com/cogivn/frx/blob/main/frx_annotation/README.md The full text of the MIT License, granting broad permissions for use, modification, and distribution of the software, while disclaiming warranty and liability. ```text MIT License Copyright (c) 2023 Your Name Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ``` -------------------------------- ### Define Default CMake Build Type Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/CMakeLists.txt Sets the default CMake build type to 'Debug' if it hasn't been specified, and defines the available build type options as 'Debug', 'Profile', and 'Release'. ```CMake if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Flutter build mode" FORCE) set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Profile" "Release") endif() ``` -------------------------------- ### Set CMake Minimum Version and Ephemeral Directory Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/flutter/CMakeLists.txt Sets the minimum required CMake version and defines the `EPHEMERAL_DIR` variable for temporary build artifacts. It also includes a generated configuration file provided by the Flutter tool. ```CMake cmake_minimum_required(VERSION 3.10) set(EPHEMERAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ephemeral") # Configuration provided via flutter tool. include(${EPHEMERAL_DIR}/generated_config.cmake) ``` -------------------------------- ### Configure CMake Policies and Build Types Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/windows/CMakeLists.txt This section explicitly opts into modern CMake behaviors to avoid warnings. It then defines build configuration types (Debug, Profile, Release) based on whether the generator supports multi-configuration, ensuring a default 'Debug' build type if not already set. ```CMake cmake_policy(VERSION 3.14...3.25) get_property(IS_MULTICONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) if(IS_MULTICONFIG) set(CMAKE_CONFIGURATION_TYPES "Debug;Profile;Release" CACHE STRING "" FORCE) else() if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Flutter build mode" FORCE) set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Profile" "Release") endif() endif() ``` -------------------------------- ### Define Flutter CMake Interface Library Source: https://github.com/cogivn/frx/blob/main/frx_annotation/example/linux/flutter/CMakeLists.txt Declares an `INTERFACE` library named `flutter`, specifying its include directories and linking it against the main Flutter library and its system dependencies (GTK, GLIB, GIO). This library serves as a dependency for other targets requiring Flutter functionalities. ```CMake add_library(flutter INTERFACE) target_include_directories(flutter INTERFACE "${EPHEMERAL_DIR}" ) target_link_libraries(flutter INTERFACE "${FLUTTER_LIBRARY}") target_link_libraries(flutter INTERFACE PkgConfig::GTK PkgConfig::GLIB PkgConfig::GIO ) add_dependencies(flutter flutter_assemble) ``` -------------------------------- ### Running build_runner for frx Code Generation Source: https://github.com/cogivn/frx/blob/main/frx_generator/README.md This command is essential for generating the necessary boilerplate code and extension methods required by the 'frx' library. It triggers the `build_runner` tool to process annotations and create the generated files. ```Dart dart run build_runner build ``` -------------------------------- ### Using the whenOrNull Pattern Matching Method Source: https://github.com/cogivn/frx/blob/main/frx_generator/README.md This code illustrates the `whenOrNull` pattern matching method, which provides flexibility by allowing handlers to return nullable values. If no handler matches the current state or if the matching handler explicitly returns `null`, the `whenOrNull` method will also return `null`, making it suitable for scenarios where a default or absent value is acceptable. ```Dart @frx sealed class Result { const factory Result.success(T data) = Success; const factory Result.error(String message) = Error; } void example(Result result) { final message = result.whenOrNull( success: (data) => data.isValid ? 'Valid user' : null, // Can return null error: (msg) => msg.isEmpty ? null : 'Error: $msg', // Can return null ); // message will be null if: // 1. No handler matched the case // 2. The matching handler returned null } ```