From 8afc294f4dab75702aee10f6bd3b219f53c8bcc6 Mon Sep 17 00:00:00 2001 From: XTxiaoting14332 Date: Sun, 12 Jan 2025 11:46:18 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20fix:=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E5=90=AF=E7=94=A8/=E7=A6=81=E7=94=A8?= =?UTF-8?q?=E5=90=8E=20pyproject.toml=20=E8=A2=AB=E6=94=B9=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ui/manage/manage_plugin.dart | 83 ++++++++++++++++++++++---------- lib/utils/manage.dart | 18 +++++-- 2 files changed, 71 insertions(+), 30 deletions(-) diff --git a/lib/ui/manage/manage_plugin.dart b/lib/ui/manage/manage_plugin.dart index fbf91dd..1aff0d8 100644 --- a/lib/ui/manage/manage_plugin.dart +++ b/lib/ui/manage/manage_plugin.dart @@ -27,6 +27,9 @@ class _HomeScreenState extends State { void initState() { super.initState(); fileListener(); + if (File('${Bot.path()}/.disabled_plugins').existsSync()) { + File('${Bot.path()}/.disabled_plugins').writeAsStringSync(''); + } } @override @@ -120,33 +123,61 @@ class _HomeScreenState extends State { ) : Expanded( // 使用 Expanded 包裹 ListView - child: ListView.separated( - itemCount: getDisabledPluginList().length, - separatorBuilder: (BuildContext context, int index) { - return const Divider(); - }, - itemBuilder: (BuildContext context, int index) { - return ListTile( - title: Text(getDisabledPluginList()[index]), - trailing: Row( - mainAxisSize: MainAxisSize.min, - children: [ - IconButton( - icon: const Icon( - Icons.open_in_browser_rounded), - tooltip: '启用', - onPressed: () { - setState(() { - Plugin.enable( - getDisabledPluginList()[index]); - }); - }, - ), - ], + child: File('${Bot.path()}/.disabled_plugins') + .existsSync() + ? ListView.separated( + itemCount: + File('${Bot.path()}/.disabled_plugins') + .readAsStringSync() + .split('\n') + .length, + separatorBuilder: + (BuildContext context, int index) { + return const Divider(); + }, + itemBuilder: (BuildContext context, int index) { + return ListTile( + title: Text( + File('${Bot.path()}/.disabled_plugins') + .readAsStringSync() + .split('\n')[index]), + trailing: Row( + mainAxisSize: MainAxisSize.min, + children: [ + IconButton( + icon: const Icon( + Icons.open_in_browser_rounded), + tooltip: '启用', + onPressed: () { + setState(() { + Plugin.enable(File( + '${Bot.path()}/.disabled_plugins') + .readAsStringSync() + .split('\n')[index]); + }); + }, + ), + IconButton( + icon: const Icon(Icons.delete), + tooltip: '卸载', + onPressed: () { + showDialog( + context: context, + builder: (BuildContext context) { + return uninstallDialog( + context, index); + }, + ); + }, + ), + ], + ), + ); + }, + ) + : const Center( + child: Text('空空如也...'), ), - ); - }, - ), ), ], ), diff --git a/lib/utils/manage.dart b/lib/utils/manage.dart index 8602cb5..195f5b3 100644 --- a/lib/utils/manage.dart +++ b/lib/utils/manage.dart @@ -1,6 +1,5 @@ import 'dart:convert'; import 'dart:io'; -import 'package:NoneBotGUI/utils/core.dart'; import 'package:NoneBotGUI/utils/global.dart'; import 'package:NoneBotGUI/utils/userConfig.dart'; import 'package:toml/toml.dart'; @@ -377,7 +376,12 @@ class Plugin { // 移除指定的插件 pluginsList.remove(name); nonebot['plugins'] = pluginsList; - String updatedTomlContent = TomlDocument.fromMap(toml).toString(); + + // 手动更新 plugins 列表 + String updatedTomlContent = pyprojectContent.replaceFirstMapped( + RegExp(r'plugins = \[([^\]]*)\]', dotAll: true), + (match) => + 'plugins = [${pluginsList.map((plugin) => '"$plugin"').join(', ')}]'); pyprojectFile.writeAsStringSync(updatedTomlContent); if (disable.readAsStringSync().isEmpty) { @@ -394,14 +398,20 @@ class Plugin { String pyprojectContent = pyprojectFile.readAsStringSync(); var toml = TomlDocument.parse(pyprojectContent).toMap(); var nonebot = toml['tool']['nonebot']; - List pluginsList = nonebot['plugins']; + List pluginsList = List.from(nonebot['plugins']); if (!pluginsList.contains(name)) { pluginsList.add(name); } nonebot['plugins'] = pluginsList; - String updatedTomlContent = TomlDocument.fromMap(toml).toString(); + + // 手动更新 plugins 列表 + String updatedTomlContent = pyprojectContent.replaceFirstMapped( + RegExp(r'plugins = \[([^\]]*)\]', dotAll: true), + (match) => + 'plugins = [${pluginsList.map((plugin) => '"$plugin"').join(', ')}]'); + pyprojectFile.writeAsStringSync(updatedTomlContent); String disabled = disable.readAsStringSync(); List disabledList = disabled.split('\n');