126 lines
3.1 KiB
Dart
126 lines
3.1 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:dart_framework/platform/process.dart';
|
|
import 'package:dart_framework/utils/system_util.dart';
|
|
|
|
class RegistPath {
|
|
static RegistPath? _instance;
|
|
// ignore: library_private_types_in_public_api
|
|
static RegistPath? get OS {
|
|
if (_instance == null) {
|
|
if (Platform.isMacOS) {
|
|
_instance = _RegistPathOSX();
|
|
} else if (Platform.isLinux) {
|
|
_instance = _RegistPathLinux();
|
|
} else {
|
|
_instance = _RegistPathWindows();
|
|
}
|
|
}
|
|
return _instance;
|
|
}
|
|
|
|
Future add(String path) async {
|
|
return await _instance?.add(path);
|
|
}
|
|
|
|
Future remove(String path) async {
|
|
return await _instance?.remove(path);
|
|
}
|
|
}
|
|
|
|
/// ************* MAC ***************
|
|
class _RegistPathOSX extends RegistPath {
|
|
String getTargetPath(String targetPath) {
|
|
return 'export PATH=\${PATH}:$targetPath';
|
|
}
|
|
|
|
String get zprofile {
|
|
var userPath = Platform.environment['HOME'];
|
|
return '$userPath/.zprofile';
|
|
}
|
|
|
|
String get bprofile {
|
|
var userPath = Platform.environment['HOME'];
|
|
return '$userPath/.bash_profile';
|
|
}
|
|
|
|
@override
|
|
Future add(String path) async {
|
|
await addProfile(zprofile, path);
|
|
await addProfile(bprofile, path);
|
|
await ProcessExecutor.run(
|
|
StringBuffer('source $zprofile && source $bprofile'));
|
|
return simpleFuture;
|
|
}
|
|
|
|
Future addProfile(String profilePath, String targetPath) async {
|
|
var file = File(profilePath);
|
|
var contents = '';
|
|
if (file.existsSync()) {
|
|
contents = await file.readAsString();
|
|
} else {
|
|
file.createSync();
|
|
}
|
|
if (contents.lastIndexOf('\n') != contents.length - 1) {
|
|
contents += '\n';
|
|
}
|
|
var addItem = getTargetPath(targetPath);
|
|
if (!contents.contains(addItem)) {
|
|
contents += addItem;
|
|
}
|
|
|
|
await file.writeAsString(contents, flush: true);
|
|
return simpleFuture;
|
|
}
|
|
|
|
@override
|
|
Future remove(String path) async {
|
|
await removeProfile(zprofile, path);
|
|
await removeProfile(bprofile, path);
|
|
await ProcessExecutor.run(
|
|
StringBuffer('source $zprofile && source $bprofile'));
|
|
return simpleFuture;
|
|
}
|
|
|
|
Future removeProfile(String profilePath, String targetPath) async {
|
|
var file = File(profilePath);
|
|
var contents = '';
|
|
if (file.existsSync()) {
|
|
contents = await file.readAsString();
|
|
var removeItem = getTargetPath(targetPath);
|
|
if (contents.contains(removeItem)) {
|
|
contents = contents.replaceAll(removeItem, '');
|
|
await file.writeAsString(contents, flush: true);
|
|
}
|
|
}
|
|
return simpleFuture;
|
|
}
|
|
}
|
|
|
|
/// ************* LINUX ***************
|
|
class _RegistPathLinux extends RegistPath {
|
|
@override
|
|
Future add(String path) async {
|
|
return null;
|
|
}
|
|
|
|
@override
|
|
Future remove(String path) async {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// ************* WINDOWS ***************
|
|
class _RegistPathWindows extends RegistPath {
|
|
@override
|
|
Future add(String path) async {
|
|
await ProcessExecutor.run(StringBuffer('setx path "%path%;$path"'));
|
|
return simpleFuture;
|
|
}
|
|
|
|
@override
|
|
Future remove(String path) async {
|
|
await ProcessExecutor.run(StringBuffer('setx path "%path:$path=%"'));
|
|
return simpleFuture;
|
|
}
|
|
}
|