268 lines
9.5 KiB
Dart
268 lines
9.5 KiB
Dart
|
|
/// 发布级模块清单:`modules/registry.json` + 每模块 `module.json` 的解析与合并。
|
|||
|
|
///
|
|||
|
|
/// 源码注册表(`lib/modules/registry.dart`)决定**编译进应用**的模块实现;
|
|||
|
|
/// 发布清单决定**发布包**里的模块顺序、显示名、启用状态与元数据。应用启动时:
|
|||
|
|
///
|
|||
|
|
/// 1. 桌面优先读取 exe 同级 `modules/registry.json`(发布包可见、可编辑);
|
|||
|
|
/// 2. 安卓 / 开发环境回退到内置资源 `assets/modules/registry.json`;
|
|||
|
|
/// 3. 清单缺失或损坏时回退到编译期注册表。
|
|||
|
|
///
|
|||
|
|
/// 注意:Flutter release 的 Dart 代码是 AOT 编译的,清单不能动态加载新的
|
|||
|
|
/// Dart 实现;清单中未编译的模块会显示为「组件未安装」(未开放)。
|
|||
|
|
library;
|
|||
|
|
|
|||
|
|
import 'dart:convert';
|
|||
|
|
|
|||
|
|
import 'package:flutter/material.dart';
|
|||
|
|
|
|||
|
|
import 'package:sequence_diagram/modules/module.dart';
|
|||
|
|
import 'package:sequence_diagram/state/app_controller.dart';
|
|||
|
|
|
|||
|
|
/// 发布清单中的单个模块配置。
|
|||
|
|
class ModuleManifestEntry {
|
|||
|
|
/// 构造模块清单项。
|
|||
|
|
const ModuleManifestEntry({
|
|||
|
|
required this.id,
|
|||
|
|
required this.name,
|
|||
|
|
required this.shortName,
|
|||
|
|
required this.icon,
|
|||
|
|
required this.accent,
|
|||
|
|
required this.suffix,
|
|||
|
|
required this.available,
|
|||
|
|
required this.defaultName,
|
|||
|
|
required this.blurb,
|
|||
|
|
this.enterStatus = '',
|
|||
|
|
this.version = '',
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
/// 模块稳定 id。
|
|||
|
|
final String id;
|
|||
|
|
/// 模块显示名。
|
|||
|
|
final String name;
|
|||
|
|
/// 短名。
|
|||
|
|
final String shortName;
|
|||
|
|
/// 图标名称(见 [moduleIconFromName])。
|
|||
|
|
final String icon;
|
|||
|
|
/// 品牌色(ARGB int)。
|
|||
|
|
final int accent;
|
|||
|
|
/// 文档后缀。
|
|||
|
|
final String suffix;
|
|||
|
|
/// 是否启用。
|
|||
|
|
final bool available;
|
|||
|
|
/// 新建文档默认文件名。
|
|||
|
|
final String defaultName;
|
|||
|
|
/// 一句话简介。
|
|||
|
|
final String blurb;
|
|||
|
|
/// 进入模块状态提示。
|
|||
|
|
final String enterStatus;
|
|||
|
|
/// 模块版本(发布包展示 / 升级判断用)。
|
|||
|
|
final String version;
|
|||
|
|
|
|||
|
|
/// 从 JSON 解析;缺失字段使用安全默认值。
|
|||
|
|
static ModuleManifestEntry fromJson(Map<String, dynamic> j) {
|
|||
|
|
return ModuleManifestEntry(
|
|||
|
|
id: (j['id'] as String?)?.trim() ?? '',
|
|||
|
|
name: (j['name'] as String?)?.trim() ?? '',
|
|||
|
|
shortName: (j['shortName'] as String?)?.trim() ?? '',
|
|||
|
|
icon: (j['icon'] as String?)?.trim() ?? '',
|
|||
|
|
accent: parseModuleAccent(j['accent']),
|
|||
|
|
suffix: (j['suffix'] as String?)?.trim() ?? '',
|
|||
|
|
available: j['available'] as bool? ?? true,
|
|||
|
|
defaultName: (j['defaultName'] as String?)?.trim() ?? '',
|
|||
|
|
blurb: (j['blurb'] as String?)?.trim() ?? '',
|
|||
|
|
enterStatus: (j['enterStatus'] as String?)?.trim() ?? '',
|
|||
|
|
version: (j['version'] as String?)?.trim() ?? '',
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 发布清单根对象。
|
|||
|
|
class ModuleManifest {
|
|||
|
|
/// 构造清单。
|
|||
|
|
const ModuleManifest({
|
|||
|
|
required this.schema,
|
|||
|
|
required this.version,
|
|||
|
|
required this.modules,
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
/// 协议标识(`pds.modules/1`)。
|
|||
|
|
final String schema;
|
|||
|
|
/// 发布版本。
|
|||
|
|
final String version;
|
|||
|
|
/// 模块列表(顺序即首页 / 菜单顺序)。
|
|||
|
|
final List<ModuleManifestEntry> modules;
|
|||
|
|
|
|||
|
|
/// 从 JSON 解析清单。
|
|||
|
|
///
|
|||
|
|
/// 支持两种写法:`{"modules":[...]}` 或直接数组 `[...]`。
|
|||
|
|
static ModuleManifest parse(String source) {
|
|||
|
|
final Object? decoded = jsonDecode(source);
|
|||
|
|
final Map<String, dynamic>? root =
|
|||
|
|
decoded is Map<String, dynamic> ? decoded : null;
|
|||
|
|
final List<dynamic> raw = root != null
|
|||
|
|
? (root['modules'] as List<dynamic>? ?? const <dynamic>[])
|
|||
|
|
: (decoded is List<dynamic> ? decoded : const <dynamic>[]);
|
|||
|
|
final List<ModuleManifestEntry> modules = <ModuleManifestEntry>[
|
|||
|
|
for (final dynamic e in raw)
|
|||
|
|
if (e is Map<String, dynamic>)
|
|||
|
|
ModuleManifestEntry.fromJson(e),
|
|||
|
|
]..removeWhere((ModuleManifestEntry e) => e.id.isEmpty);
|
|||
|
|
return ModuleManifest(
|
|||
|
|
schema: (root?['schema'] as String?) ?? 'pds.modules/1',
|
|||
|
|
version: (root?['version'] as String?) ?? '',
|
|||
|
|
modules: modules,
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 把发布清单与编译期模块合并为实际可用的模块列表。
|
|||
|
|
///
|
|||
|
|
/// - 清单顺序决定展示顺序;
|
|||
|
|
/// - 编译期存在的模块使用清单元数据覆盖显示名 / 颜色 / 启用状态;
|
|||
|
|
/// - 清单中有、但未编译进应用的模块生成为「组件未安装」占位;
|
|||
|
|
/// - 清单为空时回退编译期列表。
|
|||
|
|
List<EdcModule> resolveModulesFromManifest(
|
|||
|
|
ModuleManifest manifest,
|
|||
|
|
List<EdcModule> compiled,
|
|||
|
|
) {
|
|||
|
|
if (manifest.modules.isEmpty) return compiled;
|
|||
|
|
final Map<String, EdcModule> byId = <String, EdcModule>{
|
|||
|
|
for (final EdcModule m in compiled) m.id: m,
|
|||
|
|
};
|
|||
|
|
final List<EdcModule> out = <EdcModule>[];
|
|||
|
|
for (final ModuleManifestEntry entry in manifest.modules) {
|
|||
|
|
final EdcModule? base = byId.remove(entry.id);
|
|||
|
|
if (base == null) {
|
|||
|
|
out.add(ManifestOnlyModule(entry));
|
|||
|
|
} else {
|
|||
|
|
out.add(ConfiguredModule(base: base, entry: entry));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return out;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 编译期模块 + 发布清单元数据的包装。
|
|||
|
|
class ConfiguredModule extends EdcModule {
|
|||
|
|
/// 构造包装。
|
|||
|
|
const ConfiguredModule({required this.base, required this.entry});
|
|||
|
|
|
|||
|
|
/// 编译期实现。
|
|||
|
|
final EdcModule base;
|
|||
|
|
|
|||
|
|
/// 发布清单配置。
|
|||
|
|
final ModuleManifestEntry entry;
|
|||
|
|
|
|||
|
|
@override
|
|||
|
|
String get id => base.id;
|
|||
|
|
@override
|
|||
|
|
String get name => entry.name.isEmpty ? base.name : entry.name;
|
|||
|
|
@override
|
|||
|
|
String get shortName => entry.shortName.isEmpty ? base.shortName : entry.shortName;
|
|||
|
|
@override
|
|||
|
|
IconData get icon => moduleIconFromName(entry.icon) ?? base.icon;
|
|||
|
|
@override
|
|||
|
|
int get accent => entry.accent == 0 ? base.accent : entry.accent;
|
|||
|
|
@override
|
|||
|
|
String get suffix => entry.suffix.isEmpty ? base.suffix : entry.suffix;
|
|||
|
|
@override
|
|||
|
|
bool get available => entry.available && base.available;
|
|||
|
|
@override
|
|||
|
|
String get defaultName => entry.defaultName.isEmpty ? base.defaultName : entry.defaultName;
|
|||
|
|
@override
|
|||
|
|
String get blurb => entry.blurb.isEmpty ? base.blurb : entry.blurb;
|
|||
|
|
@override
|
|||
|
|
String get enterStatus =>
|
|||
|
|
entry.enterStatus.isEmpty ? base.enterStatus : entry.enterStatus;
|
|||
|
|
|
|||
|
|
@override
|
|||
|
|
bool get hasLeftPanel => base.hasLeftPanel;
|
|||
|
|
@override
|
|||
|
|
bool get hasRightPanel => base.hasRightPanel;
|
|||
|
|
|
|||
|
|
@override
|
|||
|
|
void newDocument(AppController controller) => base.newDocument(controller);
|
|||
|
|
@override
|
|||
|
|
void loadDocument(AppController controller, String source, {String? path}) =>
|
|||
|
|
base.loadDocument(controller, source, path: path);
|
|||
|
|
@override
|
|||
|
|
String encodeDocument(AppController controller) => base.encodeDocument(controller);
|
|||
|
|
@override
|
|||
|
|
String documentName(AppController controller) => base.documentName(controller);
|
|||
|
|
@override
|
|||
|
|
String? filePath(AppController controller) => base.filePath(controller);
|
|||
|
|
@override
|
|||
|
|
bool isDirty(AppController controller) => base.isDirty(controller);
|
|||
|
|
@override
|
|||
|
|
Future<void> saveTo(AppController controller, String path) =>
|
|||
|
|
base.saveTo(controller, path);
|
|||
|
|
@override
|
|||
|
|
Future<void> markSaved(AppController controller, String displayPath) =>
|
|||
|
|
base.markSaved(controller, displayPath);
|
|||
|
|
@override
|
|||
|
|
bool isReadOnly(AppController controller) => base.isReadOnly(controller);
|
|||
|
|
@override
|
|||
|
|
Map<ShortcutActivator, VoidCallback> buildShortcuts(ModuleUiContext ui) =>
|
|||
|
|
base.buildShortcuts(ui);
|
|||
|
|
@override
|
|||
|
|
Widget buildWorkspace(ModuleUiContext ui) => base.buildWorkspace(ui);
|
|||
|
|
@override
|
|||
|
|
Widget? buildLeftPanel(ModuleUiContext ui, {bool floating = false}) =>
|
|||
|
|
base.buildLeftPanel(ui, floating: floating);
|
|||
|
|
@override
|
|||
|
|
Widget? buildRightPanel(ModuleUiContext ui, {bool floating = false}) =>
|
|||
|
|
base.buildRightPanel(ui, floating: floating);
|
|||
|
|
@override
|
|||
|
|
Widget buildStatusBar(ModuleUiContext ui) => base.buildStatusBar(ui);
|
|||
|
|
@override
|
|||
|
|
List<Widget> buildToolbarCenter(ModuleUiContext ui, {required bool compact}) =>
|
|||
|
|
base.buildToolbarCenter(ui, compact: compact);
|
|||
|
|
@override
|
|||
|
|
List<Widget> buildToolbarRight(ModuleUiContext ui) =>
|
|||
|
|
base.buildToolbarRight(ui);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 发布清单中存在、但应用未编译该模块实现时的占位。
|
|||
|
|
class ManifestOnlyModule extends PlannedModule {
|
|||
|
|
/// 构造未安装模块。
|
|||
|
|
ManifestOnlyModule(ModuleManifestEntry entry)
|
|||
|
|
: super(
|
|||
|
|
id: entry.id,
|
|||
|
|
name: entry.name.isEmpty ? entry.id : entry.name,
|
|||
|
|
shortName: entry.shortName.isEmpty ? entry.id : entry.shortName,
|
|||
|
|
icon: moduleIconFromName(entry.icon) ?? Icons.widgets_outlined,
|
|||
|
|
accent: entry.accent == 0 ? 0xFF94A3B8 : entry.accent,
|
|||
|
|
suffix: entry.suffix,
|
|||
|
|
defaultName: entry.defaultName,
|
|||
|
|
blurb: entry.blurb.isEmpty ? '组件未安装' : '${entry.blurb} · 组件未安装',
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 清单图标名称 → Material 图标。
|
|||
|
|
IconData? moduleIconFromName(String name) {
|
|||
|
|
return switch (name) {
|
|||
|
|
'account_tree_outlined' => Icons.account_tree_outlined,
|
|||
|
|
'waterfall_chart' => Icons.waterfall_chart,
|
|||
|
|
'electric_bolt' => Icons.electric_bolt,
|
|||
|
|
'grid_on' => Icons.grid_on,
|
|||
|
|
'hub_outlined' => Icons.hub_outlined,
|
|||
|
|
'plumbing' => Icons.plumbing,
|
|||
|
|
'widgets_outlined' => Icons.widgets_outlined,
|
|||
|
|
'dashboard_outlined' => Icons.dashboard_outlined,
|
|||
|
|
'schema_outlined' => Icons.schema_outlined,
|
|||
|
|
_ => null,
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 解析清单颜色:`#RRGGBB` / `#AARRGGBB` / `0xAARRGGBB` / 十进制整数。
|
|||
|
|
int parseModuleAccent(Object? value) {
|
|||
|
|
if (value is int) return value;
|
|||
|
|
if (value is! String) return 0;
|
|||
|
|
String s = value.trim();
|
|||
|
|
if (s.isEmpty) return 0;
|
|||
|
|
if (s.startsWith('#')) s = s.substring(1);
|
|||
|
|
if (s.startsWith('0x') || s.startsWith('0X')) s = s.substring(2);
|
|||
|
|
if (s.length == 6) s = 'FF$s';
|
|||
|
|
final int? v = int.tryParse(s, radix: 16);
|
|||
|
|
return v ?? 0;
|
|||
|
|
}
|