update startup
This commit is contained in:
parent
49380c4d1c
commit
79296728ac
1 changed files with 80 additions and 7 deletions
|
|
@ -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 = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>KeepAlive</key>
|
||||
<false/>
|
||||
<key>Label</key>
|
||||
<string>{LABEL}</string>
|
||||
<key>Program</key>
|
||||
<string>{FILE_PATH}</string>
|
||||
<key>RunAtLoad</key>
|
||||
<true/>{LOG_STANDARD}{LOG_ERROR}
|
||||
</dict>
|
||||
</plist>""";
|
||||
|
||||
static final logStandardTemplate = """
|
||||
|
||||
<key>StandardOutPath</key>
|
||||
<string>{LOG_STANDARD}</string>""";
|
||||
static final logErrorTemplate = """
|
||||
|
||||
<key>StandardErrorPath</key>
|
||||
<string>{LOG_ERROR}</string>""";
|
||||
|
||||
static Future<bool> isRegistedStartup(String label) async {
|
||||
var c = Completer<bool>();
|
||||
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<bool> registStartup(String executableFilePath,
|
||||
{String? label}) async {
|
||||
static Future<bool> registStartup(String executableFilePath, String label,
|
||||
{String? logStandardFilePath, String? logErrorFilePath}) async {
|
||||
var c = Completer<bool>();
|
||||
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<bool>();
|
||||
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'];
|
||||
|
|
|
|||
Loading…
Reference in a new issue