From 363f96834b5a12830b37831d807e31fa7ac8f0e9 Mon Sep 17 00:00:00 2001 From: Toki Date: Fri, 27 Dec 2024 09:11:31 +0900 Subject: [PATCH] mac shell udpate --- lib/utils/os_startup.dart | 66 ++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/lib/utils/os_startup.dart b/lib/utils/os_startup.dart index 29cb243..71762d7 100644 --- a/lib/utils/os_startup.dart +++ b/lib/utils/os_startup.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'dart:async'; import 'package:dart_framework/utils/path.dart'; +import 'package:dart_framework/utils/system_util.dart'; import 'package:path/path.dart' as path; import 'package:dart_framework/platform/process.dart'; @@ -73,13 +74,39 @@ class _StartupMac extends _Startup { {LABEL} ProgramArguments - {FILE_PATH}{PARAMETERS} + {FILE_PATH} RunAtLoad {LOG_STANDARD}{LOG_ERROR} """; + final pathTemplate = """#!/bin/bash + +# Load .zprofile if it exists +if [ -f ~/.zprofile ]; then + source ~/.zprofile +fi + +# Load .bash_profile if it exists +if [ -f ~/.bash_profile ]; then + source ~/.bash_profile +fi + +# Load .bashrc if it exists (optional) +if [ -f ~/.bashrc ]; then + source ~/.bashrc +fi + +# Load .zshrc if it exists (optional) +if [ -f ~/.zshrc ]; then + source ~/.zshrc +fi + +# Run your command +{COMMAND} +"""; + final logStandardTemplate = """ StandardOutPath @@ -105,31 +132,29 @@ class _StartupMac extends _Startup { {List? arguments, String? logStandardFilePath, String? logErrorFilePath}) async { - var c = Completer(); final tagLabel = '{LABEL}'; final tagPath = '{FILE_PATH}'; - var success = false; - - //compose parameters - var parameters = ''; - if (arguments != null) { - for (var arg in arguments) { - parameters += """ - - $arg"""; - } - } - - //create plist in LaunchAgents final plistName = '$label.plist'; final userPath = await Path.userPath; + var success = false; + final appDir = Directory('$userPath/Library/$label'); + final file = File('${appDir.path}/launch.sh'); + + if(!appDir.existsSync()) { + appDir.createSync(recursive: true); + } + + var param = arguments == null ? '' : ' ${arguments.join(' ')}'; + var launchContent = pathTemplate.replaceAll('{COMMAND}', '$executableFilePath$param'); + file.writeAsStringSync(launchContent); + + //create plist in LaunchAgents + final tagLogStandard = '{LOG_STANDARD}'; final tagLogError = '{LOG_ERROR}'; - final tagParameters = '{PARAMETERS}'; var plistContent = plistTemplate .replaceAll(tagLabel, label) - .replaceAll(tagPath, executableFilePath) - .replaceAll(tagParameters, parameters); + .replaceAll(tagPath, file.path); if (logStandardFilePath == null) { plistContent = plistContent.replaceAll(tagLogStandard, ''); } else { @@ -147,11 +172,10 @@ class _StartupMac extends _Startup { await plistFile.writeAsString(plistContent); var result = await ProcessExecutor.run( - StringBuffer('chmod +x "$executableFilePath"')); + StringBuffer('chmod +x "$executableFilePath" && chmod +x "${file.path}" ')); success = result.exitCode == 0; - c.complete(success); - return c.future; + return dataFutrue(success); } @override