diff --git a/lib/utils/os_startup.dart b/lib/utils/os_startup.dart index b359ec4..699383b 100644 --- a/lib/utils/os_startup.dart +++ b/lib/utils/os_startup.dart @@ -25,8 +25,11 @@ class OSStartup { //shell file must have '#!/bin/sh' static Future registStartup(String executableFilePath, String label, - {String? logStandardFilePath, String? logErrorFilePath}) async { + {List? arguments, + String? logStandardFilePath, + String? logErrorFilePath}) async { return _current!.registStartup(executableFilePath, label, + arguments: arguments, logStandardFilePath: logStandardFilePath, logErrorFilePath: logErrorFilePath); } @@ -44,7 +47,9 @@ class _Startup { //shell file must have '#!/bin/sh' Future registStartup(String executableFilePath, String label, - {String? logStandardFilePath, String? logErrorFilePath}) async { + {List? arguments, + String? logStandardFilePath, + String? logErrorFilePath}) async { var c = Completer(); return c.future; } @@ -66,7 +71,7 @@ class _StartupMac extends _Startup { {LABEL} ProgramArguments - {FILE_PATH} + {FILE_PATH}{PARAMETERS} RunAtLoad {LOG_STANDARD}{LOG_ERROR} @@ -95,49 +100,53 @@ class _StartupMac extends _Startup { //shell file must have '#!/bin/sh' @override Future registStartup(String executableFilePath, String label, - {String? logStandardFilePath, String? logErrorFilePath}) async { + {List? arguments, + String? logStandardFilePath, + String? logErrorFilePath}) async { var c = Completer(); final tagLabel = '{LABEL}'; final tagPath = '{FILE_PATH}'; var success = false; - ProcessResult? result; - var enable = true; - if (await isRegistedStartup(label)) { - enable = await unregistStartup(label); + //compose parameters + var parameters = ''; + if (arguments != null) { + for (var arg in arguments) { + parameters += """ + + $arg"""; + } } - if (enable) { - //create plist in LaunchAgents - final plistName = '$label.plist'; - final userPath = Platform.environment['HOME']; - 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; + //create plist in LaunchAgents + final plistName = '$label.plist'; + final userPath = Platform.environment['HOME']; + final tagLogStandard = '{LOG_STANDARD}'; + final tagLogError = '{LOG_ERROR}'; + final tagParameters = '{PARAMETERS}'; + var plistContent = plistTemplate + .replaceAll(tagLabel, label) + .replaceAll(tagPath, executableFilePath) + .replaceAll(tagParameters, parameters); + 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; c.complete(success); return c.future; @@ -192,7 +201,9 @@ WantedBy=default.target"""; //shell file must have '#!/bin/sh' @override Future registStartup(String executableFilePath, String label, - {String? logStandardFilePath, String? logErrorFilePath}) async { + {List? arguments, + String? logStandardFilePath, + String? logErrorFilePath}) async { var c = Completer(); final tagLabel = '{LABEL}'; final tagPath = '{FILE_PATH}'; @@ -200,6 +211,11 @@ WantedBy=default.target"""; var userPath = Platform.environment['HOME'] as String; var systemPath = '$userPath/.config/systemd/user'; + if (arguments != null) { + for (var arg in arguments) { + executableFilePath += ' $arg'; + } + } var serviceContent = serviceTemplate .replaceAll(tagLabel, label) .replaceAll(tagPath, executableFilePath); @@ -249,6 +265,13 @@ class _StartupWindows extends _Startup { final windowsStartupPath = '\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\'; + final windowRoamingPath = '\\AppData\\Roaming\\'; + + final vbsTemplate = """Set WshShell = CreateObject("WScript.Shell") +WshShell.Run chr(34) & "{FILE_PATH}"{PARAMETERS} & Chr(34), 0 +Set WshShell = Nothing +"""; + @override Future isRegistedStartup(String label) async { var c = Completer(); @@ -273,19 +296,42 @@ class _StartupWindows extends _Startup { //shell file must have '#!/bin/sh' @override Future registStartup(String executableFilePath, String label, - {bool? overwrite = true, + {List? arguments, String? logStandardFilePath, String? logErrorFilePath}) async { var c = Completer(); var file = File(executableFilePath); ProcessResult? result; //For Windows 8, 10 - var fileName = path.basename(file.path); - var extension = path.extension(fileName); var userPath = Platform.environment['UserProfile']; - var symbolicPath = '$userPath$windowsStartupPath$label$extension'; + + //compose vbs content + var parameters = ''; + if (arguments != null) { + for (var arg in arguments) { + parameters += '" $arg"'; + } + } + var vbsContent = vbsTemplate + .replaceAll('{FILE_PATH}', executableFilePath) + .replaceAll('{PARAMETERS}', parameters); + + //create vbs file + var vbsInstallDir = Directory('$userPath$windowRoamingPath$label'); + if (!await vbsInstallDir.exists()) { + await vbsInstallDir.create(recursive: true); + } + var vbsFile = File(path.join(vbsInstallDir.path, '$label.vbs')); + if (await vbsFile.exists()) { + await vbsFile.delete(); + } + await vbsFile.create(); + await vbsFile.writeAsString(vbsContent); + + //create symbolic + var symbolicPath = '$userPath$windowsStartupPath$label.vbs'; result = await ProcessExecutor.run( - StringBuffer('mklink "$symbolicPath" "$executableFilePath"')); + StringBuffer('mklink "$symbolicPath" "${vbsFile.path}"')); c.complete(result.exitCode == 0); return c.future; } @@ -296,6 +342,14 @@ class _StartupWindows extends _Startup { var success = false; //For Windows 8, 10 var userPath = Platform.environment['UserProfile']; + + //delete vbs file + var vbsInstallDir = Directory('$userPath$windowRoamingPath$label'); + if (await vbsInstallDir.exists()) { + await vbsInstallDir.delete(recursive: true); + } + + //delete shorcut file var startupDir = Directory('$userPath$windowsStartupPath'); var files = startupDir.listSync(); for (var fileEntry in files) {