dart-app-core/lib/utils/os_startup.dart
2023-01-23 22:27:31 +09:00

131 lines
4.5 KiB
Dart

import 'dart:io';
import 'dart:async';
import 'package:path/path.dart';
import 'package:dart_framework/core/process.dart';
class OSStartup {
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'];
exist = File('$userPath$windowsStartupPath$label').existsSync();
} else if (Platform.isLinux) {}
c.complete(exist);
return c.future;
}
static Future<bool> registStartup(String executableFilePath, String label,
{String? logStandardFilePath, String? logErrorFilePath}) async {
var c = Completer<bool>();
var file = File(executableFilePath);
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 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(success);
return c.future;
}
static Future<bool> unregistStartup(String label) async {
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'];
var file = File('$userPath$windowsStartupPath$label');
if (file.existsSync()) {
await file.delete();
success = true;
}
} else if (Platform.isLinux) {}
c.complete(success);
return c.future;
}
}