Fix relocated desktop tool paths and package release 0.2.2

This commit is contained in:
tongtong committed 2026-11-13 12:49:10 +08:00
1 parent efaf16de18
commit e366eb5bec
14 files changed
+250 -33

No files matched your search

@@ -6,6 +6,7 @@ import 'package:path/path.dart' as p;
import 'windows_usb.dart';
import 'usb_recovery.dart';
import 'tool_paths.dart';
class Settings {
String server = 'wss://ttstd.com/ads';
@@ -95,7 +96,8 @@ class ManagerRepository {
String token = '';
Directory? root;
late Directory storage;
ManagerRepository(this.log);
final ToolPaths? paths;
ManagerRepository(this.log, {this.paths});
Future<void> init() async {
storage = Directory(
@@ -138,29 +140,30 @@ class ManagerRepository {
jsonDecode(await saved.readAsString()) as Map<String, dynamic>,
);
}
await repairToolPaths();
token = Platform.environment['ADS_CLIENT_TOKEN'] ?? '';
if (token.isEmpty && await File(settings.credential).exists()) {
await importCredential();
}
}
String findTool(String name) {
final exe = Platform.isWindows ? '$name.exe' : name;
final base = p.dirname(Platform.resolvedExecutable);
final candidates = [
p.join(base, exe),
p.join(base, 'tools', 'scrcpy', exe),
if (root != null) p.join(root!.path, 'bin', exe),
if (root != null) p.join(root!.path, 'bin', 'tools', 'scrcpy', exe),
for (final dir in (Platform.environment['PATH'] ?? '').split(
Platform.isWindows ? ';' : ':',
))
p.join(dir, exe),
];
return candidates.firstWhere(
(v) => File(v).existsSync(),
orElse: () => exe,
);
ToolPaths get toolPaths => paths ?? ToolPaths(projectRoot: root?.path);
String findTool(String name) => toolPaths.resolve(name);
Future<void> repairToolPaths() async {
var changed = false;
void repair(String name, String value, void Function(String) assign) {
final resolved = toolPaths.resolve(name, configured: value);
if (resolved != value) {
assign(resolved);
changed = true;
log('已更新 $name 工具位置:$resolved');
}
}
repair('ads-client', settings.client, (v) => settings.client = v);
repair('adb', settings.adb, (v) => settings.adb = v);
repair('scrcpy', settings.scrcpy, (v) => settings.scrcpy = v);
if (changed) await save();
}
Future<void> save() =>
@@ -292,6 +295,12 @@ class ManagerRepository {
..['NO_COLOR'] = '1';
Future<String> run(String exe, List<String> args) async {
final name = p
.basename(exe)
.replaceFirst(RegExp(r'\.exe$', caseSensitive: false), '');
if (['ads-client', 'ads-usb', 'adsctl', 'adb', 'scrcpy'].contains(name)) {
exe = toolPaths.resolve(name, configured: exe, required: true);
}
final proc = await Process.start(
exe,
args,
@@ -334,6 +343,17 @@ class ManagerRepository {
if (token.length < 32) {
throw const FormatException('请先导入凭据');
}
await repairToolPaths();
settings.client = toolPaths.resolve(
'ads-client',
configured: settings.client,
required: true,
);
settings.adb = toolPaths.resolve(
'adb',
configured: settings.adb,
required: true,
);
final usbMode = settings.usbMode;
final usbip = settings.usbip;
if (usbMode) {
@@ -343,7 +363,7 @@ class ManagerRepository {
if (!await File(usbip).exists()) {
throw StateError('找不到 usbip.exe,请检查配置路径');
}
await run(findTool('ads-usb'), ['--version']);
await run(toolPaths.resolve('ads-usb', required: true), ['--version']);
}
final port = await allocatePort(15555, 18000);
int? usbPort;
@@ -655,8 +675,18 @@ class ManagerRepository {
String id, {
bool embedded = false,
}) {
final scrcpy = toolPaths.resolve(
'scrcpy',
configured: settings.scrcpy,
required: true,
);
final selectedAdb = toolPaths.resolve(
'adb',
configured: adb,
required: true,
);
return Process.start(
settings.scrcpy,
scrcpy,
[
'--serial',
endpoint,
@@ -675,8 +705,8 @@ class ManagerRepository {
'60',
'--no-audio',
],
workingDirectory: p.dirname(p.absolute(settings.scrcpy)),
environment: environment()..['ADB'] = adb,
workingDirectory: p.dirname(scrcpy),
environment: environment()..['ADB'] = selectedAdb,
includeParentEnvironment: false,
);
}
+46
View File
@@ -0,0 +1,46 @@
import 'dart:io';
import 'package:path/path.dart' as p;
/// Resolves relocated bundled tools while preserving valid custom executables.
class ToolPaths {
ToolPaths({String? base, this.projectRoot, String? searchPath})
: base = base ?? p.dirname(Platform.resolvedExecutable),
searchPath = searchPath ?? Platform.environment['PATH'] ?? '';
final String base, searchPath;
final String? projectRoot;
static bool managed(String value) =>
RegExp(r'/programs/(?:ttstd|tongtongstudio)/ads-(?:flutter|avalonia)/')
.hasMatch(value.replaceAll(r'\', '/').toLowerCase());
String resolve(String name, {String configured = '', bool required = false}) {
final exe = Platform.isWindows ? '$name.exe' : name;
final selected = configured.trim().replaceAll(RegExp(r'^"|"$'), '');
if (selected.isNotEmpty &&
!managed(selected) &&
File(selected).existsSync()) {
return p.absolute(selected);
}
final candidates = [
p.join(base, exe),
p.join(base, 'tools', 'scrcpy', exe),
if (projectRoot != null) p.join(projectRoot!, 'bin', exe),
if (projectRoot != null)
p.join(projectRoot!, 'bin', 'tools', 'scrcpy', exe),
for (final folder in searchPath.split(Platform.isWindows ? ';' : ':'))
if (folder.trim().isNotEmpty)
p.join(folder.trim().replaceAll('"', ''), exe),
];
for (final candidate in candidates) {
if (File(candidate).existsSync()) return p.absolute(candidate);
}
if (required) {
throw FileSystemException(
'找不到 $exe。请重新安装完整安装包,或在连接配置中选择有效的工具文件。当前程序目录:$base',
selected.isEmpty ? exe : selected,
);
}
return selected.isNotEmpty ? selected : exe;
}
}