startup.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import 'dart:io';
  2. import 'package:appflowy/env/env.dart';
  3. import 'package:appflowy/workspace/application/settings/settings_location_cubit.dart';
  4. import 'package:appflowy_backend/appflowy_backend.dart';
  5. import 'package:flutter/foundation.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:get_it/get_it.dart';
  8. import 'deps_resolver.dart';
  9. import 'launch_configuration.dart';
  10. import 'plugin/plugin.dart';
  11. import 'tasks/prelude.dart';
  12. final getIt = GetIt.instance;
  13. abstract class EntryPoint {
  14. Widget create(LaunchConfiguration config);
  15. }
  16. class FlowyRunner {
  17. static Future<void> run(
  18. EntryPoint f, {
  19. LaunchConfiguration config = const LaunchConfiguration(
  20. autoRegistrationSupported: false,
  21. ),
  22. }) async {
  23. // Clear all the states in case of rebuilding.
  24. await getIt.reset();
  25. // Specify the env
  26. final env = integrationEnv();
  27. initGetIt(getIt, env, f, config);
  28. final directory = await getIt<LocalFileStorage>()
  29. .getPath()
  30. .then((value) => Directory(value));
  31. // final directory = await appFlowyDocumentDirectory();
  32. // add task
  33. final launcher = getIt<AppLauncher>();
  34. launcher.addTasks(
  35. [
  36. // handle platform errors.
  37. const PlatformErrorCatcherTask(),
  38. // localization
  39. const InitLocalizationTask(),
  40. // init the app window
  41. const InitAppWindowTask(),
  42. // Init Rust SDK
  43. InitRustSDKTask(directory: directory),
  44. // Load Plugins, like document, grid ...
  45. const PluginLoadTask(),
  46. // init the app widget
  47. // ignore in test mode
  48. if (!env.isTest()) ...[
  49. const HotKeyTask(),
  50. InitSupabaseTask(
  51. url: Env.supabaseUrl,
  52. anonKey: Env.supabaseAnonKey,
  53. key: Env.supabaseKey,
  54. jwtSecret: Env.supabaseJwtSecret,
  55. ),
  56. const InitAppWidgetTask(),
  57. const InitPlatformServiceTask()
  58. ],
  59. ],
  60. );
  61. await launcher.launch(); // execute the tasks
  62. }
  63. }
  64. Future<void> initGetIt(
  65. GetIt getIt,
  66. IntegrationMode env,
  67. EntryPoint f,
  68. LaunchConfiguration config,
  69. ) async {
  70. getIt.registerFactory<EntryPoint>(() => f);
  71. getIt.registerLazySingleton<FlowySDK>(() {
  72. return FlowySDK();
  73. });
  74. getIt.registerLazySingleton<AppLauncher>(
  75. () => AppLauncher(
  76. context: LaunchContext(
  77. getIt,
  78. env,
  79. config,
  80. ),
  81. ),
  82. );
  83. getIt.registerSingleton<PluginSandbox>(PluginSandbox());
  84. await DependencyResolver.resolve(getIt);
  85. }
  86. class LaunchContext {
  87. GetIt getIt;
  88. IntegrationMode env;
  89. LaunchConfiguration config;
  90. LaunchContext(this.getIt, this.env, this.config);
  91. }
  92. enum LaunchTaskType {
  93. dataProcessing,
  94. appLauncher,
  95. }
  96. /// The interface of an app launch task, which will trigger
  97. /// some nonresident indispensable task in app launching task.
  98. abstract class LaunchTask {
  99. const LaunchTask();
  100. LaunchTaskType get type => LaunchTaskType.dataProcessing;
  101. Future<void> initialize(LaunchContext context);
  102. }
  103. class AppLauncher {
  104. AppLauncher({
  105. required this.context,
  106. });
  107. final LaunchContext context;
  108. final List<LaunchTask> tasks = [];
  109. void addTask(LaunchTask task) {
  110. tasks.add(task);
  111. }
  112. void addTasks(Iterable<LaunchTask> tasks) {
  113. this.tasks.addAll(tasks);
  114. }
  115. Future<void> launch() async {
  116. for (final task in tasks) {
  117. await task.initialize(context);
  118. }
  119. }
  120. }
  121. enum IntegrationMode {
  122. develop,
  123. release,
  124. test,
  125. }
  126. extension IntegrationEnvExt on IntegrationMode {
  127. bool isTest() {
  128. return this == IntegrationMode.test;
  129. }
  130. }
  131. IntegrationMode integrationEnv() {
  132. if (Platform.environment.containsKey('FLUTTER_TEST')) {
  133. return IntegrationMode.test;
  134. }
  135. if (kReleaseMode) {
  136. return IntegrationMode.release;
  137. }
  138. return IntegrationMode.develop;
  139. }