Skip to content

Commit

Permalink
🔧 fix: 修复插件启用/禁用后 pyproject.toml 被改格式的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
XTxiaoting14332 committed Jan 12, 2025
1 parent e92b813 commit 8afc294
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 30 deletions.
83 changes: 57 additions & 26 deletions lib/ui/manage/manage_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class _HomeScreenState extends State<ManagePlugin> {
void initState() {
super.initState();
fileListener();
if (File('${Bot.path()}/.disabled_plugins').existsSync()) {
File('${Bot.path()}/.disabled_plugins').writeAsStringSync('');
}
}

@override
Expand Down Expand Up @@ -120,33 +123,61 @@ class _HomeScreenState extends State<ManagePlugin> {
)
: 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('空空如也...'),
),
);
},
),
),
],
),
Expand Down
18 changes: 14 additions & 4 deletions lib/utils/manage.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) {
Expand All @@ -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<String>.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<String> disabledList = disabled.split('\n');
Expand Down

0 comments on commit 8afc294

Please sign in to comment.