Skip to content

Commit

Permalink
Refactor extension installation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaoMint committed Mar 24, 2024
1 parent 3293ae1 commit 212aa52
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
27 changes: 13 additions & 14 deletions lib/utils/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ class ExtensionUtils {
}
}

static localInstall(String p, BuildContext context) async {
static install(String url, BuildContext context) async {
try {
final res = await File(p).readAsString();
final ext = ExtensionUtils.parseExtension(res);
final res = await dio.get<String>(url);
if (res.data == null) {
throw Exception("Does not seem to be an extension");
}
final ext = ExtensionUtils.parseExtension(res.data!);
final savePath = path.join(extensionsDir, '${ext.package}.js');
// 保存
File(savePath).writeAsStringSync(res);
// 保存文件
File(savePath).writeAsStringSync(res.data!);
//reload
_loadExtensions();
} catch (e) {
Expand All @@ -99,18 +102,14 @@ class ExtensionUtils {
}
}

static install(String url, BuildContext context) async {
static installByScript(String script, BuildContext context) async {
try {
final res = await dio.get<String>(url);
if (res.data == null) {
throw Exception("Does not seem to be an extension");
}
final ext = ExtensionUtils.parseExtension(res.data!);
final ext = ExtensionUtils.parseExtension(script);
final savePath = path.join(extensionsDir, '${ext.package}.js');
// 保存文件
File(savePath).writeAsStringSync(res.data!);
//reload
_loadExtensions();
File(savePath).writeAsStringSync(script);
runtimes[ext.package] = await ExtensionService().initRuntime(ext);
_reloadPage();
} catch (e) {
if (context.mounted) {
showPlatformDialog(
Expand Down
33 changes: 20 additions & 13 deletions lib/views/pages/extension/extension_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:fluent_ui/fluent_ui.dart' as fluent;
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -138,19 +140,24 @@ class _ExtensionPageState extends State<ExtensionPage> {
child: Text('extension.import.import-by-url'.i18n),
),
PlatformFilledButton(
child: Text('extension.import.import-by-local'.i18n),
onPressed: () async {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['js'],
);
final path = result?.files.single.path;
if (result == null || !mounted) {
return;
}
await ExtensionUtils.localInstall(path!, context);
RouterUtils.pop();
}),
child: Text('extension.import.import-by-local'.i18n),
onPressed: () async {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['js'],
);
if (result == null || !mounted) {
return;
}
final path = result.files.single.path;
if (path == null) {
return;
}
final script = File(path).readAsStringSync();
await ExtensionUtils.installByScript(script, context);
RouterUtils.pop();
},
),
],
);
}
Expand Down

0 comments on commit 212aa52

Please sign in to comment.