feat: migrate Flutter to Cupertino with daily logs and computer groups
This commit is contained in:
1 parent
c568749d23
commit
87e1fff59b
22 files changed
+808
-55
No files matched your search
@@ -95,7 +95,12 @@ class AgentService implements AgentApi {
|
||||
final code = await child.exitCode.timeout(const Duration(seconds: 22));
|
||||
final result = ProcessResult(child.pid, code, await out, await error);
|
||||
if (code != 0) {
|
||||
throw StateError(redact(result.stderr as String));
|
||||
var message = redact(result.stderr as String);
|
||||
if (message.contains('unknown variant') &&
|
||||
message.contains('devices_discover')) {
|
||||
message = '本机 adsctl 或 Agent 版本过旧,请同步更新两者后重启 Agent';
|
||||
}
|
||||
throw StateError(message);
|
||||
}
|
||||
return result;
|
||||
} on TimeoutException {
|
||||
@@ -142,6 +147,9 @@ class AgentService implements AgentApi {
|
||||
|
||||
@override
|
||||
Future<void> start(String path) async {
|
||||
if (!await File(path).exists()) {
|
||||
throw StateError("请先导入 agent.env 并初始化本机配置,再启动 Agent");
|
||||
}
|
||||
try {
|
||||
await local(path, {'op': 'status'});
|
||||
return;
|
||||
@@ -160,6 +168,10 @@ class AgentService implements AgentApi {
|
||||
|
||||
@override
|
||||
Future<void> install(String path) async {
|
||||
if (Platform.isLinux) {
|
||||
await _cli(['--config', path, 'service', 'install']);
|
||||
return;
|
||||
}
|
||||
if (!Platform.isWindows) {
|
||||
throw UnsupportedError('请在终端运行 adsctl service install,系统级服务加 --system');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
class DailyLog {
|
||||
DailyLog(
|
||||
this.directory, {
|
||||
this.maxBytes = 5 * 1024 * 1024,
|
||||
this.maxParts = 5,
|
||||
this.keepDays = 14,
|
||||
DateTime Function()? clock,
|
||||
}) : assert(maxBytes > 0 && maxParts > 0 && keepDays > 0),
|
||||
clock = clock ?? DateTime.now;
|
||||
final Directory directory;
|
||||
final int maxBytes, maxParts, keepDays;
|
||||
final DateTime Function() clock;
|
||||
Future<void> _pending = Future.value();
|
||||
String? error;
|
||||
String _cleaned = '';
|
||||
void add(String text) {
|
||||
final time = clock();
|
||||
_pending = _pending
|
||||
.then((_) async {
|
||||
await directory.create(recursive: true);
|
||||
final day = time.toIso8601String().substring(0, 10);
|
||||
if (_cleaned != day) {
|
||||
final cutoff = DateTime(
|
||||
time.year,
|
||||
time.month,
|
||||
time.day,
|
||||
).subtract(Duration(days: keepDays - 1));
|
||||
await for (final entry in directory.list()) {
|
||||
if (entry is! File) continue;
|
||||
final match = RegExp(r'^ads-(\d{4}-\d{2}-\d{2})(?:\.\d+)?\.log$')
|
||||
.firstMatch(p.basename(entry.path));
|
||||
if (match != null && DateTime.parse(match[1]!).isBefore(cutoff)) {
|
||||
await entry.delete();
|
||||
}
|
||||
}
|
||||
_cleaned = day;
|
||||
}
|
||||
final base = p.join(directory.path, 'ads-$day');
|
||||
final file = File('$base.log');
|
||||
if (await file.exists() && await file.length() >= maxBytes) {
|
||||
final oldest = File('$base.${maxParts - 1}.log');
|
||||
if (await oldest.exists()) await oldest.delete();
|
||||
for (var part = maxParts - 2; part >= 1; part--) {
|
||||
final old = File('$base.$part.log');
|
||||
if (await old.exists()) await old.rename('$base.${part + 1}.log');
|
||||
}
|
||||
if (maxParts > 1) {
|
||||
await file.rename('$base.1.log');
|
||||
} else {
|
||||
await file.delete();
|
||||
}
|
||||
}
|
||||
await file.writeAsString(
|
||||
'${time.toIso8601String()} $text\n',
|
||||
mode: FileMode.append,
|
||||
flush: true,
|
||||
);
|
||||
error = null;
|
||||
})
|
||||
.catchError((Object e) {
|
||||
error = e.toString();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> flush() => _pending;
|
||||
}
|
||||
@@ -59,7 +59,8 @@ class Settings {
|
||||
class Device {
|
||||
final String id;
|
||||
final bool busy;
|
||||
const Device(this.id, this.busy);
|
||||
final String agentId, computerName;
|
||||
const Device(this.id, this.busy, {this.agentId = "", this.computerName = ""});
|
||||
}
|
||||
|
||||
class Session {
|
||||
@@ -144,14 +145,16 @@ class ManagerRepository {
|
||||
}
|
||||
|
||||
String findTool(String name) {
|
||||
final exe = '$name.exe';
|
||||
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(';'))
|
||||
for (final dir in (Platform.environment['PATH'] ?? '').split(
|
||||
Platform.isWindows ? ';' : ':',
|
||||
))
|
||||
p.join(dir, exe),
|
||||
];
|
||||
return candidates.firstWhere(
|
||||
@@ -265,7 +268,12 @@ class ManagerRepository {
|
||||
if (!RegExp(r'^[a-zA-Z0-9_.-]{1,128}$').hasMatch(id)) {
|
||||
throw const FormatException('无效设备 ID');
|
||||
}
|
||||
return Device(id, j['busy'] == true);
|
||||
return Device(
|
||||
id,
|
||||
j['busy'] == true,
|
||||
agentId: j['agent_id'] as String? ?? '',
|
||||
computerName: j['computer_name'] as String? ?? '',
|
||||
);
|
||||
}).toList()..sort((a, b) => a.id.compareTo(b.id));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user