From 79296728ac8a80f6321986469a078cb4f2824a83 Mon Sep 17 00:00:00 2001 From: Toki Date: Mon, 23 Jan 2023 22:27:31 +0900 Subject: [PATCH] update startup --- lib/utils/os_startup.dart | 87 +++++++++++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 7 deletions(-) diff --git a/lib/utils/os_startup.dart b/lib/utils/os_startup.dart index 45b14f2..d2da1d1 100644 --- a/lib/utils/os_startup.dart +++ b/lib/utils/os_startup.dart @@ -5,13 +5,40 @@ import 'package:path/path.dart'; import 'package:dart_framework/core/process.dart'; class OSStartup { - static final String windowsStartupPath = + static final windowsStartupPath = '\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\'; + static final plistTemplate = """ + + + + KeepAlive + + Label + {LABEL} + Program + {FILE_PATH} + RunAtLoad + {LOG_STANDARD}{LOG_ERROR} + +"""; + + static final logStandardTemplate = """ + + StandardOutPath + {LOG_STANDARD}"""; + static final logErrorTemplate = """ + + StandardErrorPath + {LOG_ERROR}"""; + static Future isRegistedStartup(String label) async { var c = Completer(); var exist = false; if (Platform.isMacOS) { + var plistName = '$label.plist'; + var userPath = Platform.environment['HOME']; + exist = await File('$userPath/Library/LaunchAgents/$plistName').exists(); } else if (Platform.isWindows) { //For Windows 8, 10 var userPath = Platform.environment['UserProfile']; @@ -21,21 +48,60 @@ class OSStartup { return c.future; } - static Future registStartup(String executableFilePath, - {String? label}) async { + static Future registStartup(String executableFilePath, String label, + {String? logStandardFilePath, String? logErrorFilePath}) async { var c = Completer(); var file = File(executableFilePath); - var fileName = basename(file.path); - var targetName = label ?? fileName; + var success = false; ProcessResult? result; if (Platform.isMacOS) { + var enable = true; + if (await isRegistedStartup(label)) { + enable = await unregistStartup(label); + } + if (enable) { + //sh file must have '#!/bin/sh' + //create plist in LaunchAgents + final plistName = '$label.plist'; + final userPath = Platform.environment['HOME']; + final tagLabel = '{LABEL}'; + final tagPath = '{FILE_PATH}'; + final tagLogStandard = '{LOG_STANDARD}'; + final tagLogError = '{LOG_ERROR}'; + var plistContent = plistTemplate + .replaceAll(tagLabel, label) + .replaceAll(tagPath, executableFilePath); + if (logStandardFilePath == null) { + plistContent = plistContent.replaceAll(tagLogStandard, ''); + } else { + plistContent = plistContent.replaceAll( + tagLogStandard, + logStandardTemplate.replaceAll( + tagLogStandard, logStandardFilePath)); + } + if (logErrorFilePath == null) { + plistContent = plistContent.replaceAll(tagLogError, ''); + } else { + plistContent = plistContent.replaceAll(tagLogError, + logErrorTemplate.replaceAll(tagLogError, logErrorFilePath)); + } + var plistFile = File('$userPath/Library/LaunchAgents/$plistName'); + await plistFile.create(); + await plistFile.writeAsString(plistContent); + + var result = await ProcessExecutor.run( + StringBuffer('chmod +x "$executableFilePath"')); + success = result.exitCode == 0; + } } else if (Platform.isWindows) { //For Windows 8, 10 - var symbolicPath = '%userprofile%$windowsStartupPath$targetName'; + var fileName = basename(file.path); + var symbolicPath = '%userprofile%$windowsStartupPath$fileName'; result = await ProcessExecutor.run( StringBuffer('mklink "$symbolicPath" "$executableFilePath"')); + success = result == null ? false : result.exitCode == 0; } else if (Platform.isLinux) {} - c.complete(result == null ? false : result.exitCode == 0); + c.complete(success); return c.future; } @@ -43,6 +109,13 @@ class OSStartup { var c = Completer(); var success = false; if (Platform.isMacOS) { + var plistName = '$label.plist'; + var userPath = Platform.environment['HOME']; + var file = File('$userPath/Library/LaunchAgents/$plistName'); + if (file.existsSync()) { + await file.delete(); + success = true; + } } else if (Platform.isWindows) { //For Windows 8, 10 var userPath = Platform.environment['UserProfile'];